Remove useless mml1991-verbose.
[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-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.
73 Whether 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
82 ;;; mailcrypt wrapper
83
84 (autoload 'mc-sign-generic "mc-toplev")
85
86 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
87 (defvar mml1991-verify-function 'mailcrypt-verify)
88
89 (defun mml1991-mailcrypt-sign (cont)
90   (let ((text (current-buffer))
91         headers signature
92         (result-buffer (get-buffer-create "*GPG Result*")))
93     ;; Save MIME Content[^ ]+: headers from signing
94     (goto-char (point-min))
95     (while (looking-at "^Content[^ ]+:") (forward-line))
96     (unless (bobp)
97       (setq headers (buffer-string))
98       (delete-region (point-min) (point)))
99     (goto-char (point-max))
100     (unless (bolp)
101       (insert "\n"))
102     (quoted-printable-decode-region (point-min) (point-max))
103     (with-temp-buffer
104       (setq signature (current-buffer))
105       (insert-buffer-substring text)
106       (unless (mc-sign-generic (message-options-get 'message-sender)
107                                nil nil nil nil)
108         (unless (> (point-max) (point-min))
109           (pop-to-buffer result-buffer)
110           (error "Sign error")))
111       (goto-char (point-min))
112       (while (re-search-forward "\r+$" nil t)
113         (replace-match "" t t))
114       (quoted-printable-encode-region (point-min) (point-max))
115       (set-buffer text)
116       (delete-region (point-min) (point-max))
117       (if headers (insert headers))
118       (insert "\n")
119       (insert-buffer-substring signature)
120       (goto-char (point-max)))))
121
122 (declare-function mc-encrypt-generic "ext:mc-toplev"
123                   (&optional recipients scheme start end from sign))
124
125 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
126   (let ((text (current-buffer))
127         (mc-pgp-always-sign
128          (or mc-pgp-always-sign
129              sign
130              (eq t (or (message-options-get 'message-sign-encrypt)
131                        (message-options-set
132                         'message-sign-encrypt
133                         (or (y-or-n-p "Sign the message? ")
134                             'not))))
135              'never))
136         cipher
137         (result-buffer (get-buffer-create "*GPG Result*")))
138     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
139     (goto-char (point-min))
140     (while (looking-at "^Content[^ ]+:") (forward-line))
141     (unless (bobp)
142       (delete-region (point-min) (point)))
143     (mm-with-unibyte-current-buffer
144       (with-temp-buffer
145         (inline (mm-disable-multibyte))
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 (autoload 'gpg-sign-cleartext "gpg")
174
175 (declare-function gpg-sign-encrypt "ext:gpg"
176                   (plaintext ciphertext result recipients &optional
177                              passphrase sign-with-key armor textmode))
178 (declare-function gpg-encrypt "ext:gpg"
179                   (plaintext ciphertext result recipients &optional
180                              passphrase armor textmode))
181
182 (defun mml1991-gpg-sign (cont)
183   (let ((text (current-buffer))
184         headers signature
185         (result-buffer (get-buffer-create "*GPG Result*")))
186     ;; Save MIME Content[^ ]+: headers from signing
187     (goto-char (point-min))
188     (while (looking-at "^Content[^ ]+:") (forward-line))
189     (unless (bobp)
190       (setq headers (buffer-string))
191       (delete-region (point-min) (point)))
192     (goto-char (point-max))
193     (unless (bolp)
194       (insert "\n"))
195     (quoted-printable-decode-region (point-min) (point-max))
196     (with-temp-buffer
197       (unless (gpg-sign-cleartext text (setq signature (current-buffer))
198                                   result-buffer
199                                   nil
200                                   (message-options-get 'message-sender))
201         (unless (> (point-max) (point-min))
202           (pop-to-buffer result-buffer)
203           (error "Sign error")))
204       (goto-char (point-min))
205       (while (re-search-forward "\r+$" nil t)
206         (replace-match "" t t))
207       (quoted-printable-encode-region (point-min) (point-max))
208       (set-buffer text)
209       (delete-region (point-min) (point-max))
210       (if headers (insert headers))
211       (insert "\n")
212       (insert-buffer-substring signature)
213       (goto-char (point-max)))))
214
215 (defun mml1991-gpg-encrypt (cont &optional sign)
216   (let ((text (current-buffer))
217         cipher
218         (result-buffer (get-buffer-create "*GPG Result*")))
219     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
220     (goto-char (point-min))
221     (while (looking-at "^Content[^ ]+:") (forward-line))
222     (unless (bobp)
223       (delete-region (point-min) (point)))
224     (mm-with-unibyte-current-buffer
225       (with-temp-buffer
226         (inline (mm-disable-multibyte))
227         (flet ((gpg-encrypt-func
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
266 (defvar pgg-default-user-id)
267 (defvar pgg-errors-buffer)
268 (defvar pgg-output-buffer)
269
270 (defun mml1991-pgg-sign (cont)
271   (let ((pgg-text-mode t)
272         (pgg-default-user-id (or (message-options-get 'mml-sender)
273                                  pgg-default-user-id))
274         headers cte)
275     ;; Don't sign headers.
276     (goto-char (point-min))
277     (when (re-search-forward "^$" nil t)
278       (setq headers (buffer-substring (point-min) (point)))
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)
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))
296       (when cte
297         (mm-encode-content-transfer-encoding cte))
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)
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 armored.
311       (forward-line 1)
312       (delete-region (point-min) (point))
313       (when cte
314         (mm-decode-content-transfer-encoding (intern (downcase cte))))))
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))
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)
331
332 ;; epg wrapper
333
334 (defvar epg-user-id-alist)
335
336 (autoload 'epg-make-context "epg")
337 (autoload 'epg-passphrase-callback-function "epg")
338 (autoload 'epa-select-keys "epa")
339 (autoload 'epg-list-keys "epg")
340 (autoload 'epg-context-set-armor "epg")
341 (autoload 'epg-context-set-textmode "epg")
342 (autoload 'epg-context-set-signers "epg")
343 (autoload 'epg-context-set-passphrase-callback "epg")
344 (autoload 'epg-sign-string "epg")
345 (autoload 'epg-encrypt-string "epg")
346 (autoload 'epg-configuration "epg-config")
347 (autoload 'epg-expand-group "epg-config")
348
349 (defvar mml1991-epg-secret-key-id-list nil)
350
351 (defun mml1991-epg-passphrase-callback (context key-id ignore)
352   (if (eq key-id 'SYM)
353       (epg-passphrase-callback-function context key-id nil)
354     (let* ((entry (assoc key-id epg-user-id-alist))
355            (passphrase
356             (password-read
357              (format "GnuPG passphrase for %s: "
358                      (if entry
359                          (cdr entry)
360                        key-id))
361              (if (eq key-id 'PIN)
362                  "PIN"
363                key-id))))
364       (when passphrase
365         (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
366           (password-cache-add key-id passphrase))
367         (setq mml1991-epg-secret-key-id-list
368               (cons key-id mml1991-epg-secret-key-id-list))
369         (copy-sequence passphrase)))))
370
371 (defun mml1991-epg-sign (cont)
372   (let ((context (epg-make-context))
373         headers cte signers signature)
374     (if (eq mm-sign-option 'guided)
375         (setq signers (epa-select-keys context "Select keys for signing.
376 If no one is selected, default secret key is used.  "
377                                        mml1991-signers t))
378       (if mml1991-signers
379           (setq signers (mapcar (lambda (name)
380                                   (car (epg-list-keys context name t)))
381                                 mml1991-signers))))
382     (epg-context-set-armor context t)
383     (epg-context-set-textmode context t)
384     (epg-context-set-signers context signers)
385     (if mml1991-cache-passphrase
386         (epg-context-set-passphrase-callback
387          context
388          #'mml1991-epg-passphrase-callback))
389     ;; Don't sign headers.
390     (goto-char (point-min))
391     (when (re-search-forward "^$" nil t)
392       (setq headers (buffer-substring (point-min) (point)))
393       (save-restriction
394         (narrow-to-region (point-min) (point))
395         (setq cte (mail-fetch-field "content-transfer-encoding")))
396       (forward-line 1)
397       (delete-region (point-min) (point))
398       (when cte
399         (setq cte (intern (downcase cte)))
400         (mm-decode-content-transfer-encoding cte)))
401     (condition-case error
402         (setq signature (epg-sign-string context (buffer-string) 'clear)
403               mml1991-epg-secret-key-id-list nil)
404       (error
405        (while mml1991-epg-secret-key-id-list
406          (password-cache-remove (car mml1991-epg-secret-key-id-list))
407          (setq mml1991-epg-secret-key-id-list
408                (cdr mml1991-epg-secret-key-id-list)))
409        (signal (car error) (cdr error))))
410     (delete-region (point-min) (point-max))
411     (mm-with-unibyte-current-buffer
412       (insert signature)
413       (goto-char (point-min))
414       (while (re-search-forward "\r+$" nil t)
415         (replace-match "" t t))
416       (when cte
417         (mm-encode-content-transfer-encoding cte))
418       (goto-char (point-min))
419       (when headers
420         (insert headers))
421       (insert "\n"))
422     t))
423
424 (defun mml1991-epg-encrypt (cont &optional sign)
425   (goto-char (point-min))
426   (when (re-search-forward "^$" nil t)
427     (let ((cte (save-restriction
428                  (narrow-to-region (point-min) (point))
429                  (mail-fetch-field "content-transfer-encoding"))))
430       ;; Strip MIME headers since it will be ASCII armored.
431       (forward-line 1)
432       (delete-region (point-min) (point))
433       (when cte
434         (mm-decode-content-transfer-encoding (intern (downcase cte))))))
435   (let ((context (epg-make-context))
436         (recipients
437          (if (message-options-get 'message-recipients)
438              (split-string
439               (message-options-get 'message-recipients)
440               "[ \f\t\n\r\v,]+")))
441         cipher signers config)
442     ;; We should remove this check if epg-0.0.6 is released.
443     (if (and (condition-case nil
444                  (require 'epg-config)
445                (error))
446              (functionp #'epg-expand-group))
447         (setq config (epg-configuration)
448               recipients
449               (apply #'nconc
450                      (mapcar (lambda (recipient)
451                                (or (epg-expand-group config recipient)
452                                    (list recipient)))
453                              recipients))))
454     (if (eq mm-encrypt-option 'guided)
455         (setq recipients
456               (epa-select-keys context "Select recipients for encryption.
457 If no one is selected, symmetric encryption will be performed.  "
458                                recipients))
459       (setq recipients
460             (delq nil (mapcar (lambda (name)
461                                 (car (epg-list-keys context name)))
462                               recipients))))
463     (if mml1991-encrypt-to-self
464         (if mml1991-signers
465             (setq recipients
466                   (nconc recipients
467                          (mapcar (lambda (name)
468                                    (car (epg-list-keys context name)))
469                                  mml1991-signers)))
470           (error "mml1991-signers not set")))
471     (when sign
472       (if (eq mm-sign-option 'guided)
473           (setq signers (epa-select-keys context "Select keys for signing.
474 If no one is selected, default secret key is used.  "
475                                          mml1991-signers t))
476         (if mml1991-signers
477             (setq signers (mapcar (lambda (name)
478                                     (car (epg-list-keys context name t)))
479                                   mml1991-signers))))
480       (epg-context-set-signers context signers))
481     (epg-context-set-armor context t)
482     (epg-context-set-textmode context t)
483     (if mml1991-cache-passphrase
484         (epg-context-set-passphrase-callback
485          context
486          #'mml1991-epg-passphrase-callback))
487     (condition-case error
488         (setq cipher
489               (epg-encrypt-string context (buffer-string) recipients sign)
490               mml1991-epg-secret-key-id-list nil)
491       (error
492        (while mml1991-epg-secret-key-id-list
493          (password-cache-remove (car mml1991-epg-secret-key-id-list))
494          (setq mml1991-epg-secret-key-id-list
495                (cdr mml1991-epg-secret-key-id-list)))
496        (signal (car error) (cdr error))))
497     (delete-region (point-min) (point-max))
498     (insert "\n" cipher))
499   t)
500
501 ;;;###autoload
502 (defun mml1991-encrypt (cont &optional sign)
503   (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
504     (if func
505         (funcall func cont sign)
506       (error "Cannot find encrypt function"))))
507
508 ;;;###autoload
509 (defun mml1991-sign (cont)
510   (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
511     (if func
512         (funcall func cont)
513       (error "Cannot find sign function"))))
514
515 (provide 'mml1991)
516
517 ;; Local Variables:
518 ;; coding: iso-8859-1
519 ;; End:
520
521 ;;; mml1991.el ends here