Mention archiving.
[emacs.git] / lisp / gnus / mml1991.el
blob60d6e3cb4fd605d845a3f76c93d066145d708ca8
1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Sascha Ldecke <sascha@meta-x.de>,
7 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
8 ;; Keywords PGP
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 ;; For Emacs < 22.2.
30 (eval-and-compile
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33 (eval-when-compile
34 (require 'cl)
35 (require 'mm-util))
37 (require 'mml-sec)
39 (defvar mc-pgp-always-sign)
41 (autoload 'quoted-printable-decode-region "qp")
42 (autoload 'quoted-printable-encode-region "qp")
44 (autoload 'mm-decode-content-transfer-encoding "mm-bodies")
45 (autoload 'mm-encode-content-transfer-encoding "mm-bodies")
46 (autoload 'message-options-get "message")
47 (autoload 'message-options-set "message")
49 (defvar mml1991-use mml2015-use
50 "The package used for PGP.")
52 (defvar mml1991-function-alist
53 '((mailcrypt mml1991-mailcrypt-sign
54 mml1991-mailcrypt-encrypt)
55 (gpg mml1991-gpg-sign
56 mml1991-gpg-encrypt)
57 (pgg mml1991-pgg-sign
58 mml1991-pgg-encrypt)
59 (epg mml1991-epg-sign
60 mml1991-epg-encrypt))
61 "Alist of PGP functions.")
63 (defvar mml1991-verbose mml-secure-verbose
64 "If non-nil, ask the user about the current operation more verbosely.")
66 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
67 "If t, cache passphrase.")
69 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
70 "How many seconds the passphrase is cached.
71 Whether the passphrase is cached at all is controlled by
72 `mml1991-cache-passphrase'.")
74 (defvar mml1991-signers nil
75 "A list of your own key ID which will be used to sign a message.")
77 (defvar mml1991-encrypt-to-self nil
78 "If t, add your own key ID to recipient list when encryption.")
80 ;;; mailcrypt wrapper
82 (autoload 'mc-sign-generic "mc-toplev")
84 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
85 (defvar mml1991-verify-function 'mailcrypt-verify)
87 (defun mml1991-mailcrypt-sign (cont)
88 (let ((text (current-buffer))
89 headers signature
90 (result-buffer (get-buffer-create "*GPG Result*")))
91 ;; Save MIME Content[^ ]+: headers from signing
92 (goto-char (point-min))
93 (while (looking-at "^Content[^ ]+:") (forward-line))
94 (unless (bobp)
95 (setq headers (buffer-string))
96 (delete-region (point-min) (point)))
97 (goto-char (point-max))
98 (unless (bolp)
99 (insert "\n"))
100 (quoted-printable-decode-region (point-min) (point-max))
101 (with-temp-buffer
102 (setq signature (current-buffer))
103 (insert-buffer-substring text)
104 (unless (mc-sign-generic (message-options-get 'message-sender)
105 nil nil nil nil)
106 (unless (> (point-max) (point-min))
107 (pop-to-buffer result-buffer)
108 (error "Sign error")))
109 (goto-char (point-min))
110 (while (re-search-forward "\r+$" nil t)
111 (replace-match "" t t))
112 (quoted-printable-encode-region (point-min) (point-max))
113 (set-buffer text)
114 (delete-region (point-min) (point-max))
115 (if headers (insert headers))
116 (insert "\n")
117 (insert-buffer-substring signature)
118 (goto-char (point-max)))))
120 (declare-function mc-encrypt-generic "ext:mc-toplev"
121 (&optional recipients scheme start end from sign))
123 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
124 (let ((text (current-buffer))
125 (mc-pgp-always-sign
126 (or mc-pgp-always-sign
127 sign
128 (eq t (or (message-options-get 'message-sign-encrypt)
129 (message-options-set
130 'message-sign-encrypt
131 (or (y-or-n-p "Sign the message? ")
132 'not))))
133 'never))
134 cipher
135 (result-buffer (get-buffer-create "*GPG Result*")))
136 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
137 (goto-char (point-min))
138 (while (looking-at "^Content[^ ]+:") (forward-line))
139 (unless (bobp)
140 (delete-region (point-min) (point)))
141 (mm-with-unibyte-current-buffer
142 (with-temp-buffer
143 (setq cipher (current-buffer))
144 (insert-buffer-substring text)
145 (unless (mc-encrypt-generic
147 (message-options-get 'message-recipients)
148 (message-options-set 'message-recipients
149 (read-string "Recipients: ")))
151 (point-min) (point-max)
152 (message-options-get 'message-sender)
153 'sign)
154 (unless (> (point-max) (point-min))
155 (pop-to-buffer result-buffer)
156 (error "Encrypt error")))
157 (goto-char (point-min))
158 (while (re-search-forward "\r+$" nil t)
159 (replace-match "" t t))
160 (set-buffer text)
161 (delete-region (point-min) (point-max))
162 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
163 ;;(insert "Version: 1\n\n")
164 (insert "\n")
165 (insert-buffer-substring cipher)
166 (goto-char (point-max))))))
168 ;;; gpg wrapper
170 (autoload 'gpg-sign-cleartext "gpg")
172 (declare-function gpg-sign-encrypt "ext:gpg"
173 (plaintext ciphertext result recipients &optional
174 passphrase sign-with-key armor textmode))
175 (declare-function gpg-encrypt "ext:gpg"
176 (plaintext ciphertext result recipients &optional
177 passphrase armor textmode))
179 (defun mml1991-gpg-sign (cont)
180 (let ((text (current-buffer))
181 headers signature
182 (result-buffer (get-buffer-create "*GPG Result*")))
183 ;; Save MIME Content[^ ]+: headers from signing
184 (goto-char (point-min))
185 (while (looking-at "^Content[^ ]+:") (forward-line))
186 (unless (bobp)
187 (setq headers (buffer-string))
188 (delete-region (point-min) (point)))
189 (goto-char (point-max))
190 (unless (bolp)
191 (insert "\n"))
192 (quoted-printable-decode-region (point-min) (point-max))
193 (with-temp-buffer
194 (unless (gpg-sign-cleartext text (setq signature (current-buffer))
195 result-buffer
197 (message-options-get 'message-sender))
198 (unless (> (point-max) (point-min))
199 (pop-to-buffer result-buffer)
200 (error "Sign error")))
201 (goto-char (point-min))
202 (while (re-search-forward "\r+$" nil t)
203 (replace-match "" t t))
204 (quoted-printable-encode-region (point-min) (point-max))
205 (set-buffer text)
206 (delete-region (point-min) (point-max))
207 (if headers (insert headers))
208 (insert "\n")
209 (insert-buffer-substring signature)
210 (goto-char (point-max)))))
212 (defun mml1991-gpg-encrypt (cont &optional sign)
213 (let ((text (current-buffer))
214 cipher
215 (result-buffer (get-buffer-create "*GPG Result*")))
216 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
217 (goto-char (point-min))
218 (while (looking-at "^Content[^ ]+:") (forward-line))
219 (unless (bobp)
220 (delete-region (point-min) (point)))
221 (mm-with-unibyte-current-buffer
222 (with-temp-buffer
223 (flet ((gpg-encrypt-func
224 (sign plaintext ciphertext result recipients &optional
225 passphrase sign-with-key armor textmode)
226 (if sign
227 (gpg-sign-encrypt
228 plaintext ciphertext result recipients passphrase
229 sign-with-key armor textmode)
230 (gpg-encrypt
231 plaintext ciphertext result recipients passphrase
232 armor textmode))))
233 (unless (gpg-encrypt-func
234 sign
235 text (setq cipher (current-buffer))
236 result-buffer
237 (split-string
239 (message-options-get 'message-recipients)
240 (message-options-set 'message-recipients
241 (read-string "Recipients: ")))
242 "[ \f\t\n\r\v,]+")
244 (message-options-get 'message-sender)
245 t t) ; armor & textmode
246 (unless (> (point-max) (point-min))
247 (pop-to-buffer result-buffer)
248 (error "Encrypt error"))))
249 (goto-char (point-min))
250 (while (re-search-forward "\r+$" nil t)
251 (replace-match "" t t))
252 (set-buffer text)
253 (delete-region (point-min) (point-max))
254 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
255 ;;(insert "Version: 1\n\n")
256 (insert "\n")
257 (insert-buffer-substring cipher)
258 (goto-char (point-max))))))
260 ;; pgg wrapper
262 (defvar pgg-default-user-id)
263 (defvar pgg-errors-buffer)
264 (defvar pgg-output-buffer)
266 (defun mml1991-pgg-sign (cont)
267 (let ((pgg-text-mode t)
268 (pgg-default-user-id (or (message-options-get 'mml-sender)
269 pgg-default-user-id))
270 headers cte)
271 ;; Don't sign headers.
272 (goto-char (point-min))
273 (when (re-search-forward "^$" nil t)
274 (setq headers (buffer-substring (point-min) (point)))
275 (save-restriction
276 (narrow-to-region (point-min) (point))
277 (setq cte (mail-fetch-field "content-transfer-encoding")))
278 (forward-line 1)
279 (delete-region (point-min) (point))
280 (when cte
281 (setq cte (intern (downcase cte)))
282 (mm-decode-content-transfer-encoding cte)))
283 (unless (pgg-sign-region (point-min) (point-max) t)
284 (pop-to-buffer pgg-errors-buffer)
285 (error "Encrypt error"))
286 (delete-region (point-min) (point-max))
287 (mm-with-unibyte-current-buffer
288 (insert-buffer-substring pgg-output-buffer)
289 (goto-char (point-min))
290 (while (re-search-forward "\r+$" nil t)
291 (replace-match "" t t))
292 (when cte
293 (mm-encode-content-transfer-encoding cte))
294 (goto-char (point-min))
295 (when headers
296 (insert headers))
297 (insert "\n"))
300 (defun mml1991-pgg-encrypt (cont &optional sign)
301 (goto-char (point-min))
302 (when (re-search-forward "^$" nil t)
303 (let ((cte (save-restriction
304 (narrow-to-region (point-min) (point))
305 (mail-fetch-field "content-transfer-encoding"))))
306 ;; Strip MIME headers since it will be ASCII armored.
307 (forward-line 1)
308 (delete-region (point-min) (point))
309 (when cte
310 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
311 (unless (let ((pgg-text-mode t))
312 (pgg-encrypt-region
313 (point-min) (point-max)
314 (split-string
316 (message-options-get 'message-recipients)
317 (message-options-set 'message-recipients
318 (read-string "Recipients: ")))
319 "[ \f\t\n\r\v,]+")
320 sign))
321 (pop-to-buffer pgg-errors-buffer)
322 (error "Encrypt error"))
323 (delete-region (point-min) (point-max))
324 (insert "\n")
325 (insert-buffer-substring pgg-output-buffer)
328 ;; epg wrapper
330 (defvar epg-user-id-alist)
331 (defvar password-cache-expiry)
333 (autoload 'epg-make-context "epg")
334 (autoload 'epg-passphrase-callback-function "epg")
335 (autoload 'epa-select-keys "epa")
336 (autoload 'epg-list-keys "epg")
337 (autoload 'epg-context-set-armor "epg")
338 (autoload 'epg-context-set-textmode "epg")
339 (autoload 'epg-context-set-signers "epg")
340 (autoload 'epg-context-set-passphrase-callback "epg")
341 (autoload 'epg-sign-string "epg")
342 (autoload 'epg-encrypt-string "epg")
343 (autoload 'epg-configuration "epg-config")
344 (autoload 'epg-expand-group "epg-config")
346 (defvar mml1991-epg-secret-key-id-list nil)
348 (defun mml1991-epg-passphrase-callback (context key-id ignore)
349 (if (eq key-id 'SYM)
350 (epg-passphrase-callback-function context key-id nil)
351 (let* ((entry (assoc key-id epg-user-id-alist))
352 (passphrase
353 (password-read
354 (format "GnuPG passphrase for %s: "
355 (if entry
356 (cdr entry)
357 key-id))
358 (if (eq key-id 'PIN)
359 "PIN"
360 key-id))))
361 (when passphrase
362 (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
363 (password-cache-add key-id passphrase))
364 (setq mml1991-epg-secret-key-id-list
365 (cons key-id mml1991-epg-secret-key-id-list))
366 (copy-sequence passphrase)))))
368 (defun mml1991-epg-sign (cont)
369 (let ((context (epg-make-context))
370 headers cte signers signature)
371 (if mml1991-verbose
372 (setq signers (epa-select-keys context "Select keys for signing.
373 If no one is selected, default secret key is used. "
374 mml1991-signers t))
375 (if mml1991-signers
376 (setq signers (mapcar (lambda (name)
377 (car (epg-list-keys context name t)))
378 mml1991-signers))))
379 (epg-context-set-armor context t)
380 (epg-context-set-textmode context t)
381 (epg-context-set-signers context signers)
382 (if mml1991-cache-passphrase
383 (epg-context-set-passphrase-callback
384 context
385 #'mml1991-epg-passphrase-callback))
386 ;; Don't sign headers.
387 (goto-char (point-min))
388 (when (re-search-forward "^$" nil t)
389 (setq headers (buffer-substring (point-min) (point)))
390 (save-restriction
391 (narrow-to-region (point-min) (point))
392 (setq cte (mail-fetch-field "content-transfer-encoding")))
393 (forward-line 1)
394 (delete-region (point-min) (point))
395 (when cte
396 (setq cte (intern (downcase cte)))
397 (mm-decode-content-transfer-encoding cte)))
398 (condition-case error
399 (setq signature (epg-sign-string context (buffer-string) 'clear)
400 mml1991-epg-secret-key-id-list nil)
401 (error
402 (while mml1991-epg-secret-key-id-list
403 (password-cache-remove (car mml1991-epg-secret-key-id-list))
404 (setq mml1991-epg-secret-key-id-list
405 (cdr mml1991-epg-secret-key-id-list)))
406 (signal (car error) (cdr error))))
407 (delete-region (point-min) (point-max))
408 (mm-with-unibyte-current-buffer
409 (insert signature)
410 (goto-char (point-min))
411 (while (re-search-forward "\r+$" nil t)
412 (replace-match "" t t))
413 (when cte
414 (mm-encode-content-transfer-encoding cte))
415 (goto-char (point-min))
416 (when headers
417 (insert headers))
418 (insert "\n"))
421 (defun mml1991-epg-encrypt (cont &optional sign)
422 (goto-char (point-min))
423 (when (re-search-forward "^$" nil t)
424 (let ((cte (save-restriction
425 (narrow-to-region (point-min) (point))
426 (mail-fetch-field "content-transfer-encoding"))))
427 ;; Strip MIME headers since it will be ASCII armored.
428 (forward-line 1)
429 (delete-region (point-min) (point))
430 (when cte
431 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
432 (let ((context (epg-make-context))
433 (recipients
434 (if (message-options-get 'message-recipients)
435 (split-string
436 (message-options-get 'message-recipients)
437 "[ \f\t\n\r\v,]+")))
438 cipher signers config)
439 ;; We should remove this check if epg-0.0.6 is released.
440 (if (and (condition-case nil
441 (require 'epg-config)
442 (error))
443 (functionp #'epg-expand-group))
444 (setq config (epg-configuration)
445 recipients
446 (apply #'nconc
447 (mapcar (lambda (recipient)
448 (or (epg-expand-group config recipient)
449 (list recipient)))
450 recipients))))
451 (if mml1991-verbose
452 (setq recipients
453 (epa-select-keys context "Select recipients for encryption.
454 If no one is selected, symmetric encryption will be performed. "
455 recipients))
456 (setq recipients
457 (delq nil (mapcar (lambda (name)
458 (car (epg-list-keys context name)))
459 recipients))))
460 (if mml1991-encrypt-to-self
461 (if mml1991-signers
462 (setq recipients
463 (nconc recipients
464 (mapcar (lambda (name)
465 (car (epg-list-keys context name)))
466 mml1991-signers)))
467 (error "mml1991-signers not set")))
468 (when sign
469 (if mml1991-verbose
470 (setq signers (epa-select-keys context "Select keys for signing.
471 If no one is selected, default secret key is used. "
472 mml1991-signers t))
473 (if mml1991-signers
474 (setq signers (mapcar (lambda (name)
475 (car (epg-list-keys context name t)))
476 mml1991-signers))))
477 (epg-context-set-signers context signers))
478 (epg-context-set-armor context t)
479 (epg-context-set-textmode context t)
480 (if mml1991-cache-passphrase
481 (epg-context-set-passphrase-callback
482 context
483 #'mml1991-epg-passphrase-callback))
484 (condition-case error
485 (setq cipher
486 (epg-encrypt-string context (buffer-string) recipients sign)
487 mml1991-epg-secret-key-id-list nil)
488 (error
489 (while mml1991-epg-secret-key-id-list
490 (password-cache-remove (car mml1991-epg-secret-key-id-list))
491 (setq mml1991-epg-secret-key-id-list
492 (cdr mml1991-epg-secret-key-id-list)))
493 (signal (car error) (cdr error))))
494 (delete-region (point-min) (point-max))
495 (insert "\n" cipher))
498 ;;;###autoload
499 (defun mml1991-encrypt (cont &optional sign)
500 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
501 (if func
502 (funcall func cont sign)
503 (error "Cannot find encrypt function"))))
505 ;;;###autoload
506 (defun mml1991-sign (cont)
507 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
508 (if func
509 (funcall func cont)
510 (error "Cannot find sign function"))))
512 (provide 'mml1991)
514 ;; Local Variables:
515 ;; coding: iso-8859-1
516 ;; End:
518 ;; arch-tag: e542be18-ab28-4393-9b33-97fe9cf30706
519 ;;; mml1991.el ends here