1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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
44 ;; Avoid the "Recursive load suspected" error
46 (let ((recursive-load-depth-limit 100))
48 (and (fboundp 'pgg-sign-region)
53 (and (fboundp 'gpg-sign-detached)
57 (and (fboundp 'mc-encrypt-generic)
58 (fboundp 'mc-sign-generic)
59 (fboundp 'mc-cleanup-recipient-headers)
61 "The package used for PGP/MIME.
62 Valid packages include `pgg', `gpg' and `mailcrypt'.")
64 ;; Something is not RFC2015.
65 (defvar mml2015-function-alist
66 '((mailcrypt mml2015-mailcrypt-sign
67 mml2015-mailcrypt-encrypt
68 mml2015-mailcrypt-verify
69 mml2015-mailcrypt-decrypt
70 mml2015-mailcrypt-clear-verify
71 mml2015-mailcrypt-clear-decrypt)
76 mml2015-gpg-clear-verify
77 mml2015-gpg-clear-decrypt)
82 mml2015-pgg-clear-verify
83 mml2015-pgg-clear-decrypt)
88 mml2015-epg-clear-verify
89 mml2015-epg-clear-decrypt))
90 "Alist of PGP/MIME functions.")
92 (defvar mml2015-result-buffer nil)
94 (defcustom mml2015-unabbrev-trust-alist
95 '(("TRUST_UNDEFINED" . nil)
97 ("TRUST_MARGINAL" . t)
99 ("TRUST_ULTIMATE" . t))
100 "Map GnuPG trust output values to a boolean saying if you trust the key."
102 :group 'mime-security
103 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
104 (boolean :tag "Trust key"))))
106 (defcustom mml2015-verbose mml-secure-verbose
107 "If non-nil, ask the user about the current operation more verbosely."
108 :group 'mime-security
111 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
112 "If t, cache passphrase."
113 :group 'mime-security
116 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
117 "How many seconds the passphrase is cached.
118 Whether the passphrase is cached at all is controlled by
119 `mml2015-cache-passphrase'."
120 :group 'mime-security
123 (defcustom mml2015-signers nil
124 "A list of your own key ID which will be used to sign a message."
125 :group 'mime-security
126 :type '(repeat (string :tag "Key ID")))
128 (defcustom mml2015-encrypt-to-self nil
129 "If t, add your own key ID to recipient list when encryption."
130 :group 'mime-security
133 ;;; mailcrypt wrapper
136 (autoload 'mailcrypt-decrypt "mailcrypt")
137 (autoload 'mailcrypt-verify "mailcrypt")
138 (autoload 'mc-pgp-always-sign "mailcrypt")
139 (autoload 'mc-encrypt-generic "mc-toplev")
140 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
141 (autoload 'mc-sign-generic "mc-toplev"))
144 (defvar mc-default-scheme)
147 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
148 (defvar mml2015-verify-function 'mailcrypt-verify)
150 (defun mml2015-format-error (err)
151 (if (stringp (cadr err))
153 (format "%S" (cdr err))))
155 (defun mml2015-mailcrypt-decrypt (handle ctl)
157 (let (child handles result)
158 (unless (setq child (mm-find-part-by-type
160 "application/octet-stream" nil t))
161 (mm-set-handle-multipart-parameter
162 mm-security-handle 'gnus-info "Corrupted")
163 (throw 'error handle))
165 (mm-insert-part child)
168 (funcall mml2015-decrypt-function)
170 (mm-set-handle-multipart-parameter
171 mm-security-handle 'gnus-details (mml2015-format-error err))
174 (mm-set-handle-multipart-parameter
175 mm-security-handle 'gnus-details "Quit.")
178 (mm-set-handle-multipart-parameter
179 mm-security-handle 'gnus-info "Failed")
180 (throw 'error handle))
181 (setq handles (mm-dissect-buffer t)))
182 (mm-destroy-parts handle)
183 (mm-set-handle-multipart-parameter
184 mm-security-handle 'gnus-info
186 (let ((sig (with-current-buffer mml2015-result-buffer
187 (mml2015-gpg-extract-signature-details))))
188 (concat ", Signer: " sig))))
189 (if (listp (car handles))
193 (defun mml2015-mailcrypt-clear-decrypt ()
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 "OK")
209 (mm-set-handle-multipart-parameter
210 mm-security-handle 'gnus-info "Failed"))))
212 (defun mml2015-fix-micalg (alg)
214 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
215 (upcase (if (string-match "^p[gh]p-" alg)
216 (substring alg (match-end 0))
219 (defun mml2015-mailcrypt-verify (handle ctl)
222 (unless (setq part (mm-find-raw-part-by-type
223 ctl (or (mm-handle-multipart-ctl-parameter
225 "application/pgp-signature")
227 (mm-set-handle-multipart-parameter
228 mm-security-handle 'gnus-info "Corrupted")
229 (throw 'error handle))
231 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
232 (insert (format "Hash: %s\n\n"
233 (or (mml2015-fix-micalg
234 (mm-handle-multipart-ctl-parameter
238 (narrow-to-region (point) (point))
240 (goto-char (point-min))
242 (if (looking-at "^-")
245 (unless (setq part (mm-find-part-by-type
246 (cdr handle) "application/pgp-signature" nil t))
247 (mm-set-handle-multipart-parameter
248 mm-security-handle 'gnus-info "Corrupted")
249 (throw 'error handle))
251 (narrow-to-region (point) (point))
252 (mm-insert-part part)
253 (goto-char (point-min))
254 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
255 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
256 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
257 (replace-match "-----END PGP SIGNATURE-----" t t)))
258 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
259 (unless (condition-case err
261 (funcall mml2015-verify-function)
262 (if (get-buffer " *mailcrypt stderr temp")
263 (mm-set-handle-multipart-parameter
264 mm-security-handle 'gnus-details
265 (with-current-buffer " *mailcrypt stderr temp"
267 (if (get-buffer " *mailcrypt stdout temp")
268 (kill-buffer " *mailcrypt stdout temp"))
269 (if (get-buffer " *mailcrypt stderr temp")
270 (kill-buffer " *mailcrypt stderr temp"))
271 (if (get-buffer " *mailcrypt status temp")
272 (kill-buffer " *mailcrypt status temp"))
273 (if (get-buffer mc-gpg-debug-buffer)
274 (kill-buffer mc-gpg-debug-buffer)))
276 (mm-set-handle-multipart-parameter
277 mm-security-handle 'gnus-details (mml2015-format-error err))
280 (mm-set-handle-multipart-parameter
281 mm-security-handle 'gnus-details "Quit.")
283 (mm-set-handle-multipart-parameter
284 mm-security-handle 'gnus-info "Failed")
285 (throw 'error handle))))
286 (mm-set-handle-multipart-parameter
287 mm-security-handle 'gnus-info "OK")
290 (defun mml2015-mailcrypt-clear-verify ()
291 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
292 (if (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 "OK")
318 (mm-set-handle-multipart-parameter
319 mm-security-handle 'gnus-info "Failed"))))
321 (defun mml2015-mailcrypt-sign (cont)
322 (mc-sign-generic (message-options-get 'message-sender)
324 (let ((boundary (mml-compute-boundary cont))
326 (goto-char (point-min))
327 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
328 (error "Cannot find signed begin line"))
329 (goto-char (match-beginning 0))
331 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
332 (error "Cannot not find PGP hash"))
333 (setq hash (match-string 1))
334 (unless (re-search-forward "^$" nil t)
335 (error "Cannot not find PGP message"))
337 (delete-region (point-min) (point))
338 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
340 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
342 (insert (format "\n--%s\n" boundary))
344 (goto-char (point-max))
345 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
346 (error "Cannot find signature part"))
347 (replace-match "-----END PGP MESSAGE-----" t t)
348 (goto-char (match-beginning 0))
349 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
351 (error "Cannot find signature part"))
352 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
353 (goto-char (match-beginning 0))
355 (narrow-to-region point (point))
357 (while (re-search-forward "^- -" nil t)
358 (replace-match "-" t t))
359 (goto-char (point-max)))
360 (insert (format "--%s\n" boundary))
361 (insert "Content-Type: application/pgp-signature\n\n")
362 (goto-char (point-max))
363 (insert (format "--%s--\n" boundary))
364 (goto-char (point-max))))
366 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
367 (let ((mc-pgp-always-sign
368 (or mc-pgp-always-sign
370 (eq t (or (message-options-get 'message-sign-encrypt)
372 'message-sign-encrypt
373 (or (y-or-n-p "Sign the message? ")
376 (mm-with-unibyte-current-buffer
378 (or (message-options-get 'message-recipients)
379 (message-options-set 'message-recipients
380 (mc-cleanup-recipient-headers
381 (read-string "Recipients: "))))
383 (message-options-get 'message-sender))))
384 (goto-char (point-min))
385 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
386 (error "Fail to encrypt the message"))
387 (let ((boundary (mml-compute-boundary cont)))
388 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
390 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
391 (insert (format "--%s\n" boundary))
392 (insert "Content-Type: application/pgp-encrypted\n\n")
393 (insert "Version: 1\n\n")
394 (insert (format "--%s\n" boundary))
395 (insert "Content-Type: application/octet-stream\n\n")
396 (goto-char (point-max))
397 (insert (format "--%s--\n" boundary))
398 (goto-char (point-max))))
403 (autoload 'gpg-decrypt "gpg")
404 (autoload 'gpg-verify "gpg")
405 (autoload 'gpg-verify-cleartext "gpg")
406 (autoload 'gpg-sign-detached "gpg")
407 (autoload 'gpg-sign-encrypt "gpg")
408 (autoload 'gpg-encrypt "gpg")
409 (autoload 'gpg-passphrase-read "gpg"))
411 (defun mml2015-gpg-passphrase ()
412 (or (message-options-get 'gpg-passphrase)
413 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
415 (defun mml2015-gpg-decrypt-1 ()
416 (let ((cipher (current-buffer)) plain result)
417 (if (with-temp-buffer
419 (gpg-decrypt cipher (setq plain (current-buffer))
420 mml2015-result-buffer nil)
421 (mm-set-handle-multipart-parameter
422 mm-security-handle 'gnus-details
423 (with-current-buffer mml2015-result-buffer
427 (insert-buffer-substring plain)
428 (goto-char (point-min))
429 (while (search-forward "\r\n" nil t)
430 (replace-match "\n" t t))))
432 ;; Some wrong with the return value, check plain text buffer.
433 (if (> (point-max) (point-min))
437 (defun mml2015-gpg-decrypt (handle ctl)
438 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
439 (mml2015-mailcrypt-decrypt handle ctl)))
441 (defun mml2015-gpg-clear-decrypt ()
443 (setq result (mml2015-gpg-decrypt-1))
445 (mm-set-handle-multipart-parameter
446 mm-security-handle 'gnus-info "OK")
447 (mm-set-handle-multipart-parameter
448 mm-security-handle 'gnus-info "Failed"))))
450 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
452 (fpr-length (string-width fingerprint))
455 (setq fingerprint (string-to-list fingerprint))
457 (setq fpr-length (- fpr-length 4))
458 (setq slice (butlast fingerprint fpr-length))
459 (setq fingerprint (nthcdr 4 fingerprint))
460 (setq n-slice (1+ n-slice))
466 (otherwise (concat " " slice))))))
469 (defun mml2015-gpg-extract-signature-details ()
470 (goto-char (point-min))
471 (let* ((expired (re-search-forward
472 "^\\[GNUPG:\\] SIGEXPIRED$"
474 (signer (and (re-search-forward
475 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
477 (cons (match-string 1) (match-string 2))))
478 (fprint (and (re-search-forward
479 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
482 (trust (and (re-search-forward
483 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
487 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
488 (cond ((and signer fprint)
490 (unless trust-good-enough-p
491 (concat "\nUntrusted, Fingerprint: "
492 (mml2015-gpg-pretty-print-fpr fprint)))
494 (format "\nWARNING: Signature from expired key (%s)"
497 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
500 "From unknown user"))))
502 (defun mml2015-gpg-verify (handle ctl)
504 (let (part message signature info-is-set-p)
505 (unless (setq part (mm-find-raw-part-by-type
506 ctl (or (mm-handle-multipart-ctl-parameter
508 "application/pgp-signature")
510 (mm-set-handle-multipart-parameter
511 mm-security-handle 'gnus-info "Corrupted")
512 (throw 'error handle))
514 (setq message (current-buffer))
516 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
517 ;; clearsign use --textmode. The conversion is not necessary.
518 ;; In clearverify, the conversion is not necessary either.
519 (goto-char (point-min))
522 (unless (eq (char-before) ?\r)
527 (setq signature (current-buffer))
528 (unless (setq part (mm-find-part-by-type
529 (cdr handle) "application/pgp-signature" nil t))
530 (mm-set-handle-multipart-parameter
531 mm-security-handle 'gnus-info "Corrupted")
532 (throw 'error handle))
533 (mm-insert-part part)
534 (unless (condition-case err
536 (gpg-verify message signature mml2015-result-buffer)
537 (mm-set-handle-multipart-parameter
538 mm-security-handle 'gnus-details
539 (with-current-buffer mml2015-result-buffer
542 (mm-set-handle-multipart-parameter
543 mm-security-handle 'gnus-details (mml2015-format-error err))
544 (mm-set-handle-multipart-parameter
545 mm-security-handle 'gnus-info "Error.")
546 (setq info-is-set-p t)
549 (mm-set-handle-multipart-parameter
550 mm-security-handle 'gnus-details "Quit.")
551 (mm-set-handle-multipart-parameter
552 mm-security-handle 'gnus-info "Quit.")
553 (setq info-is-set-p t)
555 (unless info-is-set-p
556 (mm-set-handle-multipart-parameter
557 mm-security-handle 'gnus-info "Failed"))
558 (throw 'error handle)))
559 (mm-set-handle-multipart-parameter
560 mm-security-handle 'gnus-info
561 (with-current-buffer mml2015-result-buffer
562 (mml2015-gpg-extract-signature-details))))
565 (defun mml2015-gpg-clear-verify ()
566 (if (condition-case err
568 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
569 (mm-set-handle-multipart-parameter
570 mm-security-handle 'gnus-details
571 (with-current-buffer mml2015-result-buffer
574 (mm-set-handle-multipart-parameter
575 mm-security-handle 'gnus-details (mml2015-format-error err))
578 (mm-set-handle-multipart-parameter
579 mm-security-handle 'gnus-details "Quit.")
581 (mm-set-handle-multipart-parameter
582 mm-security-handle 'gnus-info
583 (with-current-buffer mml2015-result-buffer
584 (mml2015-gpg-extract-signature-details)))
585 (mm-set-handle-multipart-parameter
586 mm-security-handle 'gnus-info "Failed")))
588 (defun mml2015-gpg-sign (cont)
589 (let ((boundary (mml-compute-boundary cont))
590 (text (current-buffer)) signature)
591 (goto-char (point-max))
595 (unless (gpg-sign-detached text (setq signature (current-buffer))
596 mml2015-result-buffer
598 (message-options-get 'message-sender)
599 t t) ; armor & textmode
600 (unless (> (point-max) (point-min))
601 (pop-to-buffer mml2015-result-buffer)
602 (error "Sign error")))
603 (goto-char (point-min))
604 (while (re-search-forward "\r+$" nil t)
605 (replace-match "" t t))
607 (goto-char (point-min))
608 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
610 ;;; FIXME: what is the micalg?
611 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
612 (insert (format "\n--%s\n" boundary))
613 (goto-char (point-max))
614 (insert (format "\n--%s\n" boundary))
615 (insert "Content-Type: application/pgp-signature\n\n")
616 (insert-buffer-substring signature)
617 (goto-char (point-max))
618 (insert (format "--%s--\n" boundary))
619 (goto-char (point-max)))))
621 (defun mml2015-gpg-encrypt (cont &optional sign)
622 (let ((boundary (mml-compute-boundary cont))
623 (text (current-buffer))
625 (mm-with-unibyte-current-buffer
627 ;; set up a function to call the correct gpg encrypt routine
628 ;; with the right arguments. (FIXME: this should be done
630 (flet ((gpg-encrypt-func
631 (sign plaintext ciphertext result recipients &optional
632 passphrase sign-with-key armor textmode)
635 plaintext ciphertext result recipients passphrase
636 sign-with-key armor textmode)
638 plaintext ciphertext result recipients passphrase
640 (unless (gpg-encrypt-func
641 sign ; passed in when using signencrypt
642 text (setq cipher (current-buffer))
643 mml2015-result-buffer
646 (message-options-get 'message-recipients)
647 (message-options-set 'message-recipients
648 (read-string "Recipients: ")))
651 (message-options-get 'message-sender)
652 t t) ; armor & textmode
653 (unless (> (point-max) (point-min))
654 (pop-to-buffer mml2015-result-buffer)
655 (error "Encrypt error"))))
656 (goto-char (point-min))
657 (while (re-search-forward "\r+$" nil t)
658 (replace-match "" t t))
660 (delete-region (point-min) (point-max))
661 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
663 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
664 (insert (format "--%s\n" boundary))
665 (insert "Content-Type: application/pgp-encrypted\n\n")
666 (insert "Version: 1\n\n")
667 (insert (format "--%s\n" boundary))
668 (insert "Content-Type: application/octet-stream\n\n")
669 (insert-buffer-substring cipher)
670 (goto-char (point-max))
671 (insert (format "--%s--\n" boundary))
672 (goto-char (point-max))))))
677 (defvar pgg-default-user-id)
678 (defvar pgg-errors-buffer)
679 (defvar pgg-output-buffer))
682 (autoload 'pgg-decrypt-region "pgg")
683 (autoload 'pgg-verify-region "pgg")
684 (autoload 'pgg-sign-region "pgg")
685 (autoload 'pgg-encrypt-region "pgg")
686 (autoload 'pgg-parse-armor "pgg-parse"))
688 (defun mml2015-pgg-decrypt (handle ctl)
690 (let ((pgg-errors-buffer mml2015-result-buffer)
691 child handles result decrypt-status)
692 (unless (setq child (mm-find-part-by-type
694 "application/octet-stream" nil t))
695 (mm-set-handle-multipart-parameter
696 mm-security-handle 'gnus-info "Corrupted")
697 (throw 'error handle))
699 (mm-insert-part child)
700 (if (condition-case err
702 (pgg-decrypt-region (point-min) (point-max))
704 (with-current-buffer mml2015-result-buffer
706 (mm-set-handle-multipart-parameter
707 mm-security-handle 'gnus-details
710 (mm-set-handle-multipart-parameter
711 mm-security-handle 'gnus-details (mml2015-format-error err))
714 (mm-set-handle-multipart-parameter
715 mm-security-handle 'gnus-details "Quit.")
717 (with-current-buffer pgg-output-buffer
718 (goto-char (point-min))
719 (while (search-forward "\r\n" nil t)
720 (replace-match "\n" t t))
721 (setq handles (mm-dissect-buffer t))
722 (mm-destroy-parts handle)
723 (mm-set-handle-multipart-parameter
724 mm-security-handle 'gnus-info "OK")
725 (mm-set-handle-multipart-parameter
726 mm-security-handle 'gnus-details
727 (concat decrypt-status
728 (when (stringp (car handles))
729 "\n" (mm-handle-multipart-ctl-parameter
730 handles 'gnus-details))))
731 (if (listp (car handles))
734 (mm-set-handle-multipart-parameter
735 mm-security-handle 'gnus-info "Failed")
736 (throw 'error handle))))))
738 (defun mml2015-pgg-clear-decrypt ()
739 (let ((pgg-errors-buffer mml2015-result-buffer))
741 (pgg-decrypt-region (point-min) (point-max))
742 (mm-set-handle-multipart-parameter
743 mm-security-handle 'gnus-details
744 (with-current-buffer mml2015-result-buffer
748 ;; Treat data which pgg returns as a unibyte string.
749 (mm-disable-multibyte)
750 (insert-buffer-substring pgg-output-buffer)
751 (goto-char (point-min))
752 (while (search-forward "\r\n" nil t)
753 (replace-match "\n" t t))
754 (mm-set-handle-multipart-parameter
755 mm-security-handle 'gnus-info "OK"))
756 (mm-set-handle-multipart-parameter
757 mm-security-handle 'gnus-info "Failed"))))
759 (defun mml2015-pgg-verify (handle ctl)
760 (let ((pgg-errors-buffer mml2015-result-buffer)
761 signature-file part signature)
762 (if (or (null (setq part (mm-find-raw-part-by-type
763 ctl (or (mm-handle-multipart-ctl-parameter
765 "application/pgp-signature")
767 (null (setq signature (mm-find-part-by-type
768 (cdr handle) "application/pgp-signature" nil t))))
770 (mm-set-handle-multipart-parameter
771 mm-security-handle 'gnus-info "Corrupted")
775 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
776 ;; clearsign use --textmode. The conversion is not necessary.
777 ;; In clearverify, the conversion is not necessary either.
778 (goto-char (point-min))
781 (unless (eq (char-before) ?\r)
785 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
786 (mm-insert-part signature))
787 (if (condition-case err
789 (pgg-verify-region (point-min) (point-max)
791 (goto-char (point-min))
792 (while (search-forward "\r\n" nil t)
793 (replace-match "\n" t t))
794 (mm-set-handle-multipart-parameter
795 mm-security-handle 'gnus-details
796 (concat (with-current-buffer pgg-output-buffer
798 (with-current-buffer pgg-errors-buffer
801 (mm-set-handle-multipart-parameter
802 mm-security-handle 'gnus-details (mml2015-format-error err))
805 (mm-set-handle-multipart-parameter
806 mm-security-handle 'gnus-details "Quit.")
809 (delete-file signature-file)
810 (mm-set-handle-multipart-parameter
811 mm-security-handle 'gnus-info
812 (with-current-buffer pgg-errors-buffer
813 (mml2015-gpg-extract-signature-details))))
814 (delete-file signature-file)
815 (mm-set-handle-multipart-parameter
816 mm-security-handle 'gnus-info "Failed")))))
819 (defun mml2015-pgg-clear-verify ()
820 (let ((pgg-errors-buffer mml2015-result-buffer)
821 (text (buffer-string))
822 (coding-system buffer-file-coding-system))
823 (if (condition-case err
825 (mm-with-unibyte-buffer
826 (insert (encode-coding-string text coding-system))
827 (pgg-verify-region (point-min) (point-max) nil t))
828 (goto-char (point-min))
829 (while (search-forward "\r\n" nil t)
830 (replace-match "\n" t t))
831 (mm-set-handle-multipart-parameter
832 mm-security-handle 'gnus-details
833 (concat (with-current-buffer pgg-output-buffer
835 (with-current-buffer pgg-errors-buffer
838 (mm-set-handle-multipart-parameter
839 mm-security-handle 'gnus-details (mml2015-format-error err))
842 (mm-set-handle-multipart-parameter
843 mm-security-handle 'gnus-details "Quit.")
845 (mm-set-handle-multipart-parameter
846 mm-security-handle 'gnus-info
847 (with-current-buffer pgg-errors-buffer
848 (mml2015-gpg-extract-signature-details)))
849 (mm-set-handle-multipart-parameter
850 mm-security-handle 'gnus-info "Failed"))))
852 (defun mml2015-pgg-sign (cont)
853 (let ((pgg-errors-buffer mml2015-result-buffer)
854 (boundary (mml-compute-boundary cont))
855 (pgg-default-user-id (or (message-options-get 'mml-sender)
856 pgg-default-user-id))
859 (unless (pgg-sign-region (point-min) (point-max))
860 (pop-to-buffer mml2015-result-buffer)
861 (error "Sign error"))
862 (goto-char (point-min))
863 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
865 (if (setq entry (assq 2 (pgg-parse-armor
866 (with-current-buffer pgg-output-buffer
868 (setq entry (assq 'hash-algorithm (cdr entry))))
869 (insert (format "\tmicalg=%s; "
871 (downcase (format "pgp-%s" (cdr entry)))
873 (insert "protocol=\"application/pgp-signature\"\n")
874 (insert (format "\n--%s\n" boundary))
875 (goto-char (point-max))
876 (insert (format "\n--%s\n" boundary))
877 (insert "Content-Type: application/pgp-signature\n\n")
878 (insert-buffer-substring pgg-output-buffer)
879 (goto-char (point-max))
880 (insert (format "--%s--\n" boundary))
881 (goto-char (point-max))))
883 (defun mml2015-pgg-encrypt (cont &optional sign)
884 (let ((pgg-errors-buffer mml2015-result-buffer)
886 (boundary (mml-compute-boundary cont)))
887 (unless (pgg-encrypt-region (point-min) (point-max)
890 (message-options-get 'message-recipients)
891 (message-options-set 'message-recipients
892 (read-string "Recipients: ")))
895 (pop-to-buffer mml2015-result-buffer)
896 (error "Encrypt error"))
897 (delete-region (point-min) (point-max))
898 (goto-char (point-min))
899 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
901 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
902 (insert (format "--%s\n" boundary))
903 (insert "Content-Type: application/pgp-encrypted\n\n")
904 (insert "Version: 1\n\n")
905 (insert (format "--%s\n" boundary))
906 (insert "Content-Type: application/octet-stream\n\n")
907 (insert-buffer-substring pgg-output-buffer)
908 (goto-char (point-max))
909 (insert (format "--%s--\n" boundary))
910 (goto-char (point-max))))
915 (autoload 'epg-make-context "epg"))
918 (defvar epg-user-id-alist)
919 (defvar epg-digest-algorithm-alist)
920 (defvar inhibit-redisplay)
921 (autoload 'epg-context-set-armor "epg")
922 (autoload 'epg-context-set-textmode "epg")
923 (autoload 'epg-context-set-signers "epg")
924 (autoload 'epg-context-result-for "epg")
925 (autoload 'epg-new-signature-digest-algorithm "epg")
926 (autoload 'epg-verify-result-to-string "epg")
927 (autoload 'epg-list-keys "epg")
928 (autoload 'epg-decrypt-string "epg")
929 (autoload 'epg-verify-string "epg")
930 (autoload 'epg-sign-string "epg")
931 (autoload 'epg-encrypt-string "epg")
932 (autoload 'epg-passphrase-callback-function "epg")
933 (autoload 'epg-context-set-passphrase-callback "epg")
934 (autoload 'epg-configuration "epg-config")
935 (autoload 'epg-expand-group "epg-config"))
938 (defvar password-cache-expiry)
939 (autoload 'password-read "password")
940 (autoload 'password-cache-add "password")
941 (autoload 'password-cache-remove "password"))
943 (defvar mml2015-epg-secret-key-id-list nil)
945 (defun mml2015-epg-passphrase-callback (context key-id ignore)
947 (epg-passphrase-callback-function context key-id nil)
948 (let* ((entry (assoc key-id epg-user-id-alist))
951 (format "GnuPG passphrase for %s: "
959 (let ((password-cache-expiry mml2015-passphrase-cache-expiry))
960 (password-cache-add key-id passphrase))
961 (setq mml2015-epg-secret-key-id-list
962 (cons key-id mml2015-epg-secret-key-id-list))
963 (copy-sequence passphrase)))))
965 (defun mml2015-epg-decrypt (handle ctl)
967 (let ((inhibit-redisplay t)
968 context plain child handles result decrypt-status)
969 (unless (setq child (mm-find-part-by-type
971 "application/octet-stream" nil t))
972 (mm-set-handle-multipart-parameter
973 mm-security-handle 'gnus-info "Corrupted")
974 (throw 'error handle))
975 (setq context (epg-make-context))
976 (if mml2015-cache-passphrase
977 (epg-context-set-passphrase-callback
979 #'mml2015-epg-passphrase-callback))
980 (condition-case error
981 (setq plain (epg-decrypt-string context (mm-get-part child))
982 mml2015-epg-secret-key-id-list nil)
984 (while mml2015-epg-secret-key-id-list
985 (password-cache-remove (car mml2015-epg-secret-key-id-list))
986 (setq mml2015-epg-secret-key-id-list
987 (cdr mml2015-epg-secret-key-id-list)))
988 (mm-set-handle-multipart-parameter
989 mm-security-handle 'gnus-info "Failed")
990 (if (eq (car error) 'quit)
991 (mm-set-handle-multipart-parameter
992 mm-security-handle 'gnus-details "Quit.")
993 (mm-set-handle-multipart-parameter
994 mm-security-handle 'gnus-details (mml2015-format-error error)))
995 (throw 'error handle)))
998 (goto-char (point-min))
999 (while (search-forward "\r\n" nil t)
1000 (replace-match "\n" t t))
1001 (setq handles (mm-dissect-buffer t))
1002 (mm-destroy-parts handle)
1003 (if (epg-context-result-for context 'verify)
1004 (mm-set-handle-multipart-parameter
1005 mm-security-handle 'gnus-info
1007 (epg-verify-result-to-string
1008 (epg-context-result-for context 'verify))))
1009 (mm-set-handle-multipart-parameter
1010 mm-security-handle 'gnus-info "OK"))
1011 (if (stringp (car handles))
1012 (mm-set-handle-multipart-parameter
1013 mm-security-handle 'gnus-details
1014 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
1015 (if (listp (car handles))
1019 (defun mml2015-epg-clear-decrypt ()
1020 (let ((inhibit-redisplay t)
1021 (context (epg-make-context))
1023 (if mml2015-cache-passphrase
1024 (epg-context-set-passphrase-callback
1026 #'mml2015-epg-passphrase-callback))
1027 (condition-case error
1028 (setq plain (epg-decrypt-string context (buffer-string))
1029 mml2015-epg-secret-key-id-list nil)
1031 (while mml2015-epg-secret-key-id-list
1032 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1033 (setq mml2015-epg-secret-key-id-list
1034 (cdr mml2015-epg-secret-key-id-list)))
1035 (mm-set-handle-multipart-parameter
1036 mm-security-handle 'gnus-info "Failed")
1037 (if (eq (car error) 'quit)
1038 (mm-set-handle-multipart-parameter
1039 mm-security-handle 'gnus-details "Quit.")
1040 (mm-set-handle-multipart-parameter
1041 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1044 ;; Treat data which epg returns as a unibyte string.
1045 (mm-disable-multibyte)
1047 (goto-char (point-min))
1048 (while (search-forward "\r\n" nil t)
1049 (replace-match "\n" t t))
1050 (if (epg-context-result-for context 'verify)
1051 (mm-set-handle-multipart-parameter
1052 mm-security-handle 'gnus-info
1054 (epg-verify-result-to-string
1055 (epg-context-result-for context 'verify))))
1056 (mm-set-handle-multipart-parameter
1057 mm-security-handle 'gnus-info "OK")))))
1059 (defun mml2015-epg-verify (handle ctl)
1061 (let ((inhibit-redisplay t)
1062 context plain signature-file part signature)
1063 (when (or (null (setq part (mm-find-raw-part-by-type
1064 ctl (or (mm-handle-multipart-ctl-parameter
1066 "application/pgp-signature")
1068 (null (setq signature (mm-find-part-by-type
1069 (cdr handle) "application/pgp-signature"
1071 (mm-set-handle-multipart-parameter
1072 mm-security-handle 'gnus-info "Corrupted")
1073 (throw 'error handle))
1074 (setq context (epg-make-context))
1075 (condition-case error
1076 (setq plain (epg-verify-string context (mm-get-part signature) part))
1078 (mm-set-handle-multipart-parameter
1079 mm-security-handle 'gnus-info "Failed")
1080 (if (eq (car error) 'quit)
1081 (mm-set-handle-multipart-parameter
1082 mm-security-handle 'gnus-details "Quit.")
1083 (mm-set-handle-multipart-parameter
1084 mm-security-handle 'gnus-details (mml2015-format-error error)))
1085 (throw 'error handle)))
1086 (mm-set-handle-multipart-parameter
1087 mm-security-handle 'gnus-info
1088 (epg-verify-result-to-string (epg-context-result-for context 'verify)))
1091 (defun mml2015-epg-clear-verify ()
1092 (let ((inhibit-redisplay t)
1093 (context (epg-make-context))
1094 (signature (encode-coding-string (buffer-string)
1095 buffer-file-coding-system))
1097 (condition-case error
1098 (setq plain (epg-verify-string context signature))
1100 (mm-set-handle-multipart-parameter
1101 mm-security-handle 'gnus-info "Failed")
1102 (if (eq (car error) 'quit)
1103 (mm-set-handle-multipart-parameter
1104 mm-security-handle 'gnus-details "Quit.")
1105 (mm-set-handle-multipart-parameter
1106 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1108 (mm-set-handle-multipart-parameter
1109 mm-security-handle 'gnus-info
1110 (epg-verify-result-to-string
1111 (epg-context-result-for context 'verify))))))
1113 (defun mml2015-epg-sign (cont)
1114 (let ((inhibit-redisplay t)
1115 (context (epg-make-context))
1116 (boundary (mml-compute-boundary cont))
1117 signers signature micalg)
1119 (setq signers (epa-select-keys context "Select keys for signing.
1120 If no one is selected, default secret key is used. "
1123 (setq signers (mapcar (lambda (name)
1124 (car (epg-list-keys context name t)))
1126 (setq signers (list (car (epg-list-keys
1128 (message-options-get 'mml-sender)
1130 (epg-context-set-armor context t)
1131 (epg-context-set-textmode context t)
1132 (epg-context-set-signers context signers)
1133 (if mml2015-cache-passphrase
1134 (epg-context-set-passphrase-callback
1136 #'mml2015-epg-passphrase-callback))
1137 (condition-case error
1138 (setq signature (epg-sign-string context (buffer-string) t)
1139 mml2015-epg-secret-key-id-list nil)
1141 (while mml2015-epg-secret-key-id-list
1142 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1143 (setq mml2015-epg-secret-key-id-list
1144 (cdr mml2015-epg-secret-key-id-list)))
1145 (signal (car error) (cdr error))))
1146 (if (epg-context-result-for context 'sign)
1147 (setq micalg (epg-new-signature-digest-algorithm
1148 (car (epg-context-result-for context 'sign)))))
1149 (goto-char (point-min))
1150 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
1153 (insert (format "\tmicalg=%s; "
1156 epg-digest-algorithm-alist))))))
1157 (insert "protocol=\"application/pgp-signature\"\n")
1158 (insert (format "\n--%s\n" boundary))
1159 (goto-char (point-max))
1160 (insert (format "\n--%s\n" boundary))
1161 (insert "Content-Type: application/pgp-signature\n\n")
1163 (goto-char (point-max))
1164 (insert (format "--%s--\n" boundary))
1165 (goto-char (point-max))))
1167 (defun mml2015-epg-encrypt (cont &optional sign)
1168 (let ((inhibit-redisplay t)
1169 (context (epg-make-context))
1171 (if (message-options-get 'message-recipients)
1173 (message-options-get 'message-recipients)
1174 "[ \f\t\n\r\v,]+")))
1175 cipher signers config
1176 (boundary (mml-compute-boundary cont)))
1177 ;; We should remove this check if epg-0.0.6 is released.
1178 (if (and (condition-case nil
1179 (require 'epg-config)
1181 (functionp #'epg-expand-group))
1182 (setq config (epg-configuration)
1185 (mapcar (lambda (recipient)
1186 (or (epg-expand-group config recipient)
1191 (epa-select-keys context "Select recipients for encryption.
1192 If no one is selected, symmetric encryption will be performed. "
1195 (delq nil (mapcar (lambda (name)
1196 (car (epg-list-keys context name)))
1198 (if mml2015-encrypt-to-self
1202 (mapcar (lambda (name)
1203 (car (epg-list-keys context name)))
1207 (list (car (epg-list-keys context nil t)))))))
1210 (setq signers (epa-select-keys context "Select keys for signing.
1211 If no one is selected, default secret key is used. "
1214 (setq signers (mapcar (lambda (name)
1215 (car (epg-list-keys context name t)))
1217 (setq signers (list (car (epg-list-keys
1219 (message-options-get 'mml-sender)
1221 (epg-context-set-signers context signers))
1222 (epg-context-set-armor context t)
1223 (epg-context-set-textmode context t)
1224 (if mml2015-cache-passphrase
1225 (epg-context-set-passphrase-callback
1227 #'mml2015-epg-passphrase-callback))
1228 (condition-case error
1230 (epg-encrypt-string context (buffer-string) recipients sign)
1231 mml2015-epg-secret-key-id-list nil)
1233 (while mml2015-epg-secret-key-id-list
1234 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1235 (setq mml2015-epg-secret-key-id-list
1236 (cdr mml2015-epg-secret-key-id-list)))
1237 (signal (car error) (cdr error))))
1238 (delete-region (point-min) (point-max))
1239 (goto-char (point-min))
1240 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1242 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1243 (insert (format "--%s\n" boundary))
1244 (insert "Content-Type: application/pgp-encrypted\n\n")
1245 (insert "Version: 1\n\n")
1246 (insert (format "--%s\n" boundary))
1247 (insert "Content-Type: application/octet-stream\n\n")
1249 (goto-char (point-max))
1250 (insert (format "--%s--\n" boundary))
1251 (goto-char (point-max))))
1255 (defun mml2015-clean-buffer ()
1256 (if (gnus-buffer-live-p mml2015-result-buffer)
1257 (with-current-buffer mml2015-result-buffer
1260 (setq mml2015-result-buffer
1261 (gnus-get-buffer-create " *MML2015 Result*"))
1264 (defsubst mml2015-clear-decrypt-function ()
1265 (nth 6 (assq mml2015-use mml2015-function-alist)))
1267 (defsubst mml2015-clear-verify-function ()
1268 (nth 5 (assq mml2015-use mml2015-function-alist)))
1271 (defun mml2015-decrypt (handle ctl)
1272 (mml2015-clean-buffer)
1273 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1275 (funcall func handle ctl)
1279 (defun mml2015-decrypt-test (handle ctl)
1283 (defun mml2015-verify (handle ctl)
1284 (mml2015-clean-buffer)
1285 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1287 (funcall func handle ctl)
1291 (defun mml2015-verify-test (handle ctl)
1295 (defun mml2015-encrypt (cont &optional sign)
1296 (mml2015-clean-buffer)
1297 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1299 (funcall func cont sign)
1300 (error "Cannot find encrypt function"))))
1303 (defun mml2015-sign (cont)
1304 (mml2015-clean-buffer)
1305 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1308 (error "Cannot find sign function"))))
1311 (defun mml2015-self-encrypt ()
1312 (mml2015-encrypt nil))
1316 ;;; arch-tag: b04701d5-0b09-44d8-bed8-de901bf435f2
1317 ;;; mml2015.el ends here