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