1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: PGP MIME MML
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
32 ;; For Emacs <22.2 and XEmacs.
33 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
35 (if (locate-library "password-cache")
36 (require 'password-cache)
39 (eval-when-compile (require 'cl))
45 (defvar mc-pgp-always-sign)
47 (declare-function epg-check-configuration "ext:epg-config"
48 (config &optional minimum-version))
49 (declare-function epg-configuration "ext:epg-config" ())
51 (defvar mml2015-use (or
55 (epg-check-configuration (epg-configuration))
59 (ignore-errors (require 'pgg))
60 (and (fboundp 'pgg-sign-region)
64 (and (fboundp 'mc-encrypt-generic)
65 (fboundp 'mc-sign-generic)
66 (fboundp 'mc-cleanup-recipient-headers)
68 "The package used for PGP/MIME.
69 Valid packages include `epg', `pgg' and `mailcrypt'.")
71 ;; Something is not RFC2015.
72 (defvar mml2015-function-alist
73 '((mailcrypt mml2015-mailcrypt-sign
74 mml2015-mailcrypt-encrypt
75 mml2015-mailcrypt-verify
76 mml2015-mailcrypt-decrypt
77 mml2015-mailcrypt-clear-verify
78 mml2015-mailcrypt-clear-decrypt)
83 mml2015-pgg-clear-verify
84 mml2015-pgg-clear-decrypt)
89 mml2015-epg-clear-verify
90 mml2015-epg-clear-decrypt))
91 "Alist of PGP/MIME functions.")
93 (defvar mml2015-result-buffer nil)
95 (defcustom mml2015-unabbrev-trust-alist
96 '(("TRUST_UNDEFINED" . nil)
98 ("TRUST_MARGINAL" . t)
100 ("TRUST_ULTIMATE" . t))
101 "Map GnuPG trust output values to a boolean saying if you trust the key."
103 :group 'mime-security
104 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
105 (boolean :tag "Trust key"))))
107 (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase
108 "If t, cache passphrase."
109 :group 'mime-security
112 (defcustom mml2015-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
113 "How many seconds the passphrase is cached.
114 Whether the passphrase is cached at all is controlled by
115 `mml2015-cache-passphrase'."
116 :group 'mime-security
119 (defcustom mml2015-signers nil
120 "A list of your own key ID which will be used to sign a message."
121 :group 'mime-security
122 :type '(repeat (string :tag "Key ID")))
124 (defcustom mml2015-encrypt-to-self nil
125 "If t, add your own key ID to recipient list when encryption."
126 :group 'mime-security
129 (defcustom mml2015-always-trust t
130 "If t, GnuPG skip key validation on encryption."
131 :group 'mime-security
134 ;; Extract plaintext from cleartext signature. IMO, this kind of task
135 ;; should be done by GnuPG rather than Elisp, but older PGP backends
136 ;; (such as Mailcrypt, and PGG) discard the output from GnuPG.
137 (defun mml2015-extract-cleartext-signature ()
139 ;; <54a15d860801080142l70b95d7dkac4bf51a86196011@mail.gmail.com>: ``I still
140 ;; believe that the right way is to use the plaintext output from GnuPG as
141 ;; it is, and mml2015-extract-cleartext-signature is just a kludge for
142 ;; misdesigned libraries like PGG, which have no ability to do that. So, I
143 ;; think it should not have descriptive documentation.''
145 ;; This function doesn't handle NotDashEscaped correctly. EasyPG handles it
147 ;; http://thread.gmane.org/gmane.emacs.gnus.general/66062/focus=66082
148 ;; http://thread.gmane.org/gmane.emacs.gnus.general/65087/focus=65109
149 (goto-char (point-min))
151 ;; We need to be careful not to strip beyond the armor headers.
152 ;; Previously, an attacker could replace the text inside our
153 ;; markup with trailing garbage by injecting whitespace into the
155 (while (looking-at "Hash:") ; The only header allowed in cleartext
156 (forward-line)) ; signatures according to RFC2440.
157 (when (looking-at "[\t ]*$")
159 (delete-region (point-min) (point))
160 (if (re-search-forward "^-----BEGIN PGP SIGNATURE-----" nil t)
161 (delete-region (match-beginning 0) (point-max)))
162 (goto-char (point-min))
163 (while (re-search-forward "^- " nil t)
164 (replace-match "" t t)
167 ;;; mailcrypt wrapper
169 (autoload 'mailcrypt-decrypt "mailcrypt")
170 (autoload 'mailcrypt-verify "mailcrypt")
171 (autoload 'mc-pgp-always-sign "mailcrypt")
172 (autoload 'mc-encrypt-generic "mc-toplev")
173 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
174 (autoload 'mc-sign-generic "mc-toplev")
176 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
177 (defvar mml2015-verify-function 'mailcrypt-verify)
179 (defun mml2015-format-error (err)
180 (if (stringp (cadr err))
182 (format "%S" (cdr err))))
184 (defun mml2015-mailcrypt-decrypt (handle ctl)
186 (let (child handles result)
187 (unless (setq child (mm-find-part-by-type
189 "application/octet-stream" nil t))
190 (mm-set-handle-multipart-parameter
191 mm-security-handle 'gnus-info "Corrupted")
192 (throw 'error handle))
194 (mm-insert-part child)
197 (funcall mml2015-decrypt-function)
199 (mm-set-handle-multipart-parameter
200 mm-security-handle 'gnus-details (mml2015-format-error err))
203 (mm-set-handle-multipart-parameter
204 mm-security-handle 'gnus-details "Quit.")
207 (mm-set-handle-multipart-parameter
208 mm-security-handle 'gnus-info "Failed")
209 (throw 'error handle))
210 (setq handles (mm-dissect-buffer t)))
211 (mm-destroy-parts handle)
212 (mm-set-handle-multipart-parameter
213 mm-security-handle 'gnus-info
215 (let ((sig (with-current-buffer mml2015-result-buffer
216 (mml2015-gpg-extract-signature-details))))
217 (concat ", Signer: " sig))))
218 (if (listp (car handles))
222 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
224 (fpr-length (string-width fingerprint))
227 (setq fingerprint (string-to-list fingerprint))
229 (setq fpr-length (- fpr-length 4))
230 (setq slice (butlast fingerprint fpr-length))
231 (setq fingerprint (nthcdr 4 fingerprint))
232 (setq n-slice (1+ n-slice))
238 (otherwise (concat " " slice))))))
241 (defun mml2015-gpg-extract-signature-details ()
242 (goto-char (point-min))
243 (let* ((expired (re-search-forward
244 "^\\[GNUPG:\\] SIGEXPIRED$"
246 (signer (and (re-search-forward
247 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
249 (cons (match-string 1) (match-string 2))))
250 (fprint (and (re-search-forward
251 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
254 (trust (and (re-search-forward
255 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
259 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
260 (cond ((and signer fprint)
262 (unless trust-good-enough-p
263 (concat "\nUntrusted, Fingerprint: "
264 (mml2015-gpg-pretty-print-fpr fprint)))
266 (format "\nWARNING: Signature from expired key (%s)"
269 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
272 "From unknown user"))))
274 (defun mml2015-mailcrypt-clear-decrypt ()
278 (funcall mml2015-decrypt-function)
280 (mm-set-handle-multipart-parameter
281 mm-security-handle 'gnus-details (mml2015-format-error err))
284 (mm-set-handle-multipart-parameter
285 mm-security-handle 'gnus-details "Quit.")
288 (mm-set-handle-multipart-parameter