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