1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000-2012 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 (defvar mml2015-use
(or
54 (epg-check-configuration (epg-configuration))
58 (let ((abs-file (locate-library "pgg")))
59 ;; Don't load PGG if it is marked as obsolete
62 (not (string-match "/obsolete/[^/]*\\'"
64 (ignore-errors (require 'pgg
))
65 (and (fboundp 'pgg-sign-region
)
69 (and (fboundp 'mc-encrypt-generic
)
70 (fboundp 'mc-sign-generic
)
71 (fboundp 'mc-cleanup-recipient-headers
)
73 "The package used for PGP/MIME.
74 Valid packages include `epg', `pgg' and `mailcrypt'.")
76 ;; Something is not RFC2015.
77 (defvar mml2015-function-alist
78 '((mailcrypt mml2015-mailcrypt-sign
79 mml2015-mailcrypt-encrypt
80 mml2015-mailcrypt-verify
81 mml2015-mailcrypt-decrypt
82 mml2015-mailcrypt-clear-verify
83 mml2015-mailcrypt-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-cache-passphrase mml-secure-cache-passphrase
113 "If t, cache passphrase."
114 :group
'mime-security
117 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
118 "How many seconds the passphrase is cached.
119 Whether the passphrase is cached at all is controlled by
120 `mml2015-cache-passphrase'."
121 :group
'mime-security
124 (defcustom mml2015-signers nil
125 "A list of your own key ID(s) which will be used to sign a message.
126 If set, it overrides the setting of `mml2015-sign-with-sender'."
127 :group
'mime-security
128 :type
'(repeat (string :tag
"Key ID")))
130 (defcustom mml2015-sign-with-sender nil
131 "If t, use message sender so find a key to sign with."
132 :group
'mime-security
136 (defcustom mml2015-encrypt-to-self nil
137 "If t, add your own key ID to recipient list when encryption."
138 :group
'mime-security
141 (defcustom mml2015-always-trust t
142 "If t, GnuPG skip key validation on encryption."
143 :group
'mime-security
146 ;; Extract plaintext from cleartext signature. IMO, this kind of task
147 ;; should be done by GnuPG rather than Elisp, but older PGP backends
148 ;; (such as Mailcrypt, and PGG) discard the output from GnuPG.
149 (defun mml2015-extract-cleartext-signature ()
151 ;; <54a15d860801080142l70b95d7dkac4bf51a86196011@mail.gmail.com>: ``I still
152 ;; believe that the right way is to use the plaintext output from GnuPG as
153 ;; it is, and mml2015-extract-cleartext-signature is just a kludge for
154 ;; misdesigned libraries like PGG, which have no ability to do that. So, I
155 ;; think it should not have descriptive documentation.''
157 ;; This function doesn't handle NotDashEscaped correctly. EasyPG handles it
159 ;; http://thread.gmane.org/gmane.emacs.gnus.general/66062/focus=66082
160 ;; http://thread.gmane.org/gmane.emacs.gnus.general/65087/focus=65109
161 (goto-char (point-min))
163 ;; We need to be careful not to strip beyond the armor headers.
164 ;; Previously, an attacker could replace the text inside our
165 ;; markup with trailing garbage by injecting whitespace into the
167 (while (looking-at "Hash:") ; The only header allowed in cleartext
168 (forward-line)) ; signatures according to RFC2440.
169 (when (looking-at "[\t ]*$")
171 (delete-region (point-min) (point))
172 (if (re-search-forward "^-----BEGIN PGP SIGNATURE-----" nil t
)
173 (delete-region (match-beginning 0) (point-max)))
174 (goto-char (point-min))
175 (while (re-search-forward "^- " nil t
)
176 (replace-match "" t t
)
179 ;;; mailcrypt wrapper
181 (autoload 'mailcrypt-decrypt
"mailcrypt")
182 (autoload 'mailcrypt-verify
"mailcrypt")
183 (autoload 'mc-pgp-always-sign
"mailcrypt")
184 (autoload 'mc-encrypt-generic
"mc-toplev")
185 (autoload 'mc-cleanup-recipient-headers
"mc-toplev")
186 (autoload 'mc-sign-generic
"mc-toplev")
188 (defvar mml2015-decrypt-function
'mailcrypt-decrypt
)
189 (defvar mml2015-verify-function
'mailcrypt-verify
)
191 (defun mml2015-format-error (err)
192 (if (stringp (cadr err
))
194 (format "%S" (cdr err
))))
196 (defun mml2015-mailcrypt-decrypt (handle ctl
)
198 (let (child handles result
)
199 (unless (setq child
(mm-find-part-by-type
201 "application/octet-stream" nil t
))
202 (mm-set-handle-multipart-parameter
203 mm-security-handle
'gnus-info
"Corrupted")
204 (throw 'error handle
))
206 (mm-insert-part child
)
209 (funcall mml2015-decrypt-function
)
211 (mm-set-handle-multipart-parameter
212 mm-security-handle
'gnus-details
(mml2015-format-error err
))
215 (mm-set-handle-multipart-parameter
216 mm-security-handle
'gnus-details
"Quit.")
219 (mm-set-handle-multipart-parameter
220 mm-security-handle
'gnus-info
"Failed")
221 (throw 'error handle
))
222 (setq handles
(mm-dissect-buffer t
)))
223 (mm-destroy-parts handle
)
224 (mm-set-handle-multipart-parameter
225 mm-security-handle
'gnus-info
227 (let ((sig (with-current-buffer mml2015-result-buffer
228 (mml2015-gpg-extract-signature-details))))
229 (concat ", Signer: " sig
))))
230 (if (listp (car handles
))
234 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
236 (fpr-length (string-width fingerprint
))
239 (setq fingerprint
(string-to-list fingerprint
))
241 (setq fpr-length
(- fpr-length
4))
242 (setq slice
(butlast fingerprint fpr-length
))
243 (setq fingerprint
(nthcdr 4 fingerprint
))
244 (setq n-slice
(1+ n-slice
))
250 (otherwise (concat " " slice
))))))
253 (defun mml2015-gpg-extract-signature-details ()
254 (goto-char (point-min))
255 (let* ((expired (re-search-forward
256 "^\\[GNUPG:\\] SIGEXPIRED$"
258 (signer (and (re-search-forward
259 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
261 (cons (match-string 1) (match-string 2))))
262 (fprint (and (re-search-forward
263 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
266 (trust (and (re-search-forward
267 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
271 (cdr (assoc trust mml2015-unabbrev-trust-alist
))))
272 (cond ((and signer fprint
)
274 (unless trust-good-enough-p
275 (concat "\nUntrusted, Fingerprint: "
276 (mml2015-gpg-pretty-print-fpr fprint
)))
278 (format "\nWARNING: Signature from expired key (%s)"
281 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t
)
284 "From unknown user"))))
286 (defun mml2015-mailcrypt-clear-decrypt ()
290 (funcall mml2015-decrypt-function
)
292 (mm-set-handle-multipart-parameter
293 mm-security-handle
'gnus-details
(mml2015-format-error err
))
296 (mm-set-handle-multipart-parameter
297 mm-security-handle
'gnus-details
"Quit.")
300 (mm-set-handle-multipart-parameter
301 mm-security-handle
'gnus-info
"OK")
302 (mm-set-handle-multipart-parameter
303 mm-security-handle
'gnus-info
"Failed"))))
305 (defun mml2015-fix-micalg (alg)
307 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
308 (upcase (if (string-match "^p[gh]p-" alg
)
309 (substring alg
(match-end 0))
312 (defun mml2015-mailcrypt-verify (handle ctl
)
315 (unless (setq part
(mm-find-raw-part-by-type
316 ctl
(or (mm-handle-multipart-ctl-parameter
318 "application/pgp-signature")
320 (mm-set-handle-multipart-parameter
321 mm-security-handle
'gnus-info
"Corrupted")
322 (throw 'error handle
))
324 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
325 (insert (format "Hash: %s\n\n"
326 (or (mml2015-fix-micalg
327 (mm-handle-multipart-ctl-parameter
331 (narrow-to-region (point) (point))
333 (goto-char (point-min))
335 (if (looking-at "^-")
338 (unless (setq part
(mm-find-part-by-type
339 (cdr handle
) "application/pgp-signature" nil t
))
340 (mm-set-handle-multipart-parameter
341 mm-security-handle
'gnus-info
"Corrupted")
342 (throw 'error handle
))
344 (narrow-to-region (point) (point))
345 (mm-insert-part part
)
346 (goto-char (point-min))
347 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t
)
348 (replace-match "-----BEGIN PGP SIGNATURE-----" t t
))
349 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t
)
350 (replace-match "-----END PGP SIGNATURE-----" t t
)))
351 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
352 (unless (condition-case err
354 (funcall mml2015-verify-function
)
355 (if (get-buffer " *mailcrypt stderr temp")
356 (mm-set-handle-multipart-parameter
357 mm-security-handle
'gnus-details
358 (with-current-buffer " *mailcrypt stderr temp"
360 (if (get-buffer " *mailcrypt stdout temp")
361 (kill-buffer " *mailcrypt stdout temp"))
362 (if (get-buffer " *mailcrypt stderr temp")
363 (kill-buffer " *mailcrypt stderr temp"))
364 (if (get-buffer " *mailcrypt status temp")
365 (kill-buffer " *mailcrypt status temp"))
366 (if (get-buffer mc-gpg-debug-buffer
)
367 (kill-buffer mc-gpg-debug-buffer
)))
369 (mm-set-handle-multipart-parameter
370 mm-security-handle
'gnus-details
(mml2015-format-error err
))
373 (mm-set-handle-multipart-parameter
374 mm-security-handle
'gnus-details
"Quit.")
376 (mm-set-handle-multipart-parameter
377 mm-security-handle
'gnus-info
"Failed")
378 (throw 'error handle
))))
379 (mm-set-handle-multipart-parameter
380 mm-security-handle
'gnus-info
"OK")
383 (defun mml2015-mailcrypt-clear-verify ()
384 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
385 (if (condition-case err
387 (funcall mml2015-verify-function
)
388 (if (get-buffer " *mailcrypt stderr temp")
389 (mm-set-handle-multipart-parameter
390 mm-security-handle
'gnus-details
391 (with-current-buffer " *mailcrypt stderr temp"
393 (if (get-buffer " *mailcrypt stdout temp")
394 (kill-buffer " *mailcrypt stdout temp"))
395 (if (get-buffer " *mailcrypt stderr temp")
396 (kill-buffer " *mailcrypt stderr temp"))
397 (if (get-buffer " *mailcrypt status temp")
398 (kill-buffer " *mailcrypt status temp"))
399 (if (get-buffer mc-gpg-debug-buffer
)
400 (kill-buffer mc-gpg-debug-buffer
)))
402 (mm-set-handle-multipart-parameter
403 mm-security-handle
'gnus-details
(mml2015-format-error err
))
406 (mm-set-handle-multipart-parameter
407 mm-security-handle
'gnus-details
"Quit.")
409 (mm-set-handle-multipart-parameter
410 mm-security-handle
'gnus-info
"OK")
411 (mm-set-handle-multipart-parameter
412 mm-security-handle
'gnus-info
"Failed")))
413 (mml2015-extract-cleartext-signature))
415 (defun mml2015-mailcrypt-sign (cont)
416 (mc-sign-generic (message-options-get 'message-sender
)
418 (let ((boundary (mml-compute-boundary cont
))
420 (goto-char (point-min))
421 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t
)
422 (error "Cannot find signed begin line"))
423 (goto-char (match-beginning 0))
425 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
426 (error "Cannot not find PGP hash"))
427 (setq hash
(match-string 1))
428 (unless (re-search-forward "^$" nil t
)
429 (error "Cannot not find PGP message"))
431 (delete-region (point-min) (point))
432 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
434 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
436 (insert (format "\n--%s\n" boundary
))
438 (goto-char (point-max))
439 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t
)
440 (error "Cannot find signature part"))
441 (replace-match "-----END PGP MESSAGE-----" t t
)
442 (goto-char (match-beginning 0))
443 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
445 (error "Cannot find signature part"))
446 (replace-match "-----BEGIN PGP MESSAGE-----" t t
)
447 (goto-char (match-beginning 0))
449 (narrow-to-region point
(point))
451 (while (re-search-forward "^- -" nil t
)
452 (replace-match "-" t t
))
453 (goto-char (point-max)))
454 (insert (format "--%s\n" boundary
))
455 (insert "Content-Type: application/pgp-signature\n\n")
456 (goto-char (point-max))
457 (insert (format "--%s--\n" boundary
))
458 (goto-char (point-max))))
460 ;; We require mm-decode, which requires mm-bodies, which autoloads
461 ;; message-options-get (!).
462 (declare-function message-options-set
"message" (symbol value
))
464 (defun mml2015-mailcrypt-encrypt (cont &optional sign
)
465 (let ((mc-pgp-always-sign
466 (or mc-pgp-always-sign
468 (eq t
(or (message-options-get 'message-sign-encrypt
)
470 'message-sign-encrypt
471 (or (y-or-n-p "Sign the message? ")
474 (mm-with-unibyte-current-buffer
476 (or (message-options-get 'message-recipients
)
477 (message-options-set 'message-recipients
478 (mc-cleanup-recipient-headers
479 (read-string "Recipients: "))))
481 (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
(mm-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 (mm-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 inhibit-redisplay
)
740 (autoload 'epg-make-context
"epg")
741 (autoload 'epg-context-set-armor
"epg")
742 (autoload 'epg-context-set-textmode
"epg")
743 (autoload 'epg-context-set-signers
"epg")
744 (autoload 'epg-context-result-for
"epg")
745 (autoload 'epg-new-signature-digest-algorithm
"epg")
746 (autoload 'epg-verify-result-to-string
"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-configuration
"epg-config")
759 (autoload 'epg-expand-group
"epg-config")
760 (autoload 'epa-select-keys
"epa")
762 (defvar mml2015-epg-secret-key-id-list nil
)
764 (defun mml2015-epg-passphrase-callback (context key-id ignore
)
766 (epg-passphrase-callback-function context key-id nil
)
767 (let* ((password-cache-key-id
775 "Passphrase for PIN: "
776 (if (setq entry
(assoc key-id epg-user-id-alist
))
777 (format "Passphrase for %s %s: " key-id
(cdr entry
))
778 (format "Passphrase for %s: " key-id
)))
779 password-cache-key-id
)))
781 (let ((password-cache-expiry mml2015-passphrase-cache-expiry
))
782 (password-cache-add password-cache-key-id passphrase
))
783 (setq mml2015-epg-secret-key-id-list
784 (cons password-cache-key-id mml2015-epg-secret-key-id-list
))
785 (copy-sequence passphrase
)))))
787 (defun mml2015-epg-find-usable-key (keys usage
)
790 (let ((pointer (epg-key-sub-key-list (car keys
))))
791 ;; The primary key will be marked as disabled, when the entire
792 ;; key is disabled (see 12 Field, Format of colon listings, in
793 ;; gnupg/doc/DETAILS)
794 (unless (memq 'disabled
(epg-sub-key-capability (car pointer
)))
796 (if (and (memq usage
(epg-sub-key-capability (car pointer
)))
797 (not (memq (epg-sub-key-validity (car pointer
))
798 '(revoked expired
))))
799 (throw 'found
(car keys
)))
800 (setq pointer
(cdr pointer
)))))
801 (setq keys
(cdr keys
)))))
803 ;; XXX: since gpg --list-secret-keys does not return validity of each
804 ;; key, `mml2015-epg-find-usable-key' defined above is not enough for
805 ;; secret keys. The function `mml2015-epg-find-usable-secret-key'
806 ;; below looks at appropriate public keys to check usability.
807 (defun mml2015-epg-find-usable-secret-key (context name usage
)
808 (let ((secret-keys (epg-list-keys context name t
))
810 (while (and (not secret-key
) secret-keys
)
811 (if (mml2015-epg-find-usable-key
812 (epg-list-keys context
(epg-sub-key-fingerprint
813 (car (epg-key-sub-key-list
814 (car secret-keys
)))))
816 (setq secret-key
(car secret-keys
)
818 (setq secret-keys
(cdr secret-keys
))))
821 (defun mml2015-epg-decrypt (handle ctl
)
823 (let ((inhibit-redisplay t
)
824 context plain child handles result decrypt-status
)
825 (unless (setq child
(mm-find-part-by-type
827 "application/octet-stream" nil t
))
828 (mm-set-handle-multipart-parameter
829 mm-security-handle
'gnus-info
"Corrupted")
830 (throw 'error handle
))
831 (setq context
(epg-make-context))
832 (if mml2015-cache-passphrase
833 (epg-context-set-passphrase-callback
835 #'mml2015-epg-passphrase-callback
))
836 (condition-case error
837 (setq plain
(epg-decrypt-string context
(mm-get-part child
))
838 mml2015-epg-secret-key-id-list nil
)
840 (while mml2015-epg-secret-key-id-list
841 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
842 (setq mml2015-epg-secret-key-id-list
843 (cdr mml2015-epg-secret-key-id-list
)))
844 (mm-set-handle-multipart-parameter
845 mm-security-handle
'gnus-info
"Failed")
846 (if (eq (car error
) 'quit
)
847 (mm-set-handle-multipart-parameter
848 mm-security-handle
'gnus-details
"Quit.")
849 (mm-set-handle-multipart-parameter
850 mm-security-handle
'gnus-details
(mml2015-format-error error
)))
851 (throw 'error handle
)))
854 (goto-char (point-min))
855 (while (search-forward "\r\n" nil t
)
856 (replace-match "\n" t t
))
857 (setq handles
(mm-dissect-buffer t
))
858 (mm-destroy-parts handle
)
859 (if (epg-context-result-for context
'verify
)
860 (mm-set-handle-multipart-parameter
861 mm-security-handle
'gnus-info
863 (epg-verify-result-to-string
864 (epg-context-result-for context
'verify
))))
865 (mm-set-handle-multipart-parameter
866 mm-security-handle
'gnus-info
"OK"))
867 (if (stringp (car handles
))
868 (mm-set-handle-multipart-parameter
869 mm-security-handle
'gnus-details
870 (mm-handle-multipart-ctl-parameter handles
'gnus-details
))))
871 (if (listp (car handles
))
875 (defun mml2015-epg-clear-decrypt ()
876 (let ((inhibit-redisplay t
)
877 (context (epg-make-context))
879 (if mml2015-cache-passphrase
880 (epg-context-set-passphrase-callback
882 #'mml2015-epg-passphrase-callback
))
883 (condition-case error
884 (setq plain
(epg-decrypt-string context
(buffer-string))
885 mml2015-epg-secret-key-id-list nil
)
887 (while mml2015-epg-secret-key-id-list
888 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
889 (setq mml2015-epg-secret-key-id-list
890 (cdr mml2015-epg-secret-key-id-list
)))
891 (mm-set-handle-multipart-parameter
892 mm-security-handle
'gnus-info
"Failed")
893 (if (eq (car error
) 'quit
)
894 (mm-set-handle-multipart-parameter
895 mm-security-handle
'gnus-details
"Quit.")
896 (mm-set-handle-multipart-parameter
897 mm-security-handle
'gnus-details
(mml2015-format-error error
)))))
900 ;; Treat data which epg returns as a unibyte string.
901 (mm-disable-multibyte)
903 (goto-char (point-min))
904 (while (search-forward "\r\n" nil t
)
905 (replace-match "\n" t t
))
906 (mm-set-handle-multipart-parameter
907 mm-security-handle
'gnus-info
"OK")
908 (if (epg-context-result-for context
'verify
)
909 (mm-set-handle-multipart-parameter
910 mm-security-handle
'gnus-details
911 (epg-verify-result-to-string
912 (epg-context-result-for context
'verify
)))))))
914 (defun mml2015-epg-verify (handle ctl
)
916 (let ((inhibit-redisplay t
)
917 context plain signature-file part signature
)
918 (when (or (null (setq part
(mm-find-raw-part-by-type
919 ctl
(or (mm-handle-multipart-ctl-parameter
921 "application/pgp-signature")
923 (null (setq signature
(mm-find-part-by-type
924 (cdr handle
) "application/pgp-signature"
926 (mm-set-handle-multipart-parameter
927 mm-security-handle
'gnus-info
"Corrupted")
928 (throw 'error handle
))
929 (setq part
(mm-replace-in-string part
"\n" "\r\n")
930 signature
(mm-get-part signature
)
931 context
(epg-make-context))
932 (condition-case error
933 (setq plain
(epg-verify-string context signature part
))
935 (mm-set-handle-multipart-parameter
936 mm-security-handle
'gnus-info
"Failed")
937 (if (eq (car error
) 'quit
)
938 (mm-set-handle-multipart-parameter
939 mm-security-handle
'gnus-details
"Quit.")
940 (mm-set-handle-multipart-parameter
941 mm-security-handle
'gnus-details
(mml2015-format-error error
)))
942 (throw 'error handle
)))
943 (mm-set-handle-multipart-parameter
944 mm-security-handle
'gnus-info
945 (epg-verify-result-to-string (epg-context-result-for context
'verify
)))
948 (defun mml2015-epg-clear-verify ()
949 (let ((inhibit-redisplay t
)
950 (context (epg-make-context))
951 (signature (mm-encode-coding-string (buffer-string)
952 coding-system-for-write
))
954 (condition-case error
955 (setq plain
(epg-verify-string context signature
))
957 (mm-set-handle-multipart-parameter
958 mm-security-handle
'gnus-info
"Failed")
959 (if (eq (car error
) 'quit
)
960 (mm-set-handle-multipart-parameter
961 mm-security-handle
'gnus-details
"Quit.")
962 (mm-set-handle-multipart-parameter
963 mm-security-handle
'gnus-details
(mml2015-format-error error
)))))
966 (mm-set-handle-multipart-parameter
967 mm-security-handle
'gnus-info
968 (epg-verify-result-to-string
969 (epg-context-result-for context
'verify
)))
970 (delete-region (point-min) (point-max))
971 (insert (mm-decode-coding-string plain coding-system-for-read
)))
972 (mml2015-extract-cleartext-signature))))
974 (defun mml2015-epg-sign (cont)
975 (let* ((inhibit-redisplay t
)
976 (context (epg-make-context))
977 (boundary (mml-compute-boundary cont
))
978 (sender (message-options-get 'message-sender
))
979 (signer-names (or mml2015-signers
980 (if (and mml2015-sign-with-sender sender
)
981 (list (concat "<" sender
">")))))
984 (or (message-options-get 'mml2015-epg-signers
)
987 (if (eq mm-sign-option
'guided
)
988 (epa-select-keys context
"\
989 Select keys for signing.
990 If no one is selected, default secret key is used. "
993 (if (or sender mml2015-signers
)
998 (mml2015-epg-find-usable-secret-key
999 context signer
'sign
))
1000 (unless (or signer-key
1003 "No secret key for %s; skip it? "
1005 (error "No secret key for %s" signer
))
1009 (epg-context-set-armor context t
)
1010 (epg-context-set-textmode context t
)
1011 (epg-context-set-signers context signers
)
1012 (if mml2015-cache-passphrase
1013 (epg-context-set-passphrase-callback
1015 #'mml2015-epg-passphrase-callback
))
1016 (condition-case error
1017 (setq signature
(epg-sign-string context
(buffer-string) t
)
1018 mml2015-epg-secret-key-id-list nil
)
1020 (while mml2015-epg-secret-key-id-list
1021 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
1022 (setq mml2015-epg-secret-key-id-list
1023 (cdr mml2015-epg-secret-key-id-list
)))
1024 (signal (car error
) (cdr error
))))
1025 (if (epg-context-result-for context
'sign
)
1026 (setq micalg
(epg-new-signature-digest-algorithm
1027 (car (epg-context-result-for context
'sign
)))))
1028 (goto-char (point-min))
1029 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
1032 (insert (format "\tmicalg=pgp-%s; "
1035 epg-digest-algorithm-alist
))))))
1036 (insert "protocol=\"application/pgp-signature\"\n")
1037 (insert (format "\n--%s\n" boundary
))
1038 (goto-char (point-max))
1039 (insert (format "\n--%s\n" boundary
))
1040 (insert "Content-Type: application/pgp-signature\n\n")
1042 (goto-char (point-max))
1043 (insert (format "--%s--\n" boundary
))
1044 (goto-char (point-max))))
1046 (defun mml2015-epg-encrypt (cont &optional sign
)
1047 (let* ((inhibit-redisplay t
)
1048 (context (epg-make-context))
1049 (boundary (mml-compute-boundary cont
))
1050 (config (epg-configuration))
1051 (recipients (message-options-get 'mml2015-epg-recipients
))
1053 (sender (message-options-get 'message-sender
))
1054 (signer-names (or mml2015-signers
1055 (if (and mml2015-sign-with-sender sender
)
1056 (list (concat "<" sender
">")))))
1058 recipient-key signer-key
)
1064 (or (epg-expand-group config recipient
)
1065 (list (concat "<" recipient
">"))))
1067 (or (message-options-get 'message-recipients
)
1068 (message-options-set 'message-recipients
1069 (read-string "Recipients: ")))
1070 "[ \f\t\n\r\v,]+"))))
1071 (when mml2015-encrypt-to-self
1072 (unless signer-names
1073 (error "Neither message sender nor mml2015-signers are set"))
1074 (setq recipients
(nconc recipients signer-names
)))
1075 (if (eq mm-encrypt-option
'guided
)
1077 (epa-select-keys context
"\
1078 Select recipients for encryption.
1079 If no one is selected, symmetric encryption will be performed. "
1085 (setq recipient-key
(mml2015-epg-find-usable-key
1086 (epg-list-keys context recipient
)
1088 (unless (or recipient-key
1090 (format "No public key for %s; skip it? "
1092 (error "No public key for %s" recipient
))
1096 (error "No recipient specified")))
1097 (message-options-set 'mml2015-epg-recipients recipients
))
1100 (or (message-options-get 'mml2015-epg-signers
)
1101 (message-options-set
1102 'mml2015-epg-signers
1103 (if (eq mm-sign-option
'guided
)
1104 (epa-select-keys context
"\
1105 Select keys for signing.
1106 If no one is selected, default secret key is used. "
1109 (if (or sender mml2015-signers
)
1114 (mml2015-epg-find-usable-secret-key
1115 context signer
'sign
))
1116 (unless (or signer-key
1119 "No secret key for %s; skip it? "
1121 (error "No secret key for %s" signer
))
1124 (epg-context-set-signers context signers
))
1125 (epg-context-set-armor context t
)
1126 (epg-context-set-textmode context t
)
1127 (if mml2015-cache-passphrase
1128 (epg-context-set-passphrase-callback
1130 #'mml2015-epg-passphrase-callback
))
1131 (condition-case error
1133 (epg-encrypt-string context
(buffer-string) recipients sign
1134 mml2015-always-trust
)
1135 mml2015-epg-secret-key-id-list nil
)
1137 (while mml2015-epg-secret-key-id-list
1138 (password-cache-remove (car mml2015-epg-secret-key-id-list
))
1139 (setq mml2015-epg-secret-key-id-list
1140 (cdr mml2015-epg-secret-key-id-list
)))
1141 (signal (car error
) (cdr error
))))
1142 (delete-region (point-min) (point-max))
1143 (goto-char (point-min))
1144 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
1146 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
1147 (insert (format "--%s\n" boundary
))
1148 (insert "Content-Type: application/pgp-encrypted\n\n")
1149 (insert "Version: 1\n\n")
1150 (insert (format "--%s\n" boundary
))
1151 (insert "Content-Type: application/octet-stream\n\n")
1153 (goto-char (point-max))
1154 (insert (format "--%s--\n" boundary
))
1155 (goto-char (point-max))))
1159 (autoload 'gnus-buffer-live-p
"gnus-util")
1160 (autoload 'gnus-get-buffer-create
"gnus")
1162 (defun mml2015-clean-buffer ()
1163 (if (gnus-buffer-live-p mml2015-result-buffer
)
1164 (with-current-buffer mml2015-result-buffer
1167 (setq mml2015-result-buffer
1168 (gnus-get-buffer-create " *MML2015 Result*"))
1171 (defsubst mml2015-clear-decrypt-function
()
1172 (nth 6 (assq mml2015-use mml2015-function-alist
)))
1174 (defsubst mml2015-clear-verify-function
()
1175 (nth 5 (assq mml2015-use mml2015-function-alist
)))
1178 (defun mml2015-decrypt (handle ctl
)
1179 (mml2015-clean-buffer)
1180 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist
))))
1182 (funcall func handle ctl
)
1186 (defun mml2015-decrypt-test (handle ctl
)
1190 (defun mml2015-verify (handle ctl
)
1191 (mml2015-clean-buffer)
1192 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist
))))
1194 (funcall func handle ctl
)
1198 (defun mml2015-verify-test (handle ctl
)
1202 (defun mml2015-encrypt (cont &optional sign
)
1203 (mml2015-clean-buffer)
1204 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist
))))
1206 (funcall func cont sign
)
1207 (error "Cannot find encrypt function"))))
1210 (defun mml2015-sign (cont)
1211 (mml2015-clean-buffer)
1212 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist
))))
1215 (error "Cannot find sign function"))))
1218 (defun mml2015-self-encrypt ()
1219 (mml2015-encrypt nil
))
1223 ;;; mml2015.el ends here