]> code.delx.au - gnu-emacs/blob - lisp/gnus/mml2015.el
Revision: miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-505
[gnu-emacs] / lisp / gnus / mml2015.el
1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 ;; Free Software Foundation, Inc.
4
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: PGP MIME MML
7
8 ;; This file is part of GNU Emacs.
9
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
12 ;; by the Free Software Foundation; either version 2, or (at your
13 ;; option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
28 ;; with both.
29
30 ;;; Code:
31
32 (eval-when-compile (require 'cl))
33 (require 'mm-decode)
34 (require 'mm-util)
35 (require 'mml)
36
37 (defvar mml2015-use (or
38 (progn
39 (ignore-errors
40 (require 'pgg))
41 (and (fboundp 'pgg-sign-region)
42 'pgg))
43 (progn
44 (ignore-errors
45 (require 'gpg))
46 (and (fboundp 'gpg-sign-detached)
47 'gpg))
48 (progn (ignore-errors
49 (load "mc-toplev"))
50 (and (fboundp 'mc-encrypt-generic)
51 (fboundp 'mc-sign-generic)
52 (fboundp 'mc-cleanup-recipient-headers)
53 'mailcrypt)))
54 "The package used for PGP/MIME.")
55
56 ;; Something is not RFC2015.
57 (defvar mml2015-function-alist
58 '((mailcrypt mml2015-mailcrypt-sign
59 mml2015-mailcrypt-encrypt
60 mml2015-mailcrypt-verify
61 mml2015-mailcrypt-decrypt
62 mml2015-mailcrypt-clear-verify
63 mml2015-mailcrypt-clear-decrypt)
64 (gpg mml2015-gpg-sign
65 mml2015-gpg-encrypt
66 mml2015-gpg-verify
67 mml2015-gpg-decrypt
68 mml2015-gpg-clear-verify
69 mml2015-gpg-clear-decrypt)
70 (pgg mml2015-pgg-sign
71 mml2015-pgg-encrypt
72 mml2015-pgg-verify
73 mml2015-pgg-decrypt
74 mml2015-pgg-clear-verify
75 mml2015-pgg-clear-decrypt))
76 "Alist of PGP/MIME functions.")
77
78 (defvar mml2015-result-buffer nil)
79
80 (defcustom mml2015-unabbrev-trust-alist
81 '(("TRUST_UNDEFINED" . nil)
82 ("TRUST_NEVER" . nil)
83 ("TRUST_MARGINAL" . t)
84 ("TRUST_FULLY" . t)
85 ("TRUST_ULTIMATE" . t))
86 "Map GnuPG trust output values to a boolean saying if you trust the key."
87 :version "22.1"
88 :group 'mime-security
89 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
90 (boolean :tag "Trust key"))))
91
92 ;;; mailcrypt wrapper
93
94 (eval-and-compile
95 (autoload 'mailcrypt-decrypt "mailcrypt")
96 (autoload 'mailcrypt-verify "mailcrypt")
97 (autoload 'mc-pgp-always-sign "mailcrypt")
98 (autoload 'mc-encrypt-generic "mc-toplev")
99 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
100 (autoload 'mc-sign-generic "mc-toplev"))
101
102 (eval-when-compile
103 (defvar mc-default-scheme)
104 (defvar mc-schemes))
105
106 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
107 (defvar mml2015-verify-function 'mailcrypt-verify)
108
109 (defun mml2015-format-error (err)
110 (if (stringp (cadr err))
111 (cadr err)
112 (format "%S" (cdr err))))
113
114 (defun mml2015-mailcrypt-decrypt (handle ctl)
115 (catch 'error
116 (let (child handles result)
117 (unless (setq child (mm-find-part-by-type
118 (cdr handle)
119 "application/octet-stream" nil t))
120 (mm-set-handle-multipart-parameter
121 mm-security-handle 'gnus-info "Corrupted")
122 (throw 'error handle))
123 (with-temp-buffer
124 (mm-insert-part child)
125 (setq result
126 (condition-case err
127 (funcall mml2015-decrypt-function)
128 (error
129 (mm-set-handle-multipart-parameter
130 mm-security-handle 'gnus-details (mml2015-format-error err))
131 nil)
132 (quit
133 (mm-set-handle-multipart-parameter
134 mm-security-handle 'gnus-details "Quit.")
135 nil)))
136 (unless (car result)
137 (mm-set-handle-multipart-parameter
138 mm-security-handle 'gnus-info "Failed")
139 (throw 'error handle))
140 (setq handles (mm-dissect-buffer t)))
141 (mm-destroy-parts handle)
142 (mm-set-handle-multipart-parameter
143 mm-security-handle 'gnus-info
144 (concat "OK"
145 (let ((sig (with-current-buffer mml2015-result-buffer
146 (mml2015-gpg-extract-signature-details))))
147 (concat ", Signer: " sig))))
148 (if (listp (car handles))
149 handles
150 (list handles)))))
151
152 (defun mml2015-mailcrypt-clear-decrypt ()
153 (let (result)
154 (setq result
155 (condition-case err
156 (funcall mml2015-decrypt-function)
157 (error
158 (mm-set-handle-multipart-parameter
159 mm-security-handle 'gnus-details (mml2015-format-error err))
160 nil)
161 (quit
162 (mm-set-handle-multipart-parameter
163 mm-security-handle 'gnus-details "Quit.")
164 nil)))
165 (if (car result)
166 (mm-set-handle-multipart-parameter
167 mm-security-handle 'gnus-info "OK")
168 (mm-set-handle-multipart-parameter
169 mm-security-handle 'gnus-info "Failed"))))
170
171 (defun mml2015-fix-micalg (alg)
172 (and alg
173 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
174 (upcase (if (string-match "^p[gh]p-" alg)
175 (substring alg (match-end 0))
176 alg))))
177
178 (defun mml2015-mailcrypt-verify (handle ctl)
179 (catch 'error
180 (let (part)
181 (unless (setq part (mm-find-raw-part-by-type
182 ctl (or (mm-handle-multipart-ctl-parameter
183 ctl 'protocol)
184 "application/pgp-signature")
185 t))
186 (mm-set-handle-multipart-parameter
187 mm-security-handle 'gnus-info "Corrupted")
188 (throw 'error handle))
189 (with-temp-buffer
190 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
191 (insert (format "Hash: %s\n\n"
192 (or (mml2015-fix-micalg
193 (mm-handle-multipart-ctl-parameter
194 ctl 'micalg))
195 "SHA1")))
196 (save-restriction
197 (narrow-to-region (point) (point))
198 (insert part "\n")
199 (goto-char (point-min))
200 (while (not (eobp))
201 (if (looking-at "^-")
202 (insert "- "))
203 (forward-line)))
204 (unless (setq part (mm-find-part-by-type
205 (cdr handle) "application/pgp-signature" nil t))
206 (mm-set-handle-multipart-parameter
207 mm-security-handle 'gnus-info "Corrupted")
208 (throw 'error handle))
209 (save-restriction
210 (narrow-to-region (point) (point))
211 (mm-insert-part part)
212 (goto-char (point-min))
213 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
214 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
215 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
216 (replace-match "-----END PGP SIGNATURE-----" t t)))
217 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
218 (unless (condition-case err
219 (prog1
220 (funcall mml2015-verify-function)
221 (if (get-buffer " *mailcrypt stderr temp")
222 (mm-set-handle-multipart-parameter
223 mm-security-handle 'gnus-details
224 (with-current-buffer " *mailcrypt stderr temp"
225 (buffer-string))))
226 (if (get-buffer " *mailcrypt stdout temp")
227 (kill-buffer " *mailcrypt stdout temp"))
228 (if (get-buffer " *mailcrypt stderr temp")
229 (kill-buffer " *mailcrypt stderr temp"))
230 (if (get-buffer " *mailcrypt status temp")
231 (kill-buffer " *mailcrypt status temp"))
232 (if (get-buffer mc-gpg-debug-buffer)
233 (kill-buffer mc-gpg-debug-buffer)))
234 (error
235 (mm-set-handle-multipart-parameter
236 mm-security-handle 'gnus-details (mml2015-format-error err))
237 nil)
238 (quit
239 (mm-set-handle-multipart-parameter
240 mm-security-handle 'gnus-details "Quit.")
241 nil))
242 (mm-set-handle-multipart-parameter
243 mm-security-handle 'gnus-info "Failed")
244 (throw 'error handle))))
245 (mm-set-handle-multipart-parameter
246 mm-security-handle 'gnus-info "OK")
247 handle)))
248
249 (defun mml2015-mailcrypt-clear-verify ()
250 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
251 (if (condition-case err
252 (prog1
253 (funcall mml2015-verify-function)
254 (if (get-buffer " *mailcrypt stderr temp")
255 (mm-set-handle-multipart-parameter
256 mm-security-handle 'gnus-details
257 (with-current-buffer " *mailcrypt stderr temp"
258 (buffer-string))))
259 (if (get-buffer " *mailcrypt stdout temp")
260 (kill-buffer " *mailcrypt stdout temp"))
261 (if (get-buffer " *mailcrypt stderr temp")
262 (kill-buffer " *mailcrypt stderr temp"))
263 (if (get-buffer " *mailcrypt status temp")
264 (kill-buffer " *mailcrypt status temp"))
265 (if (get-buffer mc-gpg-debug-buffer)
266 (kill-buffer mc-gpg-debug-buffer)))
267 (error
268 (mm-set-handle-multipart-parameter
269 mm-security-handle 'gnus-details (mml2015-format-error err))
270 nil)
271 (quit
272 (mm-set-handle-multipart-parameter
273 mm-security-handle 'gnus-details "Quit.")
274 nil))
275 (mm-set-handle-multipart-parameter
276 mm-security-handle 'gnus-info "OK")
277 (mm-set-handle-multipart-parameter
278 mm-security-handle 'gnus-info "Failed"))))
279
280 (defun mml2015-mailcrypt-sign (cont)
281 (mc-sign-generic (message-options-get 'message-sender)
282 nil nil nil nil)
283 (let ((boundary (mml-compute-boundary cont))
284 hash point)
285 (goto-char (point-min))
286 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
287 (error "Cannot find signed begin line"))
288 (goto-char (match-beginning 0))
289 (forward-line 1)
290 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
291 (error "Cannot not find PGP hash"))
292 (setq hash (match-string 1))
293 (unless (re-search-forward "^$" nil t)
294 (error "Cannot not find PGP message"))
295 (forward-line 1)
296 (delete-region (point-min) (point))
297 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
298 boundary))
299 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
300 (downcase hash)))
301 (insert (format "\n--%s\n" boundary))
302 (setq point (point))
303 (goto-char (point-max))
304 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
305 (error "Cannot find signature part"))
306 (replace-match "-----END PGP MESSAGE-----" t t)
307 (goto-char (match-beginning 0))
308 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
309 nil t)
310 (error "Cannot find signature part"))
311 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
312 (goto-char (match-beginning 0))
313 (save-restriction
314 (narrow-to-region point (point))
315 (goto-char point)
316 (while (re-search-forward "^- -" nil t)
317 (replace-match "-" t t))
318 (goto-char (point-max)))
319 (insert (format "--%s\n" boundary))
320 (insert "Content-Type: application/pgp-signature\n\n")
321 (goto-char (point-max))
322 (insert (format "--%s--\n" boundary))
323 (goto-char (point-max))))
324
325 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
326 (let ((mc-pgp-always-sign
327 (or mc-pgp-always-sign
328 sign
329 (eq t (or (message-options-get 'message-sign-encrypt)
330 (message-options-set
331 'message-sign-encrypt
332 (or (y-or-n-p "Sign the message? ")
333 'not))))
334 'never)))
335 (mm-with-unibyte-current-buffer
336 (mc-encrypt-generic
337 (or (message-options-get 'message-recipients)
338 (message-options-set 'message-recipients
339 (mc-cleanup-recipient-headers
340 (read-string "Recipients: "))))
341 nil nil nil
342 (message-options-get 'message-sender))))
343 (goto-char (point-min))
344 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
345 (error "Fail to encrypt the message"))
346 (let ((boundary (mml-compute-boundary cont)))
347 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
348 boundary))
349 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
350 (insert (format "--%s\n" boundary))
351 (insert "Content-Type: application/pgp-encrypted\n\n")
352 (insert "Version: 1\n\n")
353 (insert (format "--%s\n" boundary))
354 (insert "Content-Type: application/octet-stream\n\n")
355 (goto-char (point-max))
356 (insert (format "--%s--\n" boundary))
357 (goto-char (point-max))))
358
359 ;;; gpg wrapper
360
361 (eval-and-compile
362 (autoload 'gpg-decrypt "gpg")
363 (autoload 'gpg-verify "gpg")
364 (autoload 'gpg-verify-cleartext "gpg")
365 (autoload 'gpg-sign-detached "gpg")
366 (autoload 'gpg-sign-encrypt "gpg")
367 (autoload 'gpg-encrypt "gpg")
368 (autoload 'gpg-passphrase-read "gpg"))
369
370 (defun mml2015-gpg-passphrase ()
371 (or (message-options-get 'gpg-passphrase)
372 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
373
374 (defun mml2015-gpg-decrypt-1 ()
375 (let ((cipher (current-buffer)) plain result)
376 (if (with-temp-buffer
377 (prog1
378 (gpg-decrypt cipher (setq plain (current-buffer))
379 mml2015-result-buffer nil)
380 (mm-set-handle-multipart-parameter
381 mm-security-handle 'gnus-details
382 (with-current-buffer mml2015-result-buffer
383 (buffer-string)))
384 (set-buffer cipher)
385 (erase-buffer)
386 (insert-buffer-substring plain)
387 (goto-char (point-min))
388 (while (search-forward "\r\n" nil t)
389 (replace-match "\n" t t))))
390 '(t)
391 ;; Some wrong with the return value, check plain text buffer.
392 (if (> (point-max) (point-min))
393 '(t)
394 nil))))
395
396 (defun mml2015-gpg-decrypt (handle ctl)
397 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
398 (mml2015-mailcrypt-decrypt handle ctl)))
399
400 (defun mml2015-gpg-clear-decrypt ()
401 (let (result)
402 (setq result (mml2015-gpg-decrypt-1))
403 (if (car result)
404 (mm-set-handle-multipart-parameter
405 mm-security-handle 'gnus-info "OK")
406 (mm-set-handle-multipart-parameter
407 mm-security-handle 'gnus-info "Failed"))))
408
409 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
410 (let* ((result "")
411 (fpr-length (string-width fingerprint))
412 (n-slice 0)
413 slice)
414 (setq fingerprint (string-to-list fingerprint))
415 (while fingerprint
416 (setq fpr-length (- fpr-length 4))
417 (setq slice (butlast fingerprint fpr-length))
418 (setq fingerprint (nthcdr 4 fingerprint))
419 (setq n-slice (1+ n-slice))
420 (setq result
421 (concat
422 result
423 (case n-slice
424 (1 slice)
425 (otherwise (concat " " slice))))))
426 result))
427
428 (defun mml2015-gpg-extract-signature-details ()
429 (goto-char (point-min))
430 (let* ((expired (re-search-forward
431 "^\\[GNUPG:\\] SIGEXPIRED$"
432 nil t))
433 (signer (and (re-search-forward
434 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
435 nil t)
436 (cons (match-string 1) (match-string 2))))
437 (fprint (and (re-search-forward
438 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
439 nil t)
440 (match-string 1)))
441 (trust (and (re-search-forward
442 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
443 nil t)
444 (match-string 1)))
445 (trust-good-enough-p
446 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
447 (cond ((and signer fprint)
448 (concat (cdr signer)
449 (unless trust-good-enough-p
450 (concat "\nUntrusted, Fingerprint: "
451 (mml2015-gpg-pretty-print-fpr fprint)))
452 (when expired
453 (format "\nWARNING: Signature from expired key (%s)"
454 (car signer)))))
455 ((re-search-forward
456 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
457 (match-string 2))
458 (t
459 "From unknown user"))))
460
461 (defun mml2015-gpg-verify (handle ctl)
462 (catch 'error
463 (let (part message signature info-is-set-p)
464 (unless (setq part (mm-find-raw-part-by-type
465 ctl (or (mm-handle-multipart-ctl-parameter
466 ctl 'protocol)
467 "application/pgp-signature")
468 t))
469 (mm-set-handle-multipart-parameter
470 mm-security-handle 'gnus-info "Corrupted")
471 (throw 'error handle))
472 (with-temp-buffer
473 (setq message (current-buffer))
474 (insert part)
475 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
476 ;; clearsign use --textmode. The conversion is not necessary.
477 ;; In clearverify, the conversion is not necessary either.
478 (goto-char (point-min))
479 (end-of-line)
480 (while (not (eobp))
481 (unless (eq (char-before) ?\r)
482 (insert "\r"))
483 (forward-line)
484 (end-of-line))
485 (with-temp-buffer
486 (setq signature (current-buffer))
487 (unless (setq part (mm-find-part-by-type
488 (cdr handle) "application/pgp-signature" nil t))
489 (mm-set-handle-multipart-parameter
490 mm-security-handle 'gnus-info "Corrupted")
491 (throw 'error handle))
492 (mm-insert-part part)
493 (unless (condition-case err
494 (prog1
495 (gpg-verify message signature mml2015-result-buffer)
496 (mm-set-handle-multipart-parameter
497 mm-security-handle 'gnus-details
498 (with-current-buffer mml2015-result-buffer
499 (buffer-string))))
500 (error
501 (mm-set-handle-multipart-parameter
502 mm-security-handle 'gnus-details (mml2015-format-error err))
503 (mm-set-handle-multipart-parameter
504 mm-security-handle 'gnus-info "Error.")
505 (setq info-is-set-p t)
506 nil)
507 (quit
508 (mm-set-handle-multipart-parameter
509 mm-security-handle 'gnus-details "Quit.")
510 (mm-set-handle-multipart-parameter
511 mm-security-handle 'gnus-info "Quit.")
512 (setq info-is-set-p t)
513 nil))
514 (unless info-is-set-p
515 (mm-set-handle-multipart-parameter
516 mm-security-handle 'gnus-info "Failed"))
517 (throw 'error handle)))
518 (mm-set-handle-multipart-parameter
519 mm-security-handle 'gnus-info
520 (with-current-buffer mml2015-result-buffer
521 (mml2015-gpg-extract-signature-details))))
522 handle)))
523
524 (defun mml2015-gpg-clear-verify ()
525 (if (condition-case err
526 (prog1
527 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
528 (mm-set-handle-multipart-parameter
529 mm-security-handle 'gnus-details
530 (with-current-buffer mml2015-result-buffer
531 (buffer-string))))
532 (error
533 (mm-set-handle-multipart-parameter
534 mm-security-handle 'gnus-details (mml2015-format-error err))
535 nil)
536 (quit
537 (mm-set-handle-multipart-parameter
538 mm-security-handle 'gnus-details "Quit.")
539 nil))
540 (mm-set-handle-multipart-parameter
541 mm-security-handle 'gnus-info
542 (with-current-buffer mml2015-result-buffer
543 (mml2015-gpg-extract-signature-details)))
544 (mm-set-handle-multipart-parameter
545 mm-security-handle 'gnus-info "Failed")))
546
547 (defun mml2015-gpg-sign (cont)
548 (let ((boundary (mml-compute-boundary cont))
549 (text (current-buffer)) signature)
550 (goto-char (point-max))
551 (unless (bolp)
552 (insert "\n"))
553 (with-temp-buffer
554 (unless (gpg-sign-detached text (setq signature (current-buffer))
555 mml2015-result-buffer
556 nil
557 (message-options-get 'message-sender)
558 t t) ; armor & textmode
559 (unless (> (point-max) (point-min))
560 (pop-to-buffer mml2015-result-buffer)
561 (error "Sign error")))
562 (goto-char (point-min))
563 (while (re-search-forward "\r+$" nil t)
564 (replace-match "" t t))
565 (set-buffer text)
566 (goto-char (point-min))
567 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
568 boundary))
569 ;;; FIXME: what is the micalg?
570 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
571 (insert (format "\n--%s\n" boundary))
572 (goto-char (point-max))
573 (insert (format "\n--%s\n" boundary))
574 (insert "Content-Type: application/pgp-signature\n\n")
575 (insert-buffer-substring signature)
576 (goto-char (point-max))
577 (insert (format "--%s--\n" boundary))
578 (goto-char (point-max)))))
579
580 (defun mml2015-gpg-encrypt (cont &optional sign)
581 (let ((boundary (mml-compute-boundary cont))
582 (text (current-buffer))
583 cipher)
584 (mm-with-unibyte-current-buffer
585 (with-temp-buffer
586 ;; set up a function to call the correct gpg encrypt routine
587 ;; with the right arguments. (FIXME: this should be done
588 ;; differently.)
589 (flet ((gpg-encrypt-func
590 (sign plaintext ciphertext result recipients &optional
591 passphrase sign-with-key armor textmode)
592 (if sign
593 (gpg-sign-encrypt
594 plaintext ciphertext result recipients passphrase
595 sign-with-key armor textmode)
596 (gpg-encrypt
597 plaintext ciphertext result recipients passphrase
598 armor textmode))))
599 (unless (gpg-encrypt-func
600 sign ; passed in when using signencrypt
601 text (setq cipher (current-buffer))
602 mml2015-result-buffer
603 (split-string
604 (or
605 (message-options-get 'message-recipients)
606 (message-options-set 'message-recipients
607 (read-string "Recipients: ")))
608 "[ \f\t\n\r\v,]+")
609 nil
610 (message-options-get 'message-sender)
611 t t) ; armor & textmode
612 (unless (> (point-max) (point-min))
613 (pop-to-buffer mml2015-result-buffer)
614 (error "Encrypt error"))))
615 (goto-char (point-min))
616 (while (re-search-forward "\r+$" nil t)
617 (replace-match "" t t))
618 (set-buffer text)
619 (delete-region (point-min) (point-max))
620 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
621 boundary))
622 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
623 (insert (format "--%s\n" boundary))
624 (insert "Content-Type: application/pgp-encrypted\n\n")
625 (insert "Version: 1\n\n")
626 (insert (format "--%s\n" boundary))
627 (insert "Content-Type: application/octet-stream\n\n")
628 (insert-buffer-substring cipher)
629 (goto-char (point-max))
630 (insert (format "--%s--\n" boundary))
631 (goto-char (point-max))))))
632
633 ;;; pgg wrapper
634
635 (eval-when-compile
636 (defvar pgg-default-user-id)
637 (defvar pgg-errors-buffer)
638 (defvar pgg-output-buffer))
639
640 (eval-and-compile
641 (autoload 'pgg-decrypt-region "pgg")
642 (autoload 'pgg-verify-region "pgg")
643 (autoload 'pgg-sign-region "pgg")
644 (autoload 'pgg-encrypt-region "pgg"))
645
646 (defun mml2015-pgg-decrypt (handle ctl)
647 (catch 'error
648 (let ((pgg-errors-buffer mml2015-result-buffer)
649 child handles result decrypt-status)
650 (unless (setq child (mm-find-part-by-type
651 (cdr handle)
652 "application/octet-stream" nil t))
653 (mm-set-handle-multipart-parameter
654 mm-security-handle 'gnus-info "Corrupted")
655 (throw 'error handle))
656 (with-temp-buffer
657 (mm-insert-part child)
658 (if (condition-case err
659 (prog1
660 (pgg-decrypt-region (point-min) (point-max))
661 (setq decrypt-status
662 (with-current-buffer mml2015-result-buffer
663 (buffer-string)))
664 (mm-set-handle-multipart-parameter
665 mm-security-handle 'gnus-details
666 decrypt-status))
667 (error
668 (mm-set-handle-multipart-parameter
669 mm-security-handle 'gnus-details (mml2015-format-error err))
670 nil)
671 (quit
672 (mm-set-handle-multipart-parameter
673 mm-security-handle 'gnus-details "Quit.")
674 nil))
675 (with-current-buffer pgg-output-buffer
676 (goto-char (point-min))
677 (while (search-forward "\r\n" nil t)
678 (replace-match "\n" t t))
679 (setq handles (mm-dissect-buffer t))
680 (mm-destroy-parts handle)
681 (mm-set-handle-multipart-parameter
682 mm-security-handle 'gnus-info "OK")
683 (mm-set-handle-multipart-parameter
684 mm-security-handle 'gnus-details
685 (concat decrypt-status
686 (when (stringp (car handles))
687 "\n" (mm-handle-multipart-ctl-parameter
688 handles 'gnus-details))))
689 (if (listp (car handles))
690 handles
691 (list handles)))
692 (mm-set-handle-multipart-parameter
693 mm-security-handle 'gnus-info "Failed")
694 (throw 'error handle))))))
695
696 (defun mml2015-pgg-clear-decrypt ()
697 (let ((pgg-errors-buffer mml2015-result-buffer))
698 (if (prog1
699 (pgg-decrypt-region (point-min) (point-max))
700 (mm-set-handle-multipart-parameter
701 mm-security-handle 'gnus-details
702 (with-current-buffer mml2015-result-buffer
703 (buffer-string))))
704 (progn
705 (erase-buffer)
706 (insert-buffer-substring pgg-output-buffer)
707 (goto-char (point-min))
708 (while (search-forward "\r\n" nil t)
709 (replace-match "\n" t t))
710 (mm-set-handle-multipart-parameter
711 mm-security-handle 'gnus-info "OK"))
712 (mm-set-handle-multipart-parameter
713 mm-security-handle 'gnus-info "Failed"))))
714
715 (defun mml2015-pgg-verify (handle ctl)
716 (let ((pgg-errors-buffer mml2015-result-buffer)
717 signature-file part signature)
718 (if (or (null (setq part (mm-find-raw-part-by-type
719 ctl (or (mm-handle-multipart-ctl-parameter
720 ctl 'protocol)
721 "application/pgp-signature")
722 t)))
723 (null (setq signature (mm-find-part-by-type
724 (cdr handle) "application/pgp-signature" nil t))))
725 (progn
726 (mm-set-handle-multipart-parameter
727 mm-security-handle 'gnus-info "Corrupted")
728 handle)
729 (with-temp-buffer
730 (insert part)
731 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
732 ;; clearsign use --textmode. The conversion is not necessary.
733 ;; In clearverify, the conversion is not necessary either.
734 (goto-char (point-min))
735 (end-of-line)
736 (while (not (eobp))
737 (unless (eq (char-before) ?\r)
738 (insert "\r"))
739 (forward-line)
740 (end-of-line))
741 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
742 (mm-insert-part signature))
743 (if (condition-case err
744 (prog1
745 (pgg-verify-region (point-min) (point-max)
746 signature-file t)
747 (goto-char (point-min))
748 (while (search-forward "\r\n" nil t)
749 (replace-match "\n" t t))
750 (mm-set-handle-multipart-parameter
751 mm-security-handle 'gnus-details
752 (concat (with-current-buffer pgg-output-buffer
753 (buffer-string))
754 (with-current-buffer pgg-errors-buffer
755 (buffer-string)))))
756 (error
757 (mm-set-handle-multipart-parameter
758 mm-security-handle 'gnus-details (mml2015-format-error err))
759 nil)
760 (quit
761 (mm-set-handle-multipart-parameter
762 mm-security-handle 'gnus-details "Quit.")
763 nil))
764 (progn
765 (delete-file signature-file)
766 (mm-set-handle-multipart-parameter
767 mm-security-handle 'gnus-info
768 (with-current-buffer pgg-errors-buffer
769 (mml2015-gpg-extract-signature-details))))
770 (delete-file signature-file)
771 (mm-set-handle-multipart-parameter
772 mm-security-handle 'gnus-info "Failed")))))
773 handle)
774
775 (defun mml2015-pgg-clear-verify ()
776 (let ((pgg-errors-buffer mml2015-result-buffer)
777 (text (buffer-string))
778 (coding-system buffer-file-coding-system))
779 (if (condition-case err
780 (prog1
781 (mm-with-unibyte-buffer
782 (insert (encode-coding-string text coding-system))
783 (pgg-verify-region (point-min) (point-max) nil t))
784 (goto-char (point-min))
785 (while (search-forward "\r\n" nil t)
786 (replace-match "\n" t t))
787 (mm-set-handle-multipart-parameter
788 mm-security-handle 'gnus-details
789 (concat (with-current-buffer pgg-output-buffer
790 (buffer-string))
791 (with-current-buffer pgg-errors-buffer
792 (buffer-string)))))
793 (error
794 (mm-set-handle-multipart-parameter
795 mm-security-handle 'gnus-details (mml2015-format-error err))
796 nil)
797 (quit
798 (mm-set-handle-multipart-parameter
799 mm-security-handle 'gnus-details "Quit.")
800 nil))
801 (mm-set-handle-multipart-parameter
802 mm-security-handle 'gnus-info
803 (with-current-buffer pgg-errors-buffer
804 (mml2015-gpg-extract-signature-details)))
805 (mm-set-handle-multipart-parameter
806 mm-security-handle 'gnus-info "Failed"))))
807
808 (defun mml2015-pgg-sign (cont)
809 (let ((pgg-errors-buffer mml2015-result-buffer)
810 (boundary (mml-compute-boundary cont))
811 (pgg-default-user-id (or (message-options-get 'mml-sender)
812 pgg-default-user-id)))
813 (unless (pgg-sign-region (point-min) (point-max))
814 (pop-to-buffer mml2015-result-buffer)
815 (error "Sign error"))
816 (goto-char (point-min))
817 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
818 boundary))
819 ;;; FIXME: what is the micalg?
820 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
821 (insert (format "\n--%s\n" boundary))
822 (goto-char (point-max))
823 (insert (format "\n--%s\n" boundary))
824 (insert "Content-Type: application/pgp-signature\n\n")
825 (insert-buffer-substring pgg-output-buffer)
826 (goto-char (point-max))
827 (insert (format "--%s--\n" boundary))
828 (goto-char (point-max))))
829
830 (defun mml2015-pgg-encrypt (cont &optional sign)
831 (let ((pgg-errors-buffer mml2015-result-buffer)
832 (boundary (mml-compute-boundary cont)))
833 (unless (pgg-encrypt-region (point-min) (point-max)
834 (split-string
835 (or
836 (message-options-get 'message-recipients)
837 (message-options-set 'message-recipients
838 (read-string "Recipients: ")))
839 "[ \f\t\n\r\v,]+")
840 sign)
841 (pop-to-buffer mml2015-result-buffer)
842 (error "Encrypt error"))
843 (delete-region (point-min) (point-max))
844 (goto-char (point-min))
845 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
846 boundary))
847 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
848 (insert (format "--%s\n" boundary))
849 (insert "Content-Type: application/pgp-encrypted\n\n")
850 (insert "Version: 1\n\n")
851 (insert (format "--%s\n" boundary))
852 (insert "Content-Type: application/octet-stream\n\n")
853 (insert-buffer-substring pgg-output-buffer)
854 (goto-char (point-max))
855 (insert (format "--%s--\n" boundary))
856 (goto-char (point-max))))
857
858 ;;; General wrapper
859
860 (defun mml2015-clean-buffer ()
861 (if (gnus-buffer-live-p mml2015-result-buffer)
862 (with-current-buffer mml2015-result-buffer
863 (erase-buffer)
864 t)
865 (setq mml2015-result-buffer
866 (gnus-get-buffer-create "*MML2015 Result*"))
867 nil))
868
869 (defsubst mml2015-clear-decrypt-function ()
870 (nth 6 (assq mml2015-use mml2015-function-alist)))
871
872 (defsubst mml2015-clear-verify-function ()
873 (nth 5 (assq mml2015-use mml2015-function-alist)))
874
875 ;;;###autoload
876 (defun mml2015-decrypt (handle ctl)
877 (mml2015-clean-buffer)
878 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
879 (if func
880 (funcall func handle ctl)
881 handle)))
882
883 ;;;###autoload
884 (defun mml2015-decrypt-test (handle ctl)
885 mml2015-use)
886
887 ;;;###autoload
888 (defun mml2015-verify (handle ctl)
889 (mml2015-clean-buffer)
890 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
891 (if func
892 (funcall func handle ctl)
893 handle)))
894
895 ;;;###autoload
896 (defun mml2015-verify-test (handle ctl)
897 mml2015-use)
898
899 ;;;###autoload
900 (defun mml2015-encrypt (cont &optional sign)
901 (mml2015-clean-buffer)
902 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
903 (if func
904 (funcall func cont sign)
905 (error "Cannot find encrypt function"))))
906
907 ;;;###autoload
908 (defun mml2015-sign (cont)
909 (mml2015-clean-buffer)
910 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
911 (if func
912 (funcall func cont)
913 (error "Cannot find sign function"))))
914
915 ;;;###autoload
916 (defun mml2015-self-encrypt ()
917 (mml2015-encrypt nil))
918
919 (provide 'mml2015)
920
921 ;;; arch-tag: b04701d5-0b09-44d8-bed8-de901bf435f2
922 ;;; mml2015.el ends here