Commit | Line | Data |
---|---|---|
23f87bed | 1 | ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP) |
e84b4b86 | 2 | |
acaf905b | 3 | ;; Copyright (C) 2000-2012 Free Software Foundation, Inc. |
23f87bed MB |
4 | |
5 | ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
6 | ;; Keywords: PGP MIME MML | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
5e809f55 GM |
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. | |
23f87bed | 14 | |
5e809f55 GM |
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. | |
23f87bed MB |
19 | |
20 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
23f87bed MB |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; RFC 2015 is updated by RFC 3156, this file should be compatible | |
26 | ;; with both. | |
27 | ||
28 | ;;; Code: | |
29 | ||
5a12b40c | 30 | (eval-and-compile |
f0b7f5a8 | 31 | ;; For Emacs <22.2 and XEmacs. |
aa8f8277 GM |
32 | (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))) |
33 | ||
34 | (if (locate-library "password-cache") | |
35 | (require 'password-cache) | |
36 | (require 'password))) | |
5a12b40c | 37 | |
23f87bed MB |
38 | (eval-when-compile (require 'cl)) |
39 | (require 'mm-decode) | |
40 | (require 'mm-util) | |
41 | (require 'mml) | |
01c52d31 | 42 | (require 'mml-sec) |
23f87bed | 43 | |
4ae9592b JB |
44 | (defvar mc-pgp-always-sign) |
45 | ||
5a12b40c GM |
46 | (declare-function epg-check-configuration "ext:epg-config" |
47 | (config &optional minimum-version)) | |
48 | (declare-function epg-configuration "ext:epg-config" ()) | |
49 | ||
23f87bed | 50 | (defvar mml2015-use (or |
01c52d31 MB |
51 | (condition-case nil |
52 | (progn | |
53 | (require 'epg-config) | |
54 | (epg-check-configuration (epg-configuration)) | |
55 | 'epg) | |
56 | (error)) | |
23f87bed | 57 | (progn |
9ccd6e90 DU |
58 | (let ((abs-file (locate-library "pgg"))) |
59 | ;; Don't load PGG if it is marked as obsolete | |
60 | ;; (Emacs 24). | |
61 | (when (and abs-file | |
34d0a923 KY |
62 | (not (string-match "/obsolete/[^/]*\\'" |
63 | abs-file))) | |
9ccd6e90 DU |
64 | (ignore-errors (require 'pgg)) |
65 | (and (fboundp 'pgg-sign-region) | |
66 | 'pgg)))) | |
23f87bed MB |
67 | (progn (ignore-errors |
68 | (load "mc-toplev")) | |
69 | (and (fboundp 'mc-encrypt-generic) | |
70 | (fboundp 'mc-sign-generic) | |
71 | (fboundp 'mc-cleanup-recipient-headers) | |
72 | 'mailcrypt))) | |
01c52d31 | 73 | "The package used for PGP/MIME. |
8ccbef23 | 74 | Valid packages include `epg', `pgg' and `mailcrypt'.") |
23f87bed MB |
75 | |
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) | |
8ccbef23 G |
84 | (pgg mml2015-pgg-sign |
85 | mml2015-pgg-encrypt | |
86 | mml2015-pgg-verify | |
87 | mml2015-pgg-decrypt | |
88 | mml2015-pgg-clear-verify | |
89 | mml2015-pgg-clear-decrypt) | |
90 | (epg mml2015-epg-sign | |
91 | mml2015-epg-encrypt | |
92 | mml2015-epg-verify | |
93 | mml2015-epg-decrypt | |
94 | mml2015-epg-clear-verify | |
95 | mml2015-epg-clear-decrypt)) | |
23f87bed MB |
96 | "Alist of PGP/MIME functions.") |
97 | ||
98 | (defvar mml2015-result-buffer nil) | |
99 | ||
100 | (defcustom mml2015-unabbrev-trust-alist | |
101 | '(("TRUST_UNDEFINED" . nil) | |
102 | ("TRUST_NEVER" . nil) | |
103 | ("TRUST_MARGINAL" . t) | |
104 | ("TRUST_FULLY" . t) | |
105 | ("TRUST_ULTIMATE" . t)) | |
106 | "Map GnuPG trust output values to a boolean saying if you trust the key." | |
bf247b6e | 107 | :version "22.1" |
e79f14a4 | 108 | :group 'mime-security |
23f87bed MB |
109 | :type '(repeat (cons (regexp :tag "GnuPG output regexp") |
110 | (boolean :tag "Trust key")))) | |
111 | ||
01c52d31 MB |
112 | (defcustom mml2015-cache-passphrase mml-secure-cache-passphrase |
113 | "If t, cache passphrase." | |
114 | :group 'mime-security | |
115 | :type 'boolean) | |
116 | ||
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 | |
122 | :type 'integer) | |
123 | ||
124 | (defcustom mml2015-signers nil | |
e8797701 | 125 | "A list of your own key ID(s) which will be used to sign a message. |
bb760c5d | 126 | If set, it overrides the setting of `mml2015-sign-with-sender'." |
01c52d31 MB |
127 | :group 'mime-security |
128 | :type '(repeat (string :tag "Key ID"))) | |
129 | ||
19cc6697 G |
130 | (defcustom mml2015-sign-with-sender nil |
131 | "If t, use message sender so find a key to sign with." | |
132 | :group 'mime-security | |
133 | :type 'boolean | |
134 | :version "24.1") | |
135 | ||
01c52d31 MB |
136 | (defcustom mml2015-encrypt-to-self nil |
137 | "If t, add your own key ID to recipient list when encryption." | |
138 | :group 'mime-security | |
139 | :type 'boolean) | |
140 | ||
141 | (defcustom mml2015-always-trust t | |
142 | "If t, GnuPG skip key validation on encryption." | |
143 | :group 'mime-security | |
144 | :type 'boolean) | |
145 | ||
146 | ;; Extract plaintext from cleartext signature. IMO, this kind of task | |
147 | ;; should be done by GnuPG rather than Elisp, but older PGP backends | |
8ccbef23 | 148 | ;; (such as Mailcrypt, and PGG) discard the output from GnuPG. |
01c52d31 | 149 | (defun mml2015-extract-cleartext-signature () |
8f7abae3 MB |
150 | ;; Daiki Ueno in |
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.'' | |
156 | ;; | |
157 | ;; This function doesn't handle NotDashEscaped correctly. EasyPG handles it | |
158 | ;; correctly. | |
159 | ;; http://thread.gmane.org/gmane.emacs.gnus.general/66062/focus=66082 | |
160 | ;; http://thread.gmane.org/gmane.emacs.gnus.general/65087/focus=65109 | |
01c52d31 MB |
161 | (goto-char (point-min)) |
162 | (forward-line) | |
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 | |
166 | ;; message. | |
167 | (while (looking-at "Hash:") ; The only header allowed in cleartext | |
168 | (forward-line)) ; signatures according to RFC2440. | |
169 | (when (looking-at "[\t ]*$") | |
170 | (forward-line)) | |
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) | |
177 | (forward-line 1))) | |
178 | ||
23f87bed MB |
179 | ;;; mailcrypt wrapper |
180 | ||
8abf1b22 GM |
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") | |
23f87bed | 187 | |
23f87bed MB |
188 | (defvar mml2015-decrypt-function 'mailcrypt-decrypt) |
189 | (defvar mml2015-verify-function 'mailcrypt-verify) | |
190 | ||
191 | (defun mml2015-format-error (err) | |
192 | (if (stringp (cadr err)) | |
193 | (cadr err) | |
194 | (format "%S" (cdr err)))) | |
195 | ||
196 | (defun mml2015-mailcrypt-decrypt (handle ctl) | |
197 | (catch 'error | |
198 | (let (child handles result) | |
199 | (unless (setq child (mm-find-part-by-type | |
200 | (cdr handle) | |
201 | "application/octet-stream" nil t)) | |
202 | (mm-set-handle-multipart-parameter | |
203 | mm-security-handle 'gnus-info "Corrupted") | |
204 | (throw 'error handle)) | |
205 | (with-temp-buffer | |
206 | (mm-insert-part child) | |
207 | (setq result | |
208 | (condition-case err | |
209 | (funcall mml2015-decrypt-function) | |
210 | (error | |
211 | (mm-set-handle-multipart-parameter | |
212 | mm-security-handle 'gnus-details (mml2015-format-error err)) | |
213 | nil) | |
214 | (quit | |
215 | (mm-set-handle-multipart-parameter | |
216 | mm-security-handle 'gnus-details "Quit.") | |
217 | nil))) | |
218 | (unless (car result) | |
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 | |
226 | (concat "OK" | |
227 | (let ((sig (with-current-buffer mml2015-result-buffer | |
228 | (mml2015-gpg-extract-signature-details)))) | |
229 | (concat ", Signer: " sig)))) | |
230 | (if (listp (car handles)) | |
231 | handles | |
232 | (list handles))))) | |
233 | ||
8ccbef23 G |
234 | (defun mml2015-gpg-pretty-print-fpr (fingerprint) |
235 | (let* ((result "") | |
236 | (fpr-length (string-width fingerprint)) | |
237 | (n-slice 0) | |
238 | slice) | |
239 | (setq fingerprint (string-to-list fingerprint)) | |
240 | (while 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)) | |
245 | (setq result | |
246 | (concat | |
247 | result | |
248 | (case n-slice | |
249 | (1 slice) | |
250 | (otherwise (concat " " slice)))))) | |
251 | result)) | |
252 | ||
253 | (defun mml2015-gpg-extract-signature-details () | |
254 | (goto-char (point-min)) | |
255 | (let* ((expired (re-search-forward | |
256 | "^\\[GNUPG:\\] SIGEXPIRED$" | |
257 | nil t)) | |
258 | (signer (and (re-search-forward | |
259 | "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$" | |
260 | nil t) | |
261 | (cons (match-string 1) (match-string 2)))) | |
262 | (fprint (and (re-search-forward | |
263 | "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) " | |
264 | nil t) | |
265 | (match-string 1))) | |
266 | (trust (and (re-search-forward | |
267 | "^\\[GNUPG:\\] \\(TRUST_.*\\)$" | |
268 | nil t) | |
269 | (match-string 1))) | |
270 | (trust-good-enough-p | |
271 | (cdr (assoc trust mml2015-unabbrev-trust-alist)))) | |
272 | (cond ((and signer fprint) | |
273 | (concat (cdr signer) | |
274 | (unless trust-good-enough-p | |
275 | (concat "\nUntrusted, Fingerprint: " | |
276 | (mml2015-gpg-pretty-print-fpr fprint))) | |
277 | (when expired | |
278 | (format "\nWARNING: Signature from expired key (%s)" | |
279 | (car signer))))) | |
280 | ((re-search-forward | |
281 | "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t) | |
282 | (match-string 2)) | |
283 | (t | |
284 | "From unknown user")))) | |
285 | ||
23f87bed MB |
286 | (defun mml2015-mailcrypt-clear-decrypt () |
287 | (let (result) | |
288 | (setq result | |
289 | (condition-case err | |
290 | (funcall mml2015-decrypt-function) | |
291 | (error | |
292 | (mm-set-handle-multipart-parameter | |
293 | mm-security-handle 'gnus-details (mml2015-format-error err)) | |
294 | nil) | |
295 | (quit | |
296 | (mm-set-handle-multipart-parameter | |
297 | mm-security-handle 'gnus-details "Quit.") | |
298 | nil))) | |
299 | (if (car result) | |
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")))) | |
304 | ||
305 | (defun mml2015-fix-micalg (alg) | |
306 | (and 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)) | |
310 | alg)))) | |
311 | ||
312 | (defun mml2015-mailcrypt-verify (handle ctl) | |
313 | (catch 'error | |
314 | (let (part) | |
315 | (unless (setq part (mm-find-raw-part-by-type | |
316 | ctl (or (mm-handle-multipart-ctl-parameter | |
317 | ctl 'protocol) | |
318 | "application/pgp-signature") | |
319 | t)) | |
320 | (mm-set-handle-multipart-parameter | |
321 | mm-security-handle 'gnus-info "Corrupted") | |
322 | (throw 'error handle)) | |
323 | (with-temp-buffer | |
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 | |
328 | ctl 'micalg)) | |
329 | "SHA1"))) | |
330 | (save-restriction | |
331 | (narrow-to-region (point) (point)) | |
332 | (insert part "\n") | |
333 | (goto-char (point-min)) | |
334 | (while (not (eobp)) | |
335 | (if (looking-at "^-") | |
336 | (insert "- ")) | |
337 | (forward-line))) | |
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)) | |
343 | (save-restriction | |
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 | |
353 | (prog1 | |
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" | |
359 | (buffer-string)))) | |
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))) | |
368 | (error | |
369 | (mm-set-handle-multipart-parameter | |
370 | mm-security-handle 'gnus-details (mml2015-format-error err)) | |
371 | nil) | |
372 | (quit | |
373 | (mm-set-handle-multipart-parameter | |
374 | mm-security-handle 'gnus-details "Quit.") | |
375 | nil)) | |
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") | |
381 | handle))) | |
382 | ||
383 | (defun mml2015-mailcrypt-clear-verify () | |
384 | (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*"))) | |
385 | (if (condition-case err | |
386 | (prog1 | |
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" | |
392 | (buffer-string)))) | |
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))) | |
401 | (error | |
402 | (mm-set-handle-multipart-parameter | |
403 | mm-security-handle 'gnus-details (mml2015-format-error err)) | |
404 | nil) | |
405 | (quit | |
406 | (mm-set-handle-multipart-parameter | |
407 | mm-security-handle 'gnus-details "Quit.") | |
408 | nil)) | |
409 | (mm-set-handle-multipart-parameter | |
410 | mm-security-handle 'gnus-info "OK") | |
411 | (mm-set-handle-multipart-parameter | |
01c52d31 MB |
412 | mm-security-handle 'gnus-info "Failed"))) |
413 | (mml2015-extract-cleartext-signature)) | |
23f87bed MB |
414 | |
415 | (defun mml2015-mailcrypt-sign (cont) | |
416 | (mc-sign-generic (message-options-get 'message-sender) | |
417 | nil nil nil nil) | |
418 | (let ((boundary (mml-compute-boundary cont)) | |
419 | hash point) | |
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)) | |
424 | (forward-line 1) | |
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")) | |
430 | (forward-line 1) | |
431 | (delete-region (point-min) (point)) | |
432 | (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n" | |
433 | boundary)) | |
434 | (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n" | |
435 | (downcase hash))) | |
436 | (insert (format "\n--%s\n" boundary)) | |
437 | (setq point (point)) | |
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?$" | |
444 | nil t) | |
445 | (error "Cannot find signature part")) | |
446 | (replace-match "-----BEGIN PGP MESSAGE-----" t t) | |
447 | (goto-char (match-beginning 0)) | |
448 | (save-restriction | |
449 | (narrow-to-region point (point)) | |
450 | (goto-char 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)))) | |
459 | ||
5a12b40c GM |
460 | ;; We require mm-decode, which requires mm-bodies, which autoloads |
461 | ;; message-options-get (!). | |
462 | (declare-function message-options-set "message" (symbol value)) | |
463 | ||
23f87bed MB |
464 | (defun mml2015-mailcrypt-encrypt (cont &optional sign) |
465 | (let ((mc-pgp-always-sign | |
466 | (or mc-pgp-always-sign | |
467 | sign | |
468 | (eq t (or (message-options-get 'message-sign-encrypt) | |
469 | (message-options-set | |
470 | 'message-sign-encrypt | |
471 | (or (y-or-n-p "Sign the message? ") | |
472 | 'not)))) | |
473 | 'never))) | |
474 | (mm-with-unibyte-current-buffer | |
475 | (mc-encrypt-generic | |
476 | (or (message-options-get 'message-recipients) | |
477 | (message-options-set 'message-recipients | |
478 | (mc-cleanup-recipient-headers | |
479 | (read-string "Recipients: ")))) | |
480 | nil nil nil | |
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" | |
487 | boundary)) | |
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)))) | |
497 | ||
23f87bed MB |
498 | ;;; pgg wrapper |
499 | ||
9efa445f DN |
500 | (defvar pgg-default-user-id) |
501 | (defvar pgg-errors-buffer) | |
502 | (defvar pgg-output-buffer) | |
23f87bed | 503 | |
8abf1b22 GM |
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") | |
23f87bed MB |
509 | |
510 | (defun mml2015-pgg-decrypt (handle ctl) | |
511 | (catch 'error | |
512 | (let ((pgg-errors-buffer mml2015-result-buffer) | |
513 | child handles result decrypt-status) | |
514 | (unless (setq child (mm-find-part-by-type | |
515 | (cdr handle) | |
516 | "application/octet-stream" nil t)) | |
517 | (mm-set-handle-multipart-parameter | |
518 | mm-security-handle 'gnus-info "Corrupted") | |
519 | (throw 'error handle)) | |
520 | (with-temp-buffer | |
521 | (mm-insert-part child) | |
522 | (if (condition-case err | |
523 | (prog1 | |
524 | (pgg-decrypt-region (point-min) (point-max)) | |
bf247b6e | 525 | (setq decrypt-status |
23f87bed MB |
526 | (with-current-buffer mml2015-result-buffer |
527 | (buffer-string))) | |
528 | (mm-set-handle-multipart-parameter | |
529 | mm-security-handle 'gnus-details | |
530 | decrypt-status)) | |
531 | (error | |
532 | (mm-set-handle-multipart-parameter | |
533 | mm-security-handle 'gnus-details (mml2015-format-error err)) | |
534 | nil) | |
535 | (quit | |
536 | (mm-set-handle-multipart-parameter | |
537 | mm-security-handle 'gnus-details "Quit.") | |
538 | nil)) | |
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)) | |
554 | handles | |
555 | (list handles))) | |
556 | (mm-set-handle-multipart-parameter | |
557 | mm-security-handle 'gnus-info "Failed") | |
558 | (throw 'error handle)))))) | |
559 | ||
560 | (defun mml2015-pgg-clear-decrypt () | |
561 | (let ((pgg-errors-buffer mml2015-result-buffer)) | |
562 | (if (prog1 | |
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 | |
567 | (buffer-string)))) | |
568 | (progn | |
569 | (erase-buffer) | |
9606f1cb MB |
570 | ;; Treat data which pgg returns as a unibyte string. |
571 | (mm-disable-multibyte) | |
23f87bed MB |
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")))) | |
580 | ||
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 | |
586 | ctl 'protocol) | |
587 | "application/pgp-signature") | |
588 | t))) | |
589 | (null (setq signature (mm-find-part-by-type | |
590 | (cdr handle) "application/pgp-signature" nil t)))) | |
591 | (progn | |
592 | (mm-set-handle-multipart-parameter | |
593 | mm-security-handle 'gnus-info "Corrupted") | |
594 | handle) | |
595 | (with-temp-buffer | |
596 | (insert part) | |
01c52d31 MB |
597 | ;; Convert <LF> to <CR><LF> in signed text. If --textmode is |
598 | ;; specified when signing, the conversion is not necessary. | |
23f87bed MB |
599 | (goto-char (point-min)) |
600 | (end-of-line) | |
601 | (while (not (eobp)) | |
602 | (unless (eq (char-before) ?\r) | |
603 | (insert "\r")) | |
604 | (forward-line) | |
605 | (end-of-line)) | |
606 | (with-temp-file (setq signature-file (mm-make-temp-file "pgg")) | |
607 | (mm-insert-part signature)) | |
608 | (if (condition-case err | |
609 | (prog1 | |
bf247b6e | 610 | (pgg-verify-region (point-min) (point-max) |
23f87bed MB |
611 | signature-file t) |
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 | |
618 | (buffer-string)) | |
619 | (with-current-buffer pgg-errors-buffer | |
620 | (buffer-string))))) | |
621 | (error | |
622 | (mm-set-handle-multipart-parameter | |
623 | mm-security-handle 'gnus-details (mml2015-format-error err)) | |
624 | nil) | |
625 | (quit | |
626 | (mm-set-handle-multipart-parameter | |
627 | mm-security-handle 'gnus-details "Quit.") | |
628 | nil)) | |
629 | (progn | |
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"))))) | |
638 | handle) | |
639 | ||
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 | |
645 | (prog1 | |
646 | (mm-with-unibyte-buffer | |
5538c331 | 647 | (insert (mm-encode-coding-string text coding-system)) |
23f87bed MB |
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 | |
655 | (buffer-string)) | |
656 | (with-current-buffer pgg-errors-buffer | |
657 | (buffer-string))))) | |
658 | (error | |
659 | (mm-set-handle-multipart-parameter | |
660 | mm-security-handle 'gnus-details (mml2015-format-error err)) | |
661 | nil) | |
662 | (quit | |
663 | (mm-set-handle-multipart-parameter | |
664 | mm-security-handle 'gnus-details "Quit.") | |
665 | nil)) | |
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 | |
01c52d31 MB |
671 | mm-security-handle 'gnus-info "Failed"))) |
672 | (mml2015-extract-cleartext-signature)) | |
23f87bed MB |
673 | |
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) | |
bcc7dd61 | 678 | pgg-default-user-id)) |
34128042 | 679 | (pgg-text-mode t) |
bcc7dd61 | 680 | entry) |
23f87bed MB |
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" | |
686 | boundary)) | |
bcc7dd61 MB |
687 | (if (setq entry (assq 2 (pgg-parse-armor |
688 | (with-current-buffer pgg-output-buffer | |
689 | (buffer-string))))) | |
690 | (setq entry (assq 'hash-algorithm (cdr entry)))) | |
691 | (insert (format "\tmicalg=%s; " | |
692 | (if (cdr entry) | |
693 | (downcase (format "pgp-%s" (cdr entry))) | |
694 | "pgp-sha1"))) | |
695 | (insert "protocol=\"application/pgp-signature\"\n") | |
23f87bed MB |
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)))) | |
704 | ||
705 | (defun mml2015-pgg-encrypt (cont &optional sign) | |
706 | (let ((pgg-errors-buffer mml2015-result-buffer) | |
34128042 | 707 | (pgg-text-mode t) |
23f87bed MB |
708 | (boundary (mml-compute-boundary cont))) |
709 | (unless (pgg-encrypt-region (point-min) (point-max) | |
710 | (split-string | |
711 | (or | |
712 | (message-options-get 'message-recipients) | |
713 | (message-options-set 'message-recipients | |
714 | (read-string "Recipients: "))) | |
715 | "[ \f\t\n\r\v,]+") | |
716 | sign) | |
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" | |
722 | boundary)) | |
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)))) | |
733 | ||
01c52d31 MB |
734 | ;;; epg wrapper |
735 | ||
9efa445f DN |
736 | (defvar epg-user-id-alist) |
737 | (defvar epg-digest-algorithm-alist) | |
f1c9aabc | 738 | (defvar epg-gpg-program) |
9efa445f | 739 | (defvar inhibit-redisplay) |
01c52d31 | 740 | |
8abf1b22 GM |
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") | |
8abf1b22 GM |
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") | |
ad09474a | 757 | (autoload 'epg-sub-key-fingerprint "epg") |
f1c9aabc DU |
758 | (autoload 'epg-signature-key-id "epg") |
759 | (autoload 'epg-signature-to-string "epg") | |
8abf1b22 GM |
760 | (autoload 'epg-configuration "epg-config") |
761 | (autoload 'epg-expand-group "epg-config") | |
762 | (autoload 'epa-select-keys "epa") | |
01c52d31 | 763 | |
01c52d31 MB |
764 | (defvar mml2015-epg-secret-key-id-list nil) |
765 | ||
766 | (defun mml2015-epg-passphrase-callback (context key-id ignore) | |
767 | (if (eq key-id 'SYM) | |
768 | (epg-passphrase-callback-function context key-id nil) | |
9b3ebcb6 MB |
769 | (let* ((password-cache-key-id |
770 | (if (eq key-id 'PIN) | |
771 | "PIN" | |
772 | key-id)) | |
773 | entry | |
01c52d31 MB |
774 | (passphrase |
775 | (password-read | |
776 | (if (eq key-id 'PIN) | |
777 | "Passphrase for PIN: " | |
778 | (if (setq entry (assoc key-id epg-user-id-alist)) | |
779 | (format "Passphrase for %s %s: " key-id (cdr entry)) | |
780 | (format "Passphrase for %s: " key-id))) | |
9b3ebcb6 | 781 | password-cache-key-id))) |
01c52d31 MB |
782 | (when passphrase |
783 | (let ((password-cache-expiry mml2015-passphrase-cache-expiry)) | |
9b3ebcb6 | 784 | (password-cache-add password-cache-key-id passphrase)) |
01c52d31 | 785 | (setq mml2015-epg-secret-key-id-list |
9b3ebcb6 | 786 | (cons password-cache-key-id mml2015-epg-secret-key-id-list)) |
01c52d31 MB |
787 | (copy-sequence passphrase))))) |
788 | ||
789 | (defun mml2015-epg-find-usable-key (keys usage) | |
790 | (catch 'found | |
791 | (while keys | |
792 | (let ((pointer (epg-key-sub-key-list (car keys)))) | |
344465fd DU |
793 | ;; The primary key will be marked as disabled, when the entire |
794 | ;; key is disabled (see 12 Field, Format of colon listings, in | |
795 | ;; gnupg/doc/DETAILS) | |
796 | (unless (memq 'disabled (epg-sub-key-capability (car pointer))) | |
797 | (while pointer | |
798 | (if (and (memq usage (epg-sub-key-capability (car pointer))) | |
799 | (not (memq (epg-sub-key-validity (car pointer)) | |
800 | '(revoked expired)))) | |
801 | (throw 'found (car keys))) | |
802 | (setq pointer (cdr pointer))))) | |
01c52d31 MB |
803 | (setq keys (cdr keys))))) |
804 | ||
ad09474a DU |
805 | ;; XXX: since gpg --list-secret-keys does not return validity of each |
806 | ;; key, `mml2015-epg-find-usable-key' defined above is not enough for | |
807 | ;; secret keys. The function `mml2015-epg-find-usable-secret-key' | |
808 | ;; below looks at appropriate public keys to check usability. | |
809 | (defun mml2015-epg-find-usable-secret-key (context name usage) | |
810 | (let ((secret-keys (epg-list-keys context name t)) | |
811 | secret-key) | |
812 | (while (and (not secret-key) secret-keys) | |
813 | (if (mml2015-epg-find-usable-key | |
814 | (epg-list-keys context (epg-sub-key-fingerprint | |
815 | (car (epg-key-sub-key-list | |
816 | (car secret-keys))))) | |
817 | usage) | |
818 | (setq secret-key (car secret-keys) | |
819 | secret-keys nil) | |
820 | (setq secret-keys (cdr secret-keys)))) | |
821 | secret-key)) | |
822 | ||
f1c9aabc DU |
823 | (defun mml2015-epg-key-image (key-id) |
824 | "Return the image of a key, if any" | |
da42d41a G |
825 | (with-temp-buffer |
826 | (mm-set-buffer-multibyte nil) | |
827 | (let* ((coding-system-for-write 'binary) | |
828 | (coding-system-for-read 'binary) | |
829 | (data (shell-command-to-string | |
830 | (format "%s --list-options no-show-photos --attribute-fd 2 --list-keys %s > /dev/null" | |
831 | epg-gpg-program key-id)))) | |
832 | (when (> (length data) 0) | |
833 | (insert (substring data 16)) | |
834 | (create-image (buffer-string) nil t))))) | |
f1c9aabc DU |
835 | |
836 | (defun mml2015-epg-key-image-to-string (key-id) | |
837 | "Return a string with the image of a key, if any" | |
838 | (let* ((result "") | |
839 | (key-image (mml2015-epg-key-image key-id))) | |
840 | (when key-image | |
841 | (setq result " ") | |
842 | (put-text-property 1 2 'display key-image result)) | |
843 | result)) | |
844 | ||
845 | (defun mml2015-epg-signature-to-string (signature) | |
846 | (concat (epg-signature-to-string signature) | |
847 | (mml2015-epg-key-image-to-string (epg-signature-key-id signature)))) | |
848 | ||
849 | (defun mml2015-epg-verify-result-to-string (verify-result) | |
850 | (mapconcat #'mml2015-epg-signature-to-string verify-result "\n")) | |
851 | ||
01c52d31 MB |
852 | (defun mml2015-epg-decrypt (handle ctl) |
853 | (catch 'error | |
854 | (let ((inhibit-redisplay t) | |
855 | context plain child handles result decrypt-status) | |
856 | (unless (setq child (mm-find-part-by-type | |
857 | (cdr handle) | |
858 | "application/octet-stream" nil t)) | |
859 | (mm-set-handle-multipart-parameter | |
860 | mm-security-handle 'gnus-info "Corrupted") | |
861 | (throw 'error handle)) | |
862 | (setq context (epg-make-context)) | |
863 | (if mml2015-cache-passphrase | |
864 | (epg-context-set-passphrase-callback | |
865 | context | |
866 | #'mml2015-epg-passphrase-callback)) | |
867 | (condition-case error | |
868 | (setq plain (epg-decrypt-string context (mm-get-part child)) | |
869 | mml2015-epg-secret-key-id-list nil) | |
870 | (error | |
871 | (while mml2015-epg-secret-key-id-list | |
872 | (password-cache-remove (car mml2015-epg-secret-key-id-list)) | |
873 | (setq mml2015-epg-secret-key-id-list | |
874 | (cdr mml2015-epg-secret-key-id-list))) | |
875 | (mm-set-handle-multipart-parameter | |
876 | mm-security-handle 'gnus-info "Failed") | |
877 | (if (eq (car error) 'quit) | |
878 | (mm-set-handle-multipart-parameter | |
879 | mm-security-handle 'gnus-details "Quit.") | |
880 | (mm-set-handle-multipart-parameter | |
881 | mm-security-handle 'gnus-details (mml2015-format-error error))) | |
882 | (throw 'error handle))) | |
883 | (with-temp-buffer | |
884 | (insert plain) | |
885 | (goto-char (point-min)) | |
886 | (while (search-forward "\r\n" nil t) | |
887 | (replace-match "\n" t t)) | |
888 | (setq handles (mm-dissect-buffer t)) | |
889 | (mm-destroy-parts handle) | |
890 | (if (epg-context-result-for context 'verify) | |
891 | (mm-set-handle-multipart-parameter | |
892 | mm-security-handle 'gnus-info | |
893 | (concat "OK\n" | |
f1c9aabc | 894 | (mml2015-epg-verify-result-to-string |
01c52d31 MB |
895 | (epg-context-result-for context 'verify)))) |
896 | (mm-set-handle-multipart-parameter | |
897 | mm-security-handle 'gnus-info "OK")) | |
898 | (if (stringp (car handles)) | |
899 | (mm-set-handle-multipart-parameter | |
900 | mm-security-handle 'gnus-details | |
901 | (mm-handle-multipart-ctl-parameter handles 'gnus-details)))) | |
902 | (if (listp (car handles)) | |
903 | handles | |
904 | (list handles))))) | |
905 | ||
906 | (defun mml2015-epg-clear-decrypt () | |
907 | (let ((inhibit-redisplay t) | |
908 | (context (epg-make-context)) | |
909 | plain) | |
910 | (if mml2015-cache-passphrase | |
911 | (epg-context-set-passphrase-callback | |
912 | context | |
913 | #'mml2015-epg-passphrase-callback)) | |
914 | (condition-case error | |
915 | (setq plain (epg-decrypt-string context (buffer-string)) | |
916 | mml2015-epg-secret-key-id-list nil) | |
917 | (error | |
918 | (while mml2015-epg-secret-key-id-list | |
919 | (password-cache-remove (car mml2015-epg-secret-key-id-list)) | |
920 | (setq mml2015-epg-secret-key-id-list | |
921 | (cdr mml2015-epg-secret-key-id-list))) | |
922 | (mm-set-handle-multipart-parameter | |
923 | mm-security-handle 'gnus-info "Failed") | |
924 | (if (eq (car error) 'quit) | |
925 | (mm-set-handle-multipart-parameter | |
926 | mm-security-handle 'gnus-details "Quit.") | |
927 | (mm-set-handle-multipart-parameter | |
928 | mm-security-handle 'gnus-details (mml2015-format-error error))))) | |
929 | (when plain | |
930 | (erase-buffer) | |
931 | ;; Treat data which epg returns as a unibyte string. | |
932 | (mm-disable-multibyte) | |
933 | (insert plain) | |
934 | (goto-char (point-min)) | |
935 | (while (search-forward "\r\n" nil t) | |
936 | (replace-match "\n" t t)) | |
937 | (mm-set-handle-multipart-parameter | |
938 | mm-security-handle 'gnus-info "OK") | |
939 | (if (epg-context-result-for context 'verify) | |
940 | (mm-set-handle-multipart-parameter | |
941 | mm-security-handle 'gnus-details | |
f1c9aabc | 942 | (mml2015-epg-verify-result-to-string |
01c52d31 MB |
943 | (epg-context-result-for context 'verify))))))) |
944 | ||
945 | (defun mml2015-epg-verify (handle ctl) | |
946 | (catch 'error | |
947 | (let ((inhibit-redisplay t) | |
948 | context plain signature-file part signature) | |
949 | (when (or (null (setq part (mm-find-raw-part-by-type | |
950 | ctl (or (mm-handle-multipart-ctl-parameter | |
951 | ctl 'protocol) | |
952 | "application/pgp-signature") | |
953 | t))) | |
954 | (null (setq signature (mm-find-part-by-type | |
955 | (cdr handle) "application/pgp-signature" | |
956 | nil t)))) | |
957 | (mm-set-handle-multipart-parameter | |
958 | mm-security-handle 'gnus-info "Corrupted") | |
959 | (throw 'error handle)) | |
0f3de88f | 960 | (setq part (mm-replace-in-string part "\n" "\r\n") |
01c52d31 MB |
961 | signature (mm-get-part signature) |
962 | context (epg-make-context)) | |
963 | (condition-case error | |
964 | (setq plain (epg-verify-string context signature part)) | |
965 | (error | |
966 | (mm-set-handle-multipart-parameter | |
967 | mm-security-handle 'gnus-info "Failed") | |
968 | (if (eq (car error) 'quit) | |
969 | (mm-set-handle-multipart-parameter | |
970 | mm-security-handle 'gnus-details "Quit.") | |
971 | (mm-set-handle-multipart-parameter | |
972 | mm-security-handle 'gnus-details (mml2015-format-error error))) | |
973 | (throw 'error handle))) | |
974 | (mm-set-handle-multipart-parameter | |
975 | mm-security-handle 'gnus-info | |
f1c9aabc DU |
976 | (mml2015-epg-verify-result-to-string |
977 | (epg-context-result-for context 'verify))) | |
01c52d31 MB |
978 | handle))) |
979 | ||
980 | (defun mml2015-epg-clear-verify () | |
981 | (let ((inhibit-redisplay t) | |
982 | (context (epg-make-context)) | |
983 | (signature (mm-encode-coding-string (buffer-string) | |
984 | coding-system-for-write)) | |
985 | plain) | |
986 | (condition-case error | |
987 | (setq plain (epg-verify-string context signature)) | |
988 | (error | |
989 | (mm-set-handle-multipart-parameter | |
990 | mm-security-handle 'gnus-info "Failed") | |
991 | (if (eq (car error) 'quit) | |
992 | (mm-set-handle-multipart-parameter | |
993 | mm-security-handle 'gnus-details "Quit.") | |
994 | (mm-set-handle-multipart-parameter | |
995 | mm-security-handle 'gnus-details (mml2015-format-error error))))) | |
996 | (if plain | |
997 | (progn | |
998 | (mm-set-handle-multipart-parameter | |
999 | mm-security-handle 'gnus-info | |
f1c9aabc | 1000 | (mml2015-epg-verify-result-to-string |
01c52d31 MB |
1001 | (epg-context-result-for context 'verify))) |
1002 | (delete-region (point-min) (point-max)) | |
1003 | (insert (mm-decode-coding-string plain coding-system-for-read))) | |
1004 | (mml2015-extract-cleartext-signature)))) | |
1005 | ||
1006 | (defun mml2015-epg-sign (cont) | |
1007 | (let* ((inhibit-redisplay t) | |
1008 | (context (epg-make-context)) | |
1009 | (boundary (mml-compute-boundary cont)) | |
bb760c5d DU |
1010 | (sender (message-options-get 'message-sender)) |
1011 | (signer-names (or mml2015-signers | |
1012 | (if (and mml2015-sign-with-sender sender) | |
1013 | (list (concat "<" sender ">"))))) | |
01c52d31 MB |
1014 | signer-key |
1015 | (signers | |
1016 | (or (message-options-get 'mml2015-epg-signers) | |
1017 | (message-options-set | |
1018 | 'mml2015-epg-signers | |
54c72c31 | 1019 | (if (eq mm-sign-option 'guided) |
01c52d31 MB |
1020 | (epa-select-keys context "\ |
1021 | Select keys for signing. | |
1022 | If no one is selected, default secret key is used. " | |
bb760c5d | 1023 | signer-names |
fdee7cc1 | 1024 | t) |
809fde05 | 1025 | (if (or sender mml2015-signers) |
52bec650 MB |
1026 | (delq nil |
1027 | (mapcar | |
1028 | (lambda (signer) | |
ad09474a DU |
1029 | (setq signer-key |
1030 | (mml2015-epg-find-usable-secret-key | |
1031 | context signer 'sign)) | |
52bec650 MB |
1032 | (unless (or signer-key |
1033 | (y-or-n-p | |
1034 | (format | |
1035 | "No secret key for %s; skip it? " | |
1036 | signer))) | |
1037 | (error "No secret key for %s" signer)) | |
1038 | signer-key) | |
bb760c5d | 1039 | signer-names))))))) |
01c52d31 MB |
1040 | signature micalg) |
1041 | (epg-context-set-armor context t) | |
1042 | (epg-context-set-textmode context t) | |
1043 | (epg-context-set-signers context signers) | |
1044 | (if mml2015-cache-passphrase | |
1045 | (epg-context-set-passphrase-callback | |
1046 | context | |
1047 | #'mml2015-epg-passphrase-callback)) | |
1048 | (condition-case error | |
1049 | (setq signature (epg-sign-string context (buffer-string) t) | |
1050 | mml2015-epg-secret-key-id-list nil) | |
1051 | (error | |
1052 | (while mml2015-epg-secret-key-id-list | |
1053 | (password-cache-remove (car mml2015-epg-secret-key-id-list)) | |
1054 | (setq mml2015-epg-secret-key-id-list | |
1055 | (cdr mml2015-epg-secret-key-id-list))) | |
1056 | (signal (car error) (cdr error)))) | |
1057 | (if (epg-context-result-for context 'sign) | |
1058 | (setq micalg (epg-new-signature-digest-algorithm | |
1059 | (car (epg-context-result-for context 'sign))))) | |
1060 | (goto-char (point-min)) | |
1061 | (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n" | |
1062 | boundary)) | |
1063 | (if micalg | |
1064 | (insert (format "\tmicalg=pgp-%s; " | |
1065 | (downcase | |
1066 | (cdr (assq micalg | |
1067 | epg-digest-algorithm-alist)))))) | |
1068 | (insert "protocol=\"application/pgp-signature\"\n") | |
1069 | (insert (format "\n--%s\n" boundary)) | |
1070 | (goto-char (point-max)) | |
1071 | (insert (format "\n--%s\n" boundary)) | |
1072 | (insert "Content-Type: application/pgp-signature\n\n") | |
1073 | (insert signature) | |
1074 | (goto-char (point-max)) | |
1075 | (insert (format "--%s--\n" boundary)) | |
1076 | (goto-char (point-max)))) | |
1077 | ||
1078 | (defun mml2015-epg-encrypt (cont &optional sign) | |
bb760c5d DU |
1079 | (let* ((inhibit-redisplay t) |
1080 | (context (epg-make-context)) | |
1081 | (boundary (mml-compute-boundary cont)) | |
1082 | (config (epg-configuration)) | |
1083 | (recipients (message-options-get 'mml2015-epg-recipients)) | |
1084 | cipher | |
1085 | (sender (message-options-get 'message-sender)) | |
1086 | (signer-names (or mml2015-signers | |
1087 | (if (and mml2015-sign-with-sender sender) | |
1088 | (list (concat "<" sender ">"))))) | |
1089 | signers | |
1090 | recipient-key signer-key) | |
01c52d31 MB |
1091 | (unless recipients |
1092 | (setq recipients | |
1093 | (apply #'nconc | |
1094 | (mapcar | |
1095 | (lambda (recipient) | |
1096 | (or (epg-expand-group config recipient) | |
1097 | (list (concat "<" recipient ">")))) | |
1098 | (split-string | |
1099 | (or (message-options-get 'message-recipients) | |
1100 | (message-options-set 'message-recipients | |
1101 | (read-string "Recipients: "))) | |
1102 | "[ \f\t\n\r\v,]+")))) | |
1103 | (when mml2015-encrypt-to-self | |
bb760c5d DU |
1104 | (unless signer-names |
1105 | (error "Neither message sender nor mml2015-signers are set")) | |
1106 | (setq recipients (nconc recipients signer-names))) | |
54c72c31 | 1107 | (if (eq mm-encrypt-option 'guided) |
01c52d31 MB |
1108 | (setq recipients |
1109 | (epa-select-keys context "\ | |
1110 | Select recipients for encryption. | |
1111 | If no one is selected, symmetric encryption will be performed. " | |
1112 | recipients)) | |
1113 | (setq recipients | |
52bec650 MB |
1114 | (delq nil |
1115 | (mapcar | |
1116 | (lambda (recipient) | |
1117 | (setq recipient-key (mml2015-epg-find-usable-key | |
1118 | (epg-list-keys context recipient) | |
1119 | 'encrypt)) | |
1120 | (unless (or recipient-key | |
1121 | (y-or-n-p | |
1122 | (format "No public key for %s; skip it? " | |
1123 | recipient))) | |
1124 | (error "No public key for %s" recipient)) | |
1125 | recipient-key) | |
1126 | recipients))) | |
01c52d31 MB |
1127 | (unless recipients |
1128 | (error "No recipient specified"))) | |
1129 | (message-options-set 'mml2015-epg-recipients recipients)) | |
1130 | (when sign | |
1131 | (setq signers | |
1132 | (or (message-options-get 'mml2015-epg-signers) | |
1133 | (message-options-set | |
1134 | 'mml2015-epg-signers | |
54c72c31 | 1135 | (if (eq mm-sign-option 'guided) |
01c52d31 MB |
1136 | (epa-select-keys context "\ |
1137 | Select keys for signing. | |
1138 | If no one is selected, default secret key is used. " | |
bb760c5d | 1139 | signer-names |
fdee7cc1 | 1140 | t) |
809fde05 | 1141 | (if (or sender mml2015-signers) |
52bec650 MB |
1142 | (delq nil |
1143 | (mapcar | |
1144 | (lambda (signer) | |
ad09474a DU |
1145 | (setq signer-key |
1146 | (mml2015-epg-find-usable-secret-key | |
1147 | context signer 'sign)) | |
52bec650 MB |
1148 | (unless (or signer-key |
1149 | (y-or-n-p | |
1150 | (format | |
1151 | "No secret key for %s; skip it? " | |
1152 | signer))) | |
1153 | (error "No secret key for %s" signer)) | |
1154 | signer-key) | |
bb760c5d | 1155 | signer-names))))))) |
01c52d31 MB |
1156 | (epg-context-set-signers context signers)) |
1157 | (epg-context-set-armor context t) | |
1158 | (epg-context-set-textmode context t) | |
1159 | (if mml2015-cache-passphrase | |
1160 | (epg-context-set-passphrase-callback | |
1161 | context | |
1162 | #'mml2015-epg-passphrase-callback)) | |
1163 | (condition-case error | |
1164 | (setq cipher | |
1165 | (epg-encrypt-string context (buffer-string) recipients sign | |
1166 | mml2015-always-trust) | |
1167 | mml2015-epg-secret-key-id-list nil) | |
1168 | (error | |
1169 | (while mml2015-epg-secret-key-id-list | |
1170 | (password-cache-remove (car mml2015-epg-secret-key-id-list)) | |
1171 | (setq mml2015-epg-secret-key-id-list | |
1172 | (cdr mml2015-epg-secret-key-id-list))) | |
1173 | (signal (car error) (cdr error)))) | |
1174 | (delete-region (point-min) (point-max)) | |
1175 | (goto-char (point-min)) | |
1176 | (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n" | |
1177 | boundary)) | |
1178 | (insert "\tprotocol=\"application/pgp-encrypted\"\n\n") | |
1179 | (insert (format "--%s\n" boundary)) | |
1180 | (insert "Content-Type: application/pgp-encrypted\n\n") | |
1181 | (insert "Version: 1\n\n") | |
1182 | (insert (format "--%s\n" boundary)) | |
1183 | (insert "Content-Type: application/octet-stream\n\n") | |
1184 | (insert cipher) | |
1185 | (goto-char (point-max)) | |
1186 | (insert (format "--%s--\n" boundary)) | |
1187 | (goto-char (point-max)))) | |
1188 | ||
23f87bed MB |
1189 | ;;; General wrapper |
1190 | ||
5a12b40c GM |
1191 | (autoload 'gnus-buffer-live-p "gnus-util") |
1192 | (autoload 'gnus-get-buffer-create "gnus") | |
1193 | ||
23f87bed MB |
1194 | (defun mml2015-clean-buffer () |
1195 | (if (gnus-buffer-live-p mml2015-result-buffer) | |
1196 | (with-current-buffer mml2015-result-buffer | |
1197 | (erase-buffer) | |
1198 | t) | |
1199 | (setq mml2015-result-buffer | |
01c52d31 | 1200 | (gnus-get-buffer-create " *MML2015 Result*")) |
23f87bed MB |
1201 | nil)) |
1202 | ||
1203 | (defsubst mml2015-clear-decrypt-function () | |
1204 | (nth 6 (assq mml2015-use mml2015-function-alist))) | |
1205 | ||
1206 | (defsubst mml2015-clear-verify-function () | |
1207 | (nth 5 (assq mml2015-use mml2015-function-alist))) | |
1208 | ||
1209 | ;;;###autoload | |
1210 | (defun mml2015-decrypt (handle ctl) | |
1211 | (mml2015-clean-buffer) | |
1212 | (let ((func (nth 4 (assq mml2015-use mml2015-function-alist)))) | |
1213 | (if func | |
1214 | (funcall func handle ctl) | |
1215 | handle))) | |
1216 | ||
1217 | ;;;###autoload | |
1218 | (defun mml2015-decrypt-test (handle ctl) | |
1219 | mml2015-use) | |
1220 | ||
1221 | ;;;###autoload | |
1222 | (defun mml2015-verify (handle ctl) | |
1223 | (mml2015-clean-buffer) | |
1224 | (let ((func (nth 3 (assq mml2015-use mml2015-function-alist)))) | |
1225 | (if func | |
1226 | (funcall func handle ctl) | |
1227 | handle))) | |
1228 | ||
1229 | ;;;###autoload | |
1230 | (defun mml2015-verify-test (handle ctl) | |
1231 | mml2015-use) | |
1232 | ||
1233 | ;;;###autoload | |
1234 | (defun mml2015-encrypt (cont &optional sign) | |
1235 | (mml2015-clean-buffer) | |
1236 | (let ((func (nth 2 (assq mml2015-use mml2015-function-alist)))) | |
1237 | (if func | |
1238 | (funcall func cont sign) | |
1239 | (error "Cannot find encrypt function")))) | |
1240 | ||
1241 | ;;;###autoload | |
1242 | (defun mml2015-sign (cont) | |
1243 | (mml2015-clean-buffer) | |
1244 | (let ((func (nth 1 (assq mml2015-use mml2015-function-alist)))) | |
1245 | (if func | |
1246 | (funcall func cont) | |
1247 | (error "Cannot find sign function")))) | |
1248 | ||
1249 | ;;;###autoload | |
1250 | (defun mml2015-self-encrypt () | |
1251 | (mml2015-encrypt nil)) | |
1252 | ||
1253 | (provide 'mml2015) | |
1254 | ||
23f87bed | 1255 | ;;; mml2015.el ends here |