1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: PGP MIME MML
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
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
33 (eval-when-compile (require 'cl))
39 (defvar mc-pgp-always-sign)
41 (defvar mml2015-use (or
45 (epg-check-configuration (epg-configuration))
50 ;; Avoid the "Recursive load suspected" error
52 (let ((recursive-load-depth-limit 100))
54 (and (fboundp 'pgg-sign-region)
59 (and (fboundp 'gpg-sign-detached)
63 (and (fboundp 'mc-encrypt-generic)
64 (fboundp 'mc-sign-generic)
65 (fboundp 'mc-cleanup-recipient-headers)
67 "The package used for PGP/MIME.
68 Valid packages include `epg', `pgg', `gpg' and `mailcrypt'.")
70 ;; Something is not RFC2015.
71 (defvar mml2015-function-alist
72 '((mailcrypt mml2015-mailcrypt-sign
73 mml2015-mailcrypt-encrypt
74 mml2015-mailcrypt-verify
75 mml2015-mailcrypt-decrypt
76 mml2015-mailcrypt-clear-verify
77 mml2015-mailcrypt-clear-decrypt)
82 mml2015-gpg-clear-verify
83 mml2015-gpg-clear-decrypt)
88 mml2015-pgg-clear-verify
89 mml2015-pgg-clear-decrypt)
94 mml2015-epg-clear-verify
95 mml2015-epg-clear-decrypt))
96 "Alist of PGP/MIME functions.")
98 (defvar mml2015-result-buffer nil)
100 (defcustom mml2015-unabbrev-trust-alist
101 '(("TRUST_UNDEFINED" . nil)
102 ("TRUST_NEVER" . nil)
103 ("TRUST_MARGINAL" . t)
105 ("TRUST_ULTIMATE" . t))
106 "Map GnuPG trust output values to a boolean saying if you trust the key."
108 :group 'mime-security
109 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
110 (boolean :tag "Trust key"))))
112 (defcustom mml2015-verbose mml-secure-verbose
113 "If non-nil, ask the user about the current operation more verbosely."
114 :group 'mime-security
117 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
118 "If t, cache passphrase."
119 :group 'mime-security
122 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
123 "How many seconds the passphrase is cached.
124 Whether the passphrase is cached at all is controlled by
125 `mml2015-cache-passphrase'."
126 :group 'mime-security
129 (defcustom mml2015-signers nil
130 "A list of your own key ID which will be used to sign a message."
131 :group 'mime-security
132 :type '(repeat (string :tag "Key ID")))
134 (defcustom mml2015-encrypt-to-self nil
135 "If t, add your own key ID to recipient list when encryption."
136 :group 'mime-security
139 (defcustom mml2015-always-trust t
140 "If t, GnuPG skip key validation on encryption."
141 :group 'mime-security
144 ;; Extract plaintext from cleartext signature. IMO, this kind of task
145 ;; should be done by GnuPG rather than Elisp, but older PGP backends
146 ;; (such as Mailcrypt, PGG, and gpg.el) discard the output from GnuPG.
147 (defun mml2015-extract-cleartext-signature ()
148 (goto-char (point-min))
150 ;; We need to be careful not to strip beyond the armor headers.
151 ;; Previously, an attacker could replace the text inside our
152 ;; markup with trailing garbage by injecting whitespace into the
154 (while (looking-at "Hash:") ; The only header allowed in cleartext
155 (forward-line)) ; signatures according to RFC2440.
156 (when (looking-at "[\t ]*$")
158 (delete-region (point-min) (point))
159 (if (re-search-forward "^-----BEGIN PGP SIGNATURE-----" nil t)
160 (delete-region (match-beginning 0) (point-max)))
161 (goto-char (point-min))
162 (while (re-search-forward "^- " nil t)
163 (replace-match "" t t)
166 ;;; mailcrypt wrapper
169 (autoload 'mailcrypt-decrypt "mailcrypt")
170 (autoload 'mailcrypt-verify "mailcrypt")
171 (autoload 'mc-pgp-always-sign "mailcrypt")
172 (autoload 'mc-encrypt-generic "mc-toplev")
173 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
174 (autoload 'mc-sign-generic "mc-toplev"))
177 (defvar mc-default-scheme)
180 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
181 (defvar mml2015-verify-function 'mailcrypt-verify)
183 (defun mml2015-format-error (err)
184 (if (stringp (cadr err))
186 (format "%S" (cdr err))))
188 (defun mml2015-mailcrypt-decrypt (handle ctl)
190 (let (child handles result)
191 (unless (setq child (mm-find-part-by-type
193 "application/octet-stream" nil t))
194 (mm-set-handle-multipart-parameter
195 mm-security-handle 'gnus-info "Corrupted")
196 (throw 'error handle))
198 (mm-insert-part child)
201 (funcall mml2015-decrypt-function)
203 (mm-set-handle-multipart-parameter
204 mm-security-handle 'gnus-details (mml2015-format-error err))
207 (mm-set-handle-multipart-parameter
208 mm-security-handle 'gnus-details "Quit.")
211 (mm-set-handle-multipart-parameter
212 mm-security-handle 'gnus-info "Failed")
213 (throw 'error handle))
214 (setq handles (mm-dissect-buffer t)))
215 (mm-destroy-parts handle)
216 (mm-set-handle-multipart-parameter
217 mm-security-handle 'gnus-info
219 (let ((sig (with-current-buffer mml2015-result-buffer
220 (mml2015-gpg-extract-signature-details))))
221 (concat ", Signer: " sig))))
222 (if (listp (car handles))
226 (defun mml2015-mailcrypt-clear-decrypt ()
230 (funcall mml2015-decrypt-function)
232 (mm-set-handle-multipart-parameter
233 mm-security-handle 'gnus-details (mml2015-format-error err))
236 (mm-set-handle-multipart-parameter
237 mm-security-handle 'gnus-details "Quit.")
240 (mm-set-handle-multipart-parameter
241 mm-security-handle 'gnus-info "OK")
242 (mm-set-handle-multipart-parameter
243 mm-security-handle 'gnus-info "Failed"))))
245 (defun mml2015-fix-micalg (alg)
247 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
248 (upcase (if (string-match "^p[gh]p-" alg)
249 (substring alg (match-end 0))
252 (defun mml2015-mailcrypt-verify (handle ctl)
255 (unless (setq part (mm-find-raw-part-by-type
256 ctl (or (mm-handle-multipart-ctl-parameter
258 "application/pgp-signature")
260 (mm-set-handle-multipart-parameter
261 mm-security-handle 'gnus-info "Corrupted")
262 (throw 'error handle))
264 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
265 (insert (format "Hash: %s\n\n"
266 (or (mml2015-fix-micalg
267 (mm-handle-multipart-ctl-parameter
271 (narrow-to-region (point) (point))
273 (goto-char (point-min))
275 (if (looking-at "^-")
278 (unless (setq part (mm-find-part-by-type
279 (cdr handle) "application/pgp-signature" nil t))
280 (mm-set-handle-multipart-parameter
281 mm-security-handle 'gnus-info "Corrupted")
282 (throw 'error handle))
284 (narrow-to-region (point) (point))
285 (mm-insert-part part)
286 (goto-char (point-min))
287 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
288 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
289 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
290 (replace-match "-----END PGP SIGNATURE-----" t t)))
291 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
292 (unless (condition-case err
294 (funcall mml2015-verify-function)
295 (if (get-buffer " *mailcrypt stderr temp")
296 (mm-set-handle-multipart-parameter
297 mm-security-handle 'gnus-details
298 (with-current-buffer " *mailcrypt stderr temp"
300 (if (get-buffer " *mailcrypt stdout temp")
301 (kill-buffer " *mailcrypt stdout temp"))
302 (if (get-buffer " *mailcrypt stderr temp")
303 (kill-buffer " *mailcrypt stderr temp"))
304 (if (get-buffer " *mailcrypt status temp")
305 (kill-buffer " *mailcrypt status temp"))
306 (if (get-buffer mc-gpg-debug-buffer)
307 (kill-buffer mc-gpg-debug-buffer)))
309 (mm-set-handle-multipart-parameter
310 mm-security-handle 'gnus-details (mml2015-format-error err))
313 (mm-set-handle-multipart-parameter
314 mm-security-handle 'gnus-details "Quit.")
316 (mm-set-handle-multipart-parameter
317 mm-security-handle 'gnus-info "Failed")
318 (throw 'error handle))))
319 (mm-set-handle-multipart-parameter
320 mm-security-handle 'gnus-info "OK")
323 (defun mml2015-mailcrypt-clear-verify ()
324 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
325 (if (condition-case err
327 (funcall mml2015-verify-function)
328 (if (get-buffer " *mailcrypt stderr temp")
329 (mm-set-handle-multipart-parameter
330 mm-security-handle 'gnus-details
331 (with-current-buffer " *mailcrypt stderr temp"
333 (if (get-buffer " *mailcrypt stdout temp")
334 (kill-buffer " *mailcrypt stdout temp"))
335 (if (get-buffer " *mailcrypt stderr temp")
336 (kill-buffer " *mailcrypt stderr temp"))
337 (if (get-buffer " *mailcrypt status temp")
338 (kill-buffer " *mailcrypt status temp"))
339 (if (get-buffer mc-gpg-debug-buffer)
340 (kill-buffer mc-gpg-debug-buffer)))
342 (mm-set-handle-multipart-parameter
343 mm-security-handle 'gnus-details (mml2015-format-error err))
346 (mm-set-handle-multipart-parameter
347 mm-security-handle 'gnus-details "Quit.")
349 (mm-set-handle-multipart-parameter
350 mm-security-handle 'gnus-info "OK")
351 (mm-set-handle-multipart-parameter
352 mm-security-handle 'gnus-info "Failed")))
353 (mml2015-extract-cleartext-signature))
355 (defun mml2015-mailcrypt-sign (cont)
356 (mc-sign-generic (message-options-get 'message-sender)
358 (let ((boundary (mml-compute-boundary cont))
360 (goto-char (point-min))
361 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
362 (error "Cannot find signed begin line"))
363 (goto-char (match-beginning 0))
365 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
366 (error "Cannot not find PGP hash"))
367 (setq hash (match-string 1))
368 (unless (re-search-forward "^$" nil t)
369 (error "Cannot not find PGP message"))
371 (delete-region (point-min) (point))
372 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
374 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
376 (insert (format "\n--%s\n" boundary))
378 (goto-char (point-max))
379 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
380 (error "Cannot find signature part"))
381 (replace-match "-----END PGP MESSAGE-----" t t)
382 (goto-char (match-beginning 0))
383 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
385 (error "Cannot find signature part"))
386 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
387 (goto-char (match-beginning 0))
389 (narrow-to-region point (point))
391 (while (re-search-forward "^- -" nil t)
392 (replace-match "-" t t))
393 (goto-char (point-max)))
394 (insert (format "--%s\n" boundary))
395 (insert "Content-Type: application/pgp-signature\n\n")
396 (goto-char (point-max))
397 (insert (format "--%s--\n" boundary))
398 (goto-char (point-max))))
400 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
401 (let ((mc-pgp-always-sign
402 (or mc-pgp-always-sign
404 (eq t (or (message-options-get 'message-sign-encrypt)
406 'message-sign-encrypt
407 (or (y-or-n-p "Sign the message? ")
410 (mm-with-unibyte-current-buffer
412 (or (message-options-get 'message-recipients)
413 (message-options-set 'message-recipients
414 (mc-cleanup-recipient-headers
415 (read-string "Recipients: "))))
417 (message-options-get 'message-sender))))
418 (goto-char (point-min))
419 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
420 (error "Fail to encrypt the message"))
421 (let ((boundary (mml-compute-boundary cont)))
422 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
424 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
425 (insert (format "--%s\n" boundary))
426 (insert "Content-Type: application/pgp-encrypted\n\n")
427 (insert "Version: 1\n\n")
428 (insert (format "--%s\n" boundary))
429 (insert "Content-Type: application/octet-stream\n\n")
430 (goto-char (point-max))
431 (insert (format "--%s--\n" boundary))
432 (goto-char (point-max))))
437 (autoload 'gpg-decrypt "gpg")
438 (autoload 'gpg-verify "gpg")
439 (autoload 'gpg-verify-cleartext "gpg")
440 (autoload 'gpg-sign-detached "gpg")
441 (autoload 'gpg-sign-encrypt "gpg")
442 (autoload 'gpg-encrypt "gpg")
443 (autoload 'gpg-passphrase-read "gpg"))
445 (defun mml2015-gpg-passphrase ()
446 (or (message-options-get 'gpg-passphrase)
447 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
449 (defun mml2015-gpg-decrypt-1 ()
450 (let ((cipher (current-buffer)) plain result)
451 (if (with-temp-buffer
453 (gpg-decrypt cipher (setq plain (current-buffer))
454 mml2015-result-buffer nil)
455 (mm-set-handle-multipart-parameter
456 mm-security-handle 'gnus-details
457 (with-current-buffer mml2015-result-buffer
461 (insert-buffer-substring plain)
462 (goto-char (point-min))
463 (while (search-forward "\r\n" nil t)
464 (replace-match "\n" t t))))
466 ;; Some wrong with the return value, check plain text buffer.
467 (if (> (point-max) (point-min))
471 (defun mml2015-gpg-decrypt (handle ctl)
472 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
473 (mml2015-mailcrypt-decrypt handle ctl)))
475 (defun mml2015-gpg-clear-decrypt ()
477 (setq result (mml2015-gpg-decrypt-1))
479 (mm-set-handle-multipart-parameter
480 mm-security-handle 'gnus-info "OK")
481 (mm-set-handle-multipart-parameter
482 mm-security-handle 'gnus-info "Failed"))))
484 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
486 (fpr-length (string-width fingerprint))
489 (setq fingerprint (string-to-list fingerprint))
491 (setq fpr-length (- fpr-length 4))
492 (setq slice (butlast fingerprint fpr-length))
493 (setq fingerprint (nthcdr 4 fingerprint))
494 (setq n-slice (1+ n-slice))
500 (otherwise (concat " " slice))))))
503 (defun mml2015-gpg-extract-signature-details ()
504 (goto-char (point-min))
505 (let* ((expired (re-search-forward
506 "^\\[GNUPG:\\] SIGEXPIRED$"
508 (signer (and (re-search-forward
509 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
511 (cons (match-string 1) (match-string 2))))
512 (fprint (and (re-search-forward
513 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
516 (trust (and (re-search-forward
517 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
521 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
522 (cond ((and signer fprint)
524 (unless trust-good-enough-p
525 (concat "\nUntrusted, Fingerprint: "
526 (mml2015-gpg-pretty-print-fpr fprint)))
528 (format "\nWARNING: Signature from expired key (%s)"
531 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
534 "From unknown user"))))
536 (defun mml2015-gpg-verify (handle ctl)
538 (let (part message signature info-is-set-p)
539 (unless (setq part (mm-find-raw-part-by-type
540 ctl (or (mm-handle-multipart-ctl-parameter
542 "application/pgp-signature")
544 (mm-set-handle-multipart-parameter
545 mm-security-handle 'gnus-info "Corrupted")
546 (throw 'error handle))
548 (setq message (current-buffer))
550 ;; Convert <LF> to <CR><LF> in signed text. If --textmode is
551 ;; specified when signing, the conversion is not necessary.
552 (goto-char (point-min))
555 (unless (eq (char-before) ?\r)
560 (setq signature (current-buffer))
561 (unless (setq part (mm-find-part-by-type
562 (cdr handle) "application/pgp-signature" nil t))
563 (mm-set-handle-multipart-parameter
564 mm-security-handle 'gnus-info "Corrupted")
565 (throw 'error handle))
566 (mm-insert-part part)
567 (unless (condition-case err
569 (gpg-verify message signature mml2015-result-buffer)
570 (mm-set-handle-multipart-parameter
571 mm-security-handle 'gnus-details
572 (with-current-buffer mml2015-result-buffer
575 (mm-set-handle-multipart-parameter
576 mm-security-handle 'gnus-details (mml2015-format-error err))
577 (mm-set-handle-multipart-parameter
578 mm-security-handle 'gnus-info "Error.")
579 (setq info-is-set-p t)
582 (mm-set-handle-multipart-parameter
583 mm-security-handle 'gnus-details "Quit.")
584 (mm-set-handle-multipart-parameter
585 mm-security-handle 'gnus-info "Quit.")
586 (setq info-is-set-p t)
588 (unless info-is-set-p
589 (mm-set-handle-multipart-parameter
590 mm-security-handle 'gnus-info "Failed"))
591 (throw 'error handle)))
592 (mm-set-handle-multipart-parameter
593 mm-security-handle 'gnus-info
594 (with-current-buffer mml2015-result-buffer
595 (mml2015-gpg-extract-signature-details))))
598 (defun mml2015-gpg-clear-verify ()
599 (if (condition-case err
601 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
602 (mm-set-handle-multipart-parameter
603 mm-security-handle 'gnus-details
604 (with-current-buffer mml2015-result-buffer
607 (mm-set-handle-multipart-parameter
608 mm-security-handle 'gnus-details (mml2015-format-error err))
611 (mm-set-handle-multipart-parameter
612 mm-security-handle 'gnus-details "Quit.")
614 (mm-set-handle-multipart-parameter
615 mm-security-handle 'gnus-info
616 (with-current-buffer mml2015-result-buffer
617 (mml2015-gpg-extract-signature-details)))
618 (mm-set-handle-multipart-parameter
619 mm-security-handle 'gnus-info "Failed"))
620 (mml2015-extract-cleartext-signature))
622 (defun mml2015-gpg-sign (cont)
623 (let ((boundary (mml-compute-boundary cont))
624 (text (current-buffer)) signature)
625 (goto-char (point-max))
629 (unless (gpg-sign-detached text (setq signature (current-buffer))
630 mml2015-result-buffer
632 (message-options-get 'message-sender)
633 t t) ; armor & textmode
634 (unless (> (point-max) (point-min))
635 (pop-to-buffer mml2015-result-buffer)
636 (error "Sign error")))
637 (goto-char (point-min))
638 (while (re-search-forward "\r+$" nil t)
639 (replace-match "" t t))
641 (goto-char (point-min))
642 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
644 ;;; FIXME: what is the micalg?
645 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
646 (insert (format "\n--%s\n" boundary))
647 (goto-char (point-max))
648 (insert (format "\n--%s\n" boundary))
649 (insert "Content-Type: application/pgp-signature\n\n")
650 (insert-buffer-substring signature)
651 (goto-char (point-max))
652 (insert (format "--%s--\n" boundary))
653 (goto-char (point-max)))))
655 (defun mml2015-gpg-encrypt (cont &optional sign)
656 (let ((boundary (mml-compute-boundary cont))
657 (text (current-buffer))
659 (mm-with-unibyte-current-buffer
661 ;; set up a function to call the correct gpg encrypt routine
662 ;; with the right arguments. (FIXME: this should be done
664 (flet ((gpg-encrypt-func
665 (sign plaintext ciphertext result recipients &optional
666 passphrase sign-with-key armor textmode)
669 plaintext ciphertext result recipients passphrase
670 sign-with-key armor textmode)
672 plaintext ciphertext result recipients passphrase
674 (unless (gpg-encrypt-func
675 sign ; passed in when using signencrypt
676 text (setq cipher (current-buffer))
677 mml2015-result-buffer
680 (message-options-get 'message-recipients)
681 (message-options-set 'message-recipients
682 (read-string "Recipients: ")))
685 (message-options-get 'message-sender)
686 t t) ; armor & textmode
687 (unless (> (point-max) (point-min))
688 (pop-to-buffer mml2015-result-buffer)
689 (error "Encrypt error"))))
690 (goto-char (point-min))
691 (while (re-search-forward "\r+$" nil t)
692 (replace-match "" t t))
694 (delete-region (point-min) (point-max))
695 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
697 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
698 (insert (format "--%s\n" boundary))
699 (insert "Content-Type: application/pgp-encrypted\n\n")
700 (insert "Version: 1\n\n")
701 (insert (format "--%s\n" boundary))
702 (insert "Content-Type: application/octet-stream\n\n")
703 (insert-buffer-substring cipher)
704 (goto-char (point-max))
705 (insert (format "--%s--\n" boundary))
706 (goto-char (point-max))))))
711 (defvar pgg-default-user-id)
712 (defvar pgg-errors-buffer)
713 (defvar pgg-output-buffer))
716 (autoload 'pgg-decrypt-region "pgg")
717 (autoload 'pgg-verify-region "pgg")
718 (autoload 'pgg-sign-region "pgg")
719 (autoload 'pgg-encrypt-region "pgg")
720 (autoload 'pgg-parse-armor "pgg-parse"))
722 (defun mml2015-pgg-decrypt (handle ctl)
724 (let ((pgg-errors-buffer mml2015-result-buffer)
725 child handles result decrypt-status)
726 (unless (setq child (mm-find-part-by-type
728 "application/octet-stream" nil t))
729 (mm-set-handle-multipart-parameter
730 mm-security-handle 'gnus-info "Corrupted")
731 (throw 'error handle))
733 (mm-insert-part child)
734 (if (condition-case err
736 (pgg-decrypt-region (point-min) (point-max))
738 (with-current-buffer mml2015-result-buffer
740 (mm-set-handle-multipart-parameter
741 mm-security-handle 'gnus-details
744 (mm-set-handle-multipart-parameter
745 mm-security-handle 'gnus-details (mml2015-format-error err))
748 (mm-set-handle-multipart-parameter
749 mm-security-handle 'gnus-details "Quit.")
751 (with-current-buffer pgg-output-buffer
752 (goto-char (point-min))
753 (while (search-forward "\r\n" nil t)
754 (replace-match "\n" t t))
755 (setq handles (mm-dissect-buffer t))
756 (mm-destroy-parts handle)
757 (mm-set-handle-multipart-parameter
758 mm-security-handle 'gnus-info "OK")
759 (mm-set-handle-multipart-parameter
760 mm-security-handle 'gnus-details
761 (concat decrypt-status
762 (when (stringp (car handles))
763 "\n" (mm-handle-multipart-ctl-parameter
764 handles 'gnus-details))))
765 (if (listp (car handles))
768 (mm-set-handle-multipart-parameter
769 mm-security-handle 'gnus-info "Failed")
770 (throw 'error handle))))))
772 (defun mml2015-pgg-clear-decrypt ()
773 (let ((pgg-errors-buffer mml2015-result-buffer))
775 (pgg-decrypt-region (point-min) (point-max))
776 (mm-set-handle-multipart-parameter
777 mm-security-handle 'gnus-details
778 (with-current-buffer mml2015-result-buffer
782 ;; Treat data which pgg returns as a unibyte string.
783 (mm-disable-multibyte)
784 (insert-buffer-substring pgg-output-buffer)
785 (goto-char (point-min))
786 (while (search-forward "\r\n" nil t)
787 (replace-match "\n" t t))
788 (mm-set-handle-multipart-parameter
789 mm-security-handle 'gnus-info "OK"))
790 (mm-set-handle-multipart-parameter
791 mm-security-handle 'gnus-info "Failed"))))
793 (defun mml2015-pgg-verify (handle ctl)
794 (let ((pgg-errors-buffer mml2015-result-buffer)
795 signature-file part signature)
796 (if (or (null (setq part (mm-find-raw-part-by-type
797 ctl (or (mm-handle-multipart-ctl-parameter
799 "application/pgp-signature")
801 (null (setq signature (mm-find-part-by-type
802 (cdr handle) "application/pgp-signature" nil t))))
804 (mm-set-handle-multipart-parameter
805 mm-security-handle 'gnus-info "Corrupted")
809 ;; Convert <LF> to <CR><LF> in signed text. If --textmode is
810 ;; specified when signing, the conversion is not necessary.
811 (goto-char (point-min))
814 (unless (eq (char-before) ?\r)
818 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
819 (mm-insert-part signature))
820 (if (condition-case err
822 (pgg-verify-region (point-min) (point-max)
824 (goto-char (point-min))
825 (while (search-forward "\r\n" nil t)
826 (replace-match "\n" t t))
827 (mm-set-handle-multipart-parameter
828 mm-security-handle 'gnus-details
829 (concat (with-current-buffer pgg-output-buffer
831 (with-current-buffer pgg-errors-buffer
834 (mm-set-handle-multipart-parameter
835 mm-security-handle 'gnus-details (mml2015-format-error err))
838 (mm-set-handle-multipart-parameter
839 mm-security-handle 'gnus-details "Quit.")
842 (delete-file signature-file)
843 (mm-set-handle-multipart-parameter
844 mm-security-handle 'gnus-info
845 (with-current-buffer pgg-errors-buffer
846 (mml2015-gpg-extract-signature-details))))
847 (delete-file signature-file)
848 (mm-set-handle-multipart-parameter
849 mm-security-handle 'gnus-info "Failed")))))
852 (defun mml2015-pgg-clear-verify ()
853 (let ((pgg-errors-buffer mml2015-result-buffer)
854 (text (buffer-string))
855 (coding-system buffer-file-coding-system))
856 (if (condition-case err
858 (mm-with-unibyte-buffer
859 (insert (mm-encode-coding-string text coding-system))
860 (pgg-verify-region (point-min) (point-max) nil t))
861 (goto-char (point-min))
862 (while (search-forward "\r\n" nil t)
863 (replace-match "\n" t t))
864 (mm-set-handle-multipart-parameter
865 mm-security-handle 'gnus-details
866 (concat (with-current-buffer pgg-output-buffer
868 (with-current-buffer pgg-errors-buffer
871 (mm-set-handle-multipart-parameter
872 mm-security-handle 'gnus-details (mml2015-format-error err))
875 (mm-set-handle-multipart-parameter
876 mm-security-handle 'gnus-details "Quit.")
878 (mm-set-handle-multipart-parameter
879 mm-security-handle 'gnus-info
880 (with-current-buffer pgg-errors-buffer
881 (mml2015-gpg-extract-signature-details)))
882 (mm-set-handle-multipart-parameter
883 mm-security-handle 'gnus-info "Failed")))
884 (mml2015-extract-cleartext-signature))
886 (defun mml2015-pgg-sign (cont)
887 (let ((pgg-errors-buffer mml2015-result-buffer)
888 (boundary (mml-compute-boundary cont))
889 (pgg-default-user-id (or (message-options-get 'mml-sender)
890 pgg-default-user-id))
893 (unless (pgg-sign-region (point-min) (point-max))
894 (pop-to-buffer mml2015-result-buffer)
895 (error "Sign error"))
896 (goto-char (point-min))
897 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
899 (if (setq entry (assq 2 (pgg-parse-armor
900 (with-current-buffer pgg-output-buffer
902 (setq entry (assq 'hash-algorithm (cdr entry))))
903 (insert (format "\tmicalg=%s; "
905 (downcase (format "pgp-%s" (cdr entry)))
907 (insert "protocol=\"application/pgp-signature\"\n")
908 (insert (format "\n--%s\n" boundary))
909 (goto-char (point-max))
910 (insert (format "\n--%s\n" boundary))
911 (insert "Content-Type: application/pgp-signature\n\n")
912 (insert-buffer-substring pgg-output-buffer)
913 (goto-char (point-max))
914 (insert (format "--%s--\n" boundary))
915 (goto-char (point-max))))
917 (defun mml2015-pgg-encrypt (cont &optional sign)
918 (let ((pgg-errors-buffer mml2015-result-buffer)
920 (boundary (mml-compute-boundary cont)))
921 (unless (pgg-encrypt-region (point-min) (point-max)
924 (message-options-get 'message-recipients)
925 (message-options-set 'message-recipients
926 (read-string "Recipients: ")))
929 (pop-to-buffer mml2015-result-buffer)
930 (error "Encrypt error"))
931 (delete-region (point-min) (point-max))
932 (goto-char (point-min))
933 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
935 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
936 (insert (format "--%s\n" boundary))
937 (insert "Content-Type: application/pgp-encrypted\n\n")
938 (insert "Version: 1\n\n")
939 (insert (format "--%s\n" boundary))
940 (insert "Content-Type: application/octet-stream\n\n")
941 (insert-buffer-substring pgg-output-buffer)
942 (goto-char (point-max))
943 (insert (format "--%s--\n" boundary))
944 (goto-char (point-max))))
949 (autoload 'epg-make-context "epg"))
952 (defvar epg-user-id-alist)
953 (defvar epg-digest-algorithm-alist)
954 (defvar inhibit-redisplay)
955 (autoload 'epg-context-set-armor "epg")
956 (autoload 'epg-context-set-textmode "epg")
957 (autoload 'epg-context-set-signers "epg")
958 (autoload 'epg-context-result-for "epg")
959 (autoload 'epg-new-signature-digest-algorithm "epg")
960 (autoload 'epg-verify-result-to-string "epg")
961 (autoload 'epg-list-keys "epg")
962 (autoload 'epg-decrypt-string "epg")
963 (autoload 'epg-verify-string "epg")
964 (autoload 'epg-sign-string "epg")
965 (autoload 'epg-encrypt-string "epg")
966 (autoload 'epg-passphrase-callback-function "epg")
967 (autoload 'epg-context-set-passphrase-callback "epg")
968 (autoload 'epg-key-sub-key-list "epg")
969 (autoload 'epg-sub-key-capability "epg")
970 (autoload 'epg-sub-key-validity "epg")
971 (autoload 'epg-configuration "epg-config")
972 (autoload 'epg-expand-group "epg-config")
973 (autoload 'epa-select-keys "epa"))
976 (defvar password-cache-expiry)
977 (autoload 'password-read "password")
978 (autoload 'password-cache-add "password")
979 (autoload 'password-cache-remove "password"))
981 (defvar mml2015-epg-secret-key-id-list nil)
983 (defun mml2015-epg-passphrase-callback (context key-id ignore)
985 (epg-passphrase-callback-function context key-id nil)
990 "Passphrase for PIN: "
991 (if (setq entry (assoc key-id epg-user-id-alist))
992 (format "Passphrase for %s %s: " key-id (cdr entry))
993 (format "Passphrase for %s: " key-id)))
998 (let ((password-cache-expiry mml2015-passphrase-cache-expiry))
999 (password-cache-add key-id passphrase))
1000 (setq mml2015-epg-secret-key-id-list
1001 (cons key-id mml2015-epg-secret-key-id-list))
1002 (copy-sequence passphrase)))))
1004 (defun mml2015-epg-find-usable-key (keys usage)
1007 (let ((pointer (epg-key-sub-key-list (car keys))))
1009 (if (and (memq usage (epg-sub-key-capability (car pointer)))
1010 (not (memq (epg-sub-key-validity (car pointer))
1011 '(revoked expired))))
1012 (throw 'found (car keys)))
1013 (setq pointer (cdr pointer))))
1014 (setq keys (cdr keys)))))
1016 (defun mml2015-epg-decrypt (handle ctl)
1018 (let ((inhibit-redisplay t)
1019 context plain child handles result decrypt-status)
1020 (unless (setq child (mm-find-part-by-type
1022 "application/octet-stream" nil t))
1023 (mm-set-handle-multipart-parameter
1024 mm-security-handle 'gnus-info "Corrupted")
1025 (throw 'error handle))
1026 (setq context (epg-make-context))
1027 (if mml2015-cache-passphrase
1028 (epg-context-set-passphrase-callback
1030 #'mml2015-epg-passphrase-callback))
1031 (condition-case error
1032 (setq plain (epg-decrypt-string context (mm-get-part child))
1033 mml2015-epg-secret-key-id-list nil)
1035 (while mml2015-epg-secret-key-id-list
1036 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1037 (setq mml2015-epg-secret-key-id-list
1038 (cdr mml2015-epg-secret-key-id-list)))
1039 (mm-set-handle-multipart-parameter
1040 mm-security-handle 'gnus-info "Failed")
1041 (if (eq (car error) 'quit)
1042 (mm-set-handle-multipart-parameter
1043 mm-security-handle 'gnus-details "Quit.")
1044 (mm-set-handle-multipart-parameter
1045 mm-security-handle 'gnus-details (mml2015-format-error error)))
1046 (throw 'error handle)))
1049 (goto-char (point-min))
1050 (while (search-forward "\r\n" nil t)
1051 (replace-match "\n" t t))
1052 (setq handles (mm-dissect-buffer t))
1053 (mm-destroy-parts handle)
1054 (if (epg-context-result-for context 'verify)
1055 (mm-set-handle-multipart-parameter
1056 mm-security-handle 'gnus-info
1058 (epg-verify-result-to-string
1059 (epg-context-result-for context 'verify))))
1060 (mm-set-handle-multipart-parameter
1061 mm-security-handle 'gnus-info "OK"))
1062 (if (stringp (car handles))
1063 (mm-set-handle-multipart-parameter
1064 mm-security-handle 'gnus-details
1065 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
1066 (if (listp (car handles))
1070 (defun mml2015-epg-clear-decrypt ()
1071 (let ((inhibit-redisplay t)
1072 (context (epg-make-context))
1074 (if mml2015-cache-passphrase
1075 (epg-context-set-passphrase-callback
1077 #'mml2015-epg-passphrase-callback))
1078 (condition-case error
1079 (setq plain (epg-decrypt-string context (buffer-string))
1080 mml2015-epg-secret-key-id-list nil)
1082 (while mml2015-epg-secret-key-id-list
1083 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1084 (setq mml2015-epg-secret-key-id-list
1085 (cdr mml2015-epg-secret-key-id-list)))
1086 (mm-set-handle-multipart-parameter
1087 mm-security-handle 'gnus-info "Failed")
1088 (if (eq (car error) 'quit)
1089 (mm-set-handle-multipart-parameter
1090 mm-security-handle 'gnus-details "Quit.")
1091 (mm-set-handle-multipart-parameter
1092 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1095 ;; Treat data which epg returns as a unibyte string.
1096 (mm-disable-multibyte)
1098 (goto-char (point-min))
1099 (while (search-forward "\r\n" nil t)
1100 (replace-match "\n" t t))
1101 (mm-set-handle-multipart-parameter
1102 mm-security-handle 'gnus-info "OK")
1103 (if (epg-context-result-for context 'verify)
1104 (mm-set-handle-multipart-parameter
1105 mm-security-handle 'gnus-details
1106 (epg-verify-result-to-string
1107 (epg-context-result-for context 'verify)))))))
1109 (defun mml2015-epg-verify (handle ctl)
1111 (let ((inhibit-redisplay t)
1112 context plain signature-file part signature)
1113 (when (or (null (setq part (mm-find-raw-part-by-type
1114 ctl (or (mm-handle-multipart-ctl-parameter
1116 "application/pgp-signature")
1118 (null (setq signature (mm-find-part-by-type
1119 (cdr handle) "application/pgp-signature"
1121 (mm-set-handle-multipart-parameter
1122 mm-security-handle 'gnus-info "Corrupted")
1123 (throw 'error handle))
1124 (setq part (mm-replace-in-string part "\n" "\r\n" t)
1125 signature (mm-get-part signature)
1126 context (epg-make-context))
1127 (condition-case error
1128 (setq plain (epg-verify-string context signature part))
1130 (mm-set-handle-multipart-parameter
1131 mm-security-handle 'gnus-info "Failed")
1132 (if (eq (car error) 'quit)
1133 (mm-set-handle-multipart-parameter
1134 mm-security-handle 'gnus-details "Quit.")
1135 (mm-set-handle-multipart-parameter
1136 mm-security-handle 'gnus-details (mml2015-format-error error)))
1137 (throw 'error handle)))
1138 (mm-set-handle-multipart-parameter
1139 mm-security-handle 'gnus-info
1140 (epg-verify-result-to-string (epg-context-result-for context 'verify)))
1143 (defun mml2015-epg-clear-verify ()
1144 (let ((inhibit-redisplay t)
1145 (context (epg-make-context))
1146 (signature (mm-encode-coding-string (buffer-string)
1147 coding-system-for-write))
1149 (condition-case error
1150 (setq plain (epg-verify-string context signature))
1152 (mm-set-handle-multipart-parameter
1153 mm-security-handle 'gnus-info "Failed")
1154 (if (eq (car error) 'quit)
1155 (mm-set-handle-multipart-parameter
1156 mm-security-handle 'gnus-details "Quit.")
1157 (mm-set-handle-multipart-parameter
1158 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1161 (mm-set-handle-multipart-parameter
1162 mm-security-handle 'gnus-info
1163 (epg-verify-result-to-string
1164 (epg-context-result-for context 'verify)))
1165 (delete-region (point-min) (point-max))
1166 (insert (mm-decode-coding-string plain coding-system-for-read)))
1167 (mml2015-extract-cleartext-signature))))
1169 (defun mml2015-epg-sign (cont)
1170 (let* ((inhibit-redisplay t)
1171 (context (epg-make-context))
1172 (boundary (mml-compute-boundary cont))
1175 (or (message-options-get 'mml2015-epg-signers)
1176 (message-options-set
1177 'mml2015-epg-signers
1179 (epa-select-keys context "\
1180 Select keys for signing.
1181 If no one is selected, default secret key is used. "
1186 (setq signer-key (mml2015-epg-find-usable-key
1187 (epg-list-keys context signer t)
1189 (unless (or signer-key
1191 (format "No secret key for %s; skip it? "
1193 (error "No secret key for %s" signer))
1195 mml2015-signers))))))
1197 (epg-context-set-armor context t)
1198 (epg-context-set-textmode context t)
1199 (epg-context-set-signers context signers)
1200 (if mml2015-cache-passphrase
1201 (epg-context-set-passphrase-callback
1203 #'mml2015-epg-passphrase-callback))
1204 (condition-case error
1205 (setq signature (epg-sign-string context (buffer-string) t)
1206 mml2015-epg-secret-key-id-list nil)
1208 (while mml2015-epg-secret-key-id-list
1209 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1210 (setq mml2015-epg-secret-key-id-list
1211 (cdr mml2015-epg-secret-key-id-list)))
1212 (signal (car error) (cdr error))))
1213 (if (epg-context-result-for context 'sign)
1214 (setq micalg (epg-new-signature-digest-algorithm
1215 (car (epg-context-result-for context 'sign)))))
1216 (goto-char (point-min))
1217 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
1220 (insert (format "\tmicalg=pgp-%s; "
1223 epg-digest-algorithm-alist))))))
1224 (insert "protocol=\"application/pgp-signature\"\n")
1225 (insert (format "\n--%s\n" boundary))
1226 (goto-char (point-max))
1227 (insert (format "\n--%s\n" boundary))
1228 (insert "Content-Type: application/pgp-signature\n\n")
1230 (goto-char (point-max))
1231 (insert (format "--%s--\n" boundary))
1232 (goto-char (point-max))))
1234 (defun mml2015-epg-encrypt (cont &optional sign)
1235 (let ((inhibit-redisplay t)
1236 (context (epg-make-context))
1237 (config (epg-configuration))
1238 (recipients (message-options-get 'mml2015-epg-recipients))
1240 (boundary (mml-compute-boundary cont))
1241 recipient-key signer-key)
1247 (or (epg-expand-group config recipient)
1248 (list (concat "<" recipient ">"))))
1250 (or (message-options-get 'message-recipients)
1251 (message-options-set 'message-recipients
1252 (read-string "Recipients: ")))
1253 "[ \f\t\n\r\v,]+"))))
1254 (when mml2015-encrypt-to-self
1255 (unless mml2015-signers
1256 (error "mml2015-signers not set"))
1257 (setq recipients (nconc recipients mml2015-signers)))
1260 (epa-select-keys context "\
1261 Select recipients for encryption.
1262 If no one is selected, symmetric encryption will be performed. "
1267 (setq recipient-key (mml2015-epg-find-usable-key
1268 (epg-list-keys context recipient)
1270 (unless (or recipient-key
1272 (format "No public key for %s; skip it? "
1274 (error "No public key for %s" recipient))
1278 (error "No recipient specified")))
1279 (message-options-set 'mml2015-epg-recipients recipients))
1282 (or (message-options-get 'mml2015-epg-signers)
1283 (message-options-set
1284 'mml2015-epg-signers
1286 (epa-select-keys context "\
1287 Select keys for signing.
1288 If no one is selected, default secret key is used. "
1293 (setq signer-key (mml2015-epg-find-usable-key
1294 (epg-list-keys context signer t)
1296 (unless (or signer-key
1299 "No secret key for %s; skip it? "
1301 (error "No secret key for %s" signer))
1303 mml2015-signers))))))
1304 (epg-context-set-signers context signers))
1305 (epg-context-set-armor context t)
1306 (epg-context-set-textmode context t)
1307 (if mml2015-cache-passphrase
1308 (epg-context-set-passphrase-callback
1310 #'mml2015-epg-passphrase-callback))
1311 (condition-case error
1313 (epg-encrypt-string context (buffer-string) recipients sign
1314 mml2015-always-trust)
1315 mml2015-epg-secret-key-id-list nil)
1317 (while mml2015-epg-secret-key-id-list
1318 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1319 (setq mml2015-epg-secret-key-id-list
1320 (cdr mml2015-epg-secret-key-id-list)))
1321 (signal (car error) (cdr error))))
1322 (delete-region (point-min) (point-max))
1323 (goto-char (point-min))
1324 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1326 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1327 (insert (format "--%s\n" boundary))
1328 (insert "Content-Type: application/pgp-encrypted\n\n")
1329 (insert "Version: 1\n\n")
1330 (insert (format "--%s\n" boundary))
1331 (insert "Content-Type: application/octet-stream\n\n")
1333 (goto-char (point-max))
1334 (insert (format "--%s--\n" boundary))
1335 (goto-char (point-max))))
1339 (defun mml2015-clean-buffer ()
1340 (if (gnus-buffer-live-p mml2015-result-buffer)
1341 (with-current-buffer mml2015-result-buffer
1344 (setq mml2015-result-buffer
1345 (gnus-get-buffer-create " *MML2015 Result*"))
1348 (defsubst mml2015-clear-decrypt-function ()
1349 (nth 6 (assq mml2015-use mml2015-function-alist)))
1351 (defsubst mml2015-clear-verify-function ()
1352 (nth 5 (assq mml2015-use mml2015-function-alist)))
1355 (defun mml2015-decrypt (handle ctl)
1356 (mml2015-clean-buffer)
1357 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1359 (funcall func handle ctl)
1363 (defun mml2015-decrypt-test (handle ctl)
1367 (defun mml2015-verify (handle ctl)
1368 (mml2015-clean-buffer)
1369 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1371 (funcall func handle ctl)
1375 (defun mml2015-verify-test (handle ctl)
1379 (defun mml2015-encrypt (cont &optional sign)
1380 (mml2015-clean-buffer)
1381 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1383 (funcall func cont sign)
1384 (error "Cannot find encrypt function"))))
1387 (defun mml2015-sign (cont)
1388 (mml2015-clean-buffer)
1389 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1392 (error "Cannot find sign function"))))
1395 (defun mml2015-self-encrypt ()
1396 (mml2015-encrypt nil))
1400 ;;; arch-tag: b04701d5-0b09-44d8-bed8-de901bf435f2
1401 ;;; mml2015.el ends here