* pop3.el (pop3-open-server): Set process buffer unibyte.
[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_.*\\)$" nil t)
423                           (match-string 1)))
424              (trust-good-enough-p
425               (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
426                       mml2015-trust-boundaries-alist))))
427         (if (and signer trust fprint)
428             (concat signer
429                     (unless trust-good-enough-p
430                       (concat "\nUntrusted, Fingerprint: "
431                               (mml2015-gpg-pretty-print-fpr fprint))))
432           "From unknown user"))
433     (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
434         (match-string 1)
435       "From unknown user")))
436
437 (defun mml2015-gpg-verify (handle ctl)
438   (catch 'error
439     (let (part message signature info-is-set-p)
440       (unless (setq part (mm-find-raw-part-by-type
441                           ctl (or (mm-handle-multipart-ctl-parameter
442                                    ctl 'protocol)
443                                   "application/pgp-signature")
444                           t))
445         (mm-set-handle-multipart-parameter
446          mm-security-handle 'gnus-info "Corrupted")
447         (throw 'error handle))
448       (with-temp-buffer
449         (setq message (current-buffer))
450         (insert part)
451         ;; Convert <LF> to <CR><LF> in verify mode.  Sign and
452         ;; clearsign use --textmode. The conversion is not necessary.
453         ;; In clearverify, the conversion is not necessary either.
454         (goto-char (point-min))
455         (end-of-line)
456         (while (not (eobp))
457           (unless (eq (char-before) ?\r)
458             (insert "\r"))
459           (forward-line)
460           (end-of-line))
461         (with-temp-buffer
462           (setq signature (current-buffer))
463           (unless (setq part (mm-find-part-by-type
464                               (cdr handle) "application/pgp-signature" nil t))
465             (mm-set-handle-multipart-parameter
466              mm-security-handle 'gnus-info "Corrupted")
467             (throw 'error handle))
468           (mm-insert-part part)
469           (unless (condition-case err
470                       (prog1
471                           (gpg-verify message signature mml2015-result-buffer)
472                         (mm-set-handle-multipart-parameter
473                          mm-security-handle 'gnus-details
474                          (with-current-buffer mml2015-result-buffer
475                            (buffer-string))))
476                     (error
477                      (mm-set-handle-multipart-parameter
478                       mm-security-handle 'gnus-details (mml2015-format-error err))
479                      (mm-set-handle-multipart-parameter
480                       mm-security-handle 'gnus-info "Error.")
481                      (setq info-is-set-p t)
482                      nil)
483                     (quit
484                      (mm-set-handle-multipart-parameter
485                       mm-security-handle 'gnus-details "Quit.")
486                      (mm-set-handle-multipart-parameter
487                       mm-security-handle 'gnus-info "Quit.")
488                      (setq info-is-set-p t)
489                      nil))
490             (unless info-is-set-p
491               (mm-set-handle-multipart-parameter
492                mm-security-handle 'gnus-info "Failed"))
493             (throw 'error handle)))
494         (mm-set-handle-multipart-parameter
495          mm-security-handle 'gnus-info
496          (with-current-buffer mml2015-result-buffer
497            (mml2015-gpg-extract-signature-details))))
498       handle)))
499
500 (defun mml2015-gpg-clear-verify ()
501   (if (condition-case err
502           (prog1
503               (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
504             (mm-set-handle-multipart-parameter
505              mm-security-handle 'gnus-details
506              (with-current-buffer mml2015-result-buffer
507                (buffer-string))))
508         (error
509          (mm-set-handle-multipart-parameter
510           mm-security-handle 'gnus-details (mml2015-format-error err))
511          nil)
512         (quit
513          (mm-set-handle-multipart-parameter
514           mm-security-handle 'gnus-details "Quit.")
515          nil))
516       (mm-set-handle-multipart-parameter
517        mm-security-handle 'gnus-info
518        (with-current-buffer mml2015-result-buffer
519          (mml2015-gpg-extract-signature-details)))
520     (mm-set-handle-multipart-parameter
521      mm-security-handle 'gnus-info "Failed")))
522
523 (defun mml2015-gpg-sign (cont)
524   (let ((boundary
525          (funcall mml-boundary-function (incf mml-multipart-number)))
526         (text (current-buffer)) signature)
527     (goto-char (point-max))
528     (unless (bolp)
529       (insert "\n"))
530     (with-temp-buffer
531       (unless (gpg-sign-detached text (setq signature (current-buffer))
532                                  mml2015-result-buffer
533                                  nil
534                                  (message-options-get 'message-sender)
535                                  t t) ; armor & textmode
536         (unless (> (point-max) (point-min))
537           (pop-to-buffer mml2015-result-buffer)
538           (error "Sign error")))
539       (goto-char (point-min))
540       (while (re-search-forward "\r+$" nil t)
541         (replace-match "" t t))
542       (set-buffer text)
543       (goto-char (point-min))
544       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
545                       boundary))
546       ;;; FIXME: what is the micalg?
547       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
548       (insert (format "\n--%s\n" boundary))
549       (goto-char (point-max))
550       (insert (format "\n--%s\n" boundary))
551       (insert "Content-Type: application/pgp-signature\n\n")
552       (insert-buffer signature)
553       (goto-char (point-max))
554       (insert (format "--%s--\n" boundary))
555       (goto-char (point-max)))))
556
557 (defun mml2015-gpg-encrypt (cont)
558   (let ((boundary
559          (funcall mml-boundary-function (incf mml-multipart-number)))
560         (text (current-buffer))
561         cipher)
562     (mm-with-unibyte-current-buffer-mule4
563       (with-temp-buffer
564         (unless (gpg-sign-encrypt
565                  text (setq cipher (current-buffer))
566                  mml2015-result-buffer
567                  (split-string
568                   (or
569                    (message-options-get 'message-recipients)
570                    (message-options-set 'message-recipients
571                                         (read-string "Recipients: ")))
572                   "[ \f\t\n\r\v,]+")
573                  nil
574                  (message-options-get 'message-sender)
575                  t t) ; armor & textmode
576           (unless (> (point-max) (point-min))
577             (pop-to-buffer mml2015-result-buffer)
578             (error "Encrypt error")))
579         (goto-char (point-min))
580         (while (re-search-forward "\r+$" nil t)
581           (replace-match "" t t))
582         (set-buffer text)
583         (delete-region (point-min) (point-max))
584         (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
585                         boundary))
586         (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
587         (insert (format "--%s\n" boundary))
588         (insert "Content-Type: application/pgp-encrypted\n\n")
589         (insert "Version: 1\n\n")
590         (insert (format "--%s\n" boundary))
591         (insert "Content-Type: application/octet-stream\n\n")
592         (insert-buffer cipher)
593         (goto-char (point-max))
594         (insert (format "--%s--\n" boundary))
595         (goto-char (point-max))))))
596
597 ;;; General wrapper
598
599 (defun mml2015-clean-buffer ()
600   (if (gnus-buffer-live-p mml2015-result-buffer)
601       (with-current-buffer mml2015-result-buffer
602         (erase-buffer)
603         t)
604     (setq mml2015-result-buffer
605           (gnus-get-buffer-create "*MML2015 Result*"))
606     nil))
607
608 (defsubst mml2015-clear-decrypt-function ()
609   (nth 6 (assq mml2015-use mml2015-function-alist)))
610
611 (defsubst mml2015-clear-verify-function ()
612   (nth 5 (assq mml2015-use mml2015-function-alist)))
613
614 ;;;###autoload
615 (defun mml2015-decrypt (handle ctl)
616   (mml2015-clean-buffer)
617   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
618     (if func
619         (funcall func handle ctl)
620       handle)))
621
622 ;;;###autoload
623 (defun mml2015-decrypt-test (handle ctl)
624   mml2015-use)
625
626 ;;;###autoload
627 (defun mml2015-verify (handle ctl)
628   (mml2015-clean-buffer)
629   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
630     (if func
631         (funcall func handle ctl)
632       handle)))
633
634 ;;;###autoload
635 (defun mml2015-verify-test (handle ctl)
636   mml2015-use)
637
638 ;;;###autoload
639 (defun mml2015-encrypt (cont)
640   (mml2015-clean-buffer)
641   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
642     (if func
643         (funcall func cont)
644       (error "Cannot find encrypt function"))))
645
646 ;;;###autoload
647 (defun mml2015-sign (cont)
648   (mml2015-clean-buffer)
649   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
650     (if func
651         (funcall func cont)
652       (error "Cannot find sign function"))))
653
654 ;;;###autoload
655 (defun mml2015-self-encrypt ()
656   (mml2015-encrypt nil))
657
658 (provide 'mml2015)
659
660 ;;; mml2015.el ends here