1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2011 Free Software Foundation, Inc.
5 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
6 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; For Emacs <22.2 and XEmacs.
30 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
)))
32 (if (locate-library "password-cache")
33 (require 'password-cache
)
43 (defvar mc-pgp-always-sign
)
45 (autoload 'quoted-printable-decode-region
"qp")
46 (autoload 'quoted-printable-encode-region
"qp")
48 (autoload 'mm-decode-content-transfer-encoding
"mm-bodies")
49 (autoload 'mm-encode-content-transfer-encoding
"mm-bodies")
50 (autoload 'message-options-get
"message")
51 (autoload 'message-options-set
"message")
53 (defvar mml1991-use mml2015-use
54 "The package used for PGP.")
56 (defvar mml1991-function-alist
57 '((mailcrypt mml1991-mailcrypt-sign
58 mml1991-mailcrypt-encrypt
)
63 "Alist of PGP functions.")
65 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
66 "If t, cache passphrase.")
68 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
69 "How many seconds the passphrase is cached.
70 Whether the passphrase is cached at all is controlled by
71 `mml1991-cache-passphrase'.")
73 (defvar mml1991-signers nil
74 "A list of your own key ID which will be used to sign a message.")
76 (defvar mml1991-encrypt-to-self nil
77 "If t, add your own key ID to recipient list when encryption.")
81 (autoload 'mc-sign-generic
"mc-toplev")
83 (defvar mml1991-decrypt-function
'mailcrypt-decrypt
)
84 (defvar mml1991-verify-function
'mailcrypt-verify
)
86 (defun mml1991-mailcrypt-sign (cont)
87 (let ((text (current-buffer))
89 (result-buffer (get-buffer-create "*GPG Result*")))
90 ;; Save MIME Content[^ ]+: headers from signing
91 (goto-char (point-min))
92 (while (looking-at "^Content[^ ]+:") (forward-line))
94 (setq headers
(buffer-string))
95 (delete-region (point-min) (point)))
96 (goto-char (point-max))
99 (quoted-printable-decode-region (point-min) (point-max))
101 (setq signature
(current-buffer))
102 (insert-buffer-substring text
)
103 (unless (mc-sign-generic (message-options-get 'message-sender
)
105 (unless (> (point-max) (point-min))
106 (pop-to-buffer result-buffer
)
107 (error "Sign error")))
108 (goto-char (point-min))
109 (while (re-search-forward "\r+$" nil t
)
110 (replace-match "" t t
))
111 (quoted-printable-encode-region (point-min) (point-max))
113 (delete-region (point-min) (point-max))
114 (if headers
(insert headers
))
116 (insert-buffer-substring signature
)
117 (goto-char (point-max)))))
119 (declare-function mc-encrypt-generic
"ext:mc-toplev"
120 (&optional recipients scheme start end from sign
))
122 (defun mml1991-mailcrypt-encrypt (cont &optional sign
)
123 (let ((text (current-buffer))
125 (or mc-pgp-always-sign
127 (eq t
(or (message-options-get 'message-sign-encrypt
)
129 'message-sign-encrypt
130 (or (y-or-n-p "Sign the message? ")
134 (result-buffer (get-buffer-create "*GPG Result*")))
135 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
136 (goto-char (point-min))
137 (while (looking-at "^Content[^ ]+:") (forward-line))
139 (delete-region (point-min) (point)))
141 (inline (mm-disable-multibyte))
142 (setq cipher
(current-buffer))
143 (insert-buffer-substring text
)
144 (unless (mc-encrypt-generic
146 (message-options-get 'message-recipients
)
147 (message-options-set 'message-recipients
148 (read-string "Recipients: ")))
150 (point-min) (point-max)
151 (message-options-get 'message-sender
)
153 (unless (> (point-max) (point-min))
154 (pop-to-buffer result-buffer
)
155 (error "Encrypt error")))
156 (goto-char (point-min))
157 (while (re-search-forward "\r+$" nil t
)
158 (replace-match "" t t
))
160 (delete-region (point-min) (point-max))
161 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
162 ;;(insert "Version: 1\n\n")
164 (insert-buffer-substring cipher
)
165 (goto-char (point-max)))))
169 (autoload 'pgg-sign-region
"pgg")
170 (autoload 'pgg-encrypt-region
"pgg")
172 (defvar pgg-default-user-id
)
173 (defvar pgg-errors-buffer
)
174 (defvar pgg-output-buffer
)
176 (defun mml1991-pgg-sign (cont)
177 (let ((pgg-text-mode t
)
178 (pgg-default-user-id (or (message-options-get 'mml-sender
)
179 pgg-default-user-id
))
181 ;; Don't sign headers.
182 (goto-char (point-min))
183 (when (re-search-forward "^$" nil t
)
184 (setq headers
(buffer-substring (point-min) (point)))
186 (narrow-to-region (point-min) (point))
187 (setq cte
(mail-fetch-field "content-transfer-encoding")))
189 (delete-region (point-min) (point))
191 (setq cte
(intern (downcase cte
)))
192 (mm-decode-content-transfer-encoding cte
)))
193 (unless (pgg-sign-region (point-min) (point-max) t
)
194 (pop-to-buffer pgg-errors-buffer
)
195 (error "Encrypt error"))
196 (delete-region (point-min) (point-max))
197 (mm-with-unibyte-current-buffer
198 (insert-buffer-substring pgg-output-buffer
)
199 (goto-char (point-min))
200 (while (re-search-forward "\r+$" nil t
)
201 (replace-match "" t t
))
203 (mm-encode-content-transfer-encoding cte
))
204 (goto-char (point-min))
210 (defun mml1991-pgg-encrypt (cont &optional sign
)
211 (goto-char (point-min))
212 (when (re-search-forward "^$" nil t
)
213 (let ((cte (save-restriction
214 (narrow-to-region (point-min) (point))
215 (mail-fetch-field "content-transfer-encoding"))))
216 ;; Strip MIME headers since it will be ASCII armored.
218 (delete-region (point-min) (point))
220 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
221 (unless (let ((pgg-text-mode t
))
223 (point-min) (point-max)
226 (message-options-get 'message-recipients
)
227 (message-options-set 'message-recipients
228 (read-string "Recipients: ")))
231 (pop-to-buffer pgg-errors-buffer
)
232 (error "Encrypt error"))
233 (delete-region (point-min) (point-max))
235 (insert-buffer-substring pgg-output-buffer
)
240 (defvar epg-user-id-alist
)
242 (autoload 'epg-make-context
"epg")
243 (autoload 'epg-passphrase-callback-function
"epg")
244 (autoload 'epa-select-keys
"epa")
245 (autoload 'epg-list-keys
"epg")
246 (autoload 'epg-context-set-armor
"epg")
247 (autoload 'epg-context-set-textmode
"epg")
248 (autoload 'epg-context-set-signers
"epg")
249 (autoload 'epg-context-set-passphrase-callback
"epg")
250 (autoload 'epg-sign-string
"epg")
251 (autoload 'epg-encrypt-string
"epg")
252 (autoload 'epg-configuration
"epg-config")
253 (autoload 'epg-expand-group
"epg-config")
255 (defvar mml1991-epg-secret-key-id-list nil
)
257 (defun mml1991-epg-passphrase-callback (context key-id ignore
)
259 (epg-passphrase-callback-function context key-id nil
)
260 (let* ((entry (assoc key-id epg-user-id-alist
))
263 (format "GnuPG passphrase for %s: "
271 (let ((password-cache-expiry mml1991-passphrase-cache-expiry
))
272 (password-cache-add key-id passphrase
))
273 (setq mml1991-epg-secret-key-id-list
274 (cons key-id mml1991-epg-secret-key-id-list
))
275 (copy-sequence passphrase
)))))
277 (defun mml1991-epg-sign (cont)
278 (let ((context (epg-make-context))
279 headers cte signers signature
)
280 (if (eq mm-sign-option
'guided
)
281 (setq signers
(epa-select-keys context
"Select keys for signing.
282 If no one is selected, default secret key is used. "
285 (setq signers
(mapcar (lambda (name)
286 (car (epg-list-keys context name t
)))
288 (epg-context-set-armor context t
)
289 (epg-context-set-textmode context t
)
290 (epg-context-set-signers context signers
)
291 (if mml1991-cache-passphrase
292 (epg-context-set-passphrase-callback
294 #'mml1991-epg-passphrase-callback
))
295 ;; Don't sign headers.
296 (goto-char (point-min))
297 (when (re-search-forward "^$" nil t
)
298 (setq headers
(buffer-substring (point-min) (point)))
300 (narrow-to-region (point-min) (point))
301 (setq cte
(mail-fetch-field "content-transfer-encoding")))
303 (delete-region (point-min) (point))
305 (setq cte
(intern (downcase cte
)))
306 (mm-decode-content-transfer-encoding cte
)))
307 (condition-case error
308 (setq signature
(epg-sign-string context
(buffer-string) 'clear
)
309 mml1991-epg-secret-key-id-list nil
)
311 (while mml1991-epg-secret-key-id-list
312 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
313 (setq mml1991-epg-secret-key-id-list
314 (cdr mml1991-epg-secret-key-id-list
)))
315 (signal (car error
) (cdr error
))))
316 (delete-region (point-min) (point-max))
317 (mm-with-unibyte-current-buffer
319 (goto-char (point-min))
320 (while (re-search-forward "\r+$" nil t
)
321 (replace-match "" t t
))
323 (mm-encode-content-transfer-encoding cte
))
324 (goto-char (point-min))
330 (defun mml1991-epg-encrypt (cont &optional sign
)
331 (goto-char (point-min))
332 (when (re-search-forward "^$" nil t
)
333 (let ((cte (save-restriction
334 (narrow-to-region (point-min) (point))
335 (mail-fetch-field "content-transfer-encoding"))))
336 ;; Strip MIME headers since it will be ASCII armored.
338 (delete-region (point-min) (point))
340 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
341 (let ((context (epg-make-context))
343 (if (message-options-get 'message-recipients
)
345 (message-options-get 'message-recipients
)
347 cipher signers config
)
348 ;; We should remove this check if epg-0.0.6 is released.
349 (if (and (condition-case nil
350 (require 'epg-config
)
352 (functionp #'epg-expand-group
))
353 (setq config
(epg-configuration)
356 (mapcar (lambda (recipient)
357 (or (epg-expand-group config recipient
)
360 (if (eq mm-encrypt-option
'guided
)
362 (epa-select-keys context
"Select recipients for encryption.
363 If no one is selected, symmetric encryption will be performed. "
366 (delq nil
(mapcar (lambda (name)
367 (car (epg-list-keys context name
)))
369 (if mml1991-encrypt-to-self
373 (mapcar (lambda (name)
374 (car (epg-list-keys context name
)))
376 (error "mml1991-signers not set")))
378 (if (eq mm-sign-option
'guided
)
379 (setq signers
(epa-select-keys context
"Select keys for signing.
380 If no one is selected, default secret key is used. "
383 (setq signers
(mapcar (lambda (name)
384 (car (epg-list-keys context name t
)))
386 (epg-context-set-signers context signers
))
387 (epg-context-set-armor context t
)
388 (epg-context-set-textmode context t
)
389 (if mml1991-cache-passphrase
390 (epg-context-set-passphrase-callback
392 #'mml1991-epg-passphrase-callback
))
393 (condition-case error
395 (epg-encrypt-string context
(buffer-string) recipients sign
)
396 mml1991-epg-secret-key-id-list nil
)
398 (while mml1991-epg-secret-key-id-list
399 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
400 (setq mml1991-epg-secret-key-id-list
401 (cdr mml1991-epg-secret-key-id-list
)))
402 (signal (car error
) (cdr error
))))
403 (delete-region (point-min) (point-max))
404 (insert "\n" cipher
))
408 (defun mml1991-encrypt (cont &optional sign
)
409 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist
))))
411 (funcall func cont sign
)
412 (error "Cannot find encrypt function"))))
415 (defun mml1991-sign (cont)
416 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist
))))
419 (error "Cannot find sign function"))))
424 ;; coding: iso-8859-1
427 ;;; mml1991.el ends here