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