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