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