* message.el (message-setup-1): Don't bind the constant -forbidden-properties.
[gnus] / lisp / mml1991.el
1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
2
3 ;; Copyright (C) 1998-2011  Free Software Foundation, Inc.
4
5 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
6 ;;      Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
7 ;; Keywords PGP
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-and-compile
29   ;; For Emacs <22.2 and XEmacs.
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
31
32   (if (locate-library "password-cache")
33       (require 'password-cache)
34     (require 'password)))
35
36 (eval-when-compile
37   (require 'cl)
38   (require 'mm-util))
39
40 (require 'mm-encode)
41 (require 'mml-sec)
42
43 (defvar mc-pgp-always-sign)
44
45 (autoload 'quoted-printable-decode-region "qp")
46 (autoload 'quoted-printable-encode-region "qp")
47
48 (autoload 'mm-decode-content-transfer-encoding "mm-bodies")
49 (autoload 'mm-encode-content-transfer-encoding "mm-bodies")
50 (autoload 'message-options-get "message")
51 (autoload 'message-options-set "message")
52
53 (defvar mml1991-use mml2015-use
54   "The package used for PGP.")
55
56 (defvar mml1991-function-alist
57   '((mailcrypt mml1991-mailcrypt-sign
58                mml1991-mailcrypt-encrypt)
59     (pgg mml1991-pgg-sign
60          mml1991-pgg-encrypt)
61     (epg mml1991-epg-sign
62          mml1991-epg-encrypt))
63   "Alist of PGP functions.")
64
65 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
66   "If t, cache passphrase.")
67
68 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
69   "How many seconds the passphrase is cached.
70 Whether the passphrase is cached at all is controlled by
71 `mml1991-cache-passphrase'.")
72
73 (defvar mml1991-signers nil
74   "A list of your own key ID which will be used to sign a message.")
75
76 (defvar mml1991-encrypt-to-self nil
77   "If t, add your own key ID to recipient list when encryption.")
78
79 ;;; mailcrypt wrapper
80
81 (autoload 'mc-sign-generic "mc-toplev")
82
83 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
84 (defvar mml1991-verify-function 'mailcrypt-verify)
85
86 (defun mml1991-mailcrypt-sign (cont)
87   (let ((text (current-buffer))
88         headers signature
89         (result-buffer (get-buffer-create "*GPG Result*")))
90     ;; Save MIME Content[^ ]+: headers from signing
91     (goto-char (point-min))
92     (while (looking-at "^Content[^ ]+:") (forward-line))
93     (unless (bobp)
94       (setq headers (buffer-string))
95       (delete-region (point-min) (point)))
96     (goto-char (point-max))
97     (unless (bolp)
98       (insert "\n"))
99     (quoted-printable-decode-region (point-min) (point-max))
100     (with-temp-buffer
101       (setq signature (current-buffer))
102       (insert-buffer-substring text)
103       (unless (mc-sign-generic (message-options-get 'message-sender)
104                                nil nil nil nil)
105         (unless (> (point-max) (point-min))
106           (pop-to-buffer result-buffer)
107           (error "Sign error")))
108       (goto-char (point-min))
109       (while (re-search-forward "\r+$" nil t)
110         (replace-match "" t t))
111       (quoted-printable-encode-region (point-min) (point-max))
112       (set-buffer text)
113       (delete-region (point-min) (point-max))
114       (if headers (insert headers))
115       (insert "\n")
116       (insert-buffer-substring signature)
117       (goto-char (point-max)))))
118
119 (declare-function mc-encrypt-generic "ext:mc-toplev"
120                   (&optional recipients scheme start end from sign))
121
122 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
123   (let ((text (current-buffer))
124         (mc-pgp-always-sign
125          (or mc-pgp-always-sign
126              sign
127              (eq t (or (message-options-get 'message-sign-encrypt)
128                        (message-options-set
129                         'message-sign-encrypt
130                         (or (y-or-n-p "Sign the message? ")
131                             'not))))
132              'never))
133         cipher
134         (result-buffer (get-buffer-create "*GPG Result*")))
135     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
136     (goto-char (point-min))
137     (while (looking-at "^Content[^ ]+:") (forward-line))
138     (unless (bobp)
139       (delete-region (point-min) (point)))
140     (mm-with-unibyte-current-buffer
141       (with-temp-buffer
142         (inline (mm-disable-multibyte))
143         (setq cipher (current-buffer))
144         (insert-buffer-substring text)
145         (unless (mc-encrypt-generic
146                  (or
147                   (message-options-get 'message-recipients)
148                   (message-options-set 'message-recipients
149                                        (read-string "Recipients: ")))
150                  nil
151                  (point-min) (point-max)
152                  (message-options-get 'message-sender)
153                  'sign)
154           (unless (> (point-max) (point-min))
155             (pop-to-buffer result-buffer)
156             (error "Encrypt error")))
157         (goto-char (point-min))
158         (while (re-search-forward "\r+$" nil t)
159           (replace-match "" t t))
160         (set-buffer text)
161         (delete-region (point-min) (point-max))
162         ;;(insert "Content-Type: application/pgp-encrypted\n\n")
163         ;;(insert "Version: 1\n\n")
164         (insert "\n")
165         (insert-buffer-substring cipher)
166         (goto-char (point-max))))))
167
168 ;; pgg wrapper
169
170 (autoload 'pgg-sign-region "pgg")
171 (autoload 'pgg-encrypt-region "pgg")
172
173 (defvar pgg-default-user-id)
174 (defvar pgg-errors-buffer)
175 (defvar pgg-output-buffer)
176
177 (defun mml1991-pgg-sign (cont)
178   (let ((pgg-text-mode t)
179         (pgg-default-user-id (or (message-options-get 'mml-sender)
180                                  pgg-default-user-id))
181         headers cte)
182     ;; Don't sign headers.
183     (goto-char (point-min))
184     (when (re-search-forward "^$" nil t)
185       (setq headers (buffer-substring (point-min) (point)))
186       (save-restriction
187         (narrow-to-region (point-min) (point))
188         (setq cte (mail-fetch-field "content-transfer-encoding")))
189       (forward-line 1)
190       (delete-region (point-min) (point))
191       (when cte
192         (setq cte (intern (downcase cte)))
193         (mm-decode-content-transfer-encoding cte)))
194     (unless (pgg-sign-region (point-min) (point-max) t)
195       (pop-to-buffer pgg-errors-buffer)
196       (error "Encrypt error"))
197     (delete-region (point-min) (point-max))
198     (mm-with-unibyte-current-buffer
199       (insert-buffer-substring pgg-output-buffer)
200       (goto-char (point-min))
201       (while (re-search-forward "\r+$" nil t)
202         (replace-match "" t t))
203       (when cte
204         (mm-encode-content-transfer-encoding cte))
205       (goto-char (point-min))
206       (when headers
207         (insert headers))
208       (insert "\n"))
209     t))
210
211 (defun mml1991-pgg-encrypt (cont &optional sign)
212   (goto-char (point-min))
213   (when (re-search-forward "^$" nil t)
214     (let ((cte (save-restriction
215                  (narrow-to-region (point-min) (point))
216                  (mail-fetch-field "content-transfer-encoding"))))
217       ;; Strip MIME headers since it will be ASCII armored.
218       (forward-line 1)
219       (delete-region (point-min) (point))
220       (when cte
221         (mm-decode-content-transfer-encoding (intern (downcase cte))))))
222   (unless (let ((pgg-text-mode t))
223             (pgg-encrypt-region
224              (point-min) (point-max)
225              (split-string
226               (or
227                (message-options-get 'message-recipients)
228                (message-options-set 'message-recipients
229                                     (read-string "Recipients: ")))
230               "[ \f\t\n\r\v,]+")
231              sign))
232     (pop-to-buffer pgg-errors-buffer)
233     (error "Encrypt error"))
234   (delete-region (point-min) (point-max))
235   (insert "\n")
236   (insert-buffer-substring pgg-output-buffer)
237   t)
238
239 ;; epg wrapper
240
241 (defvar epg-user-id-alist)
242
243 (autoload 'epg-make-context "epg")
244 (autoload 'epg-passphrase-callback-function "epg")
245 (autoload 'epa-select-keys "epa")
246 (autoload 'epg-list-keys "epg")
247 (autoload 'epg-context-set-armor "epg")
248 (autoload 'epg-context-set-textmode "epg")
249 (autoload 'epg-context-set-signers "epg")
250 (autoload 'epg-context-set-passphrase-callback "epg")
251 (autoload 'epg-sign-string "epg")
252 (autoload 'epg-encrypt-string "epg")
253 (autoload 'epg-configuration "epg-config")
254 (autoload 'epg-expand-group "epg-config")
255
256 (defvar mml1991-epg-secret-key-id-list nil)
257
258 (defun mml1991-epg-passphrase-callback (context key-id ignore)
259   (if (eq key-id 'SYM)
260       (epg-passphrase-callback-function context key-id nil)
261     (let* ((entry (assoc key-id epg-user-id-alist))
262            (passphrase
263             (password-read
264              (format "GnuPG passphrase for %s: "
265                      (if entry
266                          (cdr entry)
267                        key-id))
268              (if (eq key-id 'PIN)
269                  "PIN"
270                key-id))))
271       (when passphrase
272         (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
273           (password-cache-add key-id passphrase))
274         (setq mml1991-epg-secret-key-id-list
275               (cons key-id mml1991-epg-secret-key-id-list))
276         (copy-sequence passphrase)))))
277
278 (defun mml1991-epg-sign (cont)
279   (let ((context (epg-make-context))
280         headers cte signers signature)
281     (if (eq mm-sign-option 'guided)
282         (setq signers (epa-select-keys context "Select keys for signing.
283 If no one is selected, default secret key is used.  "
284                                        mml1991-signers t))
285       (if mml1991-signers
286           (setq signers (mapcar (lambda (name)
287                                   (car (epg-list-keys context name t)))
288                                 mml1991-signers))))
289     (epg-context-set-armor context t)
290     (epg-context-set-textmode context t)
291     (epg-context-set-signers context signers)
292     (if mml1991-cache-passphrase
293         (epg-context-set-passphrase-callback
294          context
295          #'mml1991-epg-passphrase-callback))
296     ;; Don't sign headers.
297     (goto-char (point-min))
298     (when (re-search-forward "^$" nil t)
299       (setq headers (buffer-substring (point-min) (point)))
300       (save-restriction
301         (narrow-to-region (point-min) (point))
302         (setq cte (mail-fetch-field "content-transfer-encoding")))
303       (forward-line 1)
304       (delete-region (point-min) (point))
305       (when cte
306         (setq cte (intern (downcase cte)))
307         (mm-decode-content-transfer-encoding cte)))
308     (condition-case error
309         (setq signature (epg-sign-string context (buffer-string) 'clear)
310               mml1991-epg-secret-key-id-list nil)
311       (error
312        (while mml1991-epg-secret-key-id-list
313          (password-cache-remove (car mml1991-epg-secret-key-id-list))
314          (setq mml1991-epg-secret-key-id-list
315                (cdr mml1991-epg-secret-key-id-list)))
316        (signal (car error) (cdr error))))
317     (delete-region (point-min) (point-max))
318     (mm-with-unibyte-current-buffer
319       (insert signature)
320       (goto-char (point-min))
321       (while (re-search-forward "\r+$" nil t)
322         (replace-match "" t t))
323       (when cte
324         (mm-encode-content-transfer-encoding cte))
325       (goto-char (point-min))
326       (when headers
327         (insert headers))
328       (insert "\n"))
329     t))
330
331 (defun mml1991-epg-encrypt (cont &optional sign)
332   (goto-char (point-min))
333   (when (re-search-forward "^$" nil t)
334     (let ((cte (save-restriction
335                  (narrow-to-region (point-min) (point))
336                  (mail-fetch-field "content-transfer-encoding"))))
337       ;; Strip MIME headers since it will be ASCII armored.
338       (forward-line 1)
339       (delete-region (point-min) (point))
340       (when cte
341         (mm-decode-content-transfer-encoding (intern (downcase cte))))))
342   (let ((context (epg-make-context))
343         (recipients
344          (if (message-options-get 'message-recipients)
345              (split-string
346               (message-options-get 'message-recipients)
347               "[ \f\t\n\r\v,]+")))
348         cipher signers config)
349     ;; We should remove this check if epg-0.0.6 is released.
350     (if (and (condition-case nil
351                  (require 'epg-config)
352                (error))
353              (functionp #'epg-expand-group))
354         (setq config (epg-configuration)
355               recipients
356               (apply #'nconc
357                      (mapcar (lambda (recipient)
358                                (or (epg-expand-group config recipient)
359                                    (list recipient)))
360                              recipients))))
361     (if (eq mm-encrypt-option 'guided)
362         (setq recipients
363               (epa-select-keys context "Select recipients for encryption.
364 If no one is selected, symmetric encryption will be performed.  "
365                                recipients))
366       (setq recipients
367             (delq nil (mapcar (lambda (name)
368                                 (car (epg-list-keys context name)))
369                               recipients))))
370     (if mml1991-encrypt-to-self
371         (if mml1991-signers
372             (setq recipients
373                   (nconc recipients
374                          (mapcar (lambda (name)
375                                    (car (epg-list-keys context name)))
376                                  mml1991-signers)))
377           (error "mml1991-signers not set")))
378     (when sign
379       (if (eq mm-sign-option 'guided)
380           (setq signers (epa-select-keys context "Select keys for signing.
381 If no one is selected, default secret key is used.  "
382                                          mml1991-signers t))
383         (if mml1991-signers
384             (setq signers (mapcar (lambda (name)
385                                     (car (epg-list-keys context name t)))
386                                   mml1991-signers))))
387       (epg-context-set-signers context signers))
388     (epg-context-set-armor context t)
389     (epg-context-set-textmode context t)
390     (if mml1991-cache-passphrase
391         (epg-context-set-passphrase-callback
392          context
393          #'mml1991-epg-passphrase-callback))
394     (condition-case error
395         (setq cipher
396               (epg-encrypt-string context (buffer-string) recipients sign)
397               mml1991-epg-secret-key-id-list nil)
398       (error
399        (while mml1991-epg-secret-key-id-list
400          (password-cache-remove (car mml1991-epg-secret-key-id-list))
401          (setq mml1991-epg-secret-key-id-list
402                (cdr mml1991-epg-secret-key-id-list)))
403        (signal (car error) (cdr error))))
404     (delete-region (point-min) (point-max))
405     (insert "\n" cipher))
406   t)
407
408 ;;;###autoload
409 (defun mml1991-encrypt (cont &optional sign)
410   (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
411     (if func
412         (funcall func cont sign)
413       (error "Cannot find encrypt function"))))
414
415 ;;;###autoload
416 (defun mml1991-sign (cont)
417   (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
418     (if func
419         (funcall func cont)
420       (error "Cannot find sign function"))))
421
422 (provide 'mml1991)
423
424 ;; Local Variables:
425 ;; coding: iso-8859-1
426 ;; End:
427
428 ;;; mml1991.el ends here