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