712e03b09d5da07202267f2e7fca168f8b67690b
[gnus] / lisp / mml2015.el
1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: PGP MIME MML
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
27 ;; with both.
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (require 'mm-decode)
33
34 (defvar mml2015-use (or
35                      (progn
36                        (ignore-errors
37                          (require 'gpg))
38                        (and (fboundp 'gpg-sign-detached)
39                             'gpg))
40                      (progn (ignore-errors
41                               (load "mc-toplev"))
42                             (and (fboundp 'mc-encrypt-generic)
43                                  (fboundp 'mc-sign-generic)
44                                  (fboundp 'mc-cleanup-recipient-headers)
45                                  'mailcrypt)))
46   "The package used for PGP/MIME.")
47
48 ;; Something is not RFC2015.
49 (defvar mml2015-function-alist
50   '((mailcrypt mml2015-mailcrypt-sign
51                mml2015-mailcrypt-encrypt
52                mml2015-mailcrypt-verify
53                mml2015-mailcrypt-decrypt
54                mml2015-mailcrypt-clear-verify
55                mml2015-mailcrypt-clear-decrypt)
56     (gpg mml2015-gpg-sign
57          mml2015-gpg-encrypt
58          mml2015-gpg-verify
59          mml2015-gpg-decrypt
60          mml2015-gpg-clear-verify
61          mml2015-gpg-clear-decrypt))
62   "Alist of PGP/MIME functions.")
63
64 (defvar mml2015-result-buffer nil)
65
66 (defvar mml2015-trust-boundaries-alist
67   '((trust-undefined . nil)
68     (trust-none      . nil)
69     (trust-marginal  . t)
70     (trust-full      . t)
71     (trust-ultimate  . t))
72   "Trust boundaries for a signer's GnuPG key.
73 This alist contains pairs of the form (trust-symbol . boolean), with
74 symbols that are contained in `gpg-unabbrev-trust-alist'. The boolean
75 specifies whether the given trust value is good enough to be trusted
76 by you.")
77
78 ;;; mailcrypt wrapper
79
80 (eval-and-compile
81   (autoload 'mailcrypt-decrypt "mailcrypt")
82   (autoload 'mailcrypt-verify "mailcrypt")
83   (autoload 'mc-pgp-always-sign "mailcrypt")
84   (autoload 'mc-encrypt-generic "mc-toplev")
85   (autoload 'mc-cleanup-recipient-headers "mc-toplev")
86   (autoload 'mc-sign-generic "mc-toplev"))
87
88 (eval-when-compile
89   (defvar mc-default-scheme)
90   (defvar mc-schemes))
91
92 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
93 (defvar mml2015-verify-function 'mailcrypt-verify)
94
95 (defun mml2015-format-error (err)
96   (if (stringp (cadr err))
97       (cadr err)
98     (format "%S" (cdr err))))
99
100 (defun mml2015-mailcrypt-decrypt (handle ctl)
101   (catch 'error
102     (let (child handles result)
103       (unless (setq child (mm-find-part-by-type
104                            (cdr handle)
105                            "application/octet-stream" nil t))
106         (mm-set-handle-multipart-parameter
107          mm-security-handle 'gnus-info "Corrupted")
108         (throw 'error handle))
109       (with-temp-buffer
110         (mm-insert-part child)
111         (setq result
112               (condition-case err
113                   (funcall mml2015-decrypt-function)
114                 (error
115                  (mm-set-handle-multipart-parameter
116                   mm-security-handle 'gnus-details (mml2015-format-error err))
117                  nil)
118                 (quit
119                  (mm-set-handle-multipart-parameter
120                   mm-security-handle 'gnus-details "Quit.")
121                  nil)))
122         (unless (car result)
123           (mm-set-handle-multipart-parameter
124            mm-security-handle 'gnus-info "Failed")
125           (throw 'error handle))
126         (setq handles (mm-dissect-buffer t)))
127       (mm-destroy-parts handle)
128       (mm-set-handle-multipart-parameter
129        mm-security-handle 'gnus-info
130        (concat "OK"
131                (let ((sig (with-current-buffer mml2015-result-buffer
132                             (mml2015-gpg-extract-signature-details))))
133                  (concat ", Signer: " sig))))
134       (if (listp (car handles))
135           handles
136         (list handles)))))
137
138 (defun mml2015-mailcrypt-clear-decrypt ()
139   (let (result)
140     (setq result
141           (condition-case err
142               (funcall mml2015-decrypt-function)
143             (error
144              (mm-set-handle-multipart-parameter
145               mm-security-handle 'gnus-details (mml2015-format-error err))
146              nil)
147             (quit
148              (mm-set-handle-multipart-parameter
149               mm-security-handle 'gnus-details "Quit.")
150              nil)))
151     (if (car result)
152         (mm-set-handle-multipart-parameter
153          mm-security-handle 'gnus-info "OK")
154       (mm-set-handle-multipart-parameter
155        mm-security-handle 'gnus-info "Failed"))))
156
157 (defun mml2015-fix-micalg (alg)
158   (and alg
159        ;; Mutt/1.2.5i has seen sending micalg=php-sha1
160        (upcase (if (string-match "^p[gh]p-" alg)
161                    (substring alg (match-end 0))
162                  alg))))
163
164 (defun mml2015-mailcrypt-verify (handle ctl)
165   (catch 'error
166     (let (part)
167       (unless (setq part (mm-find-raw-part-by-type
168                           ctl (or (mm-handle-multipart-ctl-parameter
169                                    ctl 'protocol)
170                                   "application/pgp-signature")
171                           t))
172         (mm-set-handle-multipart-parameter
173          mm-security-handle 'gnus-info "Corrupted")
174         (throw 'error handle))
175       (with-temp-buffer
176         (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
177         (insert (format "Hash: %s\n\n"
178                         (or (mml2015-fix-micalg
179                              (mm-handle-multipart-ctl-parameter
180                               ctl 'micalg))
181                             "SHA1")))
182         (save-restriction
183           (narrow-to-region (point) (point))
184           (insert part "\n")
185           (goto-char (point-min))
186           (while (not (eobp))
187             (if (looking-at "^-")
188                 (insert "- "))
189             (forward-line)))
190         (unless (setq part (mm-find-part-by-type
191                             (cdr handle) "application/pgp-signature" nil t))
192           (mm-set-handle-multipart-parameter
193            mm-security-handle 'gnus-info "Corrupted")
194           (throw 'error handle))
195         (save-restriction
196           (narrow-to-region (point) (point))
197           (mm-insert-part part)
198           (goto-char (point-min))
199           (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
200               (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
201           (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
202               (replace-match "-----END PGP SIGNATURE-----" t t)))
203         (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
204           (unless (condition-case err
205                       (prog1
206                           (funcall mml2015-verify-function)
207                         (if (get-buffer " *mailcrypt stderr temp")
208                             (mm-set-handle-multipart-parameter
209                              mm-security-handle 'gnus-details
210                              (with-current-buffer " *mailcrypt stderr temp"
211                                (buffer-string))))
212                         (if (get-buffer " *mailcrypt stdout temp")
213                             (kill-buffer " *mailcrypt stdout temp"))
214                         (if (get-buffer " *mailcrypt stderr temp")
215                             (kill-buffer " *mailcrypt stderr temp"))
216                         (if (get-buffer " *mailcrypt status temp")
217                             (kill-buffer " *mailcrypt status temp"))
218                         (if (get-buffer mc-gpg-debug-buffer)
219                             (kill-buffer mc-gpg-debug-buffer)))
220                     (error
221                      (mm-set-handle-multipart-parameter
222                       mm-security-handle 'gnus-details (mml2015-format-error err))
223                      nil)
224                     (quit
225                      (mm-set-handle-multipart-parameter
226                       mm-security-handle 'gnus-details "Quit.")
227                      nil))
228             (mm-set-handle-multipart-parameter
229              mm-security-handle 'gnus-info "Failed")
230             (throw 'error handle))))
231       (mm-set-handle-multipart-parameter
232        mm-security-handle 'gnus-info "OK")
233       handle)))
234
235 (defun mml2015-mailcrypt-clear-verify ()
236   (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
237     (if (condition-case err
238             (prog1
239                 (funcall mml2015-verify-function)
240               (if (get-buffer " *mailcrypt stderr temp")
241                   (mm-set-handle-multipart-parameter
242                    mm-security-handle 'gnus-details
243                    (with-current-buffer " *mailcrypt stderr temp"
244                      (buffer-string))))
245               (if (get-buffer " *mailcrypt stdout temp")
246                   (kill-buffer " *mailcrypt stdout temp"))
247               (if (get-buffer " *mailcrypt stderr temp")
248                   (kill-buffer " *mailcrypt stderr temp"))
249               (if (get-buffer " *mailcrypt status temp")
250                   (kill-buffer " *mailcrypt status temp"))
251               (if (get-buffer mc-gpg-debug-buffer)
252                   (kill-buffer mc-gpg-debug-buffer)))
253           (error
254            (mm-set-handle-multipart-parameter
255             mm-security-handle 'gnus-details (mml2015-format-error err))
256            nil)
257           (quit
258            (mm-set-handle-multipart-parameter
259             mm-security-handle 'gnus-details "Quit.")
260            nil))
261         (mm-set-handle-multipart-parameter
262          mm-security-handle 'gnus-info "OK")
263       (mm-set-handle-multipart-parameter
264        mm-security-handle 'gnus-info "Failed"))))
265
266 (defun mml2015-mailcrypt-sign (cont)
267   (mc-sign-generic (message-options-get 'message-sender)
268                    nil nil nil nil)
269   (let ((boundary
270          (funcall mml-boundary-function (incf mml-multipart-number)))
271         hash point)
272     (goto-char (point-min))
273     (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
274       (error "Cannot find signed begin line"))
275     (goto-char (match-beginning 0))
276     (forward-line 1)
277     (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
278       (error "Cannot not find PGP hash"))
279     (setq hash (match-string 1))
280     (unless (re-search-forward "^$" nil t)
281       (error "Cannot not find PGP message"))
282     (forward-line 1)
283     (delete-region (point-min) (point))
284     (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
285                     boundary))
286     (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
287                     (downcase hash)))
288     (insert (format "\n--%s\n" boundary))
289     (setq point (point))
290     (goto-char (point-max))
291     (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
292       (error "Cannot find signature part"))
293     (replace-match "-----END PGP MESSAGE-----" t t)
294     (goto-char (match-beginning 0))
295     (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
296                                 nil t)
297       (error "Cannot find signature part"))
298     (replace-match "-----BEGIN PGP MESSAGE-----" t t)
299     (goto-char (match-beginning 0))
300     (save-restriction
301       (narrow-to-region point (point))
302       (goto-char point)
303       (while (re-search-forward "^- -" nil t)
304         (replace-match "-" t t))
305       (goto-char (point-max)))
306     (insert (format "--%s\n" boundary))
307     (insert "Content-Type: application/pgp-signature\n\n")
308     (goto-char (point-max))
309     (insert (format "--%s--\n" boundary))
310     (goto-char (point-max))))
311
312 (defun mml2015-mailcrypt-encrypt (cont)
313   (let ((mc-pgp-always-sign
314          (or mc-pgp-always-sign
315              (eq t (or (message-options-get 'message-sign-encrypt)
316                        (message-options-set
317                         'message-sign-encrypt
318                         (or (y-or-n-p "Sign the message? ")
319                             'not))))
320              'never)))
321     (mm-with-unibyte-current-buffer-mule4
322       (mc-encrypt-generic
323        (or (message-options-get 'message-recipients)
324            (message-options-set 'message-recipients
325                               (mc-cleanup-recipient-headers
326                                (read-string "Recipients: "))))
327        nil nil nil
328        (message-options-get 'message-sender))))
329   (goto-char (point-min))
330   (unless (looking-at "-----BEGIN PGP MESSAGE-----")
331     (error "Fail to encrypt the message"))
332   (let ((boundary
333          (funcall mml-boundary-function (incf mml-multipart-number))))
334     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
335                     boundary))
336     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
337     (insert (format "--%s\n" boundary))
338     (insert "Content-Type: application/pgp-encrypted\n\n")
339     (insert "Version: 1\n\n")
340     (insert (format "--%s\n" boundary))
341     (insert "Content-Type: application/octet-stream\n\n")
342     (goto-char (point-max))
343     (insert (format "--%s--\n" boundary))
344     (goto-char (point-max))))
345
346 ;;; gpg wrapper
347
348 (eval-and-compile
349   (autoload 'gpg-decrypt "gpg")
350   (autoload 'gpg-verify "gpg")
351   (autoload 'gpg-verify-cleartext "gpg")
352   (autoload 'gpg-sign-detached "gpg")
353   (autoload 'gpg-sign-encrypt "gpg")
354   (autoload 'gpg-passphrase-read "gpg"))
355
356 (defun mml2015-gpg-passphrase ()
357   (or (message-options-get 'gpg-passphrase)
358       (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
359
360 (defun mml2015-gpg-decrypt-1 ()
361   (let ((cipher (current-buffer)) plain result)
362     (if (with-temp-buffer
363           (prog1
364               (gpg-decrypt cipher (setq plain (current-buffer))
365                            mml2015-result-buffer nil)
366             (mm-set-handle-multipart-parameter
367              mm-security-handle 'gnus-details
368              (with-current-buffer mml2015-result-buffer
369                (buffer-string)))
370             (set-buffer cipher)
371             (erase-buffer)
372             (insert-buffer plain)
373             (goto-char (point-min))
374             (while (search-forward "\r\n" nil t)
375               (replace-match "\n" t t))))
376         '(t)
377       ;; Some wrong with the return value, check plain text buffer.
378       (if (> (point-max) (point-min))
379           '(t)
380         nil))))
381
382 (defun mml2015-gpg-decrypt (handle ctl)
383   (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
384     (mml2015-mailcrypt-decrypt handle ctl)))
385
386 (defun mml2015-gpg-clear-decrypt ()
387   (let (result)
388     (setq result (mml2015-gpg-decrypt-1))
389     (if (car result)
390         (mm-set-handle-multipart-parameter
391          mm-security-handle 'gnus-info "OK")
392       (mm-set-handle-multipart-parameter
393        mm-security-handle 'gnus-info "Failed"))))
394
395 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
396   (let* ((result "")
397          (fpr-length (string-width fingerprint))
398          (n-slice 0)
399          slice)
400     (setq fingerprint (string-to-list fingerprint))
401     (while fingerprint
402       (setq fpr-length (- fpr-length 4))
403       (setq slice (butlast fingerprint fpr-length))
404       (setq fingerprint (nthcdr 4 fingerprint))
405       (setq n-slice (1+ n-slice))
406       (setq result
407             (concat
408              result
409              (case n-slice
410                (1  slice)
411                (otherwise (concat " " slice))))))
412     result))
413
414 (defun mml2015-gpg-extract-signature-details ()
415   (goto-char (point-min))
416   (if (boundp 'gpg-unabbrev-trust-alist)
417       (let* ((expired (re-search-forward
418                        "^\\[GNUPG:\\] SIGEXPIRED$"
419                        nil t))
420              (signer (and (re-search-forward
421                            "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
422                            nil t)
423                           (cons (match-string 1) (match-string 2))))
424              (fprint (and (re-search-forward
425                            "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
426                            nil t)
427                           (match-string 1)))
428              (trust  (and (re-search-forward
429                            "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
430                            nil t)
431                           (match-string 1)))
432              (trust-good-enough-p
433               (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
434                           mml2015-trust-boundaries-alist))))
435         (cond ((and signer fprint)
436                (concat (cdr signer)
437                        (unless trust-good-enough-p
438                          (concat "\nUntrusted, Fingerprint: "
439                                  (mml2015-gpg-pretty-print-fpr fprint)))
440                        (when expired
441                          (format "\nWARNING: Signature from expired key (%s)"
442                           (car signer)))))
443               (t
444                "From unknown user")))
445     (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
446         (match-string 1)
447       "From unknown user")))
448
449 (defun mml2015-gpg-verify (handle ctl)
450   (catch 'error
451     (let (part message signature info-is-set-p)
452       (unless (setq part (mm-find-raw-part-by-type
453                           ctl (or (mm-handle-multipart-ctl-parameter
454                                    ctl 'protocol)
455                                   "application/pgp-signature")
456                           t))
457         (mm-set-handle-multipart-parameter
458          mm-security-handle 'gnus-info "Corrupted")
459         (throw 'error handle))
460       (with-temp-buffer
461         (setq message (current-buffer))
462         (insert part)
463         ;; Convert <LF> to <CR><LF> in verify mode.  Sign and
464         ;; clearsign use --textmode. The conversion is not necessary.
465         ;; In clearverify, the conversion is not necessary either.
466         (goto-char (point-min))
467         (end-of-line)
468         (while (not (eobp))
469           (unless (eq (char-before) ?\r)
470             (insert "\r"))
471           (forward-line)
472           (end-of-line))
473         (with-temp-buffer
474           (setq signature (current-buffer))
475           (unless (setq part (mm-find-part-by-type
476                               (cdr handle) "application/pgp-signature" nil t))
477             (mm-set-handle-multipart-parameter
478              mm-security-handle 'gnus-info "Corrupted")
479             (throw 'error handle))
480           (mm-insert-part part)
481           (unless (condition-case err
482                       (prog1
483                           (gpg-verify message signature mml2015-result-buffer)
484                         (mm-set-handle-multipart-parameter
485                          mm-security-handle 'gnus-details
486                          (with-current-buffer mml2015-result-buffer
487                            (buffer-string))))
488                     (error
489                      (mm-set-handle-multipart-parameter
490                       mm-security-handle 'gnus-details (mml2015-format-error err))
491                      (mm-set-handle-multipart-parameter
492                       mm-security-handle 'gnus-info "Error.")
493                      (setq info-is-set-p t)
494                      nil)
495                     (quit
496                      (mm-set-handle-multipart-parameter
497                       mm-security-handle 'gnus-details "Quit.")
498                      (mm-set-handle-multipart-parameter
499                       mm-security-handle 'gnus-info "Quit.")
500                      (setq info-is-set-p t)
501                      nil))
502             (unless info-is-set-p
503               (mm-set-handle-multipart-parameter
504                mm-security-handle 'gnus-info "Failed"))
505             (throw 'error handle)))
506         (mm-set-handle-multipart-parameter
507          mm-security-handle 'gnus-info
508          (with-current-buffer mml2015-result-buffer
509            (mml2015-gpg-extract-signature-details))))
510       handle)))
511
512 (defun mml2015-gpg-clear-verify ()
513   (if (condition-case err
514           (prog1
515               (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
516             (mm-set-handle-multipart-parameter
517              mm-security-handle 'gnus-details
518              (with-current-buffer mml2015-result-buffer
519                (buffer-string))))
520         (error
521          (mm-set-handle-multipart-parameter
522           mm-security-handle 'gnus-details (mml2015-format-error err))
523          nil)
524         (quit
525          (mm-set-handle-multipart-parameter
526           mm-security-handle 'gnus-details "Quit.")
527          nil))
528       (mm-set-handle-multipart-parameter
529        mm-security-handle 'gnus-info
530        (with-current-buffer mml2015-result-buffer
531          (mml2015-gpg-extract-signature-details)))
532     (mm-set-handle-multipart-parameter
533      mm-security-handle 'gnus-info "Failed")))
534
535 (defun mml2015-gpg-sign (cont)
536   (let ((boundary
537          (funcall mml-boundary-function (incf mml-multipart-number)))
538         (text (current-buffer)) signature)
539     (goto-char (point-max))
540     (unless (bolp)
541       (insert "\n"))
542     (with-temp-buffer
543       (unless (gpg-sign-detached text (setq signature (current-buffer))
544                                  mml2015-result-buffer
545                                  nil
546                                  (message-options-get 'message-sender)
547                                  t t) ; armor & textmode
548         (unless (> (point-max) (point-min))
549           (pop-to-buffer mml2015-result-buffer)
550           (error "Sign error")))
551       (goto-char (point-min))
552       (while (re-search-forward "\r+$" nil t)
553         (replace-match "" t t))
554       (set-buffer text)
555       (goto-char (point-min))
556       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
557                       boundary))
558       ;;; FIXME: what is the micalg?
559       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
560       (insert (format "\n--%s\n" boundary))
561       (goto-char (point-max))
562       (insert (format "\n--%s\n" boundary))
563       (insert "Content-Type: application/pgp-signature\n\n")
564       (insert-buffer signature)
565       (goto-char (point-max))
566       (insert (format "--%s--\n" boundary))
567       (goto-char (point-max)))))
568
569 (defun mml2015-gpg-encrypt (cont &optional sign-also)
570   (let ((boundary
571          (funcall mml-boundary-function (incf mml-multipart-number)))
572         (text (current-buffer))
573         cipher)
574     (mm-with-unibyte-current-buffer-mule4
575       (with-temp-buffer
576         (flet ((gpg-encrypt-func 
577                  (sign plaintext ciphertext result recipients &optional
578                        passphrase sign-with-key armor textmode)
579                  (if sign-also
580                      (gpg-sign-encrypt
581                       plaintext ciphertext result recipients passphrase
582                       sign-with-key armor textmode)
583                    (gpg-encrypt
584                     plaintext ciphertext result recipients passphrase
585                     armor textmode))))
586           (unless (gpg-encrypt-func
587                     sign-also ; passed in when using signencrypt
588                     text (setq cipher (current-buffer))
589                     mml2015-result-buffer
590                     (split-string
591                      (or
592                       (message-options-get 'message-recipients)
593                       (message-options-set 'message-recipients
594                                            (read-string "Recipients: ")))
595                      "[ \f\t\n\r\v,]+")
596                     nil
597                     (message-options-get 'message-sender)
598                     t t) ; armor & textmode
599             (unless (> (point-max) (point-min))
600               (pop-to-buffer mml2015-result-buffer)
601               (error "Encrypt error"))))
602         (goto-char (point-min))
603         (while (re-search-forward "\r+$" nil t)
604           (replace-match "" t t))
605         (set-buffer text)
606         (delete-region (point-min) (point-max))
607         (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
608                         boundary))
609         (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
610         (insert (format "--%s\n" boundary))
611         (insert "Content-Type: application/pgp-encrypted\n\n")
612         (insert "Version: 1\n\n")
613         (insert (format "--%s\n" boundary))
614         (insert "Content-Type: application/octet-stream\n\n")
615         (insert-buffer cipher)
616         (goto-char (point-max))
617         (insert (format "--%s--\n" boundary))
618         (goto-char (point-max))))))
619
620 ;;; General wrapper
621
622 (defun mml2015-clean-buffer ()
623   (if (gnus-buffer-live-p mml2015-result-buffer)
624       (with-current-buffer mml2015-result-buffer
625         (erase-buffer)
626         t)
627     (setq mml2015-result-buffer
628           (gnus-get-buffer-create "*MML2015 Result*"))
629     nil))
630
631 (defsubst mml2015-clear-decrypt-function ()
632   (nth 6 (assq mml2015-use mml2015-function-alist)))
633
634 (defsubst mml2015-clear-verify-function ()
635   (nth 5 (assq mml2015-use mml2015-function-alist)))
636
637 ;;;###autoload
638 (defun mml2015-decrypt (handle ctl)
639   (mml2015-clean-buffer)
640   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
641     (if func
642         (funcall func handle ctl)
643       handle)))
644
645 ;;;###autoload
646 (defun mml2015-decrypt-test (handle ctl)
647   mml2015-use)
648
649 ;;;###autoload
650 (defun mml2015-verify (handle ctl)
651   (mml2015-clean-buffer)
652   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
653     (if func
654         (funcall func handle ctl)
655       handle)))
656
657 ;;;###autoload
658 (defun mml2015-verify-test (handle ctl)
659   mml2015-use)
660
661 ;;;###autoload
662 (defun mml2015-encrypt (cont &optional sign)
663   (mml2015-clean-buffer)
664   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
665     (if func
666         (funcall func cont sign)
667       (error "Cannot find encrypt function"))))
668
669 ;;;###autoload
670 (defun mml2015-sign (cont)
671   (mml2015-clean-buffer)
672   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
673     (if func
674         (funcall func cont)
675       (error "Cannot find sign function"))))
676
677 ;;;###autoload
678 (defun mml2015-self-encrypt ()
679   (mml2015-encrypt nil))
680
681 (provide 'mml2015)
682
683 ;;; mml2015.el ends here