* lisp/term.el (term-set-escape-char): Properly set term-escape-char.
[emacs.git] / lisp / gnus / mml1991.el
blob97de6f0959bac24233d08e78aa7b8b80b2d8e921
1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
5 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
6 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
7 ;; Keywords: PGP
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-and-compile
29 ;; For Emacs <22.2 and XEmacs.
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
32 (if (locate-library "password-cache")
33 (require 'password-cache)
34 (require 'password)))
36 (eval-when-compile
37 (require 'cl)
38 (require 'mm-util))
40 (require 'mm-encode)
41 (require 'mml-sec)
43 (defvar mc-pgp-always-sign)
45 (autoload 'quoted-printable-decode-region "qp")
46 (autoload 'quoted-printable-encode-region "qp")
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")
53 (defvar mml1991-use mml2015-use
54 "The package used for PGP.")
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.")
65 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
66 "If t, cache passphrase.")
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'.")
73 (defvar mml1991-signers nil
74 "A list of your own key ID which will be used to sign a message.")
76 (defvar mml1991-encrypt-to-self nil
77 "If t, add your own key ID to recipient list when encryption.")
79 ;;; mailcrypt wrapper
81 (autoload 'mc-sign-generic "mc-toplev")
83 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
84 (defvar mml1991-verify-function 'mailcrypt-verify)
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)))))
119 (declare-function mc-encrypt-generic "ext:mc-toplev"
120 (&optional recipients scheme start end from sign))
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 (with-temp-buffer
141 (inline (mm-disable-multibyte))
142 (setq cipher (current-buffer))
143 (insert-buffer-substring text)
144 (unless (mc-encrypt-generic
146 (message-options-get 'message-recipients)
147 (message-options-set 'message-recipients
148 (read-string "Recipients: ")))
150 (point-min) (point-max)
151 (message-options-get 'message-sender)
152 'sign)
153 (unless (> (point-max) (point-min))
154 (pop-to-buffer result-buffer)
155 (error "Encrypt error")))
156 (goto-char (point-min))
157 (while (re-search-forward "\r+$" nil t)
158 (replace-match "" t t))
159 (set-buffer text)
160 (delete-region (point-min) (point-max))
161 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
162 ;;(insert "Version: 1\n\n")
163 (insert "\n")
164 (insert-buffer-substring cipher)
165 (goto-char (point-max)))))
167 ;; pgg wrapper
169 (autoload 'pgg-sign-region "pgg")
170 (autoload 'pgg-encrypt-region "pgg")
172 (defvar pgg-default-user-id)
173 (defvar pgg-errors-buffer)
174 (defvar pgg-output-buffer)
176 (defun mml1991-pgg-sign (cont)
177 (let ((pgg-text-mode t)
178 (pgg-default-user-id (or (message-options-get 'mml-sender)
179 pgg-default-user-id))
180 headers cte)
181 ;; Don't sign headers.
182 (goto-char (point-min))
183 (when (re-search-forward "^$" nil t)
184 (setq headers (buffer-substring (point-min) (point)))
185 (save-restriction
186 (narrow-to-region (point-min) (point))
187 (setq cte (mail-fetch-field "content-transfer-encoding")))
188 (forward-line 1)
189 (delete-region (point-min) (point))
190 (when cte
191 (setq cte (intern (downcase cte)))
192 (mm-decode-content-transfer-encoding cte)))
193 (unless (pgg-sign-region (point-min) (point-max) t)
194 (pop-to-buffer pgg-errors-buffer)
195 (error "Encrypt error"))
196 (delete-region (point-min) (point-max))
197 (mm-with-unibyte-current-buffer
198 (insert-buffer-substring pgg-output-buffer)
199 (goto-char (point-min))
200 (while (re-search-forward "\r+$" nil t)
201 (replace-match "" t t))
202 (when cte
203 (mm-encode-content-transfer-encoding cte))
204 (goto-char (point-min))
205 (when headers
206 (insert headers))
207 (insert "\n"))
210 (defun mml1991-pgg-encrypt (cont &optional sign)
211 (goto-char (point-min))
212 (when (re-search-forward "^$" nil t)
213 (let ((cte (save-restriction
214 (narrow-to-region (point-min) (point))
215 (mail-fetch-field "content-transfer-encoding"))))
216 ;; Strip MIME headers since it will be ASCII armored.
217 (forward-line 1)
218 (delete-region (point-min) (point))
219 (when cte
220 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
221 (unless (let ((pgg-text-mode t))
222 (pgg-encrypt-region
223 (point-min) (point-max)
224 (split-string
226 (message-options-get 'message-recipients)
227 (message-options-set 'message-recipients
228 (read-string "Recipients: ")))
229 "[ \f\t\n\r\v,]+")
230 sign))
231 (pop-to-buffer pgg-errors-buffer)
232 (error "Encrypt error"))
233 (delete-region (point-min) (point-max))
234 (insert "\n")
235 (insert-buffer-substring pgg-output-buffer)
238 ;; epg wrapper
240 (defvar epg-user-id-alist)
242 (autoload 'epg-make-context "epg")
243 (autoload 'epg-passphrase-callback-function "epg")
244 (autoload 'epa-select-keys "epa")
245 (autoload 'epg-list-keys "epg")
246 (autoload 'epg-context-set-armor "epg")
247 (autoload 'epg-context-set-textmode "epg")
248 (autoload 'epg-context-set-signers "epg")
249 (autoload 'epg-context-set-passphrase-callback "epg")
250 (autoload 'epg-key-sub-key-list "epg")
251 (autoload 'epg-sub-key-capability "epg")
252 (autoload 'epg-sub-key-validity "epg")
253 (autoload 'epg-sub-key-fingerprint "epg")
254 (autoload 'epg-sign-string "epg")
255 (autoload 'epg-encrypt-string "epg")
256 (autoload 'epg-configuration "epg-config")
257 (autoload 'epg-expand-group "epg-config")
259 (defvar mml1991-epg-secret-key-id-list nil)
261 (defun mml1991-epg-passphrase-callback (context key-id ignore)
262 (if (eq key-id 'SYM)
263 (epg-passphrase-callback-function context key-id nil)
264 (let* ((entry (assoc key-id epg-user-id-alist))
265 (passphrase
266 (password-read
267 (format "GnuPG passphrase for %s: "
268 (if entry
269 (cdr entry)
270 key-id))
271 (if (eq key-id 'PIN)
272 "PIN"
273 key-id))))
274 (when passphrase
275 (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
276 (password-cache-add key-id passphrase))
277 (setq mml1991-epg-secret-key-id-list
278 (cons key-id mml1991-epg-secret-key-id-list))
279 (copy-sequence passphrase)))))
281 (defun mml1991-epg-find-usable-key (keys usage)
282 (catch 'found
283 (while keys
284 (let ((pointer (epg-key-sub-key-list (car keys))))
285 ;; The primary key will be marked as disabled, when the entire
286 ;; key is disabled (see 12 Field, Format of colon listings, in
287 ;; gnupg/doc/DETAILS)
288 (unless (memq 'disabled (epg-sub-key-capability (car pointer)))
289 (while pointer
290 (if (and (memq usage (epg-sub-key-capability (car pointer)))
291 (not (memq (epg-sub-key-validity (car pointer))
292 '(revoked expired))))
293 (throw 'found (car keys)))
294 (setq pointer (cdr pointer)))))
295 (setq keys (cdr keys)))))
297 ;; XXX: since gpg --list-secret-keys does not return validity of each
298 ;; key, `mml1991-epg-find-usable-key' defined above is not enough for
299 ;; secret keys. The function `mml1991-epg-find-usable-secret-key'
300 ;; below looks at appropriate public keys to check usability.
301 (defun mml1991-epg-find-usable-secret-key (context name usage)
302 (let ((secret-keys (epg-list-keys context name t))
303 secret-key)
304 (while (and (not secret-key) secret-keys)
305 (if (mml1991-epg-find-usable-key
306 (epg-list-keys context (epg-sub-key-fingerprint
307 (car (epg-key-sub-key-list
308 (car secret-keys)))))
309 usage)
310 (setq secret-key (car secret-keys)
311 secret-keys nil)
312 (setq secret-keys (cdr secret-keys))))
313 secret-key))
315 (defun mml1991-epg-sign (cont)
316 (let ((context (epg-make-context))
317 headers cte signer-key signers signature)
318 (if (eq mm-sign-option 'guided)
319 (setq signers (epa-select-keys context "Select keys for signing.
320 If no one is selected, default secret key is used. "
321 mml1991-signers t))
322 (if mml1991-signers
323 (setq signers (delq nil
324 (mapcar
325 (lambda (name)
326 (setq signer-key
327 (mml1991-epg-find-usable-secret-key
328 context name 'sign))
329 (unless (or signer-key
330 (y-or-n-p
331 (format
332 "No secret key for %s; skip it? "
333 name)))
334 (error "No secret key for %s" name))
335 signer-key)
336 mml1991-signers)))))
337 (epg-context-set-armor context t)
338 (epg-context-set-textmode context t)
339 (epg-context-set-signers context signers)
340 (if mml1991-cache-passphrase
341 (epg-context-set-passphrase-callback
342 context
343 #'mml1991-epg-passphrase-callback))
344 ;; Don't sign headers.
345 (goto-char (point-min))
346 (when (re-search-forward "^$" nil t)
347 (setq headers (buffer-substring (point-min) (point)))
348 (save-restriction
349 (narrow-to-region (point-min) (point))
350 (setq cte (mail-fetch-field "content-transfer-encoding")))
351 (forward-line 1)
352 (delete-region (point-min) (point))
353 (when cte
354 (setq cte (intern (downcase cte)))
355 (mm-decode-content-transfer-encoding cte)))
356 (condition-case error
357 (setq signature (epg-sign-string context (buffer-string) 'clear)
358 mml1991-epg-secret-key-id-list nil)
359 (error
360 (while mml1991-epg-secret-key-id-list
361 (password-cache-remove (car mml1991-epg-secret-key-id-list))
362 (setq mml1991-epg-secret-key-id-list
363 (cdr mml1991-epg-secret-key-id-list)))
364 (signal (car error) (cdr error))))
365 (delete-region (point-min) (point-max))
366 (mm-with-unibyte-current-buffer
367 (insert signature)
368 (goto-char (point-min))
369 (while (re-search-forward "\r+$" nil t)
370 (replace-match "" t t))
371 (when cte
372 (mm-encode-content-transfer-encoding cte))
373 (goto-char (point-min))
374 (when headers
375 (insert headers))
376 (insert "\n"))
379 (defun mml1991-epg-encrypt (cont &optional sign)
380 (goto-char (point-min))
381 (when (re-search-forward "^$" nil t)
382 (let ((cte (save-restriction
383 (narrow-to-region (point-min) (point))
384 (mail-fetch-field "content-transfer-encoding"))))
385 ;; Strip MIME headers since it will be ASCII armored.
386 (forward-line 1)
387 (delete-region (point-min) (point))
388 (when cte
389 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
390 (let ((context (epg-make-context))
391 (recipients
392 (if (message-options-get 'message-recipients)
393 (split-string
394 (message-options-get 'message-recipients)
395 "[ \f\t\n\r\v,]+")))
396 recipient-key signer-key cipher signers config)
397 (when mml1991-encrypt-to-self
398 (unless mml1991-signers
399 (error "mml1991-signers is not set"))
400 (setq recipients (nconc recipients mml1991-signers)))
401 ;; We should remove this check if epg-0.0.6 is released.
402 (if (and (condition-case nil
403 (require 'epg-config)
404 (error))
405 (functionp #'epg-expand-group))
406 (setq config (epg-configuration)
407 recipients
408 (apply #'nconc
409 (mapcar (lambda (recipient)
410 (or (epg-expand-group config recipient)
411 (list recipient)))
412 recipients))))
413 (if (eq mm-encrypt-option 'guided)
414 (setq recipients
415 (epa-select-keys context "Select recipients for encryption.
416 If no one is selected, symmetric encryption will be performed. "
417 recipients))
418 (setq recipients
419 (delq nil (mapcar
420 (lambda (name)
421 (setq recipient-key (mml1991-epg-find-usable-key
422 (epg-list-keys context name)
423 'encrypt))
424 (unless (or recipient-key
425 (y-or-n-p
426 (format "No public key for %s; skip it? "
427 name)))
428 (error "No public key for %s" name))
429 recipient-key)
430 recipients)))
431 (unless recipients
432 (error "No recipient specified")))
433 (when sign
434 (if (eq mm-sign-option 'guided)
435 (setq signers (epa-select-keys context "Select keys for signing.
436 If no one is selected, default secret key is used. "
437 mml1991-signers t))
438 (if mml1991-signers
439 (setq signers (delq nil
440 (mapcar
441 (lambda (name)
442 (mml1991-epg-find-usable-secret-key
443 context name 'sign))
444 mml1991-signers)))))
445 (epg-context-set-signers context signers))
446 (epg-context-set-armor context t)
447 (epg-context-set-textmode context t)
448 (if mml1991-cache-passphrase
449 (epg-context-set-passphrase-callback
450 context
451 #'mml1991-epg-passphrase-callback))
452 (condition-case error
453 (setq cipher
454 (epg-encrypt-string context (buffer-string) recipients sign)
455 mml1991-epg-secret-key-id-list nil)
456 (error
457 (while mml1991-epg-secret-key-id-list
458 (password-cache-remove (car mml1991-epg-secret-key-id-list))
459 (setq mml1991-epg-secret-key-id-list
460 (cdr mml1991-epg-secret-key-id-list)))
461 (signal (car error) (cdr error))))
462 (delete-region (point-min) (point-max))
463 (insert "\n" cipher))
466 ;;;###autoload
467 (defun mml1991-encrypt (cont &optional sign)
468 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
469 (if func
470 (funcall func cont sign)
471 (error "Cannot find encrypt function"))))
473 ;;;###autoload
474 (defun mml1991-sign (cont)
475 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
476 (if func
477 (funcall func cont)
478 (error "Cannot find sign function"))))
480 (provide 'mml1991)
482 ;; Local Variables:
483 ;; coding: iso-8859-1
484 ;; End:
486 ;;; mml1991.el ends here