]> code.delx.au - gnu-emacs/blob - lisp/gnus/mml2015.el
Simplify some rococo Gnus code that loads or tests for other libraries.
[gnu-emacs] / lisp / gnus / mml2015.el
1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
4
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: PGP MIME MML
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
26 ;; with both.
27
28 ;;; Code:
29
30 (eval-and-compile
31 (if (locate-library "password-cache")
32 (require 'password-cache)
33 (require 'password)))
34
35 (eval-when-compile (require 'cl))
36 (require 'mm-decode)
37 (require 'mm-util)
38 (require 'mml)
39 (require 'mml-sec)
40 (require 'epg-config)
41
42 (defvar mc-pgp-always-sign)
43
44 (declare-function epg-check-configuration "ext:epg-config"
45 (config &optional minimum-version))
46 (declare-function epg-configuration "ext:epg-config" ())
47
48 ;; Maybe this should be in eg mml-sec.el (and have a different name).
49 ;; Then mml1991 would not need to require mml2015, and mml1991-use
50 ;; could be removed.
51 (defvar mml2015-use 'epg
52 "The package used for PGP/MIME.
53 Valid packages include `epg', `pgg' and `mailcrypt'.")
54
55 ;; Something is not RFC2015.
56 (defvar mml2015-function-alist
57 '((mailcrypt mml2015-mailcrypt-sign
58 mml2015-mailcrypt-encrypt
59 mml2015-mailcrypt-verify
60 mml2015-mailcrypt-decrypt
61 mml2015-mailcrypt-clear-verify
62 mml2015-mailcrypt-clear-decrypt)
63 (pgg mml2015-pgg-sign
64 mml2015-pgg-encrypt
65 mml2015-pgg-verify
66 mml2015-pgg-decrypt
67 mml2015-pgg-clear-verify
68 mml2015-pgg-clear-decrypt)
69 (epg mml2015-epg-sign
70 mml2015-epg-encrypt
71 mml2015-epg-verify
72 mml2015-epg-decrypt
73 mml2015-epg-clear-verify
74 mml2015-epg-clear-decrypt))
75 "Alist of PGP/MIME functions.")
76
77 (defvar mml2015-result-buffer nil)
78
79 (defcustom mml2015-unabbrev-trust-alist
80 '(("TRUST_UNDEFINED" . nil)
81 ("TRUST_NEVER" . nil)
82 ("TRUST_MARGINAL" . t)
83 ("TRUST_FULLY" . t)
84 ("TRUST_ULTIMATE" . t))
85 "Map GnuPG trust output values to a boolean saying if you trust the key."
86 :version "22.1"
87 :group 'mime-security
88 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
89 (boolean :tag "Trust key"))))
90
91 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
92 "If t, cache passphrase."
93 :group 'mime-security
94 :type 'boolean)
95 (make-obsolete-variable 'mml2015-cache-passphrase
96 'mml-secure-cache-passphrase
97 "25.1")
98
99 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
100 "How many seconds the passphrase is cached.
101 Whether the passphrase is cached at all is controlled by
102 `mml2015-cache-passphrase'."
103 :group 'mime-security
104 :type 'integer)
105 (make-obsolete-variable 'mml2015-passphrase-cache-expiry
106 'mml-secure-passphrase-cache-expiry
107 "25.1")
108
109 (defcustom mml2015-signers nil
110 "A list of your own key ID(s) which will be used to sign a message.
111 If set, it overrides the setting of `mml2015-sign-with-sender'."
112 :group 'mime-security
113 :type '(repeat (string :tag "Key ID")))
114
115 (defcustom mml2015-sign-with-sender nil
116 "If t, use message sender so find a key to sign with."
117 :group 'mime-security
118 :type 'boolean
119 :version "24.1")
120
121 (defcustom mml2015-encrypt-to-self nil
122 "If t, add your own key ID to recipient list when encryption."
123 :group 'mime-security
124 :type 'boolean)
125
126 (defcustom mml2015-always-trust t
127 "If t, GnuPG skip key validation on encryption."
128 :group 'mime-security
129 :type 'boolean)
130
131 (defcustom mml2015-maximum-key-image-dimension 64
132 "The maximum dimension (width or height) of any key images."
133 :version "24.4"
134 :group 'mime-security
135 :type 'integer)
136
137 (defcustom mml2015-display-key-image t
138 "If t, try to display key images."
139 :version "24.5"
140 :group 'mime-security
141 :type 'boolean)
142
143 ;; Extract plaintext from cleartext signature. IMO, this kind of task
144 ;; should be done by GnuPG rather than Elisp, but older PGP backends
145 ;; (such as Mailcrypt, and PGG) discard the output from GnuPG.
146 (defun mml2015-extract-cleartext-signature ()
147 ;; Daiki Ueno in
148 ;; <54a15d860801080142l70b95d7dkac4bf51a86196011@mail.gmail.com>: ``I still
149 ;; believe that the right way is to use the plaintext output from GnuPG as
150 ;; it is, and mml2015-extract-cleartext-signature is just a kludge for
151 ;; misdesigned libraries like PGG, which have no ability to do that. So, I
152 ;; think it should not have descriptive documentation.''
153 ;;
154 ;; This function doesn't handle NotDashEscaped correctly. EasyPG handles it
155 ;; correctly.
156 ;; http://thread.gmane.org/gmane.emacs.gnus.general/66062/focus=66082
157 ;; http://thread.gmane.org/gmane.emacs.gnus.general/65087/focus=65109
158 (goto-char (point-min))
159 (forward-line)
160 ;; We need to be careful not to strip beyond the armor headers.
161 ;; Previously, an attacker could replace the text inside our
162 ;; markup with trailing garbage by injecting whitespace into the
163 ;; message.
164 (while (looking-at "Hash:") ; The only header allowed in cleartext
165 (forward-line)) ; signatures according to RFC2440.
166 (when (looking-at "[\t ]*$")
167 (forward-line))
168 (delete-region (point-min) (point))
169 (if (re-search-forward "^-----BEGIN PGP SIGNATURE-----" nil t)
170 (delete-region (match-beginning 0) (point-max)))
171 (goto-char (point-min))
172 (while (re-search-forward "^- " nil t)
173 (replace-match "" t t)
174 (forward-line 1)))
175
176 ;;; mailcrypt wrapper
177
178 (autoload 'mailcrypt-decrypt "mailcrypt")
179 (autoload 'mailcrypt-verify "mailcrypt")
180 (autoload 'mc-pgp-always-sign "mailcrypt")
181 (autoload 'mc-encrypt-generic "mc-toplev")
182 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
183 (autoload 'mc-sign-generic "mc-toplev")
184
185 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
186 (defvar mml2015-verify-function 'mailcrypt-verify)
187
188 (defun mml2015-format-error (err)
189 (if (stringp (cadr err))
190 (cadr err)
191 (format "%S" (cdr err))))
192
193 (defun mml2015-mailcrypt-decrypt (handle ctl)
194 (catch 'error
195 (let (child handles result)
196 (unless (setq child (mm-find-part-by-type
197 (cdr handle)
198 "application/octet-stream" nil t))
199 (mm-set-handle-multipart-parameter
200 mm-security-handle 'gnus-info "Corrupted")
201 (throw 'error handle))
202 (with-temp-buffer
203 (mm-insert-part child)
204 (setq result
205 (condition-case err
206 (funcall mml2015-decrypt-function)
207 (error
208 (mm-set-handle-multipart-parameter
209 mm-security-handle 'gnus-details (mml2015-format-error err))
210 nil)
211 (quit
212 (mm-set-handle-multipart-parameter
213 mm-security-handle 'gnus-details "Quit.")
214 nil)))
215 (unless (car result)
216 (mm-set-handle-multipart-parameter
217 mm-security-handle 'gnus-info "Failed")
218 (throw 'error handle))
219 (setq handles (mm-dissect-buffer t)))
220 (mm-destroy-parts handle)
221 (mm-set-handle-multipart-parameter
222 mm-security-handle 'gnus-info
223 (concat "OK"
224 (let ((sig (with-current-buffer mml2015-result-buffer
225 (mml2015-gpg-extract-signature-details))))
226 (concat ", Signer: " sig))))
227 (if (listp (car handles))
228 handles
229 (list handles)))))
230
231 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
232 (let* ((result "")
233 (fpr-length (string-width fingerprint))
234 (n-slice 0)
235 slice)
236 (setq fingerprint (string-to-list fingerprint))
237 (while fingerprint
238 (setq fpr-length (- fpr-length 4))
239 (setq slice (butlast fingerprint fpr-length))
240 (setq fingerprint (nthcdr 4 fingerprint))
241 (setq n-slice (1+ n-slice))
242 (setq result
243 (concat
244 result
245 (case n-slice
246 (1 slice)
247 (otherwise (concat " " slice))))))
248 result))
249
250 (defun mml2015-gpg-extract-signature-details ()
251 (goto-char (point-min))
252 (let* ((expired (re-search-forward
253 "^\\[GNUPG:\\] SIGEXPIRED$"
254 nil t))
255 (signer (and (re-search-forward
256 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
257 nil t)
258 (cons (match-string 1) (match-string 2))))
259 (fprint (and (re-search-forward
260 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
261 nil t)
262 (match-string 1)))
263 (trust (and (re-search-forward
264 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
265 nil t)
266 (match-string 1)))
267 (trust-good-enough-p
268 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
269 (cond ((and signer fprint)
270 (concat (cdr signer)
271 (unless trust-good-enough-p
272 (concat "\nUntrusted, Fingerprint: "
273 (mml2015-gpg-pretty-print-fpr fprint)))
274 (when expired
275 (format "\nWARNING: Signature from expired key (%s)"
276 (car signer)))))
277 ((re-search-forward
278 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
279 (match-string 2))
280 (t
281 "From unknown user"))))
282
283 (defun mml2015-mailcrypt-clear-decrypt ()
284 (let (result)
285 (setq result
286 (condition-case err
287 (funcall mml2015-decrypt-function)
288 (error
289 (mm-set-handle-multipart-parameter
290 mm-security-handle 'gnus-details (mml2015-format-error err))
291 nil)
292 (quit
293 (mm-set-handle-multipart-parameter
294 mm-security-handle 'gnus-details "Quit.")
295 nil)))
296 (if (car result)
297 (mm-set-handle-multipart-parameter
298 mm-security-handle 'gnus-info "OK")
299 (mm-set-handle-multipart-parameter
300 mm-security-handle 'gnus-info "Failed"))))
301
302 (defun mml2015-fix-micalg (alg)
303 (and alg
304 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
305 (upcase (if (string-match "^p[gh]p-" alg)
306 (substring alg (match-end 0))
307 alg))))
308
309 (defun mml2015-mailcrypt-verify (handle ctl)
310 (catch 'error
311 (let (part)
312 (unless (setq part (mm-find-raw-part-by-type
313 ctl (or (mm-handle-multipart-ctl-parameter
314 ctl 'protocol)
315 "application/pgp-signature")
316 t))
317 (mm-set-handle-multipart-parameter
318 mm-security-handle 'gnus-info "Corrupted")
319 (throw 'error handle))
320 (with-temp-buffer
321 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
322 (insert (format "Hash: %s\n\n"
323 (or (mml2015-fix-micalg
324 (mm-handle-multipart-ctl-parameter
325 ctl 'micalg))
326 "SHA1")))
327 (save-restriction
328 (narrow-to-region (point) (point))
329 (insert part "\n")
330 (goto-char (point-min))
331 (while (not (eobp))
332 (if (looking-at "^-")
333 (insert "- "))
334 (forward-line)))
335 (unless (setq part (mm-find-part-by-type
336 (cdr handle) "application/pgp-signature" nil t))
337 (mm-set-handle-multipart-parameter
338 mm-security-handle 'gnus-info "Corrupted")
339 (throw 'error handle))
340 (save-restriction
341 (narrow-to-region (point) (point))
342 (mm-insert-part part)
343 (goto-char (point-min))
344 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
345 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
346 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
347 (replace-match "-----END PGP SIGNATURE-----" t t)))
348 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
349 (unless (condition-case err
350 (prog1
351 (funcall mml2015-verify-function)
352 (if (get-buffer " *mailcrypt stderr temp")
353 (mm-set-handle-multipart-parameter
354 mm-security-handle 'gnus-details
355 (with-current-buffer " *mailcrypt stderr temp"
356 (buffer-string))))
357 (if (get-buffer " *mailcrypt stdout temp")
358 (kill-buffer " *mailcrypt stdout temp"))
359 (if (get-buffer " *mailcrypt stderr temp")
360 (kill-buffer " *mailcrypt stderr temp"))
361 (if (get-buffer " *mailcrypt status temp")
362 (kill-buffer " *mailcrypt status temp"))
363 (if (get-buffer mc-gpg-debug-buffer)
364 (kill-buffer mc-gpg-debug-buffer)))
365 (error
366 (mm-set-handle-multipart-parameter
367 mm-security-handle 'gnus-details (mml2015-format-error err))
368 nil)
369 (quit
370 (mm-set-handle-multipart-parameter
371 mm-security-handle 'gnus-details "Quit.")
372 nil))
373 (mm-set-handle-multipart-parameter
374 mm-security-handle 'gnus-info "Failed")
375 (throw 'error handle))))
376 (mm-set-handle-multipart-parameter
377 mm-security-handle 'gnus-info "OK")
378 handle)))
379
380 (defun mml2015-mailcrypt-clear-verify ()
381 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
382 (if (condition-case err
383 (prog1
384 (funcall mml2015-verify-function)
385 (if (get-buffer " *mailcrypt stderr temp")
386 (mm-set-handle-multipart-parameter
387 mm-security-handle 'gnus-details
388 (with-current-buffer " *mailcrypt stderr temp"
389 (buffer-string))))
390 (if (get-buffer " *mailcrypt stdout temp")
391 (kill-buffer " *mailcrypt stdout temp"))
392 (if (get-buffer " *mailcrypt stderr temp")
393 (kill-buffer " *mailcrypt stderr temp"))
394 (if (get-buffer " *mailcrypt status temp")
395 (kill-buffer " *mailcrypt status temp"))
396 (if (get-buffer mc-gpg-debug-buffer)
397 (kill-buffer mc-gpg-debug-buffer)))
398 (error
399 (mm-set-handle-multipart-parameter
400 mm-security-handle 'gnus-details (mml2015-format-error err))
401 nil)
402 (quit
403 (mm-set-handle-multipart-parameter
404 mm-security-handle 'gnus-details "Quit.")
405 nil))
406 (mm-set-handle-multipart-parameter
407 mm-security-handle 'gnus-info "OK")
408 (mm-set-handle-multipart-parameter
409 mm-security-handle 'gnus-info "Failed")))
410 (mml2015-extract-cleartext-signature))
411
412 (defun mml2015-mailcrypt-sign (cont)
413 (mc-sign-generic (message-options-get 'message-sender)
414 nil nil nil nil)
415 (let ((boundary (mml-compute-boundary cont))
416 hash point)
417 (goto-char (point-min))
418 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
419 (error "Cannot find signed begin line"))
420 (goto-char (match-beginning 0))
421 (forward-line 1)
422 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
423 (error "Cannot not find PGP hash"))
424 (setq hash (match-string 1))
425 (unless (re-search-forward "^$" nil t)
426 (error "Cannot not find PGP message"))
427 (forward-line 1)
428 (delete-region (point-min) (point))
429 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
430 boundary))
431 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
432 (downcase hash)))
433 (insert (format "\n--%s\n" boundary))
434 (setq point (point))
435 (goto-char (point-max))
436 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
437 (error "Cannot find signature part"))
438 (replace-match "-----END PGP MESSAGE-----" t t)
439 (goto-char (match-beginning 0))
440 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
441 nil t)
442 (error "Cannot find signature part"))
443 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
444 (goto-char (match-beginning 0))
445 (save-restriction
446 (narrow-to-region point (point))
447 (goto-char point)
448 (while (re-search-forward "^- -" nil t)
449 (replace-match "-" t t))
450 (goto-char (point-max)))
451 (insert (format "--%s\n" boundary))
452 (insert "Content-Type: application/pgp-signature\n\n")
453 (goto-char (point-max))
454 (insert (format "--%s--\n" boundary))
455 (goto-char (point-max))))
456
457 ;; We require mm-decode, which requires mm-bodies, which autoloads
458 ;; message-options-get (!).
459 (declare-function message-options-set "message" (symbol value))
460
461 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
462 (let ((mc-pgp-always-sign
463 (or mc-pgp-always-sign
464 sign
465 (eq t (or (message-options-get 'message-sign-encrypt)
466 (message-options-set
467 'message-sign-encrypt
468 (or (y-or-n-p "Sign the message? ")
469 'not))))
470 'never)))
471 (insert
472 (with-temp-buffer
473 (set-buffer-multibyte nil)
474 (mc-encrypt-generic
475 (or (message-options-get 'message-recipients)
476 (message-options-set 'message-recipients
477 (mc-cleanup-recipient-headers
478 (read-string "Recipients: "))))
479 nil nil nil
480 (message-options-get 'message-sender))
481 (buffer-string))))
482 (goto-char (point-min))
483 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
484 (error "Fail to encrypt the message"))
485 (let ((boundary (mml-compute-boundary cont)))
486 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
487 boundary))
488 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
489 (insert (format "--%s\n" boundary))
490 (insert "Content-Type: application/pgp-encrypted\n\n")
491 (insert "Version: 1\n\n")
492 (insert (format "--%s\n" boundary))
493 (insert "Content-Type: application/octet-stream\n\n")
494 (goto-char (point-max))
495 (insert (format "--%s--\n" boundary))
496 (goto-char (point-max))))
497
498 ;;; pgg wrapper
499
500 (defvar pgg-default-user-id)
501 (defvar pgg-errors-buffer)
502 (defvar pgg-output-buffer)
503
504 (autoload 'pgg-decrypt-region "pgg")
505 (autoload 'pgg-verify-region "pgg")
506 (autoload 'pgg-sign-region "pgg")
507 (autoload 'pgg-encrypt-region "pgg")
508 (autoload 'pgg-parse-armor "pgg-parse")
509
510 (defun mml2015-pgg-decrypt (handle ctl)
511 (catch 'error
512 (let ((pgg-errors-buffer mml2015-result-buffer)
513 child handles result decrypt-status)
514 (unless (setq child (mm-find-part-by-type
515 (cdr handle)
516 "application/octet-stream" nil t))
517 (mm-set-handle-multipart-parameter
518 mm-security-handle 'gnus-info "Corrupted")
519 (throw 'error handle))
520 (with-temp-buffer
521 (mm-insert-part child)
522 (if (condition-case err
523 (prog1
524 (pgg-decrypt-region (point-min) (point-max))
525 (setq decrypt-status
526 (with-current-buffer mml2015-result-buffer
527 (buffer-string)))
528 (mm-set-handle-multipart-parameter
529 mm-security-handle 'gnus-details
530 decrypt-status))
531 (error
532 (mm-set-handle-multipart-parameter
533 mm-security-handle 'gnus-details (mml2015-format-error err))
534 nil)
535 (quit
536 (mm-set-handle-multipart-parameter
537 mm-security-handle 'gnus-details "Quit.")
538 nil))
539 (with-current-buffer pgg-output-buffer
540 (goto-char (point-min))
541 (while (search-forward "\r\n" nil t)
542 (replace-match "\n" t t))
543 (setq handles (mm-dissect-buffer t))
544 (mm-destroy-parts handle)
545 (mm-set-handle-multipart-parameter
546 mm-security-handle 'gnus-info "OK")
547 (mm-set-handle-multipart-parameter
548 mm-security-handle 'gnus-details
549 (concat decrypt-status
550 (when (stringp (car handles))
551 "\n" (mm-handle-multipart-ctl-parameter
552 handles 'gnus-details))))
553 (if (listp (car handles))
554 handles
555 (list handles)))
556 (mm-set-handle-multipart-parameter
557 mm-security-handle 'gnus-info "Failed")
558 (throw 'error handle))))))
559
560 (defun mml2015-pgg-clear-decrypt ()
561 (let ((pgg-errors-buffer mml2015-result-buffer))
562 (if (prog1
563 (pgg-decrypt-region (point-min) (point-max))
564 (mm-set-handle-multipart-parameter
565 mm-security-handle 'gnus-details
566 (with-current-buffer mml2015-result-buffer
567 (buffer-string))))
568 (progn
569 (erase-buffer)
570 ;; Treat data which pgg returns as a unibyte string.
571 (mm-disable-multibyte)
572 (insert-buffer-substring pgg-output-buffer)
573 (goto-char (point-min))
574 (while (search-forward "\r\n" nil t)
575 (replace-match "\n" t t))
576 (mm-set-handle-multipart-parameter
577 mm-security-handle 'gnus-info "OK"))
578 (mm-set-handle-multipart-parameter
579 mm-security-handle 'gnus-info "Failed"))))
580
581 (defun mml2015-pgg-verify (handle ctl)
582 (let ((pgg-errors-buffer mml2015-result-buffer)
583 signature-file part signature)
584 (if (or (null (setq part (mm-find-raw-part-by-type
585 ctl (or (mm-handle-multipart-ctl-parameter
586 ctl 'protocol)
587 "application/pgp-signature")
588 t)))
589 (null (setq signature (mm-find-part-by-type
590 (cdr handle) "application/pgp-signature" nil t))))
591 (progn
592 (mm-set-handle-multipart-parameter
593 mm-security-handle 'gnus-info "Corrupted")
594 handle)
595 (with-temp-buffer
596 (insert part)
597 ;; Convert <LF> to <CR><LF> in signed text. If --textmode is
598 ;; specified when signing, the conversion is not necessary.
599 (goto-char (point-min))
600 (end-of-line)
601 (while (not (eobp))
602 (unless (eq (char-before) ?\r)
603 (insert "\r"))
604 (forward-line)
605 (end-of-line))
606 (with-temp-file (setq signature-file (make-temp-file "pgg"))
607 (mm-insert-part signature))
608 (if (condition-case err
609 (prog1
610 (pgg-verify-region (point-min) (point-max)
611 signature-file t)
612 (goto-char (point-min))
613 (while (search-forward "\r\n" nil t)
614 (replace-match "\n" t t))
615 (mm-set-handle-multipart-parameter
616 mm-security-handle 'gnus-details
617 (concat (with-current-buffer pgg-output-buffer
618 (buffer-string))
619 (with-current-buffer pgg-errors-buffer
620 (buffer-string)))))
621 (error
622 (mm-set-handle-multipart-parameter
623 mm-security-handle 'gnus-details (mml2015-format-error err))
624 nil)
625 (quit
626 (mm-set-handle-multipart-parameter
627 mm-security-handle 'gnus-details "Quit.")
628 nil))
629 (progn
630 (delete-file signature-file)
631 (mm-set-handle-multipart-parameter
632 mm-security-handle 'gnus-info
633 (with-current-buffer pgg-errors-buffer
634 (mml2015-gpg-extract-signature-details))))
635 (delete-file signature-file)
636 (mm-set-handle-multipart-parameter
637 mm-security-handle 'gnus-info "Failed")))))
638 handle)
639
640 (defun mml2015-pgg-clear-verify ()
641 (let ((pgg-errors-buffer mml2015-result-buffer)
642 (text (buffer-string))
643 (coding-system buffer-file-coding-system))
644 (if (condition-case err
645 (prog1
646 (mm-with-unibyte-buffer
647 (insert (encode-coding-string text coding-system))
648 (pgg-verify-region (point-min) (point-max) nil t))
649 (goto-char (point-min))
650 (while (search-forward "\r\n" nil t)
651 (replace-match "\n" t t))
652 (mm-set-handle-multipart-parameter
653 mm-security-handle 'gnus-details
654 (concat (with-current-buffer pgg-output-buffer
655 (buffer-string))
656 (with-current-buffer pgg-errors-buffer
657 (buffer-string)))))
658 (error
659 (mm-set-handle-multipart-parameter
660 mm-security-handle 'gnus-details (mml2015-format-error err))
661 nil)
662 (quit
663 (mm-set-handle-multipart-parameter
664 mm-security-handle 'gnus-details "Quit.")
665 nil))
666 (mm-set-handle-multipart-parameter
667 mm-security-handle 'gnus-info
668 (with-current-buffer pgg-errors-buffer
669 (mml2015-gpg-extract-signature-details)))
670 (mm-set-handle-multipart-parameter
671 mm-security-handle 'gnus-info "Failed")))
672 (mml2015-extract-cleartext-signature))
673
674 (defun mml2015-pgg-sign (cont)
675 (let ((pgg-errors-buffer mml2015-result-buffer)
676 (boundary (mml-compute-boundary cont))
677 (pgg-default-user-id (or (message-options-get 'mml-sender)
678 pgg-default-user-id))
679 (pgg-text-mode t)
680 entry)
681 (unless (pgg-sign-region (point-min) (point-max))
682 (pop-to-buffer mml2015-result-buffer)
683 (error "Sign error"))
684 (goto-char (point-min))
685 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
686 boundary))
687 (if (setq entry (assq 2 (pgg-parse-armor
688 (with-current-buffer pgg-output-buffer
689 (buffer-string)))))
690 (setq entry (assq 'hash-algorithm (cdr entry))))
691 (insert (format "\tmicalg=%s; "
692 (if (cdr entry)
693 (downcase (format "pgp-%s" (cdr entry)))
694 "pgp-sha1")))
695 (insert "protocol=\"application/pgp-signature\"\n")
696 (insert (format "\n--%s\n" boundary))
697 (goto-char (point-max))
698 (insert (format "\n--%s\n" boundary))
699 (insert "Content-Type: application/pgp-signature\n\n")
700 (insert-buffer-substring pgg-output-buffer)
701 (goto-char (point-max))
702 (insert (format "--%s--\n" boundary))
703 (goto-char (point-max))))
704
705 (defun mml2015-pgg-encrypt (cont &optional sign)
706 (let ((pgg-errors-buffer mml2015-result-buffer)
707 (pgg-text-mode t)
708 (boundary (mml-compute-boundary cont)))
709 (unless (pgg-encrypt-region (point-min) (point-max)
710 (split-string
711 (or
712 (message-options-get 'message-recipients)
713 (message-options-set 'message-recipients
714 (read-string "Recipients: ")))
715 "[ \f\t\n\r\v,]+")
716 sign)
717 (pop-to-buffer mml2015-result-buffer)
718 (error "Encrypt error"))
719 (delete-region (point-min) (point-max))
720 (goto-char (point-min))
721 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
722 boundary))
723 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
724 (insert (format "--%s\n" boundary))
725 (insert "Content-Type: application/pgp-encrypted\n\n")
726 (insert "Version: 1\n\n")
727 (insert (format "--%s\n" boundary))
728 (insert "Content-Type: application/octet-stream\n\n")
729 (insert-buffer-substring pgg-output-buffer)
730 (goto-char (point-max))
731 (insert (format "--%s--\n" boundary))
732 (goto-char (point-max))))
733
734 ;;; epg wrapper
735
736 (defvar epg-user-id-alist)
737 (defvar epg-digest-algorithm-alist)
738 (defvar epg-gpg-program)
739 (defvar inhibit-redisplay)
740
741 (autoload 'epg-make-context "epg")
742 (autoload 'epg-context-set-armor "epg")
743 (autoload 'epg-context-set-textmode "epg")
744 (autoload 'epg-context-set-signers "epg")
745 (autoload 'epg-context-result-for "epg")
746 (autoload 'epg-new-signature-digest-algorithm "epg")
747 (autoload 'epg-list-keys "epg")
748 (autoload 'epg-decrypt-string "epg")
749 (autoload 'epg-verify-string "epg")
750 (autoload 'epg-sign-string "epg")
751 (autoload 'epg-encrypt-string "epg")
752 (autoload 'epg-passphrase-callback-function "epg")
753 (autoload 'epg-context-set-passphrase-callback "epg")
754 (autoload 'epg-key-sub-key-list "epg")
755 (autoload 'epg-sub-key-capability "epg")
756 (autoload 'epg-sub-key-validity "epg")
757 (autoload 'epg-sub-key-fingerprint "epg")
758 (autoload 'epg-signature-key-id "epg")
759 (autoload 'epg-signature-to-string "epg")
760 (autoload 'epg-key-user-id-list "epg")
761 (autoload 'epg-user-id-string "epg")
762 (autoload 'epg-user-id-validity "epg")
763 (autoload 'epg-configuration "epg-config")
764 (autoload 'epg-expand-group "epg-config")
765 (autoload 'epa-select-keys "epa")
766
767 (defun mml2015-epg-key-image (key-id)
768 "Return the image of a key, if any"
769 (with-temp-buffer
770 (set-buffer-multibyte nil)
771 (let* ((coding-system-for-write 'binary)
772 (coding-system-for-read 'binary)
773 (data (shell-command-to-string
774 (format "%s --list-options no-show-photos --attribute-fd 3 --list-keys %s 3>&1 >/dev/null 2>&1"
775 (shell-quote-argument epg-gpg-program) key-id))))
776 (when (> (length data) 0)
777 (insert (substring data 16))
778 (condition-case nil
779 (gnus-create-image (buffer-string) nil t)
780 (error))))))
781
782 (autoload 'gnus-rescale-image "gnus-util")
783
784 (defun mml2015-epg-key-image-to-string (key-id)
785 "Return a string with the image of a key, if any"
786 (let ((key-image (mml2015-epg-key-image key-id)))
787 (if (not key-image)
788 ""
789 (condition-case error
790 (let ((result " "))
791 (put-text-property
792 1 2 'display
793 (gnus-rescale-image key-image
794 (cons mml2015-maximum-key-image-dimension
795 mml2015-maximum-key-image-dimension))
796 result)
797 result)
798 (error "")))))
799
800 (defun mml2015-epg-signature-to-string (signature)
801 (concat (epg-signature-to-string signature)
802 (when mml2015-display-key-image
803 (mml2015-epg-key-image-to-string (epg-signature-key-id signature)))))
804
805 (defun mml2015-epg-verify-result-to-string (verify-result)
806 (mapconcat #'mml2015-epg-signature-to-string verify-result "\n"))
807
808 (defun mml2015-epg-decrypt (handle ctl)
809 (catch 'error
810 (let ((inhibit-redisplay t)
811 context plain child handles result decrypt-status)
812 (unless (setq child (mm-find-part-by-type
813 (cdr handle)
814 "application/octet-stream" nil t))
815 (mm-set-handle-multipart-parameter
816 mm-security-handle 'gnus-info "Corrupted")
817 (throw 'error handle))
818 (setq context (epg-make-context))
819 (if (or mml2015-cache-passphrase mml-secure-cache-passphrase)
820 (epg-context-set-passphrase-callback
821 context
822 (cons 'mml-secure-passphrase-callback 'OpenPGP)))
823 (condition-case error
824 (setq plain (epg-decrypt-string context (mm-get-part child))
825 mml-secure-secret-key-id-list nil)
826 (error
827 (mml-secure-clear-secret-key-id-list)
828 (mm-set-handle-multipart-parameter
829 mm-security-handle 'gnus-info "Failed")
830 (if (eq (car error) 'quit)
831 (mm-set-handle-multipart-parameter
832 mm-security-handle 'gnus-details "Quit.")
833 (mm-set-handle-multipart-parameter
834 mm-security-handle 'gnus-details (mml2015-format-error error)))
835 (throw 'error handle)))
836 (with-temp-buffer
837 (insert plain)
838 (goto-char (point-min))
839 (while (search-forward "\r\n" nil t)
840 (replace-match "\n" t t))
841 (setq handles (mm-dissect-buffer t))
842 (mm-destroy-parts handle)
843 (if (epg-context-result-for context 'verify)
844 (mm-set-handle-multipart-parameter
845 mm-security-handle 'gnus-info
846 (concat "OK\n"
847 (mml2015-epg-verify-result-to-string
848 (epg-context-result-for context 'verify))))
849 (mm-set-handle-multipart-parameter
850 mm-security-handle 'gnus-info "OK"))
851 (if (stringp (car handles))
852 (mm-set-handle-multipart-parameter
853 mm-security-handle 'gnus-details
854 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
855 (if (listp (car handles))
856 handles
857 (list handles)))))
858
859 (defun mml2015-epg-clear-decrypt ()
860 (let ((inhibit-redisplay t)
861 (context (epg-make-context))
862 plain)
863 (if (or mml2015-cache-passphrase mml-secure-cache-passphrase)
864 (epg-context-set-passphrase-callback
865 context
866 (cons 'mml-secure-passphrase-callback 'OpenPGP)))
867 (condition-case error
868 (setq plain (epg-decrypt-string context (buffer-string))
869 mml-secure-secret-key-id-list nil)
870 (error
871 (mml-secure-clear-secret-key-id-list)
872 (mm-set-handle-multipart-parameter
873 mm-security-handle 'gnus-info "Failed")
874 (if (eq (car error) 'quit)
875 (mm-set-handle-multipart-parameter
876 mm-security-handle 'gnus-details "Quit.")
877 (mm-set-handle-multipart-parameter
878 mm-security-handle 'gnus-details (mml2015-format-error error)))))
879 (when plain
880 (erase-buffer)
881 ;; Treat data which epg returns as a unibyte string.
882 (mm-disable-multibyte)
883 (insert plain)
884 (goto-char (point-min))
885 (while (search-forward "\r\n" nil t)
886 (replace-match "\n" t t))
887 (mm-set-handle-multipart-parameter
888 mm-security-handle 'gnus-info "OK")
889 (if (epg-context-result-for context 'verify)
890 (mm-set-handle-multipart-parameter
891 mm-security-handle 'gnus-details
892 (mml2015-epg-verify-result-to-string
893 (epg-context-result-for context 'verify)))))))
894
895 (defun mml2015-epg-verify (handle ctl)
896 (catch 'error
897 (let ((inhibit-redisplay t)
898 context plain signature-file part signature)
899 (when (or (null (setq part (mm-find-raw-part-by-type
900 ctl (or (mm-handle-multipart-ctl-parameter
901 ctl 'protocol)
902 "application/pgp-signature")
903 t)))
904 (null (setq signature (mm-find-part-by-type
905 (cdr handle) "application/pgp-signature"
906 nil t))))
907 (mm-set-handle-multipart-parameter
908 mm-security-handle 'gnus-info "Corrupted")
909 (throw 'error handle))
910 (setq part (replace-regexp-in-string "\n" "\r\n" part)
911 signature (mm-get-part signature)
912 context (epg-make-context))
913 (condition-case error
914 (setq plain (epg-verify-string context signature part))
915 (error
916 (mm-set-handle-multipart-parameter
917 mm-security-handle 'gnus-info "Failed")
918 (if (eq (car error) 'quit)
919 (mm-set-handle-multipart-parameter
920 mm-security-handle 'gnus-details "Quit.")
921 (mm-set-handle-multipart-parameter
922 mm-security-handle 'gnus-details (mml2015-format-error error)))
923 (throw 'error handle)))
924 (mm-set-handle-multipart-parameter
925 mm-security-handle 'gnus-info
926 (mml2015-epg-verify-result-to-string
927 (epg-context-result-for context 'verify)))
928 handle)))
929
930 (defun mml2015-epg-clear-verify ()
931 (let ((inhibit-redisplay t)
932 (context (epg-make-context))
933 (signature (encode-coding-string (buffer-string)
934 coding-system-for-write))
935 plain)
936 (condition-case error
937 (setq plain (epg-verify-string context signature))
938 (error
939 (mm-set-handle-multipart-parameter
940 mm-security-handle 'gnus-info "Failed")
941 (if (eq (car error) 'quit)
942 (mm-set-handle-multipart-parameter
943 mm-security-handle 'gnus-details "Quit.")
944 (mm-set-handle-multipart-parameter
945 mm-security-handle 'gnus-details (mml2015-format-error error)))))
946 (if plain
947 (progn
948 (mm-set-handle-multipart-parameter
949 mm-security-handle 'gnus-info
950 (mml2015-epg-verify-result-to-string
951 (epg-context-result-for context 'verify)))
952 (delete-region (point-min) (point-max))
953 (insert (decode-coding-string plain coding-system-for-read)))
954 (mml2015-extract-cleartext-signature))))
955
956 (defun mml2015-epg-sign (cont)
957 (let ((inhibit-redisplay t)
958 (boundary (mml-compute-boundary cont)))
959 ;; Signed data must end with a newline (RFC 3156, 5).
960 (goto-char (point-max))
961 (unless (bolp)
962 (insert "\n"))
963 (let* ((pair (mml-secure-epg-sign 'OpenPGP t))
964 (signature (car pair))
965 (micalg (cdr pair)))
966 (goto-char (point-min))
967 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
968 boundary))
969 (if micalg
970 (insert (format "\tmicalg=pgp-%s; "
971 (downcase
972 (cdr (assq micalg
973 epg-digest-algorithm-alist))))))
974 (insert "protocol=\"application/pgp-signature\"\n")
975 (insert (format "\n--%s\n" boundary))
976 (goto-char (point-max))
977 (insert (format "\n--%s\n" boundary))
978 (insert "Content-Type: application/pgp-signature; name=\"signature.asc\"\n\n")
979 (insert signature)
980 (goto-char (point-max))
981 (insert (format "--%s--\n" boundary))
982 (goto-char (point-max)))))
983
984 (defun mml2015-epg-encrypt (cont &optional sign)
985 (let* ((inhibit-redisplay t)
986 (boundary (mml-compute-boundary cont))
987 (cipher (mml-secure-epg-encrypt 'OpenPGP cont sign)))
988 (delete-region (point-min) (point-max))
989 (goto-char (point-min))
990 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
991 boundary))
992 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
993 (insert (format "--%s\n" boundary))
994 (insert "Content-Type: application/pgp-encrypted\n\n")
995 (insert "Version: 1\n\n")
996 (insert (format "--%s\n" boundary))
997 (insert "Content-Type: application/octet-stream\n\n")
998 (insert cipher)
999 (goto-char (point-max))
1000 (insert (format "--%s--\n" boundary))
1001 (goto-char (point-max))))
1002
1003 ;;; General wrapper
1004
1005 (autoload 'gnus-buffer-live-p "gnus-util")
1006 (autoload 'gnus-get-buffer-create "gnus")
1007
1008 (defun mml2015-clean-buffer ()
1009 (if (gnus-buffer-live-p mml2015-result-buffer)
1010 (with-current-buffer mml2015-result-buffer
1011 (erase-buffer)
1012 t)
1013 (setq mml2015-result-buffer
1014 (gnus-get-buffer-create " *MML2015 Result*"))
1015 nil))
1016
1017 (defsubst mml2015-clear-decrypt-function ()
1018 (nth 6 (assq mml2015-use mml2015-function-alist)))
1019
1020 (defsubst mml2015-clear-verify-function ()
1021 (nth 5 (assq mml2015-use mml2015-function-alist)))
1022
1023 ;;;###autoload
1024 (defun mml2015-decrypt (handle ctl)
1025 (mml2015-clean-buffer)
1026 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1027 (if func
1028 (funcall func handle ctl)
1029 handle)))
1030
1031 ;;;###autoload
1032 (defun mml2015-decrypt-test (handle ctl)
1033 mml2015-use)
1034
1035 ;;;###autoload
1036 (defun mml2015-verify (handle ctl)
1037 (mml2015-clean-buffer)
1038 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1039 (if func
1040 (funcall func handle ctl)
1041 handle)))
1042
1043 ;;;###autoload
1044 (defun mml2015-verify-test (handle ctl)
1045 mml2015-use)
1046
1047 ;;;###autoload
1048 (defun mml2015-encrypt (cont &optional sign)
1049 (mml2015-clean-buffer)
1050 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1051 (if func
1052 (funcall func cont sign)
1053 (error "Cannot find encrypt function"))))
1054
1055 ;;;###autoload
1056 (defun mml2015-sign (cont)
1057 (mml2015-clean-buffer)
1058 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1059 (if func
1060 (funcall func cont)
1061 (error "Cannot find sign function"))))
1062
1063 ;;;###autoload
1064 (defun mml2015-self-encrypt ()
1065 (mml2015-encrypt nil))
1066
1067 (provide 'mml2015)
1068
1069 ;;; mml2015.el ends here