Synch to Gnus 200312071540.
[elisp/gnus.git-] / lisp / mml2015.el
1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000, 2001, 2002, 2003 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 (eval-when-compile (require 'gnus-clfns))
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   :type '(repeat (cons (regexp :tag "GnuPG output regexp")
88                        (boolean :tag "Trust key"))))
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
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-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
549          (funcall mml-boundary-function (incf mml-multipart-number)))
550         (text (current-buffer)) signature)
551     (goto-char (point-max))
552     (unless (bolp)
553       (insert "\n"))
554     (with-temp-buffer
555       (unless (gpg-sign-detached text (setq signature (current-buffer))
556                                  mml2015-result-buffer
557                                  nil
558                                  (message-options-get 'message-sender)
559                                  t t) ; armor & textmode
560         (unless (> (point-max) (point-min))
561           (pop-to-buffer mml2015-result-buffer)
562           (error "Sign error")))
563       (goto-char (point-min))
564       (while (re-search-forward "\r+$" nil t)
565         (replace-match "" t t))
566       (set-buffer text)
567       (goto-char (point-min))
568       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
569                       boundary))
570       ;;; FIXME: what is the micalg?
571       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
572       (insert (format "\n--%s\n" boundary))
573       (goto-char (point-max))
574       (insert (format "\n--%s\n" boundary))
575       (insert "Content-Type: application/pgp-signature\n\n")
576       (insert-buffer-substring signature)
577       (goto-char (point-max))
578       (insert (format "--%s--\n" boundary))
579       (goto-char (point-max)))))
580
581 (defun mml2015-gpg-encrypt (cont &optional sign)
582   (let ((boundary
583          (funcall mml-boundary-function (incf mml-multipart-number)))
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 (funcall mml-boundary-function (incf mml-multipart-number)))
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 (funcall mml-boundary-function (incf mml-multipart-number))))
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 ;;; mml2015.el ends here