1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000-2015 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 ;; For Emacs <22.2 and XEmacs.
32 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
)))
34 (if (locate-library "password-cache")
35 (require 'password-cache
)
38 (eval-when-compile (require 'cl
))
44 (defvar mc-pgp-always-sign
)
46 (declare-function epg-check-configuration
"ext:epg-config"
47 (config &optional minimum-version
))
48 (declare-function epg-configuration
"ext:epg-config" ())
50 ;; Maybe this should be in eg mml-sec.el (and have a different name).
51 ;; Then mml1991 would not need to require mml2015, and mml1991-use
53 (defvar mml2015-use
(or
57 (epg-check-configuration (epg-configuration))
61 (let ((abs-file (locate-library "pgg")))
62 ;; Don't load PGG if it is marked as obsolete
65 (not (string-match "/obsolete/[^/]*\\'"
67 (ignore-errors (require 'pgg
))
68 (and (fboundp 'pgg-sign-region
)
72 (and (fboundp 'mc-encrypt-generic
)
73 (fboundp 'mc-sign-generic
)
74 (fboundp 'mc-cleanup-recipient-headers
)
76 "The package used for PGP/MIME.
77 Valid packages include `epg', `pgg' and `mailcrypt'.")
79 ;; Something is not RFC2015.
80 (defvar mml2015-function-alist
81 '((mailcrypt mml2015-mailcrypt-sign
82 mml2015-mailcrypt-encrypt
83 mml2015-mailcrypt-verify
84 mml2015-mailcrypt-decrypt
85 mml2015-mailcrypt-clear-verify
86 mml2015-mailcrypt-clear-decrypt
)
91 mml2015-pgg-clear-verify
92 mml2015-pgg-clear-decrypt
)
97 mml2015-epg-clear-verify
98 mml2015-epg-clear-decrypt
))
99 "Alist of PGP/MIME functions.")
101 (defvar mml2015-result-buffer nil
)
103 (defcustom mml2015-unabbrev-trust-alist
104 '(("TRUST_UNDEFINED" . nil
)
105 ("TRUST_NEVER" . nil
)
106 ("TRUST_MARGINAL" . t
)
108 ("TRUST_ULTIMATE" . t
))
109 "Map GnuPG trust output values to a boolean saying if you trust the key."
111 :group
'mime-security
112 :type
'(repeat (cons (regexp :tag
"GnuPG output regexp")
113 (boolean :tag
"Trust key"))))
115 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
116 "If t, cache passphrase."
117 :group
'mime-security
120 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
121 "How many seconds the passphrase is cached.
122 Whether the passphrase is cached at all is controlled by
123 `mml2015-cache-passphrase'."
124 :group
'mime-security
127 (defcustom mml2015-signers nil
128 "A list of your own key ID(s) which will be used to sign a message.
129 If set, it overrides the setting of `mml2015-sign-with-sender'."
130 :group
'mime-security
131 :type
'(repeat (string :tag
"Key ID")))
133 (defcustom mml2015-sign-with-sender nil
134 "If t, use message sender so find a key to sign with."
135 :group
'mime-security
139 (defcustom mml2015-encrypt-to-self nil
140 "If t, add your own key ID to recipient list when encryption."
141 :group
'mime-security
144 (defcustom mml2015-always-trust t
145 "If t, GnuPG skip key validation on encryption."
146 :group
'mime-security
149 (defcustom mml2015-maximum-key-image-dimension
64
150 "The maximum dimension (width or height) of any key images."
152 :group
'mime-security
155 (defcustom mml2015-display-key-image t
156 "If t, try to display key images."
158 :group
'mime-security
161 ;; Extract plaintext from cleartext signature. IMO, this kind of task
162 ;; should be done by GnuPG rather than Elisp, but older PGP backends
163 ;; (such as Mailcrypt, and PGG) discard the output from GnuPG.
164 (defun mml2015-extract-cleartext-signature ()
166 ;; <54a15d860801080142l70b95d7dkac4bf51a86196011@mail.gmail.com>: ``I still
167 ;; believe that the right way is to use the plaintext output from GnuPG as
168 ;; it is, and mml2015-extract-cleartext-signature is just a kludge for
169 ;; misdesigned libraries like PGG, which have no ability to do that. So, I
170 ;; think it should not have descriptive documentation.''
172 ;; This function doesn't handle NotDashEscaped correctly. EasyPG handles it
174 ;; http://thread.gmane.org/gmane.emacs.gnus.general/66062/focus=66082
175 ;; http://thread.gmane.org/gmane.emacs.gnus.general/65087/focus=65109
176 (goto-char (point-min))
178 ;; We need to be careful not to strip beyond the armor headers.
179 ;; Previously, an attacker could replace the text inside our
180 ;; markup with trailing garbage by injecting whitespace into the
182 (while (looking-at "Hash:") ; The only header allowed in cleartext
183 (forward-line)) ; signatures according to RFC2440.
184 (when (looking-at "[\t ]*$")
186 (delete-region (point-min) (point))
187 (if (re-search-forward "^-----BEGIN PGP SIGNATURE-----" nil t
)
188 (delete-region (match-beginning 0) (point-max)))
189 (goto-char (point-min))
190 (while (re-search-forward "^- " nil t
)
191 (replace-match "" t t
)
194 ;;; mailcrypt wrapper
196 (autoload 'mailcrypt-decrypt
"mailcrypt")
197 (autoload 'mailcrypt-verify
"mailcrypt")
198 (autoload 'mc-pgp-always-sign
"mailcrypt")
199 (autoload 'mc-encrypt-generic
"mc-toplev")
200 (autoload 'mc-cleanup-recipient-headers
"mc-toplev")
201 (autoload 'mc-sign-generic
"mc-toplev")
203 (defvar mml2015-decrypt-function
'mailcrypt-decrypt
)
204 (defvar mml2015-verify-function
'mailcrypt-verify
)
206 (defun mml2015-format-error (err)
207 (if (stringp (cadr err
))
209 (format "%S" (cdr err
))))
211 (defun mml2015-mailcrypt-decrypt (handle ctl
)
213 (let (child handles result
)
214 (unless (setq child
(mm-find-part-by-type
216 "application/octet-stream" nil t
))
217 (mm-set-handle-multipart-parameter
218 mm-security-handle
'gnus-info
"Corrupted")
219 (throw 'error handle
))
221 (mm-insert-part child
)
224 (funcall mml2015-decrypt-function
)
226 (mm-set-handle-multipart-parameter
227 mm-security-handle
'gnus-details
(mml2015-format-error err
))
230 (mm-set-handle-multipart-parameter
231 mm-security-handle
'gnus-details
"Quit.")
234 (mm-set-handle-multipart-parameter
235 mm-security-handle
'gnus-info
"Failed")
236 (throw 'error handle
))
237 (setq handles
(mm-dissect-buffer t
)))
238 (mm-destroy-parts handle
)
239 (mm-set-handle-multipart-parameter
240 mm-security-handle
'gnus-info
242 (let ((sig (with-current-buffer mml2015-result-buffer
243 (mml2015-gpg-extract-signature-details))))
244 (concat ", Signer: " sig
))))
245 (if (listp (car handles
))
249 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
251 (fpr-length (string-width fingerprint
))
254 (setq fingerprint
(string-to-list fingerprint
))
256 (setq fpr-length
(- fpr-length
4))
257 (setq slice
(butlast fingerprint fpr-length
))
258 (setq fingerprint
(nthcdr 4 fingerprint
))
259 (setq n-slice
(1+ n-slice
))
265 (otherwise (concat " " slice
))))))
268 (defun mml2015-gpg-extract-signature-details ()
269 (goto-char (point-min))
270 (let* ((expired (re-search-forward
271 "^\\[GNUPG:\\] SIGEXPIRED$"
273 (signer (and (re-search-forward
274 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
276 (cons (match-string 1) (match-string 2))))
277 (fprint (and (re-search-forward
278 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
281 (trust (and (re-search-forward
282 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
286 (cdr (assoc trust mml2015-unabbrev-trust-alist
))))
287 (cond ((and signer fprint
)
289 (unless trust-good-enough-p
290 (concat "\nUntrusted, Fingerprint: "
291 (mml2015-gpg-pretty-print-fpr fprint
)))
293 (format "\nWARNING: Signature from expired key (%s)"
296 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t
)
299 "From unknown user"))))
301 (defun mml2015-mailcrypt-clear-decrypt ()
305 (funcall mml2015-decrypt-function
)
307 (mm-set-handle-multipart-parameter
308 mm-security-handle
'gnus-details
(mml2015-format-error err
))
311 (mm-set-handle-multipart-parameter
312 mm-security-handle
'gnus-details
"Quit.")
315 (mm-set-handle-multipart-parameter
316 mm-security-handle
'gnus-info
"OK")
317 (mm-set-handle-multipart-parameter
318 mm-security-handle
'gnus-info
"Failed"))))
320 (defun mml2015-fix-micalg (alg)
322 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
323 (upcase (if (string-match "^p[gh]p-" alg
)
324 (substring alg
(match-end 0))
327 (defun mml2015-mailcrypt-verify (handle ctl
)
330 (unless (setq part
(mm-find-raw-part-by-type
331 ctl
(or (mm-handle-multipart-ctl-parameter
333 "application/pgp-signature")
335 (mm-set-handle-multipart-parameter
336 mm-security-handle
'gnus-info
"Corrupted")
337 (throw 'error handle
))
339 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
340 (insert (format "Hash: %s\n\n"
341 (or (mml2015-fix-micalg
342 (mm-handle-multipart-ctl-parameter
346 (narrow-to-region (point) (point))
348 (goto-char (point-min))
350 (if (looking-at "^-")
353 (unless (setq part
(mm-find-part-by-type
354 (cdr handle
) "application/pgp-signature" nil t
))
355 (mm-set-handle-multipart-parameter
356 mm-security-handle
'gnus-info
"Corrupted")
357 (throw 'error handle
))
359 (narrow-to-region (point) (point))
360 (mm-insert-part part
)
361 (goto-char (point-min))
362 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t
)
363 (replace-match "-----BEGIN PGP SIGNATURE-----" t t
))
364 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t
)
365 (replace-match "-----END PGP SIGNATURE-----" t t
)))
366 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
367 (unless (condition-case err
369 (funcall mml2015-verify-function
)
370 (if (get-buffer " *mailcrypt stderr temp")
371 (mm-set-handle-multipart-parameter
372 mm-security-handle
'gnus-details
373 (with-current-buffer " *mailcrypt stderr temp"
375 (if (get-buffer " *mailcrypt stdout temp")
376 (kill-buffer " *mailcrypt stdout temp"))
377 (if (get-buffer " *mailcrypt stderr temp")
378 (kill-buffer " *mailcrypt stderr temp"))
379 (if (get-buffer " *mailcrypt status temp")
380 (kill-buffer " *mailcrypt status temp"))
381 (if (get-buffer mc-gpg-debug-buffer
)
382 (kill-buffer mc-gpg-debug-buffer
)))
384 (mm-set-handle-multipart-parameter
385 mm-security-handle
'gnus-details
(mml2015-format-error err
))
388 (mm-set-handle-multipart-parameter
389 mm-security-handle
'gnus-details
"Quit.")
391 (mm-set-handle-multipart-parameter
392 mm-security-handle
'gnus-info
"Failed")
393 (throw 'error handle
))))
394 (mm-set-handle-multipart-parameter
395 mm-security-handle
'gnus-info
"OK")
398 (defun mml2015-mailcrypt-clear-verify ()
399 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
400 (if (condition-case err
402 (funcall mml2015-verify-function
)
403 (if (get-buffer " *mailcrypt stderr temp")
404 (mm-set-handle-multipart-parameter
405 mm-security-handle
'gnus-details
406 (with-current-buffer " *mailcrypt stderr temp"
408 (if (get-buffer " *mailcrypt stdout temp")
409 (kill-buffer " *mailcrypt stdout temp"))
410 (if (get-buffer " *mailcrypt stderr temp")
411 (kill-buffer " *mailcrypt stderr temp"))
412 (if (get-buffer " *mailcrypt status temp")
413 (kill-buffer " *mailcrypt status temp"))
414 (if (get-buffer mc-gpg-debug-buffer
)
415 (kill-buffer mc-gpg-debug-buffer
)))
417 (mm-set-handle-multipart-parameter
418 mm-security-handle
'gnus-details
(mml2015-format-error err
))
421 (mm-set-handle-multipart-parameter
422 mm-security-handle
'gnus-details
"Quit.")
424 (mm-set-handle-multipart-parameter
425 mm-security-handle
'gnus-info
"OK")
426 (mm-set-handle-multipart-parameter
427 mm-security-handle
'gnus-info
"Failed")))
428 (mml2015-extract-cleartext-signature))
430 (defun mml2015-mailcrypt-sign (cont)
431 (mc-sign-generic (message-options-get 'message-sender
)
433 (let ((boundary (mml-compute-boundary cont
))
435 (goto-char (point-min))
436 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t
)
437 (error "Cannot find signed begin line"))
438 (goto-char (match-beginning 0))
440 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
441 (error "Cannot not find PGP hash"))
442 (setq hash
(match-string 1))
443 (unless (re-search-forward "^$" nil t
)
444 (error "Cannot not find PGP message"))
446 (delete-region (point-min) (point))
447 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
449 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
451 (insert (format "\n--%s\n" boundary
))
453 (goto-char (point-max))
454 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t
)
455 (error "Cannot find signature part"))
456 (replace-match "-----END PGP MESSAGE-----" t t
)
457 (goto-char (match-beginning 0))
458 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
460 (error "Cannot find signature part"))
461 (replace-match "-----BEGIN PGP MESSAGE-----" t t
)
462 (goto-char (match-beginning 0))
464 (narrow-to-region point
(point))
466 (while (re-search-forward "^- -" nil t
)
467 (replace-match "-" t t
))
468 (goto-char (point-max)))
469 (insert (format "--%s\n" boundary
))
470 (insert "Content-Type: application/pgp-signature\n\n")
471 (goto-char (point-max))
472 (insert (format "--%s--\n" boundary
))
473 (goto-char (point-max))))
475 ;; We require mm-decode, which requires mm-bodies, which autoloads
476 ;; message-options-get (!).
477 (declare-function message-options-set
"message" (symbol value
))
479 (defun mml2015-mailcrypt-encrypt (cont &optional sign
)
480 (let ((mc-pgp-always-sign
481 (or mc-pgp-always-sign
483 (eq t
(or (message-options-get 'message-sign-encrypt
)
485 'message-sign-encrypt
486 (or (y-or-n-p "Sign the message? ")
489 (mm-with-unibyte-current-buffer
491 (or (message-options-get 'message-recipients
)
492 (message-options-set 'message-recipients
493 (mc-cleanup-recipient-headers
494 (read-string "Recipients: "))))
496 (message-options-get 'message-sender
))))
497 (goto-char (point-min))
498 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
499 (error "Fail to encrypt the message"))
500 (let ((boundary (mml-compute-boundary cont
)))
501 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
503 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
504 (insert (format "--%s\n" boundary
))
505 (insert "Content-Type: application/pgp-encrypted\n\n")
506 (insert "Version: 1\n\n")
507 (insert (format "--%s\n" boundary
))
508 (insert "Content-Type: application/octet-stream\n\n")
509 (goto-char (point-max))
510 (insert (format "--%s--\n" boundary
))
511 (goto-char (point-max))))
515 (defvar pgg-default-user-id
)
516 (defvar pgg-errors-buffer
)
517 (defvar pgg-output-buffer
)
519 (autoload 'pgg-decrypt-region
"pgg")
520 (autoload 'pgg-verify-region
"pgg")
521 (autoload 'pgg-sign-region
"pgg")
522 (autoload 'pgg-encrypt-region
"pgg")
523 (autoload 'pgg-parse-armor
"pgg-parse")
525 (defun mml2015-pgg-decrypt (handle ctl
)
527 (let ((pgg-errors-buffer mml2015-result-buffer
)
528 child handles result decrypt-status
)
529 (unless (setq child
(mm-find-part-by-type
531 "application/octet-stream" nil t
))
532 (mm-set-handle-multipart-parameter
533 mm-security-handle
'gnus-info
"Corrupted")
534 (throw 'error handle
))
536 (mm-insert-part child
)
537 (if (condition-case err
539 (pgg-decrypt-region (point-min) (point-max))
541 (with-current-buffer mml2015-result-buffer
543 (mm-set-handle-multipart-parameter
544 mm-security-handle
'gnus-details
547 (mm-set-handle-multipart-parameter
548 mm-security-handle
'gnus-details
(mml2015-format-error err
))
551 (mm-set-handle-multipart-parameter
552 mm-security-handle
'gnus-details
"Quit.")
554 (with-current-buffer pgg-output-buffer
555 (goto-char (point-min))
556 (while (search-forward "\r\n" nil t
)
557 (replace-match "\n" t t
))
558 (setq handles
(mm-dissect-buffer t
))
559 (mm-destroy-parts handle
)
560 (mm-set-handle-multipart-parameter
561 mm-security-handle
'gnus-info
"OK")
562 (mm-set-handle-multipart-parameter
563 mm-security-handle
'gnus-details
564 (concat decrypt-status
565 (when (stringp (car handles
))
566 "\n" (mm-handle-multipart-ctl-parameter
567 handles
'gnus-details
))))
568 (if (listp (car handles
))
571 (mm-set-handle-multipart-parameter
572 mm-security-handle
'gnus-info
"Failed")
573 (throw 'error handle
))))))
575 (defun mml2015-pgg-clear-decrypt ()
576 (let ((pgg-errors-buffer mml2015-result-buffer
))
578 (pgg-decrypt-region (point-min) (point-max))
579 (mm-set-handle-multipart-parameter
580 mm-security-handle
'gnus-details
581 (with-current-buffer mml2015-result-buffer
585 ;; Treat data which pgg returns as a unibyte string.
586 (mm-disable-multibyte)
587 (insert-buffer-substring pgg-output-buffer
)
588 (goto-char (point-min))
589 (while (search-forward "\r\n" nil t
)
590 (replace-match "\n" t t
))
591 (mm-set-handle-multipart-parameter
592 mm-security-handle
'gnus-info
"OK"))
593 (mm-set-handle-multipart-parameter
594 mm-security-handle
'gnus-info
"Failed"))))
596 (defun mml2015-pgg-verify (handle ctl
)
597 (let ((pgg-errors-buffer mml2015-result-buffer
)
598 signature-file part signature
)
599 (if (or (null (setq part
(mm-find-raw-part-by-type
600 ctl
(or (mm-handle-multipart-ctl-parameter
602 "application/pgp-signature")
604 (null (setq signature
(mm-find-part-by-type
605 (cdr handle
) "application/pgp-signature" nil t
))))
607 (mm-set-handle-multipart-parameter
608 mm-security-handle
'gnus-info
"Corrupted")
612 ;; Convert <LF> to <CR><LF> in signed text. If --textmode is
613 ;; specified when signing, the conversion is not necessary.
614 (goto-char (point-min))
617 (unless (eq (char-before) ?
\r)
621 (with-temp-file (setq signature-file
(mm-make-temp-file "pgg"))
622 (mm-insert-part signature
))
623 (if (condition-case err
625 (pgg-verify-region (point-min) (point-max)
627 (goto-char (point-min))
628 (while (search-forward "\r\n" nil t
)
629 (replace-match "\n" t t
))
630 (mm-set-handle-multipart-parameter
631 mm-security-handle
'gnus-details
632 (concat (with-current-buffer pgg-output-buffer
634 (with-current-buffer pgg-errors-buffer
637 (mm-set-handle-multipart-parameter
638 mm-security-handle
'gnus-details
(mml2015-format-error err
))
641 (mm-set-handle-multipart-parameter
642 mm-security-handle
'gnus-details
"Quit.")
645 (delete-file signature-file
)
646 (mm-set-handle-multipart-parameter
647 mm-security-handle
'gnus-info
648 (with-current-buffer pgg-errors-buffer
649 (mml2015-gpg-extract-signature-details))))
650 (delete-file signature-file
)
651 (mm-set-handle-multipart-parameter
652 mm-security-handle
'gnus-info
"Failed")))))
655 (defun mml2015-pgg-clear-verify ()
656 (let ((pgg-errors-buffer mml2015-result-buffer
)
657 (text (buffer-string))
658 (coding-system buffer-file-coding-system
))
659 (if (condition-case err
661 (mm-with-unibyte-buffer
662 (insert (mm-encode-coding-string text coding-system
))
663 (pgg-verify-region (point-min) (point-max) nil t
))
664 (goto-char (point-min))
665 (while (search-forward "\r\n" nil t
)
666 (replace-match "\n" t t
))
667 (mm-set-handle-multipart-parameter
668 mm-security-handle
'gnus-details
669 (concat (with-current-buffer pgg-output-buffer
671 (with-current-buffer pgg-errors-buffer
674 (mm-set-handle-multipart-parameter
675 mm-security-handle
'gnus-details
(mml2015-format-error err
))
678 (mm-set-handle-multipart-parameter
679 mm-security-handle
'gnus-details
"Quit.")
681 (mm-set-handle-multipart-parameter
682 mm-security-handle
'gnus-info
683 (with-current-buffer pgg-errors-buffer
684 (mml2015-gpg-extract-signature-details)))
685 (mm-set-handle-multipart-parameter
686 mm-security-handle
'gnus-info
"Failed")))
687 (mml2015-extract-cleartext-signature))
689 (defun mml2015-pgg-sign (cont)
690 (let ((pgg-errors-buffer mml2015-result-buffer
)
691 (boundary (mml-compute-boundary cont
))
692 (pgg-default-user-id (or (message-options-get 'mml-sender
)
693 pgg-default-user-id
))
696 (unless (pgg-sign-region (point-min) (point-max))
697 (pop-to-buffer mml2015-result-buffer
)
698 (error "Sign error"))
699 (goto-char (point-min))
700 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
702 (if (setq entry
(assq 2 (pgg-parse-armor
703 (with-current-buffer pgg-output-buffer
705 (setq entry
(assq 'hash-algorithm
(cdr entry
))))
706 (insert (format "\tmicalg=%s; "
708 (downcase (format "pgp-%s" (cdr entry
)))
710 (insert "protocol=\"application/pgp-signature\"\n")
711 (insert (format "\n--%s\n" boundary
))
712 (goto-char (point-max))
713 (insert (format "\n--%s\n" boundary
))
714 (insert "Content-Type: application/pgp-signature\n\n")
715 (insert-buffer-substring pgg-output-buffer
)
716 (goto-char (point-max))
717 (insert (format "--%s--\n" boundary
))
718 (goto-char (point-max))))
720 (defun mml2015-pgg-encrypt (cont &optional sign
)
721 (let ((pgg-errors-buffer mml2015-result-buffer
)
723 (boundary (mml-compute-boundary cont
)))
724 (unless (pgg-encrypt-region (point-min) (point-max)
727 (message-options-get 'message-recipients
)
728 (message-options-set 'message-recipients
729 (read-string "Recipients: ")))
732 (pop-to-buffer mml2015-result-buffer
)
733 (error "Encrypt error"))
734 (delete-region (point-min) (point-max))
735 (goto-char (point-min))
736 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
738 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
739 (insert (format "--%s\n" boundary
))
740 (insert "Content-Type: application/pgp-encrypted\n\n")
741 (insert "Version: 1\n\n")
742 (insert (format "--%s\n" boundary
))
743 (insert "Content-Type: application/octet-stream\n\n")
744 (insert-buffer-substring pgg-output-buffer
)
745 (goto-char (point-max))
746 (insert (format "--%s--\n" boundary
))
747 (goto-char (point-max))))
751 (defvar epg-user-id-alist
)
752 (defvar epg-digest-algorithm-alist
)
753 (defvar epg-gpg-program
)
754 (defvar inhibit-redisplay
)
756 (autoload 'epg-make-context
"epg")
757 (autoload 'epg-context-set-armor
"epg")
758 (autoload 'epg-context-set-textmode
"epg")
759 (autoload 'epg-context-set-signers
"epg")
760 (autoload 'epg-context-result-for
"epg")
761 (autoload 'epg-new-signature-digest-algorithm
"epg")
762 (autoload 'epg-list-keys
"epg")
763 (autoload 'epg-decrypt-string
"epg")
764 (autoload 'epg-verify-string
"epg")
765 (autoload 'epg-sign-string
"epg")
766 (autoload 'epg-encrypt-string
"epg")
767 (autoload 'epg-passphrase-callback-function
"epg")
768 (autoload 'epg-context-set-passphrase-callback
"epg")
769 (autoload 'epg-key-sub-key-list
"epg")
770 (autoload 'epg-sub-key-capability
"epg")
771 (autoload 'epg-sub-key-validity
"epg")
772 (autoload 'epg-sub-key-fingerprint
"epg")
773 (autoload 'epg-signature-key-id
"epg")
774 (autoload 'epg-signature-to-string
"epg")
775 (autoload 'epg-key-user-id-list
"epg")
776 (autoload 'epg-user-id-string
"epg")
777 (autoload 'epg-user-id-validity
"epg")
778 (autoload 'epg-configuration
"epg-config")
779 (autoload 'epg-expand-group
"epg-config")
780 (autoload 'epa-select-keys
"epa")
782 (defvar mml2015-epg-secret-key-id-list nil
)
784 (defun mml2015-epg-passphrase-callback (context key-id ignore
)
786 (epg-passphrase-callback-function context key-id nil
)
787 (let* ((password-cache-key-id
795 "Passphrase for PIN: "
796 (if (setq entry
(assoc key-id epg-user-id-alist
))
797 (format "Passphrase for %s %s: " key-id
(cdr entry
))
798 (format "Passphrase for %s: " key-id
)))
799 password-cache-key-id
)))
801 (let ((password-cache-expiry mml2015-passphrase-cache-expiry
))
802 (password-cache-add password-cache-key-id passphrase
))
803 (setq mml2015-epg-secret-key-id-list
804 (cons password-cache-key-id mml2015-epg-secret-key-id-list
))
805 (copy-sequence passphrase
)))))
807 (defun mml2015-epg-check-user-id (key recipient
)
808 (let ((pointer (epg-key-user-id-list key
))
811 (if (and (equal (car (mail-header-parse-address
812 (epg-user-id-string (car pointer
))))
813 (car (mail-header-parse-address
815 (not (memq (epg-user-id-validity (car pointer
))
816 '(revoked expired
))))
819 (setq pointer
(cdr pointer
))))
822 (defun mml2015-epg-check-sub-key (key usage
)
823 (let ((pointer (epg-key-sub-key-list key
))
825 ;; The primary key will be marked as disabled, when the entire
826 ;; key is disabled (see 12 Field, Format of colon listings, in
827 ;; gnupg/doc/DETAILS)
828 (unless (memq 'disabled
(epg-sub-key-capability (car pointer
)))
830 (if (and (memq usage
(epg-sub-key-capability (car pointer
)))
831 (not (memq (epg-sub-key-validity (car pointer
))
832 '(revoked expired
))))
835 (setq pointer
(cdr pointer
)))))
838 (defun mml2015-epg-find-usable-key (context name usage
839 &optional name-is-key-id
)
840 (let ((keys (epg-list-keys context name
))
843 (if (and (or name-is-key-id
844 ;; Non email user-id can be supplied through
845 ;; mml2015-signers if mml2015-encrypt-to-self is set.
846 ;; Treat it as valid, as it is user's intention.
847 (not (string-match "\\`<" name
))
848 (mml2015-epg-check-user-id (car keys
) name
))
849 (mml2015-epg-check-sub-key (car keys
) usage
))
852 (setq keys
(cdr keys
))))
855 ;; XXX: since gpg --list-secret-keys does not return validity of each
856 ;; key, `mml2015-epg-find-usable-key' defined above is not enough for
857 ;; secret keys. The function `mml2015-epg-find-usable-secret-key'
858 ;; below looks at appropriate public keys to check usability.
859 (defun mml2015-epg-find-usable-secret-key (context name usage
)
860 (let ((secret-keys (epg-list-keys context name t
))
862 (while (and (not secret-key
) secret-keys
)
863 (if (mml2015-epg-find-usable-key
865 (epg-sub-key-fingerprint
866 (car (epg-key-sub-key-list
870 (setq secret-key
(car secret-keys
)
872 (setq secret-keys
(cdr secret-keys
))))
875 (autoload 'gnus-create-image
"gnus-ems")
877 (defun mml2015-epg-key-image (key-id)
878 "Return the image of a key, if any"
880 (mm-set-buffer-multibyte nil
)
881 (let* ((coding-system-for-write 'binary
)
882 (coding-system-for-read 'binary
)
883 (data (shell-command-to-string
884 (format "%s --list-options no-show-photos --attribute-fd 3 --list-keys %s 3>&1 >/dev/null 2>&1"
885 (shell-quote-argument epg-gpg-program
) key-id
))))
886 (when (> (length data
) 0)
887 (insert (substring data
16))
889 (gnus-create-image (buffer-string) nil t
)
892 (autoload 'gnus-rescale-image
"gnus-util")
894 (defun mml2015-epg-key-image-to-string (key-id)
895 "Return a string with the image of a key, if any"
896 (let ((key-image (mml2015-epg-key-image key-id
)))
899 (condition-case error
903 (gnus-rescale-image key-image
904 (cons mml2015-maximum-key-image-dimension
905 mml2015-maximum-key-image-dimension
))
910 (defun mml2015-epg-signature-to-string (signature)
911 (concat (epg-signature-to-string signature
)
912 (when mml2015-display-key-image
913 (mml2015-epg-key-image-to-string (epg-signature-key-id signature
)))))
915 (defun mml2015-epg-verify-result-to-string (verify-result)
916 (mapconcat #'mml2015-epg-signature-to-string verify-result
"\n"))
918 (defun mml2015-epg-decrypt (handle ctl
)
920 (let ((inhibit-redisplay t
)
921 context plain child handles result decrypt-status
)
922 (unless (setq child
(mm-find-part-by-type
924 "application/octet-stream" nil t
))
925 (mm-set-handle-multipart-parameter
926 mm-security-handle
'gnus-info
"Corrupted")
927 (throw 'error handle
))
928 (setq context
(epg-make-context))
929 (if mml2015-cache-passphrase
930 (epg-context-set-passphrase-callback
932 #'mml2015-epg-passphrase-callback
))
933 (condition-case error
934 (setq plain
(epg-decrypt-string context
(mm-get-part child
))
935 mml2015-epg-secret-key-id-list nil
)
937 (while mml2015-epg-secret-key-id-list
938 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
939 (setq mml2015-epg-secret-key-id-list
940 (cdr mml2015-epg-secret-key-id-list
)))
941 (mm-set-handle-multipart-parameter
942 mm-security-handle
'gnus-info
"Failed")
943 (if (eq (car error
) 'quit
)
944 (mm-set-handle-multipart-parameter
945 mm-security-handle
'gnus-details
"Quit.")
946 (mm-set-handle-multipart-parameter
947 mm-security-handle
'gnus-details
(mml2015-format-error error
)))
948 (throw 'error handle
)))
951 (goto-char (point-min))
952 (while (search-forward "\r\n" nil t
)
953 (replace-match "\n" t t
))
954 (setq handles
(mm-dissect-buffer t
))
955 (mm-destroy-parts handle
)
956 (if (epg-context-result-for context
'verify
)
957 (mm-set-handle-multipart-parameter
958 mm-security-handle
'gnus-info
960 (mml2015-epg-verify-result-to-string
961 (epg-context-result-for context
'verify
))))
962 (mm-set-handle-multipart-parameter
963 mm-security-handle
'gnus-info
"OK"))
964 (if (stringp (car handles
))
965 (mm-set-handle-multipart-parameter
966 mm-security-handle
'gnus-details
967 (mm-handle-multipart-ctl-parameter handles
'gnus-details
))))
968 (if (listp (car handles
))
972 (defun mml2015-epg-clear-decrypt ()
973 (let ((inhibit-redisplay t
)
974 (context (epg-make-context))
976 (if mml2015-cache-passphrase
977 (epg-context-set-passphrase-callback
979 #'mml2015-epg-passphrase-callback
))
980 (condition-case error
981 (setq plain
(epg-decrypt-string context
(buffer-string))
982 mml2015-epg-secret-key-id-list nil
)
984 (while mml2015-epg-secret-key-id-list
985 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
986 (setq mml2015-epg-secret-key-id-list
987 (cdr mml2015-epg-secret-key-id-list
)))
988 (mm-set-handle-multipart-parameter
989 mm-security-handle
'gnus-info
"Failed")
990 (if (eq (car error
) 'quit
)
991 (mm-set-handle-multipart-parameter
992 mm-security-handle
'gnus-details
"Quit.")
993 (mm-set-handle-multipart-parameter
994 mm-security-handle
'gnus-details
(mml2015-format-error error
)))))
997 ;; Treat data which epg returns as a unibyte string.
998 (mm-disable-multibyte)
1000 (goto-char (point-min))
1001 (while (search-forward "\r\n" nil t
)
1002 (replace-match "\n" t t
))
1003 (mm-set-handle-multipart-parameter
1004 mm-security-handle
'gnus-info
"OK")
1005 (if (epg-context-result-for context
'verify
)
1006 (mm-set-handle-multipart-parameter
1007 mm-security-handle
'gnus-details
1008 (mml2015-epg-verify-result-to-string
1009 (epg-context-result-for context
'verify
)))))))
1011 (defun mml2015-epg-verify (handle ctl
)
1013 (let ((inhibit-redisplay t
)
1014 context plain signature-file part signature
)
1015 (when (or (null (setq part
(mm-find-raw-part-by-type
1016 ctl
(or (mm-handle-multipart-ctl-parameter
1018 "application/pgp-signature")
1020 (null (setq signature
(mm-find-part-by-type
1021 (cdr handle
) "application/pgp-signature"
1023 (mm-set-handle-multipart-parameter
1024 mm-security-handle
'gnus-info
"Corrupted")
1025 (throw 'error handle
))
1026 (setq part
(mm-replace-in-string part
"\n" "\r\n")
1027 signature
(mm-get-part signature
)
1028 context
(epg-make-context))
1029 (condition-case error
1030 (setq plain
(epg-verify-string context signature part
))
1032 (mm-set-handle-multipart-parameter
1033 mm-security-handle
'gnus-info
"Failed")
1034 (if (eq (car error
) 'quit
)
1035 (mm-set-handle-multipart-parameter
1036 mm-security-handle
'gnus-details
"Quit.")
1037 (mm-set-handle-multipart-parameter
1038 mm-security-handle
'gnus-details
(mml2015-format-error error
)))
1039 (throw 'error handle
)))
1040 (mm-set-handle-multipart-parameter
1041 mm-security-handle
'gnus-info
1042 (mml2015-epg-verify-result-to-string
1043 (epg-context-result-for context
'verify
)))
1046 (defun mml2015-epg-clear-verify ()
1047 (let ((inhibit-redisplay t
)
1048 (context (epg-make-context))
1049 (signature (mm-encode-coding-string (buffer-string)
1050 coding-system-for-write
))
1052 (condition-case error
1053 (setq plain
(epg-verify-string context signature
))
1055 (mm-set-handle-multipart-parameter
1056 mm-security-handle
'gnus-info
"Failed")
1057 (if (eq (car error
) 'quit
)
1058 (mm-set-handle-multipart-parameter
1059 mm-security-handle
'gnus-details
"Quit.")
1060 (mm-set-handle-multipart-parameter
1061 mm-security-handle
'gnus-details
(mml2015-format-error error
)))))
1064 (mm-set-handle-multipart-parameter
1065 mm-security-handle
'gnus-info
1066 (mml2015-epg-verify-result-to-string
1067 (epg-context-result-for context
'verify
)))
1068 (delete-region (point-min) (point-max))
1069 (insert (mm-decode-coding-string plain coding-system-for-read
)))
1070 (mml2015-extract-cleartext-signature))))
1072 (defun mml2015-epg-sign (cont)
1073 (let* ((inhibit-redisplay t
)
1074 (context (epg-make-context))
1075 (boundary (mml-compute-boundary cont
))
1076 (sender (message-options-get 'message-sender
))
1077 (signer-names (or mml2015-signers
1078 (if (and mml2015-sign-with-sender sender
)
1079 (list (concat "<" sender
">")))))
1082 (or (message-options-get 'mml2015-epg-signers
)
1083 (message-options-set
1084 'mml2015-epg-signers
1085 (if (eq mm-sign-option
'guided
)
1086 (epa-select-keys context
"\
1087 Select keys for signing.
1088 If no one is selected, default secret key is used. "
1091 (if (or sender mml2015-signers
)
1096 (mml2015-epg-find-usable-secret-key
1097 context signer
'sign
))
1098 (unless (or signer-key
1101 "No secret key for %s; skip it? "
1103 (error "No secret key for %s" signer
))
1107 (epg-context-set-armor context t
)
1108 (epg-context-set-textmode context t
)
1109 (epg-context-set-signers context signers
)
1110 (if mml2015-cache-passphrase
1111 (epg-context-set-passphrase-callback
1113 #'mml2015-epg-passphrase-callback
))
1114 ;; Signed data must end with a newline (RFC 3156, 5).
1115 (goto-char (point-max))
1118 (condition-case error
1119 (setq signature
(epg-sign-string context
(buffer-string) t
)
1120 mml2015-epg-secret-key-id-list nil
)
1122 (while mml2015-epg-secret-key-id-list
1123 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
1124 (setq mml2015-epg-secret-key-id-list
1125 (cdr mml2015-epg-secret-key-id-list
)))
1126 (signal (car error
) (cdr error
))))
1127 (if (epg-context-result-for context
'sign
)
1128 (setq micalg
(epg-new-signature-digest-algorithm
1129 (car (epg-context-result-for context
'sign
)))))
1130 (goto-char (point-min))
1131 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
1134 (insert (format "\tmicalg=pgp-%s; "
1137 epg-digest-algorithm-alist
))))))
1138 (insert "protocol=\"application/pgp-signature\"\n")
1139 (insert (format "\n--%s\n" boundary
))
1140 (goto-char (point-max))
1141 (insert (format "\n--%s\n" boundary
))
1142 (insert "Content-Type: application/pgp-signature; name=\"signature.asc\"\n\n")
1144 (goto-char (point-max))
1145 (insert (format "--%s--\n" boundary
))
1146 (goto-char (point-max))))
1148 (defun mml2015-epg-encrypt (cont &optional sign
)
1149 (let* ((inhibit-redisplay t
)
1150 (context (epg-make-context))
1151 (boundary (mml-compute-boundary cont
))
1152 (config (epg-configuration))
1153 (recipients (message-options-get 'mml2015-epg-recipients
))
1155 (sender (message-options-get 'message-sender
))
1156 (signer-names (or mml2015-signers
1157 (if (and mml2015-sign-with-sender sender
)
1158 (list (concat "<" sender
">")))))
1160 recipient-key signer-key
)
1166 (or (epg-expand-group config recipient
)
1167 (list (concat "<" recipient
">"))))
1169 (or (message-options-get 'message-recipients
)
1170 (message-options-set 'message-recipients
1171 (read-string "Recipients: ")))
1172 "[ \f\t\n\r\v,]+"))))
1173 (when mml2015-encrypt-to-self
1174 (unless signer-names
1175 (error "Neither message sender nor mml2015-signers are set"))
1176 (setq recipients
(nconc recipients signer-names
)))
1177 (if (eq mm-encrypt-option
'guided
)
1179 (epa-select-keys context
"\
1180 Select recipients for encryption.
1181 If no one is selected, symmetric encryption will be performed. "
1187 (setq recipient-key
(mml2015-epg-find-usable-key
1188 context recipient
'encrypt
))
1189 (unless (or recipient-key
1191 (format "No public key for %s; skip it? "
1193 (error "No public key for %s" recipient
))
1197 (error "No recipient specified")))
1198 (message-options-set 'mml2015-epg-recipients recipients
))
1201 (or (message-options-get 'mml2015-epg-signers
)
1202 (message-options-set
1203 'mml2015-epg-signers
1204 (if (eq mm-sign-option
'guided
)
1205 (epa-select-keys context
"\
1206 Select keys for signing.
1207 If no one is selected, default secret key is used. "
1210 (if (or sender mml2015-signers
)
1215 (mml2015-epg-find-usable-secret-key
1216 context signer
'sign
))
1217 (unless (or signer-key
1220 "No secret key for %s; skip it? "
1222 (error "No secret key for %s" signer
))
1225 (epg-context-set-signers context signers
))
1226 (epg-context-set-armor context t
)
1227 (epg-context-set-textmode context t
)
1228 (if mml2015-cache-passphrase
1229 (epg-context-set-passphrase-callback
1231 #'mml2015-epg-passphrase-callback
))
1232 (condition-case error
1234 (epg-encrypt-string context
(buffer-string) recipients sign
1235 mml2015-always-trust
)
1236 mml2015-epg-secret-key-id-list nil
)
1238 (while mml2015-epg-secret-key-id-list
1239 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
1240 (setq mml2015-epg-secret-key-id-list
1241 (cdr mml2015-epg-secret-key-id-list
)))
1242 (signal (car error
) (cdr error
))))
1243 (delete-region (point-min) (point-max))
1244 (goto-char (point-min))
1245 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1247 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1248 (insert (format "--%s\n" boundary
))
1249 (insert "Content-Type: application/pgp-encrypted\n\n")
1250 (insert "Version: 1\n\n")
1251 (insert (format "--%s\n" boundary
))
1252 (insert "Content-Type: application/octet-stream\n\n")
1254 (goto-char (point-max))
1255 (insert (format "--%s--\n" boundary
))
1256 (goto-char (point-max))))
1260 (autoload 'gnus-buffer-live-p
"gnus-util")
1261 (autoload 'gnus-get-buffer-create
"gnus")
1263 (defun mml2015-clean-buffer ()
1264 (if (gnus-buffer-live-p mml2015-result-buffer
)
1265 (with-current-buffer mml2015-result-buffer
1268 (setq mml2015-result-buffer
1269 (gnus-get-buffer-create " *MML2015 Result*"))
1272 (defsubst mml2015-clear-decrypt-function
()
1273 (nth 6 (assq mml2015-use mml2015-function-alist
)))
1275 (defsubst mml2015-clear-verify-function
()
1276 (nth 5 (assq mml2015-use mml2015-function-alist
)))
1279 (defun mml2015-decrypt (handle ctl
)
1280 (mml2015-clean-buffer)
1281 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist
))))
1283 (funcall func handle ctl
)
1287 (defun mml2015-decrypt-test (handle ctl
)
1291 (defun mml2015-verify (handle ctl
)
1292 (mml2015-clean-buffer)
1293 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist
))))
1295 (funcall func handle ctl
)
1299 (defun mml2015-verify-test (handle ctl
)
1303 (defun mml2015-encrypt (cont &optional sign
)
1304 (mml2015-clean-buffer)
1305 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist
))))
1307 (funcall func cont sign
)
1308 (error "Cannot find encrypt function"))))
1311 (defun mml2015-sign (cont)
1312 (mml2015-clean-buffer)
1313 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist
))))
1316 (error "Cannot find sign function"))))
1319 (defun mml2015-self-encrypt ()
1320 (mml2015-encrypt nil
))
1324 ;;; mml2015.el ends here