Feedback T-gnus 6.16.
[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 (mml-compute-boundary cont))
282         hash point)
283     (goto-char (point-min))
284     (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
285       (error "Cannot find signed begin line"))
286     (goto-char (match-beginning 0))
287     (forward-line 1)
288     (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
289       (error "Cannot not find PGP hash"))
290     (setq hash (match-string 1))
291     (unless (re-search-forward "^$" nil t)
292       (error "Cannot not find PGP message"))
293     (forward-line 1)
294     (delete-region (point-min) (point))
295     (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
296                     boundary))
297     (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
298                     (downcase hash)))
299     (insert (format "\n--%s\n" boundary))
300     (setq point (point))
301     (goto-char (point-max))
302     (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
303       (error "Cannot find signature part"))
304     (replace-match "-----END PGP MESSAGE-----" t t)
305     (goto-char (match-beginning 0))
306     (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
307                                 nil t)
308       (error "Cannot find signature part"))
309     (replace-match "-----BEGIN PGP MESSAGE-----" t t)
310     (goto-char (match-beginning 0))
311     (save-restriction
312       (narrow-to-region point (point))
313       (goto-char point)
314       (while (re-search-forward "^- -" nil t)
315         (replace-match "-" t t))
316       (goto-char (point-max)))
317     (insert (format "--%s\n" boundary))
318     (insert "Content-Type: application/pgp-signature\n\n")
319     (goto-char (point-max))
320     (insert (format "--%s--\n" boundary))
321     (goto-char (point-max))))
322
323 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
324   (let ((mc-pgp-always-sign
325          (or mc-pgp-always-sign
326              sign
327              (eq t (or (message-options-get 'message-sign-encrypt)
328                        (message-options-set
329                         'message-sign-encrypt
330                         (or (y-or-n-p "Sign the message? ")
331                             'not))))
332              'never)))
333     (mm-with-unibyte-current-buffer
334       (mc-encrypt-generic
335        (or (message-options-get 'message-recipients)
336            (message-options-set 'message-recipients
337                                 (mc-cleanup-recipient-headers
338                                  (read-string "Recipients: "))))
339        nil nil nil
340        (message-options-get 'message-sender))))
341   (goto-char (point-min))
342   (unless (looking-at "-----BEGIN PGP MESSAGE-----")
343     (error "Fail to encrypt the message"))
344   (let ((boundary (mml-compute-boundary cont)))
345     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
346                     boundary))
347     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
348     (insert (format "--%s\n" boundary))
349     (insert "Content-Type: application/pgp-encrypted\n\n")
350     (insert "Version: 1\n\n")
351     (insert (format "--%s\n" boundary))
352     (insert "Content-Type: application/octet-stream\n\n")
353     (goto-char (point-max))
354     (insert (format "--%s--\n" boundary))
355     (goto-char (point-max))))
356
357 ;;; gpg wrapper
358
359 (eval-and-compile
360   (autoload 'gpg-decrypt "gpg")
361   (autoload 'gpg-verify "gpg")
362   (autoload 'gpg-verify-cleartext "gpg")
363   (autoload 'gpg-sign-detached "gpg")
364   (autoload 'gpg-sign-encrypt "gpg")
365   (autoload 'gpg-encrypt "gpg")
366   (autoload 'gpg-passphrase-read "gpg"))
367
368 (defun mml2015-gpg-passphrase ()
369   (or (message-options-get 'gpg-passphrase)
370       (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
371
372 (defun mml2015-gpg-decrypt-1 ()
373   (let ((cipher (current-buffer)) plain result)
374     (if (with-temp-buffer
375           (prog1
376               (gpg-decrypt cipher (setq plain (current-buffer))
377                            mml2015-result-buffer nil)
378             (mm-set-handle-multipart-parameter
379              mm-security-handle 'gnus-details
380              (with-current-buffer mml2015-result-buffer
381                (buffer-string)))
382             (set-buffer cipher)
383             (erase-buffer)
384             (insert-buffer-substring plain)
385             (goto-char (point-min))
386             (while (search-forward "\r\n" nil t)
387               (replace-match "\n" t t))))
388         '(t)
389       ;; Some wrong with the return value, check plain text buffer.
390       (if (> (point-max) (point-min))
391           '(t)
392         nil))))
393
394 (defun mml2015-gpg-decrypt (handle ctl)
395   (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
396     (mml2015-mailcrypt-decrypt handle ctl)))
397
398 (defun mml2015-gpg-clear-decrypt ()
399   (let (result)
400     (setq result (mml2015-gpg-decrypt-1))
401     (if (car result)
402         (mm-set-handle-multipart-parameter
403          mm-security-handle 'gnus-info "OK")
404       (mm-set-handle-multipart-parameter
405        mm-security-handle 'gnus-info "Failed"))))
406
407 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
408   (let* ((result "")
409          (fpr-length (string-width fingerprint))
410          (n-slice 0)
411          slice)
412     (setq fingerprint (string-to-list fingerprint))
413     (while fingerprint
414       (setq fpr-length (- fpr-length 4))
415       (setq slice (butlast fingerprint fpr-length))
416       (setq fingerprint (nthcdr 4 fingerprint))
417       (setq n-slice (1+ n-slice))
418       (setq result
419             (concat
420              result
421              (case n-slice
422                (1  slice)
423                (otherwise (concat " " slice))))))
424     result))
425
426 (defun mml2015-gpg-extract-signature-details ()
427   (goto-char (point-min))
428   (let* ((expired (re-search-forward
429                    "^\\[GNUPG:\\] SIGEXPIRED$"
430                    nil t))
431          (signer (and (re-search-forward
432                        "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
433                        nil t)
434                       (cons (match-string 1) (match-string 2))))
435          (fprint (and (re-search-forward
436                        "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
437                        nil t)
438                       (match-string 1)))
439          (trust  (and (re-search-forward
440                        "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
441                        nil t)
442                       (match-string 1)))
443          (trust-good-enough-p
444           (cdr (assoc trust mml2015-unabbrev-trust-alist))))
445     (cond ((and signer fprint)
446            (concat (cdr signer)
447                    (unless trust-good-enough-p
448                      (concat "\nUntrusted, Fingerprint: "
449                              (mml2015-gpg-pretty-print-fpr fprint)))
450                    (when expired
451                      (format "\nWARNING: Signature from expired key (%s)"
452                              (car signer)))))
453           ((re-search-forward
454             "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
455            (match-string 2))
456           (t
457            "From unknown user"))))
458
459 (defun mml2015-gpg-verify (handle ctl)
460   (catch 'error
461     (let (part message signature info-is-set-p)
462       (unless (setq part (mm-find-raw-part-by-type
463                           ctl (or (mm-handle-multipart-ctl-parameter
464                                    ctl 'protocol)
465                                   "application/pgp-signature")
466                           t))
467         (mm-set-handle-multipart-parameter
468          mm-security-handle 'gnus-info "Corrupted")
469         (throw 'error handle))
470       (with-temp-buffer
471         (setq message (current-buffer))
472         (insert part)
473         ;; Convert <LF> to <CR><LF> in verify mode.  Sign and
474         ;; clearsign use --textmode. The conversion is not necessary.
475         ;; In clearverify, the conversion is not necessary either.
476         (goto-char (point-min))
477         (end-of-line)
478         (while (not (eobp))
479           (unless (eq (char-before) ?\r)
480             (insert "\r"))
481           (forward-line)
482           (end-of-line))
483         (with-temp-buffer
484           (setq signature (current-buffer))
485           (unless (setq part (mm-find-part-by-type
486                               (cdr handle) "application/pgp-signature" nil t))
487             (mm-set-handle-multipart-parameter
488              mm-security-handle 'gnus-info "Corrupted")
489             (throw 'error handle))
490           (mm-insert-part part)
491           (unless (condition-case err
492                       (prog1
493                           (gpg-verify message signature mml2015-result-buffer)
494                         (mm-set-handle-multipart-parameter
495                          mm-security-handle 'gnus-details
496                          (with-current-buffer mml2015-result-buffer
497                            (buffer-string))))
498                     (error
499                      (mm-set-handle-multipart-parameter
500                       mm-security-handle 'gnus-details (mml2015-format-error err))
501                      (mm-set-handle-multipart-parameter
502                       mm-security-handle 'gnus-info "Error.")
503                      (setq info-is-set-p t)
504                      nil)
505                     (quit
506                      (mm-set-handle-multipart-parameter
507                       mm-security-handle 'gnus-details "Quit.")
508                      (mm-set-handle-multipart-parameter
509                       mm-security-handle 'gnus-info "Quit.")
510                      (setq info-is-set-p t)
511                      nil))
512             (unless info-is-set-p
513               (mm-set-handle-multipart-parameter
514                mm-security-handle 'gnus-info "Failed"))
515             (throw 'error handle)))
516         (mm-set-handle-multipart-parameter
517          mm-security-handle 'gnus-info
518          (with-current-buffer mml2015-result-buffer
519            (mml2015-gpg-extract-signature-details))))
520       handle)))
521
522 (defun mml2015-gpg-clear-verify ()
523   (if (condition-case err
524           (prog1
525               (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
526             (mm-set-handle-multipart-parameter
527              mm-security-handle 'gnus-details
528              (with-current-buffer mml2015-result-buffer
529                (buffer-string))))
530         (error
531          (mm-set-handle-multipart-parameter
532           mm-security-handle 'gnus-details (mml2015-format-error err))
533          nil)
534         (quit
535          (mm-set-handle-multipart-parameter
536           mm-security-handle 'gnus-details "Quit.")
537          nil))
538       (mm-set-handle-multipart-parameter
539        mm-security-handle 'gnus-info
540        (with-current-buffer mml2015-result-buffer
541          (mml2015-gpg-extract-signature-details)))
542     (mm-set-handle-multipart-parameter
543      mm-security-handle 'gnus-info "Failed")))
544
545 (defun mml2015-gpg-sign (cont)
546   (let ((boundary (mml-compute-boundary cont))
547         (text (current-buffer)) signature)
548     (goto-char (point-max))
549     (unless (bolp)
550       (insert "\n"))
551     (with-temp-buffer
552       (unless (gpg-sign-detached text (setq signature (current-buffer))
553                                  mml2015-result-buffer
554                                  nil
555                                  (message-options-get 'message-sender)
556                                  t t) ; armor & textmode
557         (unless (> (point-max) (point-min))
558           (pop-to-buffer mml2015-result-buffer)
559           (error "Sign error")))
560       (goto-char (point-min))
561       (while (re-search-forward "\r+$" nil t)
562         (replace-match "" t t))
563       (set-buffer text)
564       (goto-char (point-min))
565       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
566                       boundary))
567       ;;; FIXME: what is the micalg?
568       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
569       (insert (format "\n--%s\n" boundary))
570       (goto-char (point-max))
571       (insert (format "\n--%s\n" boundary))
572       (insert "Content-Type: application/pgp-signature\n\n")
573       (insert-buffer-substring signature)
574       (goto-char (point-max))
575       (insert (format "--%s--\n" boundary))
576       (goto-char (point-max)))))
577
578 (defun mml2015-gpg-encrypt (cont &optional sign)
579   (let ((boundary (mml-compute-boundary cont))
580         (text (current-buffer))
581         cipher)
582     (mm-with-unibyte-current-buffer
583       (with-temp-buffer
584         ;; set up a function to call the correct gpg encrypt routine
585         ;; with the right arguments. (FIXME: this should be done
586         ;; differently.)
587         (flet ((gpg-encrypt-func
588                  (sign plaintext ciphertext result recipients &optional
589                        passphrase sign-with-key armor textmode)
590                  (if sign
591                      (gpg-sign-encrypt
592                       plaintext ciphertext result recipients passphrase
593                       sign-with-key armor textmode)
594                    (gpg-encrypt
595                     plaintext ciphertext result recipients passphrase
596                     armor textmode))))
597           (unless (gpg-encrypt-func
598                     sign ; passed in when using signencrypt
599                     text (setq cipher (current-buffer))
600                     mml2015-result-buffer
601                     (split-string
602                      (or
603                       (message-options-get 'message-recipients)
604                       (message-options-set 'message-recipients
605                                            (read-string "Recipients: ")))
606                      "[ \f\t\n\r\v,]+")
607                     nil
608                     (message-options-get 'message-sender)
609                     t t) ; armor & textmode
610             (unless (> (point-max) (point-min))
611               (pop-to-buffer mml2015-result-buffer)
612               (error "Encrypt error"))))
613         (goto-char (point-min))
614         (while (re-search-forward "\r+$" nil t)
615           (replace-match "" t t))
616         (set-buffer text)
617         (delete-region (point-min) (point-max))
618         (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
619                         boundary))
620         (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
621         (insert (format "--%s\n" boundary))
622         (insert "Content-Type: application/pgp-encrypted\n\n")
623         (insert "Version: 1\n\n")
624         (insert (format "--%s\n" boundary))
625         (insert "Content-Type: application/octet-stream\n\n")
626         (insert-buffer-substring cipher)
627         (goto-char (point-max))
628         (insert (format "--%s--\n" boundary))
629         (goto-char (point-max))))))
630
631 ;;; pgg wrapper
632
633 (eval-when-compile
634   (defvar pgg-errors-buffer)
635   (defvar pgg-output-buffer))
636
637 (eval-and-compile
638   (autoload 'pgg-decrypt-region "pgg")
639   (autoload 'pgg-verify-region "pgg")
640   (autoload 'pgg-sign-region "pgg")
641   (autoload 'pgg-encrypt-region "pgg"))
642
643 (defun mml2015-pgg-decrypt (handle ctl)
644   (catch 'error
645     (let ((pgg-errors-buffer mml2015-result-buffer)
646           child handles result decrypt-status)
647       (unless (setq child (mm-find-part-by-type
648                            (cdr handle)
649                            "application/octet-stream" nil t))
650         (mm-set-handle-multipart-parameter
651          mm-security-handle 'gnus-info "Corrupted")
652         (throw 'error handle))
653       (with-temp-buffer
654         (mm-insert-part child)
655         (if (condition-case err
656                 (prog1
657                     (pgg-decrypt-region (point-min) (point-max))
658                   (setq decrypt-status
659                         (with-current-buffer mml2015-result-buffer
660                           (buffer-string)))
661                   (mm-set-handle-multipart-parameter
662                    mm-security-handle 'gnus-details
663                    decrypt-status))
664               (error
665                (mm-set-handle-multipart-parameter
666                 mm-security-handle 'gnus-details (mml2015-format-error err))
667                nil)
668               (quit
669                (mm-set-handle-multipart-parameter
670                 mm-security-handle 'gnus-details "Quit.")
671                nil))
672             (with-current-buffer pgg-output-buffer
673               (goto-char (point-min))
674               (while (search-forward "\r\n" nil t)
675                 (replace-match "\n" t t))
676               (setq handles (mm-dissect-buffer t))
677               (mm-destroy-parts handle)
678               (mm-set-handle-multipart-parameter
679                mm-security-handle 'gnus-info "OK")
680               (mm-set-handle-multipart-parameter
681                mm-security-handle 'gnus-details
682                (concat decrypt-status
683                        (when (stringp (car handles))
684                          "\n" (mm-handle-multipart-ctl-parameter
685                                handles 'gnus-details))))
686               (if (listp (car handles))
687                   handles
688                 (list handles)))
689           (mm-set-handle-multipart-parameter
690            mm-security-handle 'gnus-info "Failed")
691           (throw 'error handle))))))
692
693 (defun mml2015-pgg-clear-decrypt ()
694   (let ((pgg-errors-buffer mml2015-result-buffer))
695     (if (prog1
696             (pgg-decrypt-region (point-min) (point-max))
697           (mm-set-handle-multipart-parameter
698            mm-security-handle 'gnus-details
699            (with-current-buffer mml2015-result-buffer
700              (buffer-string))))
701         (progn
702           (erase-buffer)
703           (insert-buffer-substring pgg-output-buffer)
704           (goto-char (point-min))
705           (while (search-forward "\r\n" nil t)
706             (replace-match "\n" t t))
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                   (goto-char (point-min))
745                   (while (search-forward "\r\n" nil t)
746                     (replace-match "\n" t t))
747                   (mm-set-handle-multipart-parameter
748                    mm-security-handle 'gnus-details
749                    (concat (with-current-buffer pgg-output-buffer
750                              (buffer-string))
751                            (with-current-buffer pgg-errors-buffer
752                              (buffer-string)))))
753               (error
754                (mm-set-handle-multipart-parameter
755                 mm-security-handle 'gnus-details (mml2015-format-error err))
756                nil)
757               (quit
758                (mm-set-handle-multipart-parameter
759                 mm-security-handle 'gnus-details "Quit.")
760                nil))
761             (progn
762               (delete-file signature-file)
763               (mm-set-handle-multipart-parameter
764                mm-security-handle 'gnus-info
765                (with-current-buffer pgg-errors-buffer
766                  (mml2015-gpg-extract-signature-details))))
767           (delete-file signature-file)
768           (mm-set-handle-multipart-parameter
769            mm-security-handle 'gnus-info "Failed")))))
770   handle)
771
772 (defun mml2015-pgg-clear-verify ()
773   (let ((pgg-errors-buffer mml2015-result-buffer)
774         (text (buffer-string))
775         (coding-system buffer-file-coding-system))
776     (if (condition-case err
777             (prog1
778                 (mm-with-unibyte-buffer
779                   (insert (encode-coding-string text coding-system))
780                   (pgg-verify-region (point-min) (point-max) nil t))
781               (goto-char (point-min))
782               (while (search-forward "\r\n" nil t)
783                 (replace-match "\n" t t))
784               (mm-set-handle-multipart-parameter
785                mm-security-handle 'gnus-details
786                (concat (with-current-buffer pgg-output-buffer
787                          (buffer-string))
788                        (with-current-buffer pgg-errors-buffer
789                          (buffer-string)))))
790           (error
791            (mm-set-handle-multipart-parameter
792             mm-security-handle 'gnus-details (mml2015-format-error err))
793            nil)
794           (quit
795            (mm-set-handle-multipart-parameter
796             mm-security-handle 'gnus-details "Quit.")
797            nil))
798         (mm-set-handle-multipart-parameter
799          mm-security-handle 'gnus-info
800          (with-current-buffer pgg-errors-buffer
801            (mml2015-gpg-extract-signature-details)))
802       (mm-set-handle-multipart-parameter
803        mm-security-handle 'gnus-info "Failed"))))
804
805 (defun mml2015-pgg-sign (cont)
806   (let ((pgg-errors-buffer mml2015-result-buffer)
807         (boundary (mml-compute-boundary cont))
808         (pgg-default-user-id (or (message-options-get 'mml-sender)
809                                  pgg-default-user-id)))
810     (unless (pgg-sign-region (point-min) (point-max))
811       (pop-to-buffer mml2015-result-buffer)
812       (error "Sign error"))
813     (goto-char (point-min))
814     (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
815                     boundary))
816       ;;; FIXME: what is the micalg?
817     (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
818     (insert (format "\n--%s\n" boundary))
819     (goto-char (point-max))
820     (insert (format "\n--%s\n" boundary))
821     (insert "Content-Type: application/pgp-signature\n\n")
822     (insert-buffer-substring pgg-output-buffer)
823     (goto-char (point-max))
824     (insert (format "--%s--\n" boundary))
825     (goto-char (point-max))))
826
827 (defun mml2015-pgg-encrypt (cont &optional sign)
828   (let ((pgg-errors-buffer mml2015-result-buffer)
829         (boundary (mml-compute-boundary cont)))
830     (unless (pgg-encrypt-region (point-min) (point-max)
831                                 (split-string
832                                  (or
833                                   (message-options-get 'message-recipients)
834                                   (message-options-set 'message-recipients
835                                                        (read-string "Recipients: ")))
836                                  "[ \f\t\n\r\v,]+")
837                                 sign)
838       (pop-to-buffer mml2015-result-buffer)
839       (error "Encrypt error"))
840     (delete-region (point-min) (point-max))
841     (goto-char (point-min))
842     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
843                     boundary))
844     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
845     (insert (format "--%s\n" boundary))
846     (insert "Content-Type: application/pgp-encrypted\n\n")
847     (insert "Version: 1\n\n")
848     (insert (format "--%s\n" boundary))
849     (insert "Content-Type: application/octet-stream\n\n")
850     (insert-buffer-substring pgg-output-buffer)
851     (goto-char (point-max))
852     (insert (format "--%s--\n" boundary))
853     (goto-char (point-max))))
854
855 ;;; General wrapper
856
857 (defun mml2015-clean-buffer ()
858   (if (gnus-buffer-live-p mml2015-result-buffer)
859       (with-current-buffer mml2015-result-buffer
860         (erase-buffer)
861         t)
862     (setq mml2015-result-buffer
863           (gnus-get-buffer-create "*MML2015 Result*"))
864     nil))
865
866 (defsubst mml2015-clear-decrypt-function ()
867   (nth 6 (assq mml2015-use mml2015-function-alist)))
868
869 (defsubst mml2015-clear-verify-function ()
870   (nth 5 (assq mml2015-use mml2015-function-alist)))
871
872 ;;;###autoload
873 (defun mml2015-decrypt (handle ctl)
874   (mml2015-clean-buffer)
875   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
876     (if func
877         (funcall func handle ctl)
878       handle)))
879
880 ;;;###autoload
881 (defun mml2015-decrypt-test (handle ctl)
882   mml2015-use)
883
884 ;;;###autoload
885 (defun mml2015-verify (handle ctl)
886   (mml2015-clean-buffer)
887   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
888     (if func
889         (funcall func handle ctl)
890       handle)))
891
892 ;;;###autoload
893 (defun mml2015-verify-test (handle ctl)
894   mml2015-use)
895
896 ;;;###autoload
897 (defun mml2015-encrypt (cont &optional sign)
898   (mml2015-clean-buffer)
899   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
900     (if func
901         (funcall func cont sign)
902       (error "Cannot find encrypt function"))))
903
904 ;;;###autoload
905 (defun mml2015-sign (cont)
906   (mml2015-clean-buffer)
907   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
908     (if func
909         (funcall func cont)
910       (error "Cannot find sign function"))))
911
912 ;;;###autoload
913 (defun mml2015-self-encrypt ()
914   (mml2015-encrypt nil))
915
916 (provide 'mml2015)
917
918 ;;; mml2015.el ends here