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
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 ;;; mailcrypt wrapper
147 (autoload 'mailcrypt-decrypt "mailcrypt")
148 (autoload 'mailcrypt-verify "mailcrypt")
149 (autoload 'mc-pgp-always-sign "mailcrypt")
150 (autoload 'mc-encrypt-generic "mc-toplev")
151 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
152 (autoload 'mc-sign-generic "mc-toplev"))
155 (defvar mc-default-scheme)
158 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
159 (defvar mml2015-verify-function 'mailcrypt-verify)
161 (defun mml2015-format-error (err)
162 (if (stringp (cadr err))
164 (format "%S" (cdr err))))
166 (defun mml2015-mailcrypt-decrypt (handle ctl)
168 (let (child handles result)
169 (unless (setq child (mm-find-part-by-type
171 "application/octet-stream" nil t))
172 (mm-set-handle-multipart-parameter
173 mm-security-handle 'gnus-info "Corrupted")
174 (throw 'error handle))
176 (mm-insert-part child)
179 (funcall mml2015-decrypt-function)
181 (mm-set-handle-multipart-parameter
182 mm-security-handle 'gnus-details (mml2015-format-error err))
185 (mm-set-handle-multipart-parameter
186 mm-security-handle 'gnus-details "Quit.")
189 (mm-set-handle-multipart-parameter
190 mm-security-handle 'gnus-info "Failed")
191 (throw 'error handle))
192 (setq handles (mm-dissect-buffer t)))
193 (mm-destroy-parts handle)
194 (mm-set-handle-multipart-parameter
195 mm-security-handle 'gnus-info
197 (let ((sig (with-current-buffer mml2015-result-buffer
198 (mml2015-gpg-extract-signature-details))))
199 (concat ", Signer: " sig))))
200 (if (listp (car handles))
204 (defun mml2015-mailcrypt-clear-decrypt ()
208 (funcall mml2015-decrypt-function)
210 (mm-set-handle-multipart-parameter
211 mm-security-handle 'gnus-details (mml2015-format-error err))
214 (mm-set-handle-multipart-parameter
215 mm-security-handle 'gnus-details "Quit.")
218 (mm-set-handle-multipart-parameter
219 mm-security-handle 'gnus-info "OK")
220 (mm-set-handle-multipart-parameter
221 mm-security-handle 'gnus-info "Failed"))))
223 (defun mml2015-fix-micalg (alg)
225 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
226 (upcase (if (string-match "^p[gh]p-" alg)
227 (substring alg (match-end 0))
230 (defun mml2015-mailcrypt-verify (handle ctl)
233 (unless (setq part (mm-find-raw-part-by-type
234 ctl (or (mm-handle-multipart-ctl-parameter
236 "application/pgp-signature")
238 (mm-set-handle-multipart-parameter
239 mm-security-handle 'gnus-info "Corrupted")
240 (throw 'error handle))
242 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
243 (insert (format "Hash: %s\n\n"
244 (or (mml2015-fix-micalg
245 (mm-handle-multipart-ctl-parameter
249 (narrow-to-region (point) (point))
251 (goto-char (point-min))
253 (if (looking-at "^-")
256 (unless (setq part (mm-find-part-by-type
257 (cdr handle) "application/pgp-signature" nil t))
258 (mm-set-handle-multipart-parameter
259 mm-security-handle 'gnus-info "Corrupted")
260 (throw 'error handle))
262 (narrow-to-region (point) (point))
263 (mm-insert-part part)
264 (goto-char (point-min))
265 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
266 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
267 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
268 (replace-match "-----END PGP SIGNATURE-----" t t)))
269 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
270 (unless (condition-case err
272 (funcall mml2015-verify-function)
273 (if (get-buffer " *mailcrypt stderr temp")
274 (mm-set-handle-multipart-parameter
275 mm-security-handle 'gnus-details
276 (with-current-buffer " *mailcrypt stderr temp"
278 (if (get-buffer " *mailcrypt stdout temp")
279 (kill-buffer " *mailcrypt stdout temp"))
280 (if (get-buffer " *mailcrypt stderr temp")
281 (kill-buffer " *mailcrypt stderr temp"))
282 (if (get-buffer " *mailcrypt status temp")
283 (kill-buffer " *mailcrypt status temp"))
284 (if (get-buffer mc-gpg-debug-buffer)
285 (kill-buffer mc-gpg-debug-buffer)))
287 (mm-set-handle-multipart-parameter
288 mm-security-handle 'gnus-details (mml2015-format-error err))
291 (mm-set-handle-multipart-parameter
292 mm-security-handle 'gnus-details "Quit.")
294 (mm-set-handle-multipart-parameter
295 mm-security-handle 'gnus-info "Failed")
296 (throw 'error handle))))
297 (mm-set-handle-multipart-parameter
298 mm-security-handle 'gnus-info "OK")
301 (defun mml2015-mailcrypt-clear-verify ()
302 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
303 (if (condition-case err
305 (funcall mml2015-verify-function)
306 (if (get-buffer " *mailcrypt stderr temp")
307 (mm-set-handle-multipart-parameter
308 mm-security-handle 'gnus-details
309 (with-current-buffer " *mailcrypt stderr temp"
311 (if (get-buffer " *mailcrypt stdout temp")
312 (kill-buffer " *mailcrypt stdout temp"))
313 (if (get-buffer " *mailcrypt stderr temp")
314 (kill-buffer " *mailcrypt stderr temp"))
315 (if (get-buffer " *mailcrypt status temp")
316 (kill-buffer " *mailcrypt status temp"))
317 (if (get-buffer mc-gpg-debug-buffer)
318 (kill-buffer mc-gpg-debug-buffer)))
320 (mm-set-handle-multipart-parameter
321 mm-security-handle 'gnus-details (mml2015-format-error err))
324 (mm-set-handle-multipart-parameter
325 mm-security-handle 'gnus-details "Quit.")
327 (mm-set-handle-multipart-parameter
328 mm-security-handle 'gnus-info "OK")
329 (mm-set-handle-multipart-parameter
330 mm-security-handle 'gnus-info "Failed"))))
332 (defun mml2015-mailcrypt-sign (cont)
333 (mc-sign-generic (message-options-get 'message-sender)
335 (let ((boundary (mml-compute-boundary cont))
337 (goto-char (point-min))
338 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
339 (error "Cannot find signed begin line"))
340 (goto-char (match-beginning 0))
342 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
343 (error "Cannot not find PGP hash"))
344 (setq hash (match-string 1))
345 (unless (re-search-forward "^$" nil t)
346 (error "Cannot not find PGP message"))
348 (delete-region (point-min) (point))
349 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
351 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
353 (insert (format "\n--%s\n" boundary))
355 (goto-char (point-max))
356 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
357 (error "Cannot find signature part"))
358 (replace-match "-----END PGP MESSAGE-----" t t)
359 (goto-char (match-beginning 0))
360 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
362 (error "Cannot find signature part"))
363 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
364 (goto-char (match-beginning 0))
366 (narrow-to-region point (point))
368 (while (re-search-forward "^- -" nil t)
369 (replace-match "-" t t))
370 (goto-char (point-max)))
371 (insert (format "--%s\n" boundary))
372 (insert "Content-Type: application/pgp-signature\n\n")
373 (goto-char (point-max))
374 (insert (format "--%s--\n" boundary))
375 (goto-char (point-max))))
377 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
378 (let ((mc-pgp-always-sign
379 (or mc-pgp-always-sign
381 (eq t (or (message-options-get 'message-sign-encrypt)
383 'message-sign-encrypt
384 (or (y-or-n-p "Sign the message? ")
387 (mm-with-unibyte-current-buffer
389 (or (message-options-get 'message-recipients)
390 (message-options-set 'message-recipients
391 (mc-cleanup-recipient-headers
392 (read-string "Recipients: "))))
394 (message-options-get 'message-sender))))
395 (goto-char (point-min))
396 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
397 (error "Fail to encrypt the message"))
398 (let ((boundary (mml-compute-boundary cont)))
399 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
401 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
402 (insert (format "--%s\n" boundary))
403 (insert "Content-Type: application/pgp-encrypted\n\n")
404 (insert "Version: 1\n\n")
405 (insert (format "--%s\n" boundary))
406 (insert "Content-Type: application/octet-stream\n\n")
407 (goto-char (point-max))
408 (insert (format "--%s--\n" boundary))
409 (goto-char (point-max))))
414 (autoload 'gpg-decrypt "gpg")
415 (autoload 'gpg-verify "gpg")
416 (autoload 'gpg-verify-cleartext "gpg")
417 (autoload 'gpg-sign-detached "gpg")
418 (autoload 'gpg-sign-encrypt "gpg")
419 (autoload 'gpg-encrypt "gpg")
420 (autoload 'gpg-passphrase-read "gpg"))
422 (defun mml2015-gpg-passphrase ()
423 (or (message-options-get 'gpg-passphrase)
424 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
426 (defun mml2015-gpg-decrypt-1 ()
427 (let ((cipher (current-buffer)) plain result)
428 (if (with-temp-buffer
430 (gpg-decrypt cipher (setq plain (current-buffer))
431 mml2015-result-buffer nil)
432 (mm-set-handle-multipart-parameter
433 mm-security-handle 'gnus-details
434 (with-current-buffer mml2015-result-buffer
438 (insert-buffer-substring plain)
439 (goto-char (point-min))
440 (while (search-forward "\r\n" nil t)
441 (replace-match "\n" t t))))
443 ;; Some wrong with the return value, check plain text buffer.
444 (if (> (point-max) (point-min))
448 (defun mml2015-gpg-decrypt (handle ctl)
449 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
450 (mml2015-mailcrypt-decrypt handle ctl)))
452 (defun mml2015-gpg-clear-decrypt ()
454 (setq result (mml2015-gpg-decrypt-1))
456 (mm-set-handle-multipart-parameter
457 mm-security-handle 'gnus-info "OK")
458 (mm-set-handle-multipart-parameter
459 mm-security-handle 'gnus-info "Failed"))))
461 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
463 (fpr-length (string-width fingerprint))
466 (setq fingerprint (string-to-list fingerprint))
468 (setq fpr-length (- fpr-length 4))
469 (setq slice (butlast fingerprint fpr-length))
470 (setq fingerprint (nthcdr 4 fingerprint))
471 (setq n-slice (1+ n-slice))
477 (otherwise (concat " " slice))))))
480 (defun mml2015-gpg-extract-signature-details ()
481 (goto-char (point-min))
482 (let* ((expired (re-search-forward
483 "^\\[GNUPG:\\] SIGEXPIRED$"
485 (signer (and (re-search-forward
486 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
488 (cons (match-string 1) (match-string 2))))
489 (fprint (and (re-search-forward
490 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
493 (trust (and (re-search-forward
494 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
498 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
499 (cond ((and signer fprint)
501 (unless trust-good-enough-p
502 (concat "\nUntrusted, Fingerprint: "
503 (mml2015-gpg-pretty-print-fpr fprint)))
505 (format "\nWARNING: Signature from expired key (%s)"
508 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
511 "From unknown user"))))
513 (defun mml2015-gpg-verify (handle ctl)
515 (let (part message signature info-is-set-p)
516 (unless (setq part (mm-find-raw-part-by-type
517 ctl (or (mm-handle-multipart-ctl-parameter
519 "application/pgp-signature")
521 (mm-set-handle-multipart-parameter
522 mm-security-handle 'gnus-info "Corrupted")
523 (throw 'error handle))
525 (setq message (current-buffer))
527 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
528 ;; clearsign use --textmode. The conversion is not necessary.
529 ;; In clearverify, the conversion is not necessary either.
530 (goto-char (point-min))
533 (unless (eq (char-before) ?\r)
538 (setq signature (current-buffer))
539 (unless (setq part (mm-find-part-by-type
540 (cdr handle) "application/pgp-signature" nil t))
541 (mm-set-handle-multipart-parameter
542 mm-security-handle 'gnus-info "Corrupted")
543 (throw 'error handle))
544 (mm-insert-part part)
545 (unless (condition-case err
547 (gpg-verify message signature mml2015-result-buffer)
548 (mm-set-handle-multipart-parameter
549 mm-security-handle 'gnus-details
550 (with-current-buffer mml2015-result-buffer
553 (mm-set-handle-multipart-parameter
554 mm-security-handle 'gnus-details (mml2015-format-error err))
555 (mm-set-handle-multipart-parameter
556 mm-security-handle 'gnus-info "Error.")
557 (setq info-is-set-p t)
560 (mm-set-handle-multipart-parameter
561 mm-security-handle 'gnus-details "Quit.")
562 (mm-set-handle-multipart-parameter
563 mm-security-handle 'gnus-info "Quit.")
564 (setq info-is-set-p t)
566 (unless info-is-set-p
567 (mm-set-handle-multipart-parameter
568 mm-security-handle 'gnus-info "Failed"))
569 (throw 'error handle)))
570 (mm-set-handle-multipart-parameter
571 mm-security-handle 'gnus-info
572 (with-current-buffer mml2015-result-buffer
573 (mml2015-gpg-extract-signature-details))))
576 (defun mml2015-gpg-clear-verify ()
577 (if (condition-case err
579 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
580 (mm-set-handle-multipart-parameter
581 mm-security-handle 'gnus-details
582 (with-current-buffer mml2015-result-buffer
585 (mm-set-handle-multipart-parameter
586 mm-security-handle 'gnus-details (mml2015-format-error err))
589 (mm-set-handle-multipart-parameter
590 mm-security-handle 'gnus-details "Quit.")
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)))
596 (mm-set-handle-multipart-parameter
597 mm-security-handle 'gnus-info "Failed")))
599 (defun mml2015-gpg-sign (cont)
600 (let ((boundary (mml-compute-boundary cont))
601 (text (current-buffer)) signature)
602 (goto-char (point-max))
606 (unless (gpg-sign-detached text (setq signature (current-buffer))
607 mml2015-result-buffer
609 (message-options-get 'message-sender)
610 t t) ; armor & textmode
611 (unless (> (point-max) (point-min))
612 (pop-to-buffer mml2015-result-buffer)
613 (error "Sign error")))
614 (goto-char (point-min))
615 (while (re-search-forward "\r+$" nil t)
616 (replace-match "" t t))
618 (goto-char (point-min))
619 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
621 ;;; FIXME: what is the micalg?
622 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
623 (insert (format "\n--%s\n" boundary))
624 (goto-char (point-max))
625 (insert (format "\n--%s\n" boundary))
626 (insert "Content-Type: application/pgp-signature\n\n")
627 (insert-buffer-substring signature)
628 (goto-char (point-max))
629 (insert (format "--%s--\n" boundary))
630 (goto-char (point-max)))))
632 (defun mml2015-gpg-encrypt (cont &optional sign)
633 (let ((boundary (mml-compute-boundary cont))
634 (text (current-buffer))
636 (mm-with-unibyte-current-buffer
638 ;; set up a function to call the correct gpg encrypt routine
639 ;; with the right arguments. (FIXME: this should be done
641 (flet ((gpg-encrypt-func
642 (sign plaintext ciphertext result recipients &optional
643 passphrase sign-with-key armor textmode)
646 plaintext ciphertext result recipients passphrase
647 sign-with-key armor textmode)
649 plaintext ciphertext result recipients passphrase
651 (unless (gpg-encrypt-func
652 sign ; passed in when using signencrypt
653 text (setq cipher (current-buffer))
654 mml2015-result-buffer
657 (message-options-get 'message-recipients)
658 (message-options-set 'message-recipients
659 (read-string "Recipients: ")))
662 (message-options-get 'message-sender)
663 t t) ; armor & textmode
664 (unless (> (point-max) (point-min))
665 (pop-to-buffer mml2015-result-buffer)
666 (error "Encrypt error"))))
667 (goto-char (point-min))
668 (while (re-search-forward "\r+$" nil t)
669 (replace-match "" t t))
671 (delete-region (point-min) (point-max))
672 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
674 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
675 (insert (format "--%s\n" boundary))
676 (insert "Content-Type: application/pgp-encrypted\n\n")
677 (insert "Version: 1\n\n")
678 (insert (format "--%s\n" boundary))
679 (insert "Content-Type: application/octet-stream\n\n")
680 (insert-buffer-substring cipher)
681 (goto-char (point-max))
682 (insert (format "--%s--\n" boundary))
683 (goto-char (point-max))))))
688 (defvar pgg-default-user-id)
689 (defvar pgg-errors-buffer)
690 (defvar pgg-output-buffer))
693 (autoload 'pgg-decrypt-region "pgg")
694 (autoload 'pgg-verify-region "pgg")
695 (autoload 'pgg-sign-region "pgg")
696 (autoload 'pgg-encrypt-region "pgg")
697 (autoload 'pgg-parse-armor "pgg-parse"))
699 (defun mml2015-pgg-decrypt (handle ctl)
701 (let ((pgg-errors-buffer mml2015-result-buffer)
702 child handles result decrypt-status)
703 (unless (setq child (mm-find-part-by-type
705 "application/octet-stream" nil t))
706 (mm-set-handle-multipart-parameter
707 mm-security-handle 'gnus-info "Corrupted")
708 (throw 'error handle))
710 (mm-insert-part child)
711 (if (condition-case err
713 (pgg-decrypt-region (point-min) (point-max))
715 (with-current-buffer mml2015-result-buffer
717 (mm-set-handle-multipart-parameter
718 mm-security-handle 'gnus-details
721 (mm-set-handle-multipart-parameter
722 mm-security-handle 'gnus-details (mml2015-format-error err))
725 (mm-set-handle-multipart-parameter
726 mm-security-handle 'gnus-details "Quit.")
728 (with-current-buffer pgg-output-buffer
729 (goto-char (point-min))
730 (while (search-forward "\r\n" nil t)
731 (replace-match "\n" t t))
732 (setq handles (mm-dissect-buffer t))
733 (mm-destroy-parts handle)
734 (mm-set-handle-multipart-parameter
735 mm-security-handle 'gnus-info "OK")
736 (mm-set-handle-multipart-parameter
737 mm-security-handle 'gnus-details
738 (concat decrypt-status
739 (when (stringp (car handles))
740 "\n" (mm-handle-multipart-ctl-parameter
741 handles 'gnus-details))))
742 (if (listp (car handles))
745 (mm-set-handle-multipart-parameter
746 mm-security-handle 'gnus-info "Failed")
747 (throw 'error handle))))))
749 (defun mml2015-pgg-clear-decrypt ()
750 (let ((pgg-errors-buffer mml2015-result-buffer))
752 (pgg-decrypt-region (point-min) (point-max))
753 (mm-set-handle-multipart-parameter
754 mm-security-handle 'gnus-details
755 (with-current-buffer mml2015-result-buffer
759 ;; Treat data which pgg returns as a unibyte string.
760 (mm-disable-multibyte)
761 (insert-buffer-substring pgg-output-buffer)
762 (goto-char (point-min))
763 (while (search-forward "\r\n" nil t)
764 (replace-match "\n" t t))
765 (mm-set-handle-multipart-parameter
766 mm-security-handle 'gnus-info "OK"))
767 (mm-set-handle-multipart-parameter
768 mm-security-handle 'gnus-info "Failed"))))
770 (defun mml2015-pgg-verify (handle ctl)
771 (let ((pgg-errors-buffer mml2015-result-buffer)
772 signature-file part signature)
773 (if (or (null (setq part (mm-find-raw-part-by-type
774 ctl (or (mm-handle-multipart-ctl-parameter
776 "application/pgp-signature")
778 (null (setq signature (mm-find-part-by-type
779 (cdr handle) "application/pgp-signature" nil t))))
781 (mm-set-handle-multipart-parameter
782 mm-security-handle 'gnus-info "Corrupted")
786 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
787 ;; clearsign use --textmode. The conversion is not necessary.
788 ;; In clearverify, the conversion is not necessary either.
789 (goto-char (point-min))
792 (unless (eq (char-before) ?\r)
796 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
797 (mm-insert-part signature))
798 (if (condition-case err
800 (pgg-verify-region (point-min) (point-max)
802 (goto-char (point-min))
803 (while (search-forward "\r\n" nil t)
804 (replace-match "\n" t t))
805 (mm-set-handle-multipart-parameter
806 mm-security-handle 'gnus-details
807 (concat (with-current-buffer pgg-output-buffer
809 (with-current-buffer pgg-errors-buffer
812 (mm-set-handle-multipart-parameter
813 mm-security-handle 'gnus-details (mml2015-format-error err))
816 (mm-set-handle-multipart-parameter
817 mm-security-handle 'gnus-details "Quit.")
820 (delete-file signature-file)
821 (mm-set-handle-multipart-parameter
822 mm-security-handle 'gnus-info
823 (with-current-buffer pgg-errors-buffer
824 (mml2015-gpg-extract-signature-details))))
825 (delete-file signature-file)
826 (mm-set-handle-multipart-parameter
827 mm-security-handle 'gnus-info "Failed")))))
830 (defun mml2015-pgg-clear-verify ()
831 (let ((pgg-errors-buffer mml2015-result-buffer)
832 (text (buffer-string))
833 (coding-system buffer-file-coding-system))
834 (if (condition-case err
836 (mm-with-unibyte-buffer
837 (insert (encode-coding-string text coding-system))
838 (pgg-verify-region (point-min) (point-max) nil t))
839 (goto-char (point-min))
840 (while (search-forward "\r\n" nil t)
841 (replace-match "\n" t t))
842 (mm-set-handle-multipart-parameter
843 mm-security-handle 'gnus-details
844 (concat (with-current-buffer pgg-output-buffer
846 (with-current-buffer pgg-errors-buffer
849 (mm-set-handle-multipart-parameter
850 mm-security-handle 'gnus-details (mml2015-format-error err))
853 (mm-set-handle-multipart-parameter
854 mm-security-handle 'gnus-details "Quit.")
856 (mm-set-handle-multipart-parameter
857 mm-security-handle 'gnus-info
858 (with-current-buffer pgg-errors-buffer
859 (mml2015-gpg-extract-signature-details)))
860 (mm-set-handle-multipart-parameter
861 mm-security-handle 'gnus-info "Failed"))))
863 (defun mml2015-pgg-sign (cont)
864 (let ((pgg-errors-buffer mml2015-result-buffer)
865 (boundary (mml-compute-boundary cont))
866 (pgg-default-user-id (or (message-options-get 'mml-sender)
867 pgg-default-user-id))
870 (unless (pgg-sign-region (point-min) (point-max))
871 (pop-to-buffer mml2015-result-buffer)
872 (error "Sign error"))
873 (goto-char (point-min))
874 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
876 (if (setq entry (assq 2 (pgg-parse-armor
877 (with-current-buffer pgg-output-buffer
879 (setq entry (assq 'hash-algorithm (cdr entry))))
880 (insert (format "\tmicalg=%s; "
882 (downcase (format "pgp-%s" (cdr entry)))
884 (insert "protocol=\"application/pgp-signature\"\n")
885 (insert (format "\n--%s\n" boundary))
886 (goto-char (point-max))
887 (insert (format "\n--%s\n" boundary))
888 (insert "Content-Type: application/pgp-signature\n\n")
889 (insert-buffer-substring pgg-output-buffer)
890 (goto-char (point-max))
891 (insert (format "--%s--\n" boundary))
892 (goto-char (point-max))))
894 (defun mml2015-pgg-encrypt (cont &optional sign)
895 (let ((pgg-errors-buffer mml2015-result-buffer)
897 (boundary (mml-compute-boundary cont)))
898 (unless (pgg-encrypt-region (point-min) (point-max)
901 (message-options-get 'message-recipients)
902 (message-options-set 'message-recipients
903 (read-string "Recipients: ")))
906 (pop-to-buffer mml2015-result-buffer)
907 (error "Encrypt error"))
908 (delete-region (point-min) (point-max))
909 (goto-char (point-min))
910 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
912 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
913 (insert (format "--%s\n" boundary))
914 (insert "Content-Type: application/pgp-encrypted\n\n")
915 (insert "Version: 1\n\n")
916 (insert (format "--%s\n" boundary))
917 (insert "Content-Type: application/octet-stream\n\n")
918 (insert-buffer-substring pgg-output-buffer)
919 (goto-char (point-max))
920 (insert (format "--%s--\n" boundary))
921 (goto-char (point-max))))
926 (autoload 'epg-make-context "epg"))
929 (defvar epg-user-id-alist)
930 (defvar epg-digest-algorithm-alist)
931 (defvar inhibit-redisplay)
932 (autoload 'epg-context-set-armor "epg")
933 (autoload 'epg-context-set-textmode "epg")
934 (autoload 'epg-context-set-signers "epg")
935 (autoload 'epg-context-result-for "epg")
936 (autoload 'epg-new-signature-digest-algorithm "epg")
937 (autoload 'epg-verify-result-to-string "epg")
938 (autoload 'epg-list-keys "epg")
939 (autoload 'epg-decrypt-string "epg")
940 (autoload 'epg-verify-string "epg")
941 (autoload 'epg-sign-string "epg")
942 (autoload 'epg-encrypt-string "epg")
943 (autoload 'epg-passphrase-callback-function "epg")
944 (autoload 'epg-context-set-passphrase-callback "epg")
945 (autoload 'epg-configuration "epg-config")
946 (autoload 'epg-expand-group "epg-config"))
949 (defvar password-cache-expiry)
950 (autoload 'password-read "password")
951 (autoload 'password-cache-add "password")
952 (autoload 'password-cache-remove "password"))
954 (defvar mml2015-epg-secret-key-id-list nil)
956 (defun mml2015-epg-passphrase-callback (context key-id ignore)
958 (epg-passphrase-callback-function context key-id nil)
963 "Passphrase for PIN: "
964 (if (setq entry (assoc key-id epg-user-id-alist))
965 (format "Passphrase for %s %s: " key-id (cdr entry))
966 (format "Passphrase for %s: " key-id)))
971 (let ((password-cache-expiry mml2015-passphrase-cache-expiry))
972 (password-cache-add key-id passphrase))
973 (setq mml2015-epg-secret-key-id-list
974 (cons key-id mml2015-epg-secret-key-id-list))
975 (copy-sequence passphrase)))))
977 (defun mml2015-epg-decrypt (handle ctl)
979 (let ((inhibit-redisplay t)
980 context plain child handles result decrypt-status)
981 (unless (setq child (mm-find-part-by-type
983 "application/octet-stream" nil t))
984 (mm-set-handle-multipart-parameter
985 mm-security-handle 'gnus-info "Corrupted")
986 (throw 'error handle))
987 (setq context (epg-make-context))
988 (if mml2015-cache-passphrase
989 (epg-context-set-passphrase-callback
991 #'mml2015-epg-passphrase-callback))
992 (condition-case error
993 (setq plain (epg-decrypt-string context (mm-get-part child))
994 mml2015-epg-secret-key-id-list nil)
996 (while mml2015-epg-secret-key-id-list
997 (password-cache-remove (car mml2015-epg-secret-key-id-list))
998 (setq mml2015-epg-secret-key-id-list
999 (cdr mml2015-epg-secret-key-id-list)))
1000 (mm-set-handle-multipart-parameter
1001 mm-security-handle 'gnus-info "Failed")
1002 (if (eq (car error) 'quit)
1003 (mm-set-handle-multipart-parameter
1004 mm-security-handle 'gnus-details "Quit.")
1005 (mm-set-handle-multipart-parameter
1006 mm-security-handle 'gnus-details (mml2015-format-error error)))
1007 (throw 'error handle)))
1010 (goto-char (point-min))
1011 (while (search-forward "\r\n" nil t)
1012 (replace-match "\n" t t))
1013 (setq handles (mm-dissect-buffer t))
1014 (mm-destroy-parts handle)
1015 (if (epg-context-result-for context 'verify)
1016 (mm-set-handle-multipart-parameter
1017 mm-security-handle 'gnus-info
1019 (epg-verify-result-to-string
1020 (epg-context-result-for context 'verify))))
1021 (mm-set-handle-multipart-parameter
1022 mm-security-handle 'gnus-info "OK"))
1023 (if (stringp (car handles))
1024 (mm-set-handle-multipart-parameter
1025 mm-security-handle 'gnus-details
1026 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
1027 (if (listp (car handles))
1031 (defun mml2015-epg-clear-decrypt ()
1032 (let ((inhibit-redisplay t)
1033 (context (epg-make-context))
1035 (if mml2015-cache-passphrase
1036 (epg-context-set-passphrase-callback
1038 #'mml2015-epg-passphrase-callback))
1039 (condition-case error
1040 (setq plain (epg-decrypt-string context (buffer-string))
1041 mml2015-epg-secret-key-id-list nil)
1043 (while mml2015-epg-secret-key-id-list
1044 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1045 (setq mml2015-epg-secret-key-id-list
1046 (cdr mml2015-epg-secret-key-id-list)))
1047 (mm-set-handle-multipart-parameter
1048 mm-security-handle 'gnus-info "Failed")
1049 (if (eq (car error) 'quit)
1050 (mm-set-handle-multipart-parameter
1051 mm-security-handle 'gnus-details "Quit.")
1052 (mm-set-handle-multipart-parameter
1053 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1056 ;; Treat data which epg returns as a unibyte string.
1057 (mm-disable-multibyte)
1059 (goto-char (point-min))
1060 (while (search-forward "\r\n" nil t)
1061 (replace-match "\n" t t))
1062 (mm-set-handle-multipart-parameter
1063 mm-security-handle 'gnus-info "OK")
1064 (if (epg-context-result-for context 'verify)
1065 (mm-set-handle-multipart-parameter
1066 mm-security-handle 'gnus-details
1067 (epg-verify-result-to-string
1068 (epg-context-result-for context 'verify)))))))
1070 (defun mml2015-epg-verify (handle ctl)
1072 (let ((inhibit-redisplay t)
1073 context plain signature-file part signature)
1074 (when (or (null (setq part (mm-find-raw-part-by-type
1075 ctl (or (mm-handle-multipart-ctl-parameter
1077 "application/pgp-signature")
1079 (null (setq signature (mm-find-part-by-type
1080 (cdr handle) "application/pgp-signature"
1082 (mm-set-handle-multipart-parameter
1083 mm-security-handle 'gnus-info "Corrupted")
1084 (throw 'error handle))
1085 (setq context (epg-make-context))
1086 (condition-case error
1087 (setq plain (epg-verify-string context (mm-get-part signature) part))
1089 (mm-set-handle-multipart-parameter
1090 mm-security-handle 'gnus-info "Failed")
1091 (if (eq (car error) 'quit)
1092 (mm-set-handle-multipart-parameter
1093 mm-security-handle 'gnus-details "Quit.")
1094 (mm-set-handle-multipart-parameter
1095 mm-security-handle 'gnus-details (mml2015-format-error error)))
1096 (throw 'error handle)))
1097 (mm-set-handle-multipart-parameter
1098 mm-security-handle 'gnus-info
1099 (epg-verify-result-to-string (epg-context-result-for context 'verify)))
1102 (defun mml2015-epg-clear-verify ()
1103 (let ((inhibit-redisplay t)
1104 (context (epg-make-context))
1105 (signature (encode-coding-string (buffer-string)
1106 buffer-file-coding-system))
1108 (condition-case error
1109 (setq plain (epg-verify-string context signature))
1111 (mm-set-handle-multipart-parameter
1112 mm-security-handle 'gnus-info "Failed")
1113 (if (eq (car error) 'quit)
1114 (mm-set-handle-multipart-parameter
1115 mm-security-handle 'gnus-details "Quit.")
1116 (mm-set-handle-multipart-parameter
1117 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1119 (mm-set-handle-multipart-parameter
1120 mm-security-handle 'gnus-info
1121 (epg-verify-result-to-string
1122 (epg-context-result-for context 'verify))))))
1124 (defun mml2015-epg-sign (cont)
1125 (let* ((inhibit-redisplay t)
1126 (context (epg-make-context))
1127 (boundary (mml-compute-boundary cont))
1129 (or (message-options-get 'mml2015-epg-signers)
1130 (message-options-set
1131 'mml2015-epg-signers
1133 (epa-select-keys context "\
1134 Select keys for signing.
1135 If no one is selected, default secret key is used. "
1138 (mapcar (lambda (name)
1139 (car (epg-list-keys context name t)))
1140 mml2015-signers))))))
1142 (epg-context-set-armor context t)
1143 (epg-context-set-textmode context t)
1144 (epg-context-set-signers context signers)
1145 (if mml2015-cache-passphrase
1146 (epg-context-set-passphrase-callback
1148 #'mml2015-epg-passphrase-callback))
1149 (condition-case error
1150 (setq signature (epg-sign-string context (buffer-string) t)
1151 mml2015-epg-secret-key-id-list nil)
1153 (while mml2015-epg-secret-key-id-list
1154 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1155 (setq mml2015-epg-secret-key-id-list
1156 (cdr mml2015-epg-secret-key-id-list)))
1157 (signal (car error) (cdr error))))
1158 (if (epg-context-result-for context 'sign)
1159 (setq micalg (epg-new-signature-digest-algorithm
1160 (car (epg-context-result-for context 'sign)))))
1161 (goto-char (point-min))
1162 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
1165 (insert (format "\tmicalg=%s; "
1168 epg-digest-algorithm-alist))))))
1169 (insert "protocol=\"application/pgp-signature\"\n")
1170 (insert (format "\n--%s\n" boundary))
1171 (goto-char (point-max))
1172 (insert (format "\n--%s\n" boundary))
1173 (insert "Content-Type: application/pgp-signature\n\n")
1175 (goto-char (point-max))
1176 (insert (format "--%s--\n" boundary))
1177 (goto-char (point-max))))
1179 (defun mml2015-epg-encrypt (cont &optional sign)
1180 (let ((inhibit-redisplay t)
1181 (context (epg-make-context))
1182 (config (epg-configuration))
1183 (recipients (message-options-get 'mml2015-epg-recipients))
1185 (boundary (mml-compute-boundary cont)))
1191 (or (epg-expand-group config recipient)
1194 (or (message-options-get 'message-recipients)
1195 (message-options-set 'message-recipients
1196 (read-string "Recipients: ")))
1197 "[ \f\t\n\r\v,]+"))))
1200 (epa-select-keys context "\
1201 Select recipients for encryption.
1202 If no one is selected, symmetric encryption will be performed. "
1205 (delq nil (mapcar (lambda (name)
1206 (car (epg-list-keys context name)))
1208 (if mml2015-encrypt-to-self
1212 (mapcar (lambda (name)
1213 (car (epg-list-keys context name)))
1215 (error "mml2015-signers not set")))
1216 (message-options-set 'message-recipients recipients))
1219 (or (message-options-get 'mml2015-epg-signers)
1220 (message-options-set
1221 'mml2015-epg-signers
1223 (epa-select-keys context "\
1224 Select keys for signing.
1225 If no one is selected, default secret key is used. "
1228 (mapcar (lambda (name)
1229 (car (epg-list-keys context name t)))
1230 mml2015-signers))))))
1231 (epg-context-set-signers context signers))
1232 (epg-context-set-armor context t)
1233 (epg-context-set-textmode context t)
1234 (if mml2015-cache-passphrase
1235 (epg-context-set-passphrase-callback
1237 #'mml2015-epg-passphrase-callback))
1238 (condition-case error
1240 (epg-encrypt-string context (buffer-string) recipients sign
1241 mml2015-always-trust)
1242 mml2015-epg-secret-key-id-list nil)
1244 (while mml2015-epg-secret-key-id-list
1245 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1246 (setq mml2015-epg-secret-key-id-list
1247 (cdr mml2015-epg-secret-key-id-list)))
1248 (signal (car error) (cdr error))))
1249 (delete-region (point-min) (point-max))
1250 (goto-char (point-min))
1251 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1253 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1254 (insert (format "--%s\n" boundary))
1255 (insert "Content-Type: application/pgp-encrypted\n\n")
1256 (insert "Version: 1\n\n")
1257 (insert (format "--%s\n" boundary))
1258 (insert "Content-Type: application/octet-stream\n\n")
1260 (goto-char (point-max))
1261 (insert (format "--%s--\n" boundary))
1262 (goto-char (point-max))))
1266 (defun mml2015-clean-buffer ()
1267 (if (gnus-buffer-live-p mml2015-result-buffer)
1268 (with-current-buffer mml2015-result-buffer
1271 (setq mml2015-result-buffer
1272 (gnus-get-buffer-create " *MML2015 Result*"))
1275 (defsubst mml2015-clear-decrypt-function ()
1276 (nth 6 (assq mml2015-use mml2015-function-alist)))
1278 (defsubst mml2015-clear-verify-function ()
1279 (nth 5 (assq mml2015-use mml2015-function-alist)))
1282 (defun mml2015-decrypt (handle ctl)
1283 (mml2015-clean-buffer)
1284 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1286 (funcall func handle ctl)
1290 (defun mml2015-decrypt-test (handle ctl)
1294 (defun mml2015-verify (handle ctl)
1295 (mml2015-clean-buffer)
1296 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1298 (funcall func handle ctl)
1302 (defun mml2015-verify-test (handle ctl)
1306 (defun mml2015-encrypt (cont &optional sign)
1307 (mml2015-clean-buffer)
1308 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1310 (funcall func cont sign)
1311 (error "Cannot find encrypt function"))))
1314 (defun mml2015-sign (cont)
1315 (mml2015-clean-buffer)
1316 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1319 (error "Cannot find sign function"))))
1322 (defun mml2015-self-encrypt ()
1323 (mml2015-encrypt nil))
1327 ;;; arch-tag: b04701d5-0b09-44d8-bed8-de901bf435f2
1328 ;;; mml2015.el ends here