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 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 (mm-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")
947 (autoload 'epa-select-keys "epa"))
950 (defvar password-cache-expiry)
951 (autoload 'password-read "password")
952 (autoload 'password-cache-add "password")
953 (autoload 'password-cache-remove "password"))
955 (defvar mml2015-epg-secret-key-id-list nil)
957 (defun mml2015-epg-passphrase-callback (context key-id ignore)
959 (epg-passphrase-callback-function context key-id nil)
964 "Passphrase for PIN: "
965 (if (setq entry (assoc key-id epg-user-id-alist))
966 (format "Passphrase for %s %s: " key-id (cdr entry))
967 (format "Passphrase for %s: " key-id)))
972 (let ((password-cache-expiry mml2015-passphrase-cache-expiry))
973 (password-cache-add key-id passphrase))
974 (setq mml2015-epg-secret-key-id-list
975 (cons key-id mml2015-epg-secret-key-id-list))
976 (copy-sequence passphrase)))))
978 (defun mml2015-epg-decrypt (handle ctl)
980 (let ((inhibit-redisplay t)
981 context plain child handles result decrypt-status)
982 (unless (setq child (mm-find-part-by-type
984 "application/octet-stream" nil t))
985 (mm-set-handle-multipart-parameter
986 mm-security-handle 'gnus-info "Corrupted")
987 (throw 'error handle))
988 (setq context (epg-make-context))
989 (if mml2015-cache-passphrase
990 (epg-context-set-passphrase-callback
992 #'mml2015-epg-passphrase-callback))
993 (condition-case error
994 (setq plain (epg-decrypt-string context (mm-get-part child))
995 mml2015-epg-secret-key-id-list nil)
997 (while mml2015-epg-secret-key-id-list
998 (password-cache-remove (car mml2015-epg-secret-key-id-list))
999 (setq mml2015-epg-secret-key-id-list
1000 (cdr mml2015-epg-secret-key-id-list)))
1001 (mm-set-handle-multipart-parameter
1002 mm-security-handle 'gnus-info "Failed")
1003 (if (eq (car error) 'quit)
1004 (mm-set-handle-multipart-parameter
1005 mm-security-handle 'gnus-details "Quit.")
1006 (mm-set-handle-multipart-parameter
1007 mm-security-handle 'gnus-details (mml2015-format-error error)))
1008 (throw 'error handle)))
1011 (goto-char (point-min))
1012 (while (search-forward "\r\n" nil t)
1013 (replace-match "\n" t t))
1014 (setq handles (mm-dissect-buffer t))
1015 (mm-destroy-parts handle)
1016 (if (epg-context-result-for context 'verify)
1017 (mm-set-handle-multipart-parameter
1018 mm-security-handle 'gnus-info
1020 (epg-verify-result-to-string
1021 (epg-context-result-for context 'verify))))
1022 (mm-set-handle-multipart-parameter
1023 mm-security-handle 'gnus-info "OK"))
1024 (if (stringp (car handles))
1025 (mm-set-handle-multipart-parameter
1026 mm-security-handle 'gnus-details
1027 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
1028 (if (listp (car handles))
1032 (defun mml2015-epg-clear-decrypt ()
1033 (let ((inhibit-redisplay t)
1034 (context (epg-make-context))
1036 (if mml2015-cache-passphrase
1037 (epg-context-set-passphrase-callback
1039 #'mml2015-epg-passphrase-callback))
1040 (condition-case error
1041 (setq plain (epg-decrypt-string context (buffer-string))
1042 mml2015-epg-secret-key-id-list nil)
1044 (while mml2015-epg-secret-key-id-list
1045 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1046 (setq mml2015-epg-secret-key-id-list
1047 (cdr mml2015-epg-secret-key-id-list)))
1048 (mm-set-handle-multipart-parameter
1049 mm-security-handle 'gnus-info "Failed")
1050 (if (eq (car error) 'quit)
1051 (mm-set-handle-multipart-parameter
1052 mm-security-handle 'gnus-details "Quit.")
1053 (mm-set-handle-multipart-parameter
1054 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1057 ;; Treat data which epg returns as a unibyte string.
1058 (mm-disable-multibyte)
1060 (goto-char (point-min))
1061 (while (search-forward "\r\n" nil t)
1062 (replace-match "\n" t t))
1063 (mm-set-handle-multipart-parameter
1064 mm-security-handle 'gnus-info "OK")
1065 (if (epg-context-result-for context 'verify)
1066 (mm-set-handle-multipart-parameter
1067 mm-security-handle 'gnus-details
1068 (epg-verify-result-to-string
1069 (epg-context-result-for context 'verify)))))))
1071 (defun mml2015-epg-verify (handle ctl)
1073 (let ((inhibit-redisplay t)
1074 context plain signature-file part signature (index 0))
1075 (when (or (null (setq part (mm-find-raw-part-by-type
1076 ctl (or (mm-handle-multipart-ctl-parameter
1078 "application/pgp-signature")
1080 (null (setq signature (mm-find-part-by-type
1081 (cdr handle) "application/pgp-signature"
1083 (mm-set-handle-multipart-parameter
1084 mm-security-handle 'gnus-info "Corrupted")
1085 (throw 'error handle))
1086 (while (string-match "\r?\n" part index)
1087 (if (eq (aref part (match-beginning 0)) ?\r)
1088 (setq index (match-end 0))
1089 (setq part (replace-match "\r\n" t t part)
1090 index (1+ (match-end 0)))))
1091 (setq context (epg-make-context))
1092 (condition-case error
1093 (setq plain (epg-verify-string context (mm-get-part signature) part))
1095 (mm-set-handle-multipart-parameter
1096 mm-security-handle 'gnus-info "Failed")
1097 (if (eq (car error) 'quit)
1098 (mm-set-handle-multipart-parameter
1099 mm-security-handle 'gnus-details "Quit.")
1100 (mm-set-handle-multipart-parameter
1101 mm-security-handle 'gnus-details (mml2015-format-error error)))
1102 (throw 'error handle)))
1103 (mm-set-handle-multipart-parameter
1104 mm-security-handle 'gnus-info
1105 (epg-verify-result-to-string (epg-context-result-for context 'verify)))
1108 (defun mml2015-epg-clear-verify ()
1109 (let ((inhibit-redisplay t)
1110 (context (epg-make-context))
1111 (signature (mm-encode-coding-string (buffer-string)
1112 buffer-file-coding-system))
1114 (condition-case error
1115 (setq plain (epg-verify-string context signature))
1117 (mm-set-handle-multipart-parameter
1118 mm-security-handle 'gnus-info "Failed")
1119 (if (eq (car error) 'quit)
1120 (mm-set-handle-multipart-parameter
1121 mm-security-handle 'gnus-details "Quit.")
1122 (mm-set-handle-multipart-parameter
1123 mm-security-handle 'gnus-details (mml2015-format-error error)))))
1125 (mm-set-handle-multipart-parameter
1126 mm-security-handle 'gnus-info
1127 (epg-verify-result-to-string
1128 (epg-context-result-for context 'verify))))))
1130 (defun mml2015-epg-sign (cont)
1131 (let* ((inhibit-redisplay t)
1132 (context (epg-make-context))
1133 (boundary (mml-compute-boundary cont))
1136 (or (message-options-get 'mml2015-epg-signers)
1137 (message-options-set
1138 'mml2015-epg-signers
1140 (epa-select-keys context "\
1141 Select keys for signing.
1142 If no one is selected, default secret key is used. "
1149 (epg-list-keys context signer t))
1150 (unless (or signer-keys
1153 "No secret key for %s; skip it? "
1155 (error "No secret key for %s" signer))
1157 mml2015-signers)))))))
1159 (epg-context-set-armor context t)
1160 (epg-context-set-textmode context t)
1161 (epg-context-set-signers context signers)
1162 (if mml2015-cache-passphrase
1163 (epg-context-set-passphrase-callback
1165 #'mml2015-epg-passphrase-callback))
1166 (condition-case error
1167 (setq signature (epg-sign-string context (buffer-string) t)
1168 mml2015-epg-secret-key-id-list nil)
1170 (while mml2015-epg-secret-key-id-list
1171 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1172 (setq mml2015-epg-secret-key-id-list
1173 (cdr mml2015-epg-secret-key-id-list)))
1174 (signal (car error) (cdr error))))
1175 (if (epg-context-result-for context 'sign)
1176 (setq micalg (epg-new-signature-digest-algorithm
1177 (car (epg-context-result-for context 'sign)))))
1178 (goto-char (point-min))
1179 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
1182 (insert (format "\tmicalg=pgp-%s; "
1185 epg-digest-algorithm-alist))))))
1186 (insert "protocol=\"application/pgp-signature\"\n")
1187 (insert (format "\n--%s\n" boundary))
1188 (goto-char (point-max))
1189 (insert (format "\n--%s\n" boundary))
1190 (insert "Content-Type: application/pgp-signature\n\n")
1192 (goto-char (point-max))
1193 (insert (format "--%s--\n" boundary))
1194 (goto-char (point-max))))
1196 (defun mml2015-epg-encrypt (cont &optional sign)
1197 (let ((inhibit-redisplay t)
1198 (context (epg-make-context))
1199 (config (epg-configuration))
1200 (recipients (message-options-get 'mml2015-epg-recipients))
1202 (boundary (mml-compute-boundary cont))
1203 recipient-keys signer-keys)
1209 (or (epg-expand-group config recipient)
1210 (list (concat "<" recipient ">"))))
1212 (or (message-options-get 'message-recipients)
1213 (message-options-set 'message-recipients
1214 (read-string "Recipients: ")))
1215 "[ \f\t\n\r\v,]+"))))
1216 (when mml2015-encrypt-to-self
1217 (unless mml2015-signers
1218 (error "mml2015-signers not set"))
1219 (setq recipients (nconc recipients mml2015-signers)))
1222 (epa-select-keys context "\
1223 Select recipients for encryption.
1224 If no one is selected, symmetric encryption will be performed. "
1230 (setq recipient-keys (epg-list-keys context recipient))
1231 (unless (or recipient-keys
1233 (format "No public key for %s; skip it? "
1235 (error "No public key for %s" recipient))
1239 (error "No recipient specified")))
1240 (message-options-set 'mml2015-epg-recipients recipients))
1243 (or (message-options-get 'mml2015-epg-signers)
1244 (message-options-set
1245 'mml2015-epg-signers
1247 (epa-select-keys context "\
1248 Select keys for signing.
1249 If no one is selected, default secret key is used. "
1256 (epg-list-keys context signer t))
1257 (unless (or signer-keys
1260 "No secret key for %s; skip it? "
1262 (error "No secret key for %s" signer))
1264 mml2015-signers)))))))
1265 (epg-context-set-signers context signers))
1266 (epg-context-set-armor context t)
1267 (epg-context-set-textmode context t)
1268 (if mml2015-cache-passphrase
1269 (epg-context-set-passphrase-callback
1271 #'mml2015-epg-passphrase-callback))
1272 (condition-case error
1274 (epg-encrypt-string context (buffer-string) recipients sign
1275 mml2015-always-trust)
1276 mml2015-epg-secret-key-id-list nil)
1278 (while mml2015-epg-secret-key-id-list
1279 (password-cache-remove (car mml2015-epg-secret-key-id-list))
1280 (setq mml2015-epg-secret-key-id-list
1281 (cdr mml2015-epg-secret-key-id-list)))
1282 (signal (car error) (cdr error))))
1283 (delete-region (point-min) (point-max))
1284 (goto-char (point-min))
1285 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1287 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1288 (insert (format "--%s\n" boundary))
1289 (insert "Content-Type: application/pgp-encrypted\n\n")
1290 (insert "Version: 1\n\n")
1291 (insert (format "--%s\n" boundary))
1292 (insert "Content-Type: application/octet-stream\n\n")
1294 (goto-char (point-max))
1295 (insert (format "--%s--\n" boundary))
1296 (goto-char (point-max))))
1300 (defun mml2015-clean-buffer ()
1301 (if (gnus-buffer-live-p mml2015-result-buffer)
1302 (with-current-buffer mml2015-result-buffer
1305 (setq mml2015-result-buffer
1306 (gnus-get-buffer-create " *MML2015 Result*"))
1309 (defsubst mml2015-clear-decrypt-function ()
1310 (nth 6 (assq mml2015-use mml2015-function-alist)))
1312 (defsubst mml2015-clear-verify-function ()
1313 (nth 5 (assq mml2015-use mml2015-function-alist)))
1316 (defun mml2015-decrypt (handle ctl)
1317 (mml2015-clean-buffer)
1318 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1320 (funcall func handle ctl)
1324 (defun mml2015-decrypt-test (handle ctl)
1328 (defun mml2015-verify (handle ctl)
1329 (mml2015-clean-buffer)
1330 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1332 (funcall func handle ctl)
1336 (defun mml2015-verify-test (handle ctl)
1340 (defun mml2015-encrypt (cont &optional sign)
1341 (mml2015-clean-buffer)
1342 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1344 (funcall func cont sign)
1345 (error "Cannot find encrypt function"))))
1348 (defun mml2015-sign (cont)
1349 (mml2015-clean-buffer)
1350 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1353 (error "Cannot find sign function"))))
1356 (defun mml2015-self-encrypt ()
1357 (mml2015-encrypt nil))
1361 ;;; arch-tag: b04701d5-0b09-44d8-bed8-de901bf435f2
1362 ;;; mml2015.el ends here