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