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