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