1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: PGP MIME MML
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
33 (eval-when-compile (require 'cl))
39 (defvar mc-pgp-always-sign)
41 (defvar mml2015-use (or
45 (epg-check-configuration (epg-configuration))
50 ;; Avoid the "Recursive load suspected" error
52 (let ((recursive-load-depth-limit 100))
54 (and (fboundp 'pgg-sign-region)
59 (and (fboundp 'gpg-sign-detached)
63 (and (fboundp 'mc-encrypt-generic)
64 (fboundp 'mc-sign-generic)
65 (fboundp 'mc-cleanup-recipient-headers)
67 "The package used for PGP/MIME.
68 Valid packages include `epg', `pgg', `gpg' and `mailcrypt'.")
70 ;; Something is not RFC2015.
71 (defvar mml2015-function-alist
72 '((mailcrypt mml2015-mailcrypt-sign
73 mml2015-mailcrypt-encrypt
74 mml2015-mailcrypt-verify
75 mml2015-mailcrypt-decrypt
76 mml2015-mailcrypt-clear-verify
77 mml2015-mailcrypt-clear-decrypt)
82 mml2015-gpg-clear-verify
83 mml2015-gpg-clear-decrypt)
88 mml2015-pgg-clear-verify
89 mml2015-pgg-clear-decrypt)
94 mml2015-epg-clear-verify
95 mml2015-epg-clear-decrypt))
96 "Alist of PGP/MIME functions.")
98 (defvar mml2015-result-buffer nil)
100 (defcustom mml2015-unabbrev-trust-alist
101 '(("TRUST_UNDEFINED" . nil)
102 ("TRUST_NEVER" . nil)
103 ("TRUST_MARGINAL" . t)
105 ("TRUST_ULTIMATE" . t))
106 "Map GnuPG trust output values to a boolean saying if you trust the key."
108 :group 'mime-security
109 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
110 (boolean :tag "Trust key"))))
112 (defcustom mml2015-verbose mml-secure-verbose
113 "If non-nil, ask the user about the current operation more verbosely."
114 :group 'mime-security
117 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
118 "If t, cache passphrase."
119 :group 'mime-security
122 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
123 "How many seconds the passphrase is cached.
124 Whether the passphrase is cached at all is controlled by
125 `mml2015-cache-passphrase'."
126 :group 'mime-security
129 (defcustom mml2015-signers nil
130 "A list of your own key ID which will be used to sign a message."
131 :group 'mime-security
132 :type '(repeat (string :tag "Key ID")))
134 (defcustom mml2015-encrypt-to-self nil
135 "If t, add your own key ID to recipient list when encryption."
136 :group 'mime-security
139 (defcustom mml2015-always-trust t
140 "If t, GnuPG skip key validation on encryption."
141 :group 'mime-security
144 ;;; 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 signed text. If --textmode is
528 ;; specified when signing, the conversion is not necessary.
529 (goto-char (point-min))
532 (unless (eq (char-before) ?\r)
537 (setq signature (current-buffer))
538 (unless (setq part (mm-find-part-by-type
539 (cdr handle) "application/pgp-signature" nil t))
540 (mm-set-handle-multipart-parameter
541 mm-security-handle 'gnus-info "Corrupted")
542 (throw 'error handle))
543 (mm-insert-part part)
544 (unless (condition-case err
546 (gpg-verify message signature mml2015-result-buffer)
547 (mm-set-handle-multipart-parameter
548 mm-security-handle 'gnus-details
549 (with-current-buffer mml2015-result-buffer
552 (mm-set-handle-multipart-parameter
553 mm-security-handle 'gnus-details (mml2015-format-error err))
554 (mm-set-handle-multipart-parameter
555 mm-security-handle 'gnus-info "Error.")
556 (setq info-is-set-p t)
559 (mm-set-handle-multipart-parameter
560 mm-security-handle 'gnus-details "Quit.")
561 (mm-set-handle-multipart-parameter
562 mm-security-handle 'gnus-info "Quit.")
563 (setq info-is-set-p t)
565 (unless info-is-set-p
566 (mm-set-handle-multipart-parameter
567 mm-security-handle 'gnus-info "Failed"))
568 (throw 'error handle)))
569 (mm-set-handle-multipart-parameter
570 mm-security-handle 'gnus-info
571 (with-current-buffer mml2015-result-buffer
572 (mml2015-gpg-extract-signature-details))))
575 (defun mml2015-gpg-clear-verify ()
576 (if (condition-case err
578 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
579 (mm-set-handle-multipart-parameter
580 mm-security-handle 'gnus-details
581 (with-current-buffer mml2015-result-buffer
584 (mm-set-handle-multipart-parameter
585 mm-security-handle 'gnus-details (mml2015-format-error err))
588 (mm-set-handle-multipart-parameter
589 mm-security-handle 'gnus-details "Quit.")
591 (mm-set-handle-multipart-parameter
592 mm-security-handle 'gnus-info
593 (with-current-buffer mml2015-result-buffer
594 (mml2015-gpg-extract-signature-details)))
595 (mm-set-handle-multipart-parameter
596 mm-security-handle 'gnus-info "Failed")))
598 (defun mml2015-gpg-sign (cont)
599 (let ((boundary (mml-compute-boundary cont))
600 (text (current-buffer)) signature)
601 (goto-char (point-max))
605 (unless (gpg-sign-detached text (setq signature (current-buffer))
606 mml2015-result-buffer
608 (message-options-get 'message-sender)
609 t t) ; armor & textmode
610 (unless (> (point-max) (point-min))
611 (pop-to-buffer mml2015-result-buffer)
612 (error "Sign error")))
613 (goto-char (point-min))
614 (while (re-search-forward "\r+$" nil t)
615 (replace-match "" t t))
617 (goto-char (point-min))
618 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
620 ;;; FIXME: what is the micalg?
621 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
622 (insert (format "\n--%s\n" boundary))
623 (goto-char (point-max))
624 (insert (format "\n--%s\n" boundary))
625 (insert "Content-Type: application/pgp-signature\n\n")
626 (insert-buffer-substring signature)
627 (goto-char (point-max))
628 (insert (format "--%s--\n" boundary))
629 (goto-char (point-max)))))
631 (defun mml2015-gpg-encrypt (cont &optional sign)
632 (let ((boundary (mml-compute-boundary cont))
633 (text (current-buffer))
635 (mm-with-unibyte-current-buffer
637 ;; set up a function to call the correct gpg encrypt routine
638 ;; with the right arguments. (FIXME: this should be done
640 (flet ((gpg-encrypt-func
641 (sign plaintext ciphertext result recipients &optional
642 passphrase sign-with-key armor textmode)
645 plaintext ciphertext result recipients passphrase
646 sign-with-key armor textmode)
648 plaintext ciphertext result recipients passphrase
650 (unless (gpg-encrypt-func
651 sign ; passed in when using signencrypt
652 text (setq cipher (current-buffer))
653 mml2015-result-buffer
656 (message-options-get 'message-recipients)
657 (message-options-set 'message-recipients
658 (read-string "Recipients: ")))
661 (message-options-get 'message-sender)
662 t t) ; armor & textmode
663 (unless (> (point-max) (point-min))
664 (pop-to-buffer mml2015-result-buffer)
665 (error "Encrypt error"))))
666 (goto-char (point-min))
667 (while (re-search-forward "\r+$" nil t)
668 (replace-match "" t t))
670 (delete-region (point-min) (point-max))
671 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
673 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
674 (insert (format "--%s\n" boundary))
675 (insert "Content-Type: application/pgp-encrypted\n\n")
676 (insert "Version: 1\n\n")
677 (insert (format "--%s\n" boundary))
678 (insert "Content-Type: application/octet-stream\n\n")
679 (insert-buffer-substring cipher)
680 (goto-char (point-max))
681 (insert (format "--%s--\n" boundary))
682 (goto-char (point-max))))))
687 (defvar pgg-default-user-id)
688 (defvar pgg-errors-buffer)
689 (defvar pgg-output-buffer))
692 (autoload 'pgg-decrypt-region "pgg")
693 (autoload 'pgg-verify-region "pgg")
694 (autoload 'pgg-sign-region "pgg")
695 (autoload 'pgg-encrypt-region "pgg")
696 (autoload 'pgg-parse-armor "pgg-parse"))
698 (defun mml2015-pgg-decrypt (handle ctl)
700 (let ((pgg-errors-buffer mml2015-result-buffer)
701 child handles result decrypt-status)
702 (unless (setq child (mm-find-part-by-type
704 "application/octet-stream" nil t))
705 (mm-set-handle-multipart-parameter
706 mm-security-handle 'gnus-info "Corrupted")
707 (throw 'error handle))
709 (mm-insert-part child)
710 (if (condition-case err
712 (pgg-decrypt-region (point-min) (point-max))
714 (with-current-buffer mml2015-result-buffer
716 (mm-set-handle-multipart-parameter
717 mm-security-handle 'gnus-details
720 (mm-set-handle-multipart-parameter
721 mm-security-handle 'gnus-details (mml2015-format-error err))
724 (mm-set-handle-multipart-parameter
725 mm-security-handle 'gnus-details "Quit.")
727 (with-current-buffer pgg-output-buffer
728 (goto-char (point-min))
729 (while (search-forward "\r\n" nil t)
730 (replace-match "\n" t t))
731 (setq handles (mm-dissect-buffer t))
732 (mm-destroy-parts handle)
733 (mm-set-handle-multipart-parameter
734 mm-security-handle 'gnus-info "OK")
735 (mm-set-handle-multipart-parameter
736 mm-security-handle 'gnus-details
737 (concat decrypt-status
738 (when (stringp (car handles))
739 "\n" (mm-handle-multipart-ctl-parameter
740 handles 'gnus-details))))
741 (if (listp (car handles))
744 (mm-set-handle-multipart-parameter
745 mm-security-handle 'gnus-info "Failed")
746 (throw 'error handle))))))
748 (defun mml2015-pgg-clear-decrypt ()
749 (let ((pgg-errors-buffer mml2015-result-buffer))
751 (pgg-decrypt-region (point-min) (point-max))
752 (mm-set-handle-multipart-parameter
753 mm-security-handle 'gnus-details
754 (with-current-buffer mml2015-result-buffer
758 ;; Treat data which pgg returns as a unibyte string.
759 (mm-disable-multibyte)
760 (insert-buffer-substring pgg-output-buffer)
761 (goto-char (point-min))
762 (while (search-forward "\r\n" nil t)
763 (replace-match "\n" t t))
764 (mm-set-handle-multipart-parameter
765 mm-security-handle 'gnus-info "OK"))
766 (mm-set-handle-multipart-parameter
767 mm-security-handle 'gnus-info "Failed"))))
769 (defun mml2015-pgg-verify (handle ctl)
770 (let ((pgg-errors-buffer mml2015-result-buffer)
771 signature-file part signature)
772 (if (or (null (setq part (mm-find-raw-part-by-type
773 ctl (or (mm-handle-multipart-ctl-parameter
775 "application/pgp-signature")
777 (null (setq signature (mm-find-part-by-type
778 (cdr handle) "application/pgp-signature" nil t))))
780 (mm-set-handle-multipart-parameter
781 mm-security-handle 'gnus-info "Corrupted")
785 ;; Convert <LF> to <CR><LF> in signed text. If --textmode is
786 ;; specified when signing, the conversion is not necessary.
787 (goto-char (point-min))
790 (unless (eq (char-before) ?\r)
794 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
795 (mm-insert-part signature))
796 (if (condition-case err
798 (pgg-verify-region (point-min) (point-max)
800 (goto-char (point-min))
801 (while (search-forward "\r\n" nil t)
802 (replace-match "\n" t t))
803 (mm-set-handle-multipart-parameter
804 mm-security-handle 'gnus-details
805 (concat (with-current-buffer pgg-output-buffer
807 (with-current-buffer pgg-errors-buffer
810 (mm-set-handle-multipart-parameter
811 mm-security-handle 'gnus-details (mml2015-format-error err))
814 (mm-set-handle-multipart-parameter
815 mm-security-handle 'gnus-details "Quit.")
818 (delete-file signature-file)
819 (mm-set-handle-multipart-parameter
820 mm-security-handle 'gnus-info
821 (with-current-buffer pgg-errors-buffer
822 (mml2015-gpg-extract-signature-details))))
823 (delete-file signature-file)
824 (mm-set-handle-multipart-parameter
825 mm-security-handle 'gnus-info "Failed")))))
828 (defun mml2015-pgg-clear-verify ()
829 (let ((pgg-errors-buffer mml2015-result-buffer)
830 (text (buffer-string))
831 (coding-system buffer-file-coding-system))
832 (if (condition-case err
834 (mm-with-unibyte-buffer
835 (insert (mm-encode-coding-string text coding-system))
836 (pgg-verify-region (point-min) (point-max) nil t))
837 (goto-char (point-min))
838 (while (search-forward "\r\n" nil t)
839 (replace-match "\n" t t))
840 (mm-set-handle-multipart-parameter
841 mm-security-handle 'gnus-details
842 (concat (with-current-buffer pgg-output-buffer
844 (with-current-buffer pgg-errors-buffer
847 (mm-set-handle-multipart-parameter
848 mm-security-handle 'gnus-details (mml2015-format-error err))
851 (mm-set-handle-multipart-parameter
852 mm-security-handle 'gnus-details "Quit.")
854 (mm-set-handle-multipart-parameter
855 mm-security-handle 'gnus-info
856 (with-current-buffer pgg-errors-buffer
857 (mml2015-gpg-extract-signature-details)))
858 (mm-set-handle-multipart-parameter
859 mm-security-handle 'gnus-info "Failed"))))
861 (defun mml2015-pgg-sign (cont)
862 (let ((pgg-errors-buffer mml2015-result-buffer)
863 (boundary (mml-compute-boundary cont))
864 (pgg-default-user-id (or (message-options-get 'mml-sender)
865 pgg-default-user-id))
868 (unless (pgg-sign-region (point-min) (point-max))
869 (pop-to-buffer mml2015-result-buffer)
870 (error "Sign error"))
871 (goto-char (point-min))
872 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
874 (if (setq entry (assq 2 (pgg-parse-armor
875 (with-current-buffer pgg-output-buffer
877 (setq entry (assq 'hash-algorithm (cdr entry))))
878 (insert (format "\tmicalg=%s; "
880 (downcase (format "pgp-%s" (cdr entry)))
882 (insert "protocol=\"application/pgp-signature\"\n")
883 (insert (format "\n--%s\n" boundary))
884 (goto-char (point-max))
885 (insert (format "\n--%s\n" boundary))
886 (insert "Content-Type: application/pgp-signature\n\n")
887 (insert-buffer-substring pgg-output-buffer)
888 (goto-char (point-max))
889 (insert (format "--%s--\n" boundary))
890 (goto-char (point-max))))
892 (defun mml2015-pgg-encrypt (cont &optional sign)
893 (let ((pgg-errors-buffer mml2015-result-buffer)
895 (boundary (mml-compute-boundary cont)))
896 (unless (pgg-encrypt-region (point-min) (point-max)
899 (message-options-get 'message-recipients)
900 (message-options-set 'message-recipients
901 (read-string "Recipients: ")))
904 (pop-to-buffer mml2015-result-buffer)
905 (error "Encrypt error"))
906 (delete-region (point-min) (point-max))
907 (goto-char (point-min))
908 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
910 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
911 (insert (format "--%s\n" boundary))
912 (insert "Content-Type: application/pgp-encrypted\n\n")
913 (insert "Version: 1\n\n")
914 (insert (format "--%s\n" boundary))
915 (insert "Content-Type: application/octet-stream\n\n")
916 (insert-buffer-substring pgg-output-buffer)
917 (goto-char (point-max))
918 (insert (format "--%s--\n" boundary))
919 (goto-char (point-max))))
924 (autoload 'epg-make-context "epg"))
927 (defvar epg-user-id-alist)
928 (defvar epg-digest-algorithm-alist)
929 (defvar inhibit-redisplay)
930 (autoload 'epg-context-set-armor "epg")
931 (autoload 'epg-context-set-textmode "epg")
932 (autoload 'epg-context-set-signers "epg")
933 (autoload 'epg-context-result-for "epg")
934 (autoload 'epg-new-signature-digest-algorithm "epg")
935 (autoload 'epg-verify-result-to-string "epg")
936 (autoload 'epg-list-keys "epg")
937 (autoload 'epg-decrypt-string "epg")
938 (autoload 'epg-verify-string "epg")
939 (autoload 'epg-sign-string "epg")
940 (autoload 'epg-encrypt-string "epg")
941 (autoload 'epg-passphrase-callback-function "epg")
942 (autoload 'epg-context-set-passphrase-callback "epg")
943 (autoload 'epg-configuration "epg-config")
944 (autoload 'epg-expand-group "epg-config")
945 (autoload 'epa-select-keys "epa"))
948 (defvar password-cache-expiry)
949 (autoload 'password-read "password")
950 (autoload 'password-cache-add "password")
951 (autoload 'password-cache-remove "password"))
953 (defvar mml2015-epg-secret-key-id-list nil)
955 (defun mml2015-epg-passphrase-callback (context key-id ignore)
957 (epg-passphrase-callback-function context key-id nil)
962 "Passphrase for PIN: "
963 (if (setq entry (assoc key-id epg-user-id-alist))
964 (format "Passphrase for %s %s: " key-id (cdr entry))
965 (format "Passphrase for %s: " key-id)))
970 (let ((password-cache-expiry mml2015-passphrase-cache-expiry))
971 (password-cache-add key-id passphrase))
972 (setq mml2015-epg-secret-key-id-list
973 (cons key-id mml2015-epg-secret-key-id-list))
974 (copy-sequence passphrase)))))
976 (defun mml2015-epg-decrypt (handle ctl)
978 (let ((inhibit-redisplay t)
979 context plain child handles result decrypt-status)
980 (unless (setq child (mm-find-part-by-type
982 "application/octet-stream" nil t))
983 (mm-set-handle-multipart-parameter
984 mm-security-handle 'gnus-info "Corrupted")
985 (throw 'error handle))
986 (setq context (epg-make-context))
987 (if mml2015-cache-passphrase
988 (epg-context-set-passphrase-callback
990 #'mml2015-epg-passphrase-callback))
991 (condition-case error
992 (setq plain (epg-decrypt-string context (mm-get-part child))
993 mml2015-epg-secret-key-id-list nil)
995 (while mml2015-epg-secret-key-id-list
996 (password-cache-remove (car mml2015-epg-secret-key-id-list))
997 (setq mml2015-epg-secret-key-id-list
998 (cdr mml2015-epg-secret-key-id-list)))
999 (mm-set-handle-multipart-parameter
1000 mm-security-handle 'gnus-info "Failed")
1001 (if (eq (car error) 'quit)
1002 (mm-set-handle-multipart-parameter
1003 mm-security-handle 'gnus-details "Quit.")
1004 (mm-set-handle-multipart-parameter
1005 mm-security-handle 'gnus-details (mml2015-format-error error)))
1006 (throw 'error handle)))
1009 (goto-char (point-min))
1010 (while (search-forward "\r\n" nil t)
1011 (replace-match "\n" t t))
1012 (setq handles (mm-dissect-buffer t))
1013 (mm-destroy-parts handle)
1014 (if (epg-context-result-for context 'verify)
1015 (mm-set-handle-multipart-parameter
1016 mm-security-handle 'gnus-info
1018 (epg-verify-result-to-string
1019 (epg-context-result-for context 'verify))))
1020 (mm-set-handle-multipart-parameter
1021 mm-security-handle 'gnus-info "OK"))
1022 (if (stringp (car handles))
1023 (mm-set-handle-multipart-parameter
1024 mm-security-handle 'gnus-details
1025 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
1026 (if (listp (car handles))
1030 (defun mml2015-epg-clear-decrypt ()
1031 (let ((inhibit-redisplay t)
1032 (context (epg-make-context))
1034 (if mml2015-cache-passphrase
1035 (epg-context-set-passphrase-callback
1037 #'mml2015-epg-passphrase-callback))
1038 (condition-case error
1039 (setq plain (epg-decrypt-string context (buffer-string))
1040 mml2015-epg-secret-key-id-list nil)
1042 (while mml2015-epg-secret-key-id-list
1043 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1044 (setq mml2015-epg-secret-key-id-list
1045 (cdr mml2015-epg-secret-key-id-list)))
1046 (mm-set-handle-multipart-parameter
1047 mm-security-handle 'gnus-info "Failed")
1048 (if (eq (car error) 'quit)
1049 (mm-set-handle-multipart-parameter
1050 mm-security-handle 'gnus-details "Quit.")
1051 (mm-set-handle-multipart-parameter
1052 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1055 ;; Treat data which epg returns as a unibyte string.
1056 (mm-disable-multibyte)
1058 (goto-char (point-min))
1059 (while (search-forward "\r\n" nil t)
1060 (replace-match "\n" t t))
1061 (mm-set-handle-multipart-parameter
1062 mm-security-handle 'gnus-info "OK")
1063 (if (epg-context-result-for context 'verify)
1064 (mm-set-handle-multipart-parameter
1065 mm-security-handle 'gnus-details
1066 (epg-verify-result-to-string
1067 (epg-context-result-for context 'verify)))))))
1069 (defun mml2015-epg-verify (handle ctl)
1071 (let ((inhibit-redisplay t)
1072 context plain signature-file part signature)
1073 (when (or (null (setq part (mm-find-raw-part-by-type
1074 ctl (or (mm-handle-multipart-ctl-parameter
1076 "application/pgp-signature")
1078 (null (setq signature (mm-find-part-by-type
1079 (cdr handle) "application/pgp-signature"
1081 (mm-set-handle-multipart-parameter
1082 mm-security-handle 'gnus-info "Corrupted")
1083 (throw 'error handle))
1084 (setq part (mm-replace-in-string part "\n" "\r\n")
1085 signature (mm-get-part signature)
1086 context (epg-make-context))
1087 (condition-case error
1088 (setq plain (epg-verify-string context signature part))
1090 (mm-set-handle-multipart-parameter
1091 mm-security-handle 'gnus-info "Failed")
1092 (if (eq (car error) 'quit)
1093 (mm-set-handle-multipart-parameter
1094 mm-security-handle 'gnus-details "Quit.")
1095 (mm-set-handle-multipart-parameter
1096 mm-security-handle 'gnus-details (mml2015-format-error error)))
1097 (throw 'error handle)))
1098 (mm-set-handle-multipart-parameter
1099 mm-security-handle 'gnus-info
1100 (epg-verify-result-to-string (epg-context-result-for context 'verify)))
1103 (defun mml2015-epg-clear-verify ()
1104 (let ((inhibit-redisplay t)
1105 (context (epg-make-context))
1106 (signature (mm-encode-coding-string (buffer-string)
1107 buffer-file-coding-system))
1109 (condition-case error
1110 (setq plain (epg-verify-string context signature))
1112 (mm-set-handle-multipart-parameter
1113 mm-security-handle 'gnus-info "Failed")
1114 (if (eq (car error) 'quit)
1115 (mm-set-handle-multipart-parameter
1116 mm-security-handle 'gnus-details "Quit.")
1117 (mm-set-handle-multipart-parameter
1118 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1120 (mm-set-handle-multipart-parameter
1121 mm-security-handle 'gnus-info
1122 (epg-verify-result-to-string
1123 (epg-context-result-for context 'verify))))))
1125 (defun mml2015-epg-sign (cont)
1126 (let* ((inhibit-redisplay t)
1127 (context (epg-make-context))
1128 (boundary (mml-compute-boundary cont))
1131 (or (message-options-get 'mml2015-epg-signers)
1132 (message-options-set
1133 'mml2015-epg-signers
1135 (epa-select-keys context "\
1136 Select keys for signing.
1137 If no one is selected, default secret key is used. "
1144 (epg-list-keys context signer t))
1145 (unless (or signer-keys
1148 "No secret key for %s; skip it? "
1150 (error "No secret key for %s" signer))
1152 mml2015-signers)))))))
1154 (epg-context-set-armor context t)
1155 (epg-context-set-textmode context t)
1156 (epg-context-set-signers context signers)
1157 (if mml2015-cache-passphrase
1158 (epg-context-set-passphrase-callback
1160 #'mml2015-epg-passphrase-callback))
1161 (condition-case error
1162 (setq signature (epg-sign-string context (buffer-string) t)
1163 mml2015-epg-secret-key-id-list nil)
1165 (while mml2015-epg-secret-key-id-list
1166 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1167 (setq mml2015-epg-secret-key-id-list
1168 (cdr mml2015-epg-secret-key-id-list)))
1169 (signal (car error) (cdr error))))
1170 (if (epg-context-result-for context 'sign)
1171 (setq micalg (epg-new-signature-digest-algorithm
1172 (car (epg-context-result-for context 'sign)))))
1173 (goto-char (point-min))
1174 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
1177 (insert (format "\tmicalg=pgp-%s; "
1180 epg-digest-algorithm-alist))))))
1181 (insert "protocol=\"application/pgp-signature\"\n")
1182 (insert (format "\n--%s\n" boundary))
1183 (goto-char (point-max))
1184 (insert (format "\n--%s\n" boundary))
1185 (insert "Content-Type: application/pgp-signature\n\n")
1187 (goto-char (point-max))
1188 (insert (format "--%s--\n" boundary))
1189 (goto-char (point-max))))
1191 (defun mml2015-epg-encrypt (cont &optional sign)
1192 (let ((inhibit-redisplay t)
1193 (context (epg-make-context))
1194 (config (epg-configuration))
1195 (recipients (message-options-get 'mml2015-epg-recipients))
1197 (boundary (mml-compute-boundary cont))
1198 recipient-keys signer-keys)
1204 (or (epg-expand-group config recipient)
1205 (list (concat "<" recipient ">"))))
1207 (or (message-options-get 'message-recipients)
1208 (message-options-set 'message-recipients
1209 (read-string "Recipients: ")))
1210 "[ \f\t\n\r\v,]+"))))
1211 (when mml2015-encrypt-to-self
1212 (unless mml2015-signers
1213 (error "mml2015-signers not set"))
1214 (setq recipients (nconc recipients mml2015-signers)))
1217 (epa-select-keys context "\
1218 Select recipients for encryption.
1219 If no one is selected, symmetric encryption will be performed. "
1225 (setq recipient-keys (epg-list-keys context recipient))
1226 (unless (or recipient-keys
1228 (format "No public key for %s; skip it? "
1230 (error "No public key for %s" recipient))
1234 (error "No recipient specified")))
1235 (message-options-set 'mml2015-epg-recipients recipients))
1238 (or (message-options-get 'mml2015-epg-signers)
1239 (message-options-set
1240 'mml2015-epg-signers
1242 (epa-select-keys context "\
1243 Select keys for signing.
1244 If no one is selected, default secret key is used. "
1251 (epg-list-keys context signer t))
1252 (unless (or signer-keys
1255 "No secret key for %s; skip it? "
1257 (error "No secret key for %s" signer))
1259 mml2015-signers)))))))
1260 (epg-context-set-signers context signers))
1261 (epg-context-set-armor context t)
1262 (epg-context-set-textmode context t)
1263 (if mml2015-cache-passphrase
1264 (epg-context-set-passphrase-callback
1266 #'mml2015-epg-passphrase-callback))
1267 (condition-case error
1269 (epg-encrypt-string context (buffer-string) recipients sign
1270 mml2015-always-trust)
1271 mml2015-epg-secret-key-id-list nil)
1273 (while mml2015-epg-secret-key-id-list
1274 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1275 (setq mml2015-epg-secret-key-id-list
1276 (cdr mml2015-epg-secret-key-id-list)))
1277 (signal (car error) (cdr error))))
1278 (delete-region (point-min) (point-max))
1279 (goto-char (point-min))
1280 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1282 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1283 (insert (format "--%s\n" boundary))
1284 (insert "Content-Type: application/pgp-encrypted\n\n")
1285 (insert "Version: 1\n\n")
1286 (insert (format "--%s\n" boundary))
1287 (insert "Content-Type: application/octet-stream\n\n")
1289 (goto-char (point-max))
1290 (insert (format "--%s--\n" boundary))
1291 (goto-char (point-max))))
1295 (defun mml2015-clean-buffer ()
1296 (if (gnus-buffer-live-p mml2015-result-buffer)
1297 (with-current-buffer mml2015-result-buffer
1300 (setq mml2015-result-buffer
1301 (gnus-get-buffer-create " *MML2015 Result*"))
1304 (defsubst mml2015-clear-decrypt-function ()
1305 (nth 6 (assq mml2015-use mml2015-function-alist)))
1307 (defsubst mml2015-clear-verify-function ()
1308 (nth 5 (assq mml2015-use mml2015-function-alist)))
1311 (defun mml2015-decrypt (handle ctl)
1312 (mml2015-clean-buffer)
1313 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1315 (funcall func handle ctl)
1319 (defun mml2015-decrypt-test (handle ctl)
1323 (defun mml2015-verify (handle ctl)
1324 (mml2015-clean-buffer)
1325 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1327 (funcall func handle ctl)
1331 (defun mml2015-verify-test (handle ctl)
1335 (defun mml2015-encrypt (cont &optional sign)
1336 (mml2015-clean-buffer)
1337 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1339 (funcall func cont sign)
1340 (error "Cannot find encrypt function"))))
1343 (defun mml2015-sign (cont)
1344 (mml2015-clean-buffer)
1345 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1348 (error "Cannot find sign function"))))
1351 (defun mml2015-self-encrypt ()
1352 (mml2015-encrypt nil))
1356 ;;; arch-tag: b04701d5-0b09-44d8-bed8-de901bf435f2
1357 ;;; mml2015.el ends here