1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: PGP MIME MML
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
31 (if (locate-library "password-cache")
32 (require 'password-cache)
35 (eval-when-compile (require 'cl))
42 (defvar mc-pgp-always-sign)
44 (declare-function epg-check-configuration "ext:epg-config"
45 (config &optional minimum-version))
46 (declare-function epg-configuration "ext:epg-config" ())
48 ;; Maybe this should be in eg mml-sec.el (and have a different name).
49 ;; Then mml1991 would not need to require mml2015, and mml1991-use
51 (defvar mml2015-use 'epg
52 "The package used for PGP/MIME.
53 Valid packages include `epg', `pgg' and `mailcrypt'.")
55 ;; Something is not RFC2015.
56 (defvar mml2015-function-alist
57 '((mailcrypt mml2015-mailcrypt-sign
58 mml2015-mailcrypt-encrypt
59 mml2015-mailcrypt-verify
60 mml2015-mailcrypt-decrypt
61 mml2015-mailcrypt-clear-verify
62 mml2015-mailcrypt-clear-decrypt)
67 mml2015-pgg-clear-verify
68 mml2015-pgg-clear-decrypt)
73 mml2015-epg-clear-verify
74 mml2015-epg-clear-decrypt))
75 "Alist of PGP/MIME functions.")
77 (defvar mml2015-result-buffer nil)
79 (defcustom mml2015-unabbrev-trust-alist
80 '(("TRUST_UNDEFINED" . nil)
82 ("TRUST_MARGINAL" . t)
84 ("TRUST_ULTIMATE" . t))
85 "Map GnuPG trust output values to a boolean saying if you trust the key."
88 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
89 (boolean :tag "Trust key"))))
91 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
92 "If t, cache passphrase."
95 (make-obsolete-variable 'mml2015-cache-passphrase
96 'mml-secure-cache-passphrase
99 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
100 "How many seconds the passphrase is cached.
101 Whether the passphrase is cached at all is controlled by
102 `mml2015-cache-passphrase'."
103 :group 'mime-security
105 (make-obsolete-variable 'mml2015-passphrase-cache-expiry
106 'mml-secure-passphrase-cache-expiry
109 (defcustom mml2015-signers nil
110 "A list of your own key ID(s) which will be used to sign a message.
111 If set, it overrides the setting of `mml2015-sign-with-sender'."
112 :group 'mime-security
113 :type '(repeat (string :tag "Key ID")))
115 (defcustom mml2015-sign-with-sender nil
116 "If t, use message sender so find a key to sign with."
117 :group 'mime-security
121 (defcustom mml2015-encrypt-to-self nil
122 "If t, add your own key ID to recipient list when encryption."
123 :group 'mime-security
126 (defcustom mml2015-always-trust t
127 "If t, GnuPG skip key validation on encryption."
128 :group 'mime-security
131 (defcustom mml2015-maximum-key-image-dimension 64
132 "The maximum dimension (width or height) of any key images."
134 :group 'mime-security
137 (defcustom mml2015-display-key-image t
138 "If t, try to display key images."
140 :group 'mime-security
143 ;; Extract plaintext from cleartext signature. IMO, this kind of task
144 ;; should be done by GnuPG rather than Elisp, but older PGP backends
145 ;; (such as Mailcrypt, and PGG) discard the output from GnuPG.
146 (defun mml2015-extract-cleartext-signature ()
148 ;; <54a15d860801080142l70b95d7dkac4bf51a86196011@mail.gmail.com>: ``I still
149 ;; believe that the right way is to use the plaintext output from GnuPG as
150 ;; it is, and mml2015-extract-cleartext-signature is just a kludge for
151 ;; misdesigned libraries like PGG, which have no ability to do that. So, I
152 ;; think it should not have descriptive documentation.''
154 ;; This function doesn't handle NotDashEscaped correctly. EasyPG handles it
156 ;; http://thread.gmane.org/gmane.emacs.gnus.general/66062/focus=66082
157 ;; http://thread.gmane.org/gmane.emacs.gnus.general/65087/focus=65109
158 (goto-char (point-min))
160 ;; We need to be careful not to strip beyond the armor headers.
161 ;; Previously, an attacker could replace the text inside our
162 ;; markup with trailing garbage by injecting whitespace into the
164 (while (looking-at "Hash:") ; The only header allowed in cleartext
165 (forward-line)) ; signatures according to RFC2440.
166 (when (looking-at "[\t ]*$")
168 (delete-region (point-min) (point))
169 (if (re-search-forward "^-----BEGIN PGP SIGNATURE-----" nil t)
170 (delete-region (match-beginning 0) (point-max)))
171 (goto-char (point-min))
172 (while (re-search-forward "^- " nil t)
173 (replace-match "" t t)
176 ;;; mailcrypt wrapper
178 (autoload 'mailcrypt-decrypt "mailcrypt")
179 (autoload 'mailcrypt-verify "mailcrypt")
180 (autoload 'mc-pgp-always-sign "mailcrypt")
181 (autoload 'mc-encrypt-generic "mc-toplev")
182 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
183 (autoload 'mc-sign-generic "mc-toplev")
185 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
186 (defvar mml2015-verify-function 'mailcrypt-verify)
188 (defun mml2015-format-error (err)
189 (if (stringp (cadr err))
191 (format "%S" (cdr err))))
193 (defun mml2015-mailcrypt-decrypt (handle ctl)
195 (let (child handles result)
196 (unless (setq child (mm-find-part-by-type
198 "application/octet-stream" nil t))
199 (mm-set-handle-multipart-parameter
200 mm-security-handle 'gnus-info "Corrupted")
201 (throw 'error handle))
203 (mm-insert-part child)
206 (funcall mml2015-decrypt-function)
208 (mm-set-handle-multipart-parameter
209 mm-security-handle 'gnus-details (mml2015-format-error err))
212 (mm-set-handle-multipart-parameter
213 mm-security-handle 'gnus-details "Quit.")
216 (mm-set-handle-multipart-parameter
217 mm-security-handle 'gnus-info "Failed")
218 (throw 'error handle))
219 (setq handles (mm-dissect-buffer t)))
220 (mm-destroy-parts handle)
221 (mm-set-handle-multipart-parameter
222 mm-security-handle 'gnus-info
224 (let ((sig (with-current-buffer mml2015-result-buffer
225 (mml2015-gpg-extract-signature-details))))
226 (concat ", Signer: " sig))))
227 (if (listp (car handles))
231 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
233 (fpr-length (string-width fingerprint))
236 (setq fingerprint (string-to-list fingerprint))
238 (setq fpr-length (- fpr-length 4))
239 (setq slice (butlast fingerprint fpr-length))
240 (setq fingerprint (nthcdr 4 fingerprint))
241 (setq n-slice (1+ n-slice))
247 (otherwise (concat " " slice))))))
250 (defun mml2015-gpg-extract-signature-details ()
251 (goto-char (point-min))
252 (let* ((expired (re-search-forward
253 "^\\[GNUPG:\\] SIGEXPIRED$"
255 (signer (and (re-search-forward
256 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
258 (cons (match-string 1) (match-string 2))))
259 (fprint (and (re-search-forward
260 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
263 (trust (and (re-search-forward
264 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
268 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
269 (cond ((and signer fprint)
271 (unless trust-good-enough-p
272 (concat "\nUntrusted, Fingerprint: "
273 (mml2015-gpg-pretty-print-fpr fprint)))
275 (format "\nWARNING: Signature from expired key (%s)"
278 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
281 "From unknown user"))))
283 (defun mml2015-mailcrypt-clear-decrypt ()
287 (funcall mml2015-decrypt-function)
289 (mm-set-handle-multipart-parameter
290 mm-security-handle 'gnus-details (mml2015-format-error err))
293 (mm-set-handle-multipart-parameter
294 mm-security-handle 'gnus-details "Quit.")
297 (mm-set-handle-multipart-parameter
298 mm-security-handle 'gnus-info "OK")
299 (mm-set-handle-multipart-parameter
300 mm-security-handle 'gnus-info "Failed"))))
302 (defun mml2015-fix-micalg (alg)
304 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
305 (upcase (if (string-match "^p[gh]p-" alg)
306 (substring alg (match-end 0))
309 (defun mml2015-mailcrypt-verify (handle ctl)
312 (unless (setq part (mm-find-raw-part-by-type
313 ctl (or (mm-handle-multipart-ctl-parameter
315 "application/pgp-signature")
317 (mm-set-handle-multipart-parameter
318 mm-security-handle 'gnus-info "Corrupted")
319 (throw 'error handle))
321 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
322 (insert (format "Hash: %s\n\n"
323 (or (mml2015-fix-micalg
324 (mm-handle-multipart-ctl-parameter
328 (narrow-to-region (point) (point))
330 (goto-char (point-min))
332 (if (looking-at "^-")
335 (unless (setq part (mm-find-part-by-type
336 (cdr handle) "application/pgp-signature" nil t))
337 (mm-set-handle-multipart-parameter
338 mm-security-handle 'gnus-info "Corrupted")
339 (throw 'error handle))
341 (narrow-to-region (point) (point))
342 (mm-insert-part part)
343 (goto-char (point-min))
344 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
345 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
346 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
347 (replace-match "-----END PGP SIGNATURE-----" t t)))
348 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
349 (unless (condition-case err
351 (funcall mml2015-verify-function)
352 (if (get-buffer " *mailcrypt stderr temp")
353 (mm-set-handle-multipart-parameter
354 mm-security-handle 'gnus-details
355 (with-current-buffer " *mailcrypt stderr temp"
357 (if (get-buffer " *mailcrypt stdout temp")
358 (kill-buffer " *mailcrypt stdout temp"))
359 (if (get-buffer " *mailcrypt stderr temp")
360 (kill-buffer " *mailcrypt stderr temp"))
361 (if (get-buffer " *mailcrypt status temp")
362 (kill-buffer " *mailcrypt status temp"))
363 (if (get-buffer mc-gpg-debug-buffer)
364 (kill-buffer mc-gpg-debug-buffer)))
366 (mm-set-handle-multipart-parameter
367 mm-security-handle 'gnus-details (mml2015-format-error err))
370 (mm-set-handle-multipart-parameter
371 mm-security-handle 'gnus-details "Quit.")
373 (mm-set-handle-multipart-parameter
374 mm-security-handle 'gnus-info "Failed")
375 (throw 'error handle))))
376 (mm-set-handle-multipart-parameter
377 mm-security-handle 'gnus-info "OK")
380 (defun mml2015-mailcrypt-clear-verify ()
381 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
382 (if (condition-case err
384 (funcall mml2015-verify-function)
385 (if (get-buffer " *mailcrypt stderr temp")
386 (mm-set-handle-multipart-parameter
387 mm-security-handle 'gnus-details
388 (with-current-buffer " *mailcrypt stderr temp"
390 (if (get-buffer " *mailcrypt stdout temp")
391 (kill-buffer " *mailcrypt stdout temp"))
392 (if (get-buffer " *mailcrypt stderr temp")
393 (kill-buffer " *mailcrypt stderr temp"))
394 (if (get-buffer " *mailcrypt status temp")
395 (kill-buffer " *mailcrypt status temp"))
396 (if (get-buffer mc-gpg-debug-buffer)
397 (kill-buffer mc-gpg-debug-buffer)))
399 (mm-set-handle-multipart-parameter
400 mm-security-handle 'gnus-details (mml2015-format-error err))
403 (mm-set-handle-multipart-parameter
404 mm-security-handle 'gnus-details "Quit.")
406 (mm-set-handle-multipart-parameter
407 mm-security-handle 'gnus-info "OK")
408 (mm-set-handle-multipart-parameter
409 mm-security-handle 'gnus-info "Failed")))
410 (mml2015-extract-cleartext-signature))
412 (defun mml2015-mailcrypt-sign (cont)
413 (mc-sign-generic (message-options-get 'message-sender)
415 (let ((boundary (mml-compute-boundary cont))
417 (goto-char (point-min))
418 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
419 (error "Cannot find signed begin line"))
420 (goto-char (match-beginning 0))
422 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
423 (error "Cannot not find PGP hash"))
424 (setq hash (match-string 1))
425 (unless (re-search-forward "^$" nil t)
426 (error "Cannot not find PGP message"))
428 (delete-region (point-min) (point))
429 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
431 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
433 (insert (format "\n--%s\n" boundary))
435 (goto-char (point-max))
436 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
437 (error "Cannot find signature part"))
438 (replace-match "-----END PGP MESSAGE-----" t t)
439 (goto-char (match-beginning 0))
440 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
442 (error "Cannot find signature part"))
443 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
444 (goto-char (match-beginning 0))
446 (narrow-to-region point (point))
448 (while (re-search-forward "^- -" nil t)
449 (replace-match "-" t t))
450 (goto-char (point-max)))
451 (insert (format "--%s\n" boundary))
452 (insert "Content-Type: application/pgp-signature\n\n")
453 (goto-char (point-max))
454 (insert (format "--%s--\n" boundary))
455 (goto-char (point-max))))
457 ;; We require mm-decode, which requires mm-bodies, which autoloads
458 ;; message-options-get (!).
459 (declare-function message-options-set "message" (symbol value))
461 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
462 (let ((mc-pgp-always-sign
463 (or mc-pgp-always-sign
465 (eq t (or (message-options-get 'message-sign-encrypt)
467 'message-sign-encrypt
468 (or (y-or-n-p "Sign the message? ")
473 (set-buffer-multibyte nil)
475 (or (message-options-get 'message-recipients)
476 (message-options-set 'message-recipients
477 (mc-cleanup-recipient-headers
478 (read-string "Recipients: "))))
480 (message-options-get 'message-sender))
482 (goto-char (point-min))
483 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
484 (error "Fail to encrypt the message"))
485 (let ((boundary (mml-compute-boundary cont)))
486 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
488 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
489 (insert (format "--%s\n" boundary))
490 (insert "Content-Type: application/pgp-encrypted\n\n")
491 (insert "Version: 1\n\n")
492 (insert (format "--%s\n" boundary))
493 (insert "Content-Type: application/octet-stream\n\n")
494 (goto-char (point-max))
495 (insert (format "--%s--\n" boundary))
496 (goto-char (point-max))))
500 (defvar pgg-default-user-id)
501 (defvar pgg-errors-buffer)
502 (defvar pgg-output-buffer)
504 (autoload 'pgg-decrypt-region "pgg")
505 (autoload 'pgg-verify-region "pgg")
506 (autoload 'pgg-sign-region "pgg")
507 (autoload 'pgg-encrypt-region "pgg")
508 (autoload 'pgg-parse-armor "pgg-parse")
510 (defun mml2015-pgg-decrypt (handle ctl)
512 (let ((pgg-errors-buffer mml2015-result-buffer)
513 child handles result decrypt-status)
514 (unless (setq child (mm-find-part-by-type
516 "application/octet-stream" nil t))
517 (mm-set-handle-multipart-parameter
518 mm-security-handle 'gnus-info "Corrupted")
519 (throw 'error handle))
521 (mm-insert-part child)
522 (if (condition-case err
524 (pgg-decrypt-region (point-min) (point-max))
526 (with-current-buffer mml2015-result-buffer
528 (mm-set-handle-multipart-parameter
529 mm-security-handle 'gnus-details
532 (mm-set-handle-multipart-parameter
533 mm-security-handle 'gnus-details (mml2015-format-error err))
536 (mm-set-handle-multipart-parameter
537 mm-security-handle 'gnus-details "Quit.")
539 (with-current-buffer pgg-output-buffer
540 (goto-char (point-min))
541 (while (search-forward "\r\n" nil t)
542 (replace-match "\n" t t))
543 (setq handles (mm-dissect-buffer t))
544 (mm-destroy-parts handle)
545 (mm-set-handle-multipart-parameter
546 mm-security-handle 'gnus-info "OK")
547 (mm-set-handle-multipart-parameter
548 mm-security-handle 'gnus-details
549 (concat decrypt-status
550 (when (stringp (car handles))
551 "\n" (mm-handle-multipart-ctl-parameter
552 handles 'gnus-details))))
553 (if (listp (car handles))
556 (mm-set-handle-multipart-parameter
557 mm-security-handle 'gnus-info "Failed")
558 (throw 'error handle))))))
560 (defun mml2015-pgg-clear-decrypt ()
561 (let ((pgg-errors-buffer mml2015-result-buffer))
563 (pgg-decrypt-region (point-min) (point-max))
564 (mm-set-handle-multipart-parameter
565 mm-security-handle 'gnus-details
566 (with-current-buffer mml2015-result-buffer
570 ;; Treat data which pgg returns as a unibyte string.
571 (mm-disable-multibyte)
572 (insert-buffer-substring pgg-output-buffer)
573 (goto-char (point-min))
574 (while (search-forward "\r\n" nil t)
575 (replace-match "\n" t t))
576 (mm-set-handle-multipart-parameter
577 mm-security-handle 'gnus-info "OK"))
578 (mm-set-handle-multipart-parameter
579 mm-security-handle 'gnus-info "Failed"))))
581 (defun mml2015-pgg-verify (handle ctl)
582 (let ((pgg-errors-buffer mml2015-result-buffer)
583 signature-file part signature)
584 (if (or (null (setq part (mm-find-raw-part-by-type
585 ctl (or (mm-handle-multipart-ctl-parameter
587 "application/pgp-signature")
589 (null (setq signature (mm-find-part-by-type
590 (cdr handle) "application/pgp-signature" nil t))))
592 (mm-set-handle-multipart-parameter
593 mm-security-handle 'gnus-info "Corrupted")
597 ;; Convert <LF> to <CR><LF> in signed text. If --textmode is
598 ;; specified when signing, the conversion is not necessary.
599 (goto-char (point-min))
602 (unless (eq (char-before) ?\r)
606 (with-temp-file (setq signature-file (make-temp-file "pgg"))
607 (mm-insert-part signature))
608 (if (condition-case err
610 (pgg-verify-region (point-min) (point-max)
612 (goto-char (point-min))
613 (while (search-forward "\r\n" nil t)
614 (replace-match "\n" t t))
615 (mm-set-handle-multipart-parameter
616 mm-security-handle 'gnus-details
617 (concat (with-current-buffer pgg-output-buffer
619 (with-current-buffer pgg-errors-buffer
622 (mm-set-handle-multipart-parameter
623 mm-security-handle 'gnus-details (mml2015-format-error err))
626 (mm-set-handle-multipart-parameter
627 mm-security-handle 'gnus-details "Quit.")
630 (delete-file signature-file)
631 (mm-set-handle-multipart-parameter
632 mm-security-handle 'gnus-info
633 (with-current-buffer pgg-errors-buffer
634 (mml2015-gpg-extract-signature-details))))
635 (delete-file signature-file)
636 (mm-set-handle-multipart-parameter
637 mm-security-handle 'gnus-info "Failed")))))
640 (defun mml2015-pgg-clear-verify ()
641 (let ((pgg-errors-buffer mml2015-result-buffer)
642 (text (buffer-string))
643 (coding-system buffer-file-coding-system))
644 (if (condition-case err
646 (mm-with-unibyte-buffer
647 (insert (encode-coding-string text coding-system))
648 (pgg-verify-region (point-min) (point-max) nil t))
649 (goto-char (point-min))
650 (while (search-forward "\r\n" nil t)
651 (replace-match "\n" t t))
652 (mm-set-handle-multipart-parameter
653 mm-security-handle 'gnus-details
654 (concat (with-current-buffer pgg-output-buffer
656 (with-current-buffer pgg-errors-buffer
659 (mm-set-handle-multipart-parameter
660 mm-security-handle 'gnus-details (mml2015-format-error err))
663 (mm-set-handle-multipart-parameter
664 mm-security-handle 'gnus-details "Quit.")
666 (mm-set-handle-multipart-parameter
667 mm-security-handle 'gnus-info
668 (with-current-buffer pgg-errors-buffer
669 (mml2015-gpg-extract-signature-details)))
670 (mm-set-handle-multipart-parameter
671 mm-security-handle 'gnus-info "Failed")))
672 (mml2015-extract-cleartext-signature))
674 (defun mml2015-pgg-sign (cont)
675 (let ((pgg-errors-buffer mml2015-result-buffer)
676 (boundary (mml-compute-boundary cont))
677 (pgg-default-user-id (or (message-options-get 'mml-sender)
678 pgg-default-user-id))
681 (unless (pgg-sign-region (point-min) (point-max))
682 (pop-to-buffer mml2015-result-buffer)
683 (error "Sign error"))
684 (goto-char (point-min))
685 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
687 (if (setq entry (assq 2 (pgg-parse-armor
688 (with-current-buffer pgg-output-buffer
690 (setq entry (assq 'hash-algorithm (cdr entry))))
691 (insert (format "\tmicalg=%s; "
693 (downcase (format "pgp-%s" (cdr entry)))
695 (insert "protocol=\"application/pgp-signature\"\n")
696 (insert (format "\n--%s\n" boundary))
697 (goto-char (point-max))
698 (insert (format "\n--%s\n" boundary))
699 (insert "Content-Type: application/pgp-signature\n\n")
700 (insert-buffer-substring pgg-output-buffer)
701 (goto-char (point-max))
702 (insert (format "--%s--\n" boundary))
703 (goto-char (point-max))))
705 (defun mml2015-pgg-encrypt (cont &optional sign)
706 (let ((pgg-errors-buffer mml2015-result-buffer)
708 (boundary (mml-compute-boundary cont)))
709 (unless (pgg-encrypt-region (point-min) (point-max)
712 (message-options-get 'message-recipients)
713 (message-options-set 'message-recipients
714 (read-string "Recipients: ")))
717 (pop-to-buffer mml2015-result-buffer)
718 (error "Encrypt error"))
719 (delete-region (point-min) (point-max))
720 (goto-char (point-min))
721 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
723 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
724 (insert (format "--%s\n" boundary))
725 (insert "Content-Type: application/pgp-encrypted\n\n")
726 (insert "Version: 1\n\n")
727 (insert (format "--%s\n" boundary))
728 (insert "Content-Type: application/octet-stream\n\n")
729 (insert-buffer-substring pgg-output-buffer)
730 (goto-char (point-max))
731 (insert (format "--%s--\n" boundary))
732 (goto-char (point-max))))
736 (defvar epg-user-id-alist)
737 (defvar epg-digest-algorithm-alist)
738 (defvar epg-gpg-program)
739 (defvar inhibit-redisplay)
741 (autoload 'epg-make-context "epg")
742 (autoload 'epg-context-set-armor "epg")
743 (autoload 'epg-context-set-textmode "epg")
744 (autoload 'epg-context-set-signers "epg")
745 (autoload 'epg-context-result-for "epg")
746 (autoload 'epg-new-signature-digest-algorithm "epg")
747 (autoload 'epg-list-keys "epg")
748 (autoload 'epg-decrypt-string "epg")
749 (autoload 'epg-verify-string "epg")
750 (autoload 'epg-sign-string "epg")
751 (autoload 'epg-encrypt-string "epg")
752 (autoload 'epg-passphrase-callback-function "epg")
753 (autoload 'epg-context-set-passphrase-callback "epg")
754 (autoload 'epg-key-sub-key-list "epg")
755 (autoload 'epg-sub-key-capability "epg")
756 (autoload 'epg-sub-key-validity "epg")
757 (autoload 'epg-sub-key-fingerprint "epg")
758 (autoload 'epg-signature-key-id "epg")
759 (autoload 'epg-signature-to-string "epg")
760 (autoload 'epg-key-user-id-list "epg")
761 (autoload 'epg-user-id-string "epg")
762 (autoload 'epg-user-id-validity "epg")
763 (autoload 'epg-configuration "epg-config")
764 (autoload 'epg-expand-group "epg-config")
765 (autoload 'epa-select-keys "epa")
767 (defun mml2015-epg-key-image (key-id)
768 "Return the image of a key, if any"
770 (set-buffer-multibyte nil)
771 (let* ((coding-system-for-write 'binary)
772 (coding-system-for-read 'binary)
773 (data (shell-command-to-string
774 (format "%s --list-options no-show-photos --attribute-fd 3 --list-keys %s 3>&1 >/dev/null 2>&1"
775 (shell-quote-argument epg-gpg-program) key-id))))
776 (when (> (length data) 0)
777 (insert (substring data 16))
779 (gnus-create-image (buffer-string) nil t)
782 (autoload 'gnus-rescale-image "gnus-util")
784 (defun mml2015-epg-key-image-to-string (key-id)
785 "Return a string with the image of a key, if any"
786 (let ((key-image (mml2015-epg-key-image key-id)))
789 (condition-case error
793 (gnus-rescale-image key-image
794 (cons mml2015-maximum-key-image-dimension
795 mml2015-maximum-key-image-dimension))
800 (defun mml2015-epg-signature-to-string (signature)
801 (concat (epg-signature-to-string signature)
802 (when mml2015-display-key-image
803 (mml2015-epg-key-image-to-string (epg-signature-key-id signature)))))
805 (defun mml2015-epg-verify-result-to-string (verify-result)
806 (mapconcat #'mml2015-epg-signature-to-string verify-result "\n"))
808 (defun mml2015-epg-decrypt (handle ctl)
810 (let ((inhibit-redisplay t)
811 context plain child handles result decrypt-status)
812 (unless (setq child (mm-find-part-by-type
814 "application/octet-stream" nil t))
815 (mm-set-handle-multipart-parameter
816 mm-security-handle 'gnus-info "Corrupted")
817 (throw 'error handle))
818 (setq context (epg-make-context))
819 (if (or mml2015-cache-passphrase mml-secure-cache-passphrase)
820 (epg-context-set-passphrase-callback
822 (cons 'mml-secure-passphrase-callback 'OpenPGP)))
823 (condition-case error
824 (setq plain (epg-decrypt-string context (mm-get-part child))
825 mml-secure-secret-key-id-list nil)
827 (mml-secure-clear-secret-key-id-list)
828 (mm-set-handle-multipart-parameter
829 mm-security-handle 'gnus-info "Failed")
830 (if (eq (car error) 'quit)
831 (mm-set-handle-multipart-parameter
832 mm-security-handle 'gnus-details "Quit.")
833 (mm-set-handle-multipart-parameter
834 mm-security-handle 'gnus-details (mml2015-format-error error)))
835 (throw 'error handle)))
838 (goto-char (point-min))
839 (while (search-forward "\r\n" nil t)
840 (replace-match "\n" t t))
841 (setq handles (mm-dissect-buffer t))
842 (mm-destroy-parts handle)
843 (if (epg-context-result-for context 'verify)
844 (mm-set-handle-multipart-parameter
845 mm-security-handle 'gnus-info
847 (mml2015-epg-verify-result-to-string
848 (epg-context-result-for context 'verify))))
849 (mm-set-handle-multipart-parameter
850 mm-security-handle 'gnus-info "OK"))
851 (if (stringp (car handles))
852 (mm-set-handle-multipart-parameter
853 mm-security-handle 'gnus-details
854 (mm-handle-multipart-ctl-parameter handles 'gnus-details))))
855 (if (listp (car handles))
859 (defun mml2015-epg-clear-decrypt ()
860 (let ((inhibit-redisplay t)
861 (context (epg-make-context))
863 (if (or mml2015-cache-passphrase mml-secure-cache-passphrase)
864 (epg-context-set-passphrase-callback
866 (cons 'mml-secure-passphrase-callback 'OpenPGP)))
867 (condition-case error
868 (setq plain (epg-decrypt-string context (buffer-string))
869 mml-secure-secret-key-id-list nil)
871 (mml-secure-clear-secret-key-id-list)
872 (mm-set-handle-multipart-parameter
873 mm-security-handle 'gnus-info "Failed")
874 (if (eq (car error) 'quit)
875 (mm-set-handle-multipart-parameter
876 mm-security-handle 'gnus-details "Quit.")
877 (mm-set-handle-multipart-parameter
878 mm-security-handle 'gnus-details (mml2015-format-error error)))))
881 ;; Treat data which epg returns as a unibyte string.
882 (mm-disable-multibyte)
884 (goto-char (point-min))
885 (while (search-forward "\r\n" nil t)
886 (replace-match "\n" t t))
887 (mm-set-handle-multipart-parameter
888 mm-security-handle 'gnus-info "OK")
889 (if (epg-context-result-for context 'verify)
890 (mm-set-handle-multipart-parameter
891 mm-security-handle 'gnus-details
892 (mml2015-epg-verify-result-to-string
893 (epg-context-result-for context 'verify)))))))
895 (defun mml2015-epg-verify (handle ctl)
897 (let ((inhibit-redisplay t)
898 context plain signature-file part signature)
899 (when (or (null (setq part (mm-find-raw-part-by-type
900 ctl (or (mm-handle-multipart-ctl-parameter
902 "application/pgp-signature")
904 (null (setq signature (mm-find-part-by-type
905 (cdr handle) "application/pgp-signature"
907 (mm-set-handle-multipart-parameter
908 mm-security-handle 'gnus-info "Corrupted")
909 (throw 'error handle))
910 (setq part (replace-regexp-in-string "\n" "\r\n" part)
911 signature (mm-get-part signature)
912 context (epg-make-context))
913 (condition-case error
914 (setq plain (epg-verify-string context signature part))
916 (mm-set-handle-multipart-parameter
917 mm-security-handle 'gnus-info "Failed")
918 (if (eq (car error) 'quit)
919 (mm-set-handle-multipart-parameter
920 mm-security-handle 'gnus-details "Quit.")
921 (mm-set-handle-multipart-parameter
922 mm-security-handle 'gnus-details (mml2015-format-error error)))
923 (throw 'error handle)))
924 (mm-set-handle-multipart-parameter
925 mm-security-handle 'gnus-info
926 (mml2015-epg-verify-result-to-string
927 (epg-context-result-for context 'verify)))
930 (defun mml2015-epg-clear-verify ()
931 (let ((inhibit-redisplay t)
932 (context (epg-make-context))
933 (signature (encode-coding-string (buffer-string)
934 coding-system-for-write))
936 (condition-case error
937 (setq plain (epg-verify-string context signature))
939 (mm-set-handle-multipart-parameter
940 mm-security-handle 'gnus-info "Failed")
941 (if (eq (car error) 'quit)
942 (mm-set-handle-multipart-parameter
943 mm-security-handle 'gnus-details "Quit.")
944 (mm-set-handle-multipart-parameter
945 mm-security-handle 'gnus-details (mml2015-format-error error)))))
948 (mm-set-handle-multipart-parameter
949 mm-security-handle 'gnus-info
950 (mml2015-epg-verify-result-to-string
951 (epg-context-result-for context 'verify)))
952 (delete-region (point-min) (point-max))
953 (insert (decode-coding-string plain coding-system-for-read)))
954 (mml2015-extract-cleartext-signature))))
956 (defun mml2015-epg-sign (cont)
957 (let ((inhibit-redisplay t)
958 (boundary (mml-compute-boundary cont)))
959 ;; Signed data must end with a newline (RFC 3156, 5).
960 (goto-char (point-max))
963 (let* ((pair (mml-secure-epg-sign 'OpenPGP t))
964 (signature (car pair))
966 (goto-char (point-min))
967 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
970 (insert (format "\tmicalg=pgp-%s; "
973 epg-digest-algorithm-alist))))))
974 (insert "protocol=\"application/pgp-signature\"\n")
975 (insert (format "\n--%s\n" boundary))
976 (goto-char (point-max))
977 (insert (format "\n--%s\n" boundary))
978 (insert "Content-Type: application/pgp-signature; name=\"signature.asc\"\n\n")
980 (goto-char (point-max))
981 (insert (format "--%s--\n" boundary))
982 (goto-char (point-max)))))
984 (defun mml2015-epg-encrypt (cont &optional sign)
985 (let* ((inhibit-redisplay t)
986 (boundary (mml-compute-boundary cont))
987 (cipher (mml-secure-epg-encrypt 'OpenPGP cont sign)))
988 (delete-region (point-min) (point-max))
989 (goto-char (point-min))
990 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
992 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
993 (insert (format "--%s\n" boundary))
994 (insert "Content-Type: application/pgp-encrypted\n\n")
995 (insert "Version: 1\n\n")
996 (insert (format "--%s\n" boundary))
997 (insert "Content-Type: application/octet-stream\n\n")
999 (goto-char (point-max))
1000 (insert (format "--%s--\n" boundary))
1001 (goto-char (point-max))))
1005 (autoload 'gnus-buffer-live-p "gnus-util")
1006 (autoload 'gnus-get-buffer-create "gnus")
1008 (defun mml2015-clean-buffer ()
1009 (if (gnus-buffer-live-p mml2015-result-buffer)
1010 (with-current-buffer mml2015-result-buffer
1013 (setq mml2015-result-buffer
1014 (gnus-get-buffer-create " *MML2015 Result*"))
1017 (defsubst mml2015-clear-decrypt-function ()
1018 (nth 6 (assq mml2015-use mml2015-function-alist)))
1020 (defsubst mml2015-clear-verify-function ()
1021 (nth 5 (assq mml2015-use mml2015-function-alist)))
1024 (defun mml2015-decrypt (handle ctl)
1025 (mml2015-clean-buffer)
1026 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
1028 (funcall func handle ctl)
1032 (defun mml2015-decrypt-test (handle ctl)
1036 (defun mml2015-verify (handle ctl)
1037 (mml2015-clean-buffer)
1038 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
1040 (funcall func handle ctl)
1044 (defun mml2015-verify-test (handle ctl)
1048 (defun mml2015-encrypt (cont &optional sign)
1049 (mml2015-clean-buffer)
1050 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
1052 (funcall func cont sign)
1053 (error "Cannot find encrypt function"))))
1056 (defun mml2015-sign (cont)
1057 (mml2015-clean-buffer)
1058 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
1061 (error "Cannot find sign function"))))
1064 (defun mml2015-self-encrypt ()
1065 (mml2015-encrypt nil))
1069 ;;; mml2015.el ends here