1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 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 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/>.
26 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
32 ;; For Emacs <22.2 and XEmacs.
33 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
35 (if (locate-library "password-cache")
36 (require 'password-cache)
39 (eval-when-compile (require 'cl))
45 (defvar mc-pgp-always-sign)
47 (declare-function epg-check-configuration "ext:epg-config"
48 (config &optional minimum-version))
49 (declare-function epg-configuration "ext:epg-config" ())
51 (defvar mml2015-use (or
55 (epg-check-configuration (epg-configuration))
59 (ignore-errors (require 'pgg))
60 (and (fboundp 'pgg-sign-region)
64 (and (fboundp 'mc-encrypt-generic)
65 (fboundp 'mc-sign-generic)
66 (fboundp 'mc-cleanup-recipient-headers)
68 "The package used for PGP/MIME.
69 Valid packages include `epg', `pgg' and `mailcrypt'.")
71 ;; Something is not RFC2015.
72 (defvar mml2015-function-alist
73 '((mailcrypt mml2015-mailcrypt-sign
74 mml2015-mailcrypt-encrypt
75 mml2015-mailcrypt-verify
76 mml2015-mailcrypt-decrypt
77 mml2015-mailcrypt-clear-verify
78 mml2015-mailcrypt-clear-decrypt)
83 mml2015-pgg-clear-verify
84 mml2015-pgg-clear-decrypt)
89 mml2015-epg-clear-verify
90 mml2015-epg-clear-decrypt))
91 "Alist of PGP/MIME functions.")
93 (defvar mml2015-result-buffer nil)
95 (defcustom mml2015-unabbrev-trust-alist
96 '(("TRUST_UNDEFINED" . nil)
98 ("TRUST_MARGINAL" . t)
100 ("TRUST_ULTIMATE" . t))
101 "Map GnuPG trust output values to a boolean saying if you trust the key."
103 :group 'mime-security
104 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
105 (boolean :tag "Trust key"))))
107 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
108 "If t, cache passphrase."
109 :group 'mime-security
112 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
113 "How many seconds the passphrase is cached.
114 Whether the passphrase is cached at all is controlled by
115 `mml2015-cache-passphrase'."
116 :group 'mime-security
119 (defcustom mml2015-signers nil
120 "A list of your own key ID which will be used to sign a message."
121 :group 'mime-security
122 :type '(repeat (string :tag "Key ID")))
124 (defcustom mml2015-encrypt-to-self nil
125 "If t, add your own key ID to recipient list when encryption."
126 :group 'mime-security
129 (defcustom mml2015-always-trust t
130 "If t, GnuPG skip key validation on encryption."
131 :group 'mime-security
134 ;; Extract plaintext from cleartext signature. IMO, this kind of task
135 ;; should be done by GnuPG rather than Elisp, but older PGP backends
136 ;; (such as Mailcrypt, and PGG) discard the output from GnuPG.
137 (defun mml2015-extract-cleartext-signature ()
139 ;; <54a15d860801080142l70b95d7dkac4bf51a86196011@mail.gmail.com>: ``I still
140 ;; believe that the right way is to use the plaintext output from GnuPG as
141 ;; it is, and mml2015-extract-cleartext-signature is just a kludge for
142 ;; misdesigned libraries like PGG, which have no ability to do that. So, I
143 ;; think it should not have descriptive documentation.''
145 ;; This function doesn't handle NotDashEscaped correctly. EasyPG handles it
147 ;; http://thread.gmane.org/gmane.emacs.gnus.general/66062/focus=66082
148 ;; http://thread.gmane.org/gmane.emacs.gnus.general/65087/focus=65109
149 (goto-char (point-min))
151 ;; We need to be careful not to strip beyond the armor headers.
152 ;; Previously, an attacker could replace the text inside our
153 ;; markup with trailing garbage by injecting whitespace into the
155 (while (looking-at "Hash:") ; The only header allowed in cleartext
156 (forward-line)) ; signatures according to RFC2440.
157 (when (looking-at "[\t ]*$")
159 (delete-region (point-min) (point))
160 (if (re-search-forward "^-----BEGIN PGP SIGNATURE-----" nil t)
161 (delete-region (match-beginning 0) (point-max)))
162 (goto-char (point-min))
163 (while (re-search-forward "^- " nil t)
164 (replace-match "" t t)
167 ;;; 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")
176 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
177 (defvar mml2015-verify-function 'mailcrypt-verify)
179 (defun mml2015-format-error (err)
180 (if (stringp (cadr err))
182 (format "%S" (cdr err))))
184 (defun mml2015-mailcrypt-decrypt (handle ctl)
186 (let (child handles result)
187 (unless (setq child (mm-find-part-by-type
189 "application/octet-stream" nil t))
190 (mm-set-handle-multipart-parameter
191 mm-security-handle 'gnus-info "Corrupted")
192 (throw 'error handle))
194 (mm-insert-part child)
197 (funcall mml2015-decrypt-function)
199 (mm-set-handle-multipart-parameter
200 mm-security-handle 'gnus-details (mml2015-format-error err))
203 (mm-set-handle-multipart-parameter
204 mm-security-handle 'gnus-details "Quit.")
207 (mm-set-handle-multipart-parameter
208 mm-security-handle 'gnus-info "Failed")
209 (throw 'error handle))
210 (setq handles (mm-dissect-buffer t)))
211 (mm-destroy-parts handle)
212 (mm-set-handle-multipart-parameter
213 mm-security-handle 'gnus-info
215 (let ((sig (with-current-buffer mml2015-result-buffer
216 (mml2015-gpg-extract-signature-details))))
217 (concat ", Signer: " sig))))
218 (if (listp (car handles))
222 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
224 (fpr-length (string-width fingerprint))
227 (setq fingerprint (string-to-list fingerprint))
229 (setq fpr-length (- fpr-length 4))
230 (setq slice (butlast fingerprint fpr-length))
231 (setq fingerprint (nthcdr 4 fingerprint))
232 (setq n-slice (1+ n-slice))
238 (otherwise (concat " " slice))))))
241 (defun mml2015-gpg-extract-signature-details ()
242 (goto-char (point-min))
243 (let* ((expired (re-search-forward
244 "^\\[GNUPG:\\] SIGEXPIRED$"
246 (signer (and (re-search-forward
247 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
249 (cons (match-string 1) (match-string 2))))
250 (fprint (and (re-search-forward
251 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
254 (trust (and (re-search-forward
255 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
259 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
260 (cond ((and signer fprint)
262 (unless trust-good-enough-p
263 (concat "\nUntrusted, Fingerprint: "
264 (mml2015-gpg-pretty-print-fpr fprint)))
266 (format "\nWARNING: Signature from expired key (%s)"
269 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
272 "From unknown user"))))
274 (defun mml2015-mailcrypt-clear-decrypt ()
278 (funcall mml2015-decrypt-function)
280 (mm-set-handle-multipart-parameter
281 mm-security-handle 'gnus-details (mml2015-format-error err))
284 (mm-set-handle-multipart-parameter
285 mm-security-handle 'gnus-details "Quit.")
288 (mm-set-handle-multipart-parameter
289 mm-security-handle 'gnus-info "OK")
290 (mm-set-handle-multipart-parameter
291 mm-security-handle 'gnus-info "Failed"))))
293 (defun mml2015-fix-micalg (alg)
295 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
296 (upcase (if (string-match "^p[gh]p-" alg)
297 (substring alg (match-end 0))
300 (defun mml2015-mailcrypt-verify (handle ctl)
303 (unless (setq part (mm-find-raw-part-by-type
304 ctl (or (mm-handle-multipart-ctl-parameter
306 "application/pgp-signature")
308 (mm-set-handle-multipart-parameter
309 mm-security-handle 'gnus-info "Corrupted")
310 (throw 'error handle))
312 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
313 (insert (format "Hash: %s\n\n"
314 (or (mml2015-fix-micalg
315 (mm-handle-multipart-ctl-parameter
319 (narrow-to-region (point) (point))
321 (goto-char (point-min))
323 (if (looking-at "^-")
326 (unless (setq part (mm-find-part-by-type
327 (cdr handle) "application/pgp-signature" nil t))
328 (mm-set-handle-multipart-parameter
329 mm-security-handle 'gnus-info "Corrupted")
330 (throw 'error handle))
332 (narrow-to-region (point) (point))
333 (mm-insert-part part)
334 (goto-char (point-min))
335 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
336 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
337 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
338 (replace-match "-----END PGP SIGNATURE-----" t t)))
339 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
340 (unless (condition-case err
342 (funcall mml2015-verify-function)
343 (if (get-buffer " *mailcrypt stderr temp")
344 (mm-set-handle-multipart-parameter
345 mm-security-handle 'gnus-details
346 (with-current-buffer " *mailcrypt stderr temp"
348 (if (get-buffer " *mailcrypt stdout temp")
349 (kill-buffer " *mailcrypt stdout temp"))
350 (if (get-buffer " *mailcrypt stderr temp")
351 (kill-buffer " *mailcrypt stderr temp"))
352 (if (get-buffer " *mailcrypt status temp")
353 (kill-buffer " *mailcrypt status temp"))
354 (if (get-buffer mc-gpg-debug-buffer)
355 (kill-buffer mc-gpg-debug-buffer)))
357 (mm-set-handle-multipart-parameter
358 mm-security-handle 'gnus-details (mml2015-format-error err))
361 (mm-set-handle-multipart-parameter
362 mm-security-handle 'gnus-details "Quit.")
364 (mm-set-handle-multipart-parameter
365 mm-security-handle 'gnus-info "Failed")
366 (throw 'error handle))))
367 (mm-set-handle-multipart-parameter
368 mm-security-handle 'gnus-info "OK")
371 (defun mml2015-mailcrypt-clear-verify ()
372 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
373 (if (condition-case err
375 (funcall mml2015-verify-function)
376 (if (get-buffer " *mailcrypt stderr temp")
377 (mm-set-handle-multipart-parameter
378 mm-security-handle 'gnus-details
379 (with-current-buffer " *mailcrypt stderr temp"
381 (if (get-buffer " *mailcrypt stdout temp")
382 (kill-buffer " *mailcrypt stdout temp"))
383 (if (get-buffer " *mailcrypt stderr temp")
384 (kill-buffer " *mailcrypt stderr temp"))
385 (if (get-buffer " *mailcrypt status temp")
386 (kill-buffer " *mailcrypt status temp"))
387 (if (get-buffer mc-gpg-debug-buffer)
388 (kill-buffer mc-gpg-debug-buffer)))
390 (mm-set-handle-multipart-parameter
391 mm-security-handle 'gnus-details (mml2015-format-error err))
394 (mm-set-handle-multipart-parameter
395 mm-security-handle 'gnus-details "Quit.")
397 (mm-set-handle-multipart-parameter
398 mm-security-handle 'gnus-info "OK")
399 (mm-set-handle-multipart-parameter
400 mm-security-handle 'gnus-info "Failed")))
401 (mml2015-extract-cleartext-signature))
403 (defun mml2015-mailcrypt-sign (cont)
404 (mc-sign-generic (message-options-get 'message-sender)
406 (let ((boundary (mml-compute-boundary cont))
408 (goto-char (point-min))
409 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
410 (error "Cannot find signed begin line"))
411 (goto-char (match-beginning 0))
413 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
414 (error "Cannot not find PGP hash"))
415 (setq hash (match-string 1))
416 (unless (re-search-forward "^$" nil t)
417 (error "Cannot not find PGP message"))
419 (delete-region (point-min) (point))
420 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
422 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
424 (insert (format "\n--%s\n" boundary))
426 (goto-char (point-max))
427 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
428 (error "Cannot find signature part"))
429 (replace-match "-----END PGP MESSAGE-----" t t)
430 (goto-char (match-beginning 0))
431 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
433 (error "Cannot find signature part"))
434 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
435 (goto-char (match-beginning 0))
437 (narrow-to-region point (point))
439 (while (re-search-forward "^- -" nil t)
440 (replace-match "-" t t))
441 (goto-char (point-max)))
442 (insert (format "--%s\n" boundary))
443 (insert "Content-Type: application/pgp-signature\n\n")
444 (goto-char (point-max))
445 (insert (format "--%s--\n" boundary))
446 (goto-char (point-max))))
448 ;; We require mm-decode, which requires mm-bodies, which autoloads
449 ;; message-options-get (!).
450 (declare-function message-options-set "message" (symbol value))
452 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
453 (let ((mc-pgp-always-sign
454 (or mc-pgp-always-sign
456 (eq t (or (message-options-get 'message-sign-encrypt)
458 'message-sign-encrypt
459 (or (y-or-n-p "Sign the message? ")
462 (mm-with-unibyte-current-buffer
464 (or (message-options-get 'message-recipients)
465 (message-options-set 'message-recipients
466 (mc-cleanup-recipient-headers
467 (read-string "Recipients: "))))
469 (message-options-get 'message-sender))))
470 (goto-char (point-min))
471 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
472 (error "Fail to encrypt the message"))
473 (let ((boundary (mml-compute-boundary cont)))
474 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
476 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
477 (insert (format "--%s\n" boundary))
478 (insert "Content-Type: application/pgp-encrypted\n\n")
479 (insert "Version: 1\n\n")
480 (insert (format "--%s\n" boundary))
481 (insert "Content-Type: application/octet-stream\n\n")
482 (goto-char (point-max))
483 (insert (format "--%s--\n" boundary))
484 (goto-char (point-max))))
488 (defvar pgg-default-user-id)
489 (defvar pgg-errors-buffer)
490 (defvar pgg-output-buffer)
492 (autoload 'pgg-decrypt-region "pgg")
493 (autoload 'pgg-verify-region "pgg")
494 (autoload 'pgg-sign-region "pgg")
495 (autoload 'pgg-encrypt-region "pgg")
496 (autoload 'pgg-parse-armor "pgg-parse")
498 (defun mml2015-pgg-decrypt (handle ctl)
500 (let ((pgg-errors-buffer mml2015-result-buffer)
501 child handles result decrypt-status)
502 (unless (setq child (mm-find-part-by-type
504 "application/octet-stream" nil t))
505 (mm-set-handle-multipart-parameter
506 mm-security-handle 'gnus-info "Corrupted")
507 (throw 'error handle))
509 (mm-insert-part child)
510 (if (condition-case err
512 (pgg-decrypt-region (point-min) (point-max))
514 (with-current-buffer mml2015-result-buffer
516 (mm-set-handle-multipart-parameter
517 mm-security-handle 'gnus-details
520 (mm-set-handle-multipart-parameter
521 mm-security-handle 'gnus-details (mml2015-format-error err))
524 (mm-set-handle-multipart-parameter
525 mm-security-handle 'gnus-details "Quit.")
527 (with-current-buffer pgg-output-buffer
528 (goto-char (point-min))
529 (while (search-forward "\r\n" nil t)
530 (replace-match "\n" t t))
531 (setq handles (mm-dissect-buffer t))
532 (mm-destroy-parts handle)
533 (mm-set-handle-multipart-parameter
534 mm-security-handle 'gnus-info "OK")
535 (mm-set-handle-multipart-parameter
536 mm-security-handle 'gnus-details
537 (concat decrypt-status
538 (when (stringp (car handles))
539 "\n" (mm-handle-multipart-ctl-parameter
540 handles 'gnus-details))))
541 (if (listp (car handles))
544 (mm-set-handle-multipart-parameter
545 mm-security-handle 'gnus-info "Failed")
546 (throw 'error handle))))))
548 (defun mml2015-pgg-clear-decrypt ()
549 (let ((pgg-errors-buffer mml2015-result-buffer))
551 (pgg-decrypt-region (point-min) (point-max))
552 (mm-set-handle-multipart-parameter
553 mm-security-handle 'gnus-details
554 (with-current-buffer mml2015-result-buffer
558 ;; Treat data which pgg returns as a unibyte string.
559 (mm-disable-multibyte)
560 (insert-buffer-substring pgg-output-buffer)
561 (goto-char (point-min))
562 (while (search-forward "\r\n" nil t)
563 (replace-match "\n" t t))
564 (mm-set-handle-multipart-parameter
565 mm-security-handle 'gnus-info "OK"))
566 (mm-set-handle-multipart-parameter
567 mm-security-handle 'gnus-info "Failed"))))
569 (defun mml2015-pgg-verify (handle ctl)
570 (let ((pgg-errors-buffer mml2015-result-buffer)
571 signature-file part signature)
572 (if (or (null (setq part (mm-find-raw-part-by-type
573 ctl (or (mm-handle-multipart-ctl-parameter
575 "application/pgp-signature")
577 (null (setq signature (mm-find-part-by-type
578 (cdr handle) "application/pgp-signature" nil t))))
580 (mm-set-handle-multipart-parameter
581 mm-security-handle 'gnus-info "Corrupted")
585 ;; Convert <LF> to <CR><LF> in signed text. If --textmode is
586 ;; specified when signing, the conversion is not necessary.
587 (goto-char (point-min))
590 (unless (eq (char-before) ?\r)
594 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
595 (mm-insert-part signature))
596 (if (condition-case err
598 (pgg-verify-region (point-min) (point-max)
600 (goto-char (point-min))
601 (while (search-forward "\r\n" nil t)
602 (replace-match "\n" t t))
603 (mm-set-handle-multipart-parameter
604 mm-security-handle 'gnus-details
605 (concat (with-current-buffer pgg-output-buffer
607 (with-current-buffer pgg-errors-buffer
610 (mm-set-handle-multipart-parameter
611 mm-security-handle 'gnus-details (mml2015-format-error err))
614 (mm-set-handle-multipart-parameter
615 mm-security-handle 'gnus-details "Quit.")
618 (delete-file signature-file)
619 (mm-set-handle-multipart-parameter
620 mm-security-handle 'gnus-info
621 (with-current-buffer pgg-errors-buffer
622 (mml2015-gpg-extract-signature-details))))
623 (delete-file signature-file)
624 (mm-set-handle-multipart-parameter
625 mm-security-handle 'gnus-info "Failed")))))
628 (defun mml2015-pgg-clear-verify ()
629 (let ((pgg-errors-buffer mml2015-result-buffer)
630 (text (buffer-string))
631 (coding-system buffer-file-coding-system))
632 (if (condition-case err
634 (mm-with-unibyte-buffer
635 (insert (mm-encode-coding-string text coding-system))
636 (pgg-verify-region (point-min) (point-max) nil t))
637 (goto-char (point-min))
638 (while (search-forward "\r\n" nil t)
639 (replace-match "\n" t t))
640 (mm-set-handle-multipart-parameter
641 mm-security-handle 'gnus-details
642 (concat (with-current-buffer pgg-output-buffer
644 (with-current-buffer pgg-errors-buffer
647 (mm-set-handle-multipart-parameter
648 mm-security-handle 'gnus-details (mml2015-format-error err))
651 (mm-set-handle-multipart-parameter
652 mm-security-handle 'gnus-details "Quit.")
654 (mm-set-handle-multipart-parameter
655 mm-security-handle 'gnus-info
656 (with-current-buffer pgg-errors-buffer
657 (mml2015-gpg-extract-signature-details)))
658 (mm-set-handle-multipart-parameter
659 mm-security-handle 'gnus-info "Failed")))
660 (mml2015-extract-cleartext-signature))
662 (defun mml2015-pgg-sign (cont)
663 (let ((pgg-errors-buffer mml2015-result-buffer)
664 (boundary (mml-compute-boundary cont))
665 (pgg-default-user-id (or (message-options-get 'mml-sender)
666 pgg-default-user-id))
669 (unless (pgg-sign-region (point-min) (point-max))
670 (pop-to-buffer mml2015-result-buffer)
671 (error "Sign error"))
672 (goto-char (point-min))
673 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
675 (if (setq entry (assq 2 (pgg-parse-armor
676 (with-current-buffer pgg-output-buffer
678 (setq entry (assq 'hash-algorithm (cdr entry))))
679 (insert (format "\tmicalg=%s; "
681 (downcase (format "pgp-%s" (cdr entry)))
683 (insert "protocol=\"application/pgp-signature\"\n")
684 (insert (format "\n--%s\n" boundary))
685 (goto-char (point-max))
686 (insert (format "\n--%s\n" boundary))
687 (insert "Content-Type: application/pgp-signature\n\n")
688 (insert-buffer-substring pgg-output-buffer)
689 (goto-char (point-max))
690 (insert (format "--%s--\n" boundary))
691 (goto-char (point-max))))
693 (defun mml2015-pgg-encrypt (cont &optional sign)
694 (let ((pgg-errors-buffer mml2015-result-buffer)
696 (boundary (mml-compute-boundary cont)))
697 (unless (pgg-encrypt-region (point-min) (point-max)
700 (message-options-get 'message-recipients)
701 (message-options-set 'message-recipients
702 (read-string "Recipients: ")))
705 (pop-to-buffer mml2015-result-buffer)
706 (error "Encrypt error"))
707 (delete-region (point-min) (point-max))
708 (goto-char (point-min))
709 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
711 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
712 (insert (format "--%s\n" boundary))
713 (insert "Content-Type: application/pgp-encrypted\n\n")
714 (insert "Version: 1\n\n")
715 (insert (format "--%s\n" boundary))
716 (insert "Content-Type: application/octet-stream\n\n")
717 (insert-buffer-substring pgg-output-buffer)
718 (goto-char (point-max))
719 (insert (format "--%s--\n" boundary))
720 (goto-char (point-max))))
724 (defvar epg-user-id-alist)
725 (defvar epg-digest-algorithm-alist)
726 (defvar inhibit-redisplay)
728 (autoload 'epg-make-context "epg")
729 (autoload 'epg-context-set-armor "epg")
730 (autoload 'epg-context-set-textmode "epg")
731 (autoload 'epg-context-set-signers "epg")
732 (autoload 'epg-context-result-for "epg")
733 (autoload 'epg-new-signature-digest-algorithm "epg")
734 (autoload 'epg-verify-result-to-string "epg")
735 (autoload 'epg-list-keys "epg")
736 (autoload 'epg-decrypt-string "epg")
737 (autoload 'epg-verify-string "epg")
738 (autoload 'epg-sign-string "epg")
739 (autoload 'epg-encrypt-string "epg")
740 (autoload 'epg-passphrase-callback-function "epg")
741 (autoload 'epg-context-set-passphrase-callback "epg")
742 (autoload 'epg-key-sub-key-list "epg")
743 (autoload 'epg-sub-key-capability "epg")
744 (autoload 'epg-sub-key-validity "epg")
745 (autoload 'epg-configuration "epg-config")
746 (autoload 'epg-expand-group "epg-config")
747 (autoload 'epa-select-keys "epa")
749 (defvar mml2015-epg-secret-key-id-list nil)
751 (defun mml2015-epg-passphrase-callback (context key-id ignore)
753 (epg-passphrase-callback-function context key-id nil)
754 (let* ((password-cache-key-id
762 "Passphrase for PIN: "
763 (if (setq entry (assoc key-id epg-user-id-alist))
764 (format "Passphrase for %s %s: " key-id (cdr entry))
765 (format "Passphrase for %s: " key-id)))
766 password-cache-key-id)))
768 (let ((password-cache-expiry mml2015-passphrase-cache-expiry))
769 (password-cache-add password-cache-key-id passphrase))
770 (setq mml2015-epg-secret-key-id-list
771 (cons password-cache-key-id mml2015-epg-secret-key-id-list))
772 (copy-sequence passphrase)))))
774 (defun mml2015-epg-find-usable-key (keys usage)
777 (let ((pointer (epg-key-sub-key-list (car keys))))
779 (if (and (memq usage (epg-sub-key-capability (car pointer)))
780 (not (memq 'disabled (epg-sub-key-capability (car pointer))))
781 (not (memq (epg-sub-key-validity (car pointer))
782 '(revoked expired))))
783 (throw 'found (car keys)))
784 (setq pointer (cdr pointer))))
785 (setq keys (cdr keys)))))
787 (defun mml2015-epg-decrypt (handle ctl)
789 (let ((inhibit-redisplay t)
790 context plain child handles result decrypt-status)
791 (unless (setq child (mm-find-part-by-type
793 "application/octet-stream" nil t))
794 (mm-set-handle-multipart-parameter
795 mm-security-handle 'gnus-info "Corrupted")
796 (throw 'error handle))
797 (setq context (epg-make-context))
798 (if mml2015-cache-passphrase
799 (epg-context-set-passphrase-callback
801 #'mml2015-epg-passphrase-callback))
802 (condition-case error
803 (setq plain (epg-decrypt-string context (mm-get-part child))
804 mml2015-epg-secret-key-id-list nil)
806 (while mml2015-epg-secret-key-id-list
807 (password-cache-remove (car mml2015-epg-secret-key-id-list))
808 (setq mml2015-epg-secret-key-id-list
809 (cdr mml2015-epg-secret-key-id-list)))
810 (mm-set-handle-multipart-parameter
811 mm-security-handle 'gnus-info "Failed")
812 (if (eq (car error) 'quit)
813 (mm-set-handle-multipart-parameter
814 mm-security-handle 'gnus-details "Quit.")
815 (mm-set-handle-multipart-parameter
816 mm-security-handle 'gnus-details (mml2015-format-error error)))
817 (throw 'error handle)))
820 (goto-char (point-min))
821 (while (search-forward "\r\n" nil t)
822 (replace-match "\n" t t))
823 (setq handles (mm-dissect-buffer t))
824 (mm-destroy-parts handle)
825 (if (epg-context-result-for context 'verify)
826 (mm-set-handle-multipart-parameter
827 mm-security-handle 'gnus-info
829 (epg-verify-result-to-string
830 (epg-context-result-for context 'verify))))
831 (mm-set-handle-multipart-parameter
832 mm-security-handle 'gnus-info "OK"))
833 (if (stringp (car handles))
834 (mm-set-handle-multipart-parameter
835 mm-security-handle 'gnus-details
836 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
837 (if (listp (car handles))
841 (defun mml2015-epg-clear-decrypt ()
842 (let ((inhibit-redisplay t)
843 (context (epg-make-context))
845 (if mml2015-cache-passphrase
846 (epg-context-set-passphrase-callback
848 #'mml2015-epg-passphrase-callback))
849 (condition-case error
850 (setq plain (epg-decrypt-string context (buffer-string))
851 mml2015-epg-secret-key-id-list nil)
853 (while mml2015-epg-secret-key-id-list
854 (password-cache-remove (car mml2015-epg-secret-key-id-list))
855 (setq mml2015-epg-secret-key-id-list
856 (cdr mml2015-epg-secret-key-id-list)))
857 (mm-set-handle-multipart-parameter
858 mm-security-handle 'gnus-info "Failed")
859 (if (eq (car error) 'quit)
860 (mm-set-handle-multipart-parameter
861 mm-security-handle 'gnus-details "Quit.")
862 (mm-set-handle-multipart-parameter
863 mm-security-handle 'gnus-details (mml2015-format-error error)))))
866 ;; Treat data which epg returns as a unibyte string.
867 (mm-disable-multibyte)
869 (goto-char (point-min))
870 (while (search-forward "\r\n" nil t)
871 (replace-match "\n" t t))
872 (mm-set-handle-multipart-parameter
873 mm-security-handle 'gnus-info "OK")
874 (if (epg-context-result-for context 'verify)
875 (mm-set-handle-multipart-parameter
876 mm-security-handle 'gnus-details
877 (epg-verify-result-to-string
878 (epg-context-result-for context 'verify)))))))
880 (defun mml2015-epg-verify (handle ctl)
882 (let ((inhibit-redisplay t)
883 context plain signature-file part signature)
884 (when (or (null (setq part (mm-find-raw-part-by-type
885 ctl (or (mm-handle-multipart-ctl-parameter
887 "application/pgp-signature")
889 (null (setq signature (mm-find-part-by-type
890 (cdr handle) "application/pgp-signature"
892 (mm-set-handle-multipart-parameter
893 mm-security-handle 'gnus-info "Corrupted")
894 (throw 'error handle))
895 (setq part (mm-replace-in-string part "\n" "\r\n" t)
896 signature (mm-get-part signature)
897 context (epg-make-context))
898 (condition-case error
899 (setq plain (epg-verify-string context signature part))
901 (mm-set-handle-multipart-parameter
902 mm-security-handle 'gnus-info "Failed")
903 (if (eq (car error) 'quit)
904 (mm-set-handle-multipart-parameter
905 mm-security-handle 'gnus-details "Quit.")
906 (mm-set-handle-multipart-parameter
907 mm-security-handle 'gnus-details (mml2015-format-error error)))
908 (throw 'error handle)))
909 (mm-set-handle-multipart-parameter
910 mm-security-handle 'gnus-info
911 (epg-verify-result-to-string (epg-context-result-for context 'verify)))
914 (defun mml2015-epg-clear-verify ()
915 (let ((inhibit-redisplay t)
916 (context (epg-make-context))
917 (signature (mm-encode-coding-string (buffer-string)
918 coding-system-for-write))
920 (condition-case error
921 (setq plain (epg-verify-string context signature))
923 (mm-set-handle-multipart-parameter
924 mm-security-handle 'gnus-info "Failed")
925 (if (eq (car error) 'quit)
926 (mm-set-handle-multipart-parameter
927 mm-security-handle 'gnus-details "Quit.")
928 (mm-set-handle-multipart-parameter
929 mm-security-handle 'gnus-details (mml2015-format-error error)))))
932 (mm-set-handle-multipart-parameter
933 mm-security-handle 'gnus-info
934 (epg-verify-result-to-string
935 (epg-context-result-for context 'verify)))
936 (delete-region (point-min) (point-max))
937 (insert (mm-decode-coding-string plain coding-system-for-read)))
938 (mml2015-extract-cleartext-signature))))
940 (defun mml2015-epg-sign (cont)
941 (let* ((inhibit-redisplay t)
942 (context (epg-make-context))
943 (boundary (mml-compute-boundary cont))
946 (or (message-options-get 'mml2015-epg-signers)
949 (if (eq mm-sign-option 'guided)
950 (epa-select-keys context "\
951 Select keys for signing.
952 If no one is selected, default secret key is used. "
958 (setq signer-key (mml2015-epg-find-usable-key
959 (epg-list-keys context signer t)
961 (unless (or signer-key
964 "No secret key for %s; skip it? "
966 (error "No secret key for %s" signer))
968 mml2015-signers)))))))
970 (epg-context-set-armor context t)
971 (epg-context-set-textmode context t)
972 (epg-context-set-signers context signers)
973 (if mml2015-cache-passphrase
974 (epg-context-set-passphrase-callback
976 #'mml2015-epg-passphrase-callback))
977 (condition-case error
978 (setq signature (epg-sign-string context (buffer-string) t)
979 mml2015-epg-secret-key-id-list nil)
981 (while mml2015-epg-secret-key-id-list
982 (password-cache-remove (car mml2015-epg-secret-key-id-list))
983 (setq mml2015-epg-secret-key-id-list
984 (cdr mml2015-epg-secret-key-id-list)))
985 (signal (car error) (cdr error))))
986 (if (epg-context-result-for context 'sign)
987 (setq micalg (epg-new-signature-digest-algorithm
988 (car (epg-context-result-for context 'sign)))))
989 (goto-char (point-min))
990 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
993 (insert (format "\tmicalg=pgp-%s; "
996 epg-digest-algorithm-alist))))))
997 (insert "protocol=\"application/pgp-signature\"\n")
998 (insert (format "\n--%s\n" boundary))
999 (goto-char (point-max))
1000 (insert (format "\n--%s\n" boundary))
1001 (insert "Content-Type: application/pgp-signature\n\n")
1003 (goto-char (point-max))
1004 (insert (format "--%s--\n" boundary))
1005 (goto-char (point-max))))
1007 (defun mml2015-epg-encrypt (cont &optional sign)
1008 (let ((inhibit-redisplay t)
1009 (context (epg-make-context))
1010 (config (epg-configuration))
1011 (recipients (message-options-get 'mml2015-epg-recipients))
1013 (boundary (mml-compute-boundary cont))
1014 recipient-key signer-key)
1020 (or (epg-expand-group config recipient)
1021 (list (concat "<" recipient ">"))))
1023 (or (message-options-get 'message-recipients)
1024 (message-options-set 'message-recipients
1025 (read-string "Recipients: ")))
1026 "[ \f\t\n\r\v,]+"))))
1027 (when mml2015-encrypt-to-self
1028 (unless mml2015-signers
1029 (error "mml2015-signers not set"))
1030 (setq recipients (nconc recipients mml2015-signers)))
1031 (if (eq mm-encrypt-option 'guided)
1033 (epa-select-keys context "\
1034 Select recipients for encryption.
1035 If no one is selected, symmetric encryption will be performed. "
1041 (setq recipient-key (mml2015-epg-find-usable-key
1042 (epg-list-keys context recipient)
1044 (unless (or recipient-key
1046 (format "No public key for %s; skip it? "
1048 (error "No public key for %s" recipient))
1052 (error "No recipient specified")))
1053 (message-options-set 'mml2015-epg-recipients recipients))
1056 (or (message-options-get 'mml2015-epg-signers)
1057 (message-options-set
1058 'mml2015-epg-signers
1059 (if (eq mm-sign-option 'guided)
1060 (epa-select-keys context "\
1061 Select keys for signing.
1062 If no one is selected, default secret key is used. "
1068 (setq signer-key (mml2015-epg-find-usable-key
1069 (epg-list-keys context signer t)
1071 (unless (or signer-key
1074 "No secret key for %s; skip it? "
1076 (error "No secret key for %s" signer))
1078 mml2015-signers)))))))
1079 (epg-context-set-signers context signers))
1080 (epg-context-set-armor context t)
1081 (epg-context-set-textmode context t)
1082 (if mml2015-cache-passphrase
1083 (epg-context-set-passphrase-callback
1085 #'mml2015-epg-passphrase-callback))
1086 (condition-case error
1088 (epg-encrypt-string context (buffer-string) recipients sign
1089 mml2015-always-trust)
1090 mml2015-epg-secret-key-id-list nil)
1092 (while mml2015-epg-secret-key-id-list
1093 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1094 (setq mml2015-epg-secret-key-id-list
1095 (cdr mml2015-epg-secret-key-id-list)))
1096 (signal (car error) (cdr error))))
1097 (delete-region (point-min) (point-max))
1098 (goto-char (point-min))
1099 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1101 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1102 (insert (format "--%s\n" boundary))
1103 (insert "Content-Type: application/pgp-encrypted\n\n")
1104 (insert "Version: 1\n\n")
1105 (insert (format "--%s\n" boundary))
1106 (insert "Content-Type: application/octet-stream\n\n")
1108 (goto-char (point-max))
1109 (insert (format "--%s--\n" boundary))
1110 (goto-char (point-max))))
1114 (autoload 'gnus-buffer-live-p "gnus-util")
1115 (autoload 'gnus-get-buffer-create "gnus")
1117 (defun mml2015-clean-buffer ()
1118 (if (gnus-buffer-live-p mml2015-result-buffer)
1119 (with-current-buffer mml2015-result-buffer
1122 (setq mml2015-result-buffer
1123 (gnus-get-buffer-create " *MML2015 Result*"))
1126 (defsubst mml2015-clear-decrypt-function ()
1127 (nth 6 (assq mml2015-use mml2015-function-alist)))
1129 (defsubst mml2015-clear-verify-function ()
1130 (nth 5 (assq mml2015-use mml2015-function-alist)))
1133 (defun mml2015-decrypt (handle ctl)
1134 (mml2015-clean-buffer)
1135 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1137 (funcall func handle ctl)
1141 (defun mml2015-decrypt-test (handle ctl)
1145 (defun mml2015-verify (handle ctl)
1146 (mml2015-clean-buffer)
1147 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1149 (funcall func handle ctl)
1153 (defun mml2015-verify-test (handle ctl)
1157 (defun mml2015-encrypt (cont &optional sign)
1158 (mml2015-clean-buffer)
1159 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1161 (funcall func cont sign)
1162 (error "Cannot find encrypt function"))))
1165 (defun mml2015-sign (cont)
1166 (mml2015-clean-buffer)
1167 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1170 (error "Cannot find sign function"))))
1173 (defun mml2015-self-encrypt ()
1174 (mml2015-encrypt nil))
1178 ;;; mml2015.el ends here