1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: PGP MIME MML
7 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
31 (eval-when-compile (require 'cl))
35 (defvar mml2015-use (or
39 (and (fboundp 'pgg-sign-region)
44 (and (fboundp 'gpg-sign-detached)
48 (and (fboundp 'mc-encrypt-generic)
49 (fboundp 'mc-sign-generic)
50 (fboundp 'mc-cleanup-recipient-headers)
52 "The package used for PGP/MIME.")
54 ;; Something is not RFC2015.
55 (defvar mml2015-function-alist
56 '((mailcrypt mml2015-mailcrypt-sign
57 mml2015-mailcrypt-encrypt
58 mml2015-mailcrypt-verify
59 mml2015-mailcrypt-decrypt
60 mml2015-mailcrypt-clear-verify
61 mml2015-mailcrypt-clear-decrypt)
66 mml2015-gpg-clear-verify
67 mml2015-gpg-clear-decrypt)
72 mml2015-pgg-clear-verify
73 mml2015-pgg-clear-decrypt))
74 "Alist of PGP/MIME functions.")
76 (defvar mml2015-result-buffer nil)
78 (defcustom mml2015-unabbrev-trust-alist
79 '(("TRUST_UNDEFINED" . nil)
81 ("TRUST_MARGINAL" . t)
83 ("TRUST_ULTIMATE" . t))
84 "Map GnuPG trust output values to a boolean saying if you trust the key."
85 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
86 (boolean :tag "Trust key"))))
91 (autoload 'mailcrypt-decrypt "mailcrypt")
92 (autoload 'mailcrypt-verify "mailcrypt")
93 (autoload 'mc-pgp-always-sign "mailcrypt")
94 (autoload 'mc-encrypt-generic "mc-toplev")
95 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
96 (autoload 'mc-sign-generic "mc-toplev"))
99 (defvar mc-default-scheme)
102 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
103 (defvar mml2015-verify-function 'mailcrypt-verify)
105 (defun mml2015-format-error (err)
106 (if (stringp (cadr err))
108 (format "%S" (cdr err))))
110 (defun mml2015-mailcrypt-decrypt (handle ctl)
112 (let (child handles result)
113 (unless (setq child (mm-find-part-by-type
115 "application/octet-stream" nil t))
116 (mm-set-handle-multipart-parameter
117 mm-security-handle 'gnus-info "Corrupted")
118 (throw 'error handle))
120 (mm-insert-part child)
123 (funcall mml2015-decrypt-function)
125 (mm-set-handle-multipart-parameter
126 mm-security-handle 'gnus-details (mml2015-format-error err))
129 (mm-set-handle-multipart-parameter
130 mm-security-handle 'gnus-details "Quit.")
133 (mm-set-handle-multipart-parameter
134 mm-security-handle 'gnus-info "Failed")
135 (throw 'error handle))
136 (setq handles (mm-dissect-buffer t)))
137 (mm-destroy-parts handle)
138 (mm-set-handle-multipart-parameter
139 mm-security-handle 'gnus-info
141 (let ((sig (with-current-buffer mml2015-result-buffer
142 (mml2015-gpg-extract-signature-details))))
143 (concat ", Signer: " sig))))
144 (if (listp (car handles))
148 (defun mml2015-mailcrypt-clear-decrypt ()
152 (funcall mml2015-decrypt-function)
154 (mm-set-handle-multipart-parameter
155 mm-security-handle 'gnus-details (mml2015-format-error err))
158 (mm-set-handle-multipart-parameter
159 mm-security-handle 'gnus-details "Quit.")
162 (mm-set-handle-multipart-parameter
163 mm-security-handle 'gnus-info "OK")
164 (mm-set-handle-multipart-parameter
165 mm-security-handle 'gnus-info "Failed"))))
167 (defun mml2015-fix-micalg (alg)
169 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
170 (upcase (if (string-match "^p[gh]p-" alg)
171 (substring alg (match-end 0))
174 (defun mml2015-mailcrypt-verify (handle ctl)
177 (unless (setq part (mm-find-raw-part-by-type
178 ctl (or (mm-handle-multipart-ctl-parameter
180 "application/pgp-signature")
182 (mm-set-handle-multipart-parameter
183 mm-security-handle 'gnus-info "Corrupted")
184 (throw 'error handle))
186 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
187 (insert (format "Hash: %s\n\n"
188 (or (mml2015-fix-micalg
189 (mm-handle-multipart-ctl-parameter
193 (narrow-to-region (point) (point))
195 (goto-char (point-min))
197 (if (looking-at "^-")
200 (unless (setq part (mm-find-part-by-type
201 (cdr handle) "application/pgp-signature" nil t))
202 (mm-set-handle-multipart-parameter
203 mm-security-handle 'gnus-info "Corrupted")
204 (throw 'error handle))
206 (narrow-to-region (point) (point))
207 (mm-insert-part part)
208 (goto-char (point-min))
209 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
210 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
211 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
212 (replace-match "-----END PGP SIGNATURE-----" t t)))
213 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
214 (unless (condition-case err
216 (funcall mml2015-verify-function)
217 (if (get-buffer " *mailcrypt stderr temp")
218 (mm-set-handle-multipart-parameter
219 mm-security-handle 'gnus-details
220 (with-current-buffer " *mailcrypt stderr temp"
222 (if (get-buffer " *mailcrypt stdout temp")
223 (kill-buffer " *mailcrypt stdout temp"))
224 (if (get-buffer " *mailcrypt stderr temp")
225 (kill-buffer " *mailcrypt stderr temp"))
226 (if (get-buffer " *mailcrypt status temp")
227 (kill-buffer " *mailcrypt status temp"))
228 (if (get-buffer mc-gpg-debug-buffer)
229 (kill-buffer mc-gpg-debug-buffer)))
231 (mm-set-handle-multipart-parameter
232 mm-security-handle 'gnus-details (mml2015-format-error err))
235 (mm-set-handle-multipart-parameter
236 mm-security-handle 'gnus-details "Quit.")
238 (mm-set-handle-multipart-parameter
239 mm-security-handle 'gnus-info "Failed")
240 (throw 'error handle))))
241 (mm-set-handle-multipart-parameter
242 mm-security-handle 'gnus-info "OK")
245 (defun mml2015-mailcrypt-clear-verify ()
246 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
247 (if (condition-case err
249 (funcall mml2015-verify-function)
250 (if (get-buffer " *mailcrypt stderr temp")
251 (mm-set-handle-multipart-parameter
252 mm-security-handle 'gnus-details
253 (with-current-buffer " *mailcrypt stderr temp"
255 (if (get-buffer " *mailcrypt stdout temp")
256 (kill-buffer " *mailcrypt stdout temp"))
257 (if (get-buffer " *mailcrypt stderr temp")
258 (kill-buffer " *mailcrypt stderr temp"))
259 (if (get-buffer " *mailcrypt status temp")
260 (kill-buffer " *mailcrypt status temp"))
261 (if (get-buffer mc-gpg-debug-buffer)
262 (kill-buffer mc-gpg-debug-buffer)))
264 (mm-set-handle-multipart-parameter
265 mm-security-handle 'gnus-details (mml2015-format-error err))
268 (mm-set-handle-multipart-parameter
269 mm-security-handle 'gnus-details "Quit.")
271 (mm-set-handle-multipart-parameter
272 mm-security-handle 'gnus-info "OK")
273 (mm-set-handle-multipart-parameter
274 mm-security-handle 'gnus-info "Failed"))))
276 (defun mml2015-mailcrypt-sign (cont)
277 (mc-sign-generic (message-options-get 'message-sender)
280 (funcall mml-boundary-function (incf mml-multipart-number)))
282 (goto-char (point-min))
283 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
284 (error "Cannot find signed begin line"))
285 (goto-char (match-beginning 0))
287 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
288 (error "Cannot not find PGP hash"))
289 (setq hash (match-string 1))
290 (unless (re-search-forward "^$" nil t)
291 (error "Cannot not find PGP message"))
293 (delete-region (point-min) (point))
294 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
296 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
298 (insert (format "\n--%s\n" boundary))
300 (goto-char (point-max))
301 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
302 (error "Cannot find signature part"))
303 (replace-match "-----END PGP MESSAGE-----" t t)
304 (goto-char (match-beginning 0))
305 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
307 (error "Cannot find signature part"))
308 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
309 (goto-char (match-beginning 0))
311 (narrow-to-region point (point))
313 (while (re-search-forward "^- -" nil t)
314 (replace-match "-" t t))
315 (goto-char (point-max)))
316 (insert (format "--%s\n" boundary))
317 (insert "Content-Type: application/pgp-signature\n\n")
318 (goto-char (point-max))
319 (insert (format "--%s--\n" boundary))
320 (goto-char (point-max))))
322 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
323 (let ((mc-pgp-always-sign
324 (or mc-pgp-always-sign
326 (eq t (or (message-options-get 'message-sign-encrypt)
328 'message-sign-encrypt
329 (or (y-or-n-p "Sign the message? ")
332 (mm-with-unibyte-current-buffer-mule4
334 (or (message-options-get 'message-recipients)
335 (message-options-set 'message-recipients
336 (mc-cleanup-recipient-headers
337 (read-string "Recipients: "))))
339 (message-options-get 'message-sender))))
340 (goto-char (point-min))
341 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
342 (error "Fail to encrypt the message"))
344 (funcall mml-boundary-function (incf mml-multipart-number))))
345 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
347 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
348 (insert (format "--%s\n" boundary))
349 (insert "Content-Type: application/pgp-encrypted\n\n")
350 (insert "Version: 1\n\n")
351 (insert (format "--%s\n" boundary))
352 (insert "Content-Type: application/octet-stream\n\n")
353 (goto-char (point-max))
354 (insert (format "--%s--\n" boundary))
355 (goto-char (point-max))))
360 (autoload 'gpg-decrypt "gpg")
361 (autoload 'gpg-verify "gpg")
362 (autoload 'gpg-verify-cleartext "gpg")
363 (autoload 'gpg-sign-detached "gpg")
364 (autoload 'gpg-sign-encrypt "gpg")
365 (autoload 'gpg-encrypt "gpg")
366 (autoload 'gpg-passphrase-read "gpg"))
368 (defun mml2015-gpg-passphrase ()
369 (or (message-options-get 'gpg-passphrase)
370 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
372 (defun mml2015-gpg-decrypt-1 ()
373 (let ((cipher (current-buffer)) plain result)
374 (if (with-temp-buffer
376 (gpg-decrypt cipher (setq plain (current-buffer))
377 mml2015-result-buffer nil)
378 (mm-set-handle-multipart-parameter
379 mm-security-handle 'gnus-details
380 (with-current-buffer mml2015-result-buffer
384 (insert-buffer plain)
385 (goto-char (point-min))
386 (while (search-forward "\r\n" nil t)
387 (replace-match "\n" t t))))
389 ;; Some wrong with the return value, check plain text buffer.
390 (if (> (point-max) (point-min))
394 (defun mml2015-gpg-decrypt (handle ctl)
395 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
396 (mml2015-mailcrypt-decrypt handle ctl)))
398 (defun mml2015-gpg-clear-decrypt ()
400 (setq result (mml2015-gpg-decrypt-1))
402 (mm-set-handle-multipart-parameter
403 mm-security-handle 'gnus-info "OK")
404 (mm-set-handle-multipart-parameter
405 mm-security-handle 'gnus-info "Failed"))))
407 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
409 (fpr-length (string-width fingerprint))
412 (setq fingerprint (string-to-list fingerprint))
414 (setq fpr-length (- fpr-length 4))
415 (setq slice (butlast fingerprint fpr-length))
416 (setq fingerprint (nthcdr 4 fingerprint))
417 (setq n-slice (1+ n-slice))
423 (otherwise (concat " " slice))))))
426 (defun mml2015-gpg-extract-signature-details ()
427 (goto-char (point-min))
428 (let* ((expired (re-search-forward
429 "^\\[GNUPG:\\] SIGEXPIRED$"
431 (signer (and (re-search-forward
432 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
434 (cons (match-string 1) (match-string 2))))
435 (fprint (and (re-search-forward
436 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
439 (trust (and (re-search-forward
440 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
444 (cdr (assoc trust mml2015-unabbrev-trust-alist))))
445 (cond ((and signer fprint)
447 (unless trust-good-enough-p
448 (concat "\nUntrusted, Fingerprint: "
449 (mml2015-gpg-pretty-print-fpr fprint)))
451 (format "\nWARNING: Signature from expired key (%s)"
454 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t)
457 "From unknown user"))))
459 (defun mml2015-gpg-verify (handle ctl)
461 (let (part message signature info-is-set-p)
462 (unless (setq part (mm-find-raw-part-by-type
463 ctl (or (mm-handle-multipart-ctl-parameter
465 "application/pgp-signature")
467 (mm-set-handle-multipart-parameter
468 mm-security-handle 'gnus-info "Corrupted")
469 (throw 'error handle))
471 (setq message (current-buffer))
473 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
474 ;; clearsign use --textmode. The conversion is not necessary.
475 ;; In clearverify, the conversion is not necessary either.
476 (goto-char (point-min))
479 (unless (eq (char-before) ?\r)
484 (setq signature (current-buffer))
485 (unless (setq part (mm-find-part-by-type
486 (cdr handle) "application/pgp-signature" nil t))
487 (mm-set-handle-multipart-parameter
488 mm-security-handle 'gnus-info "Corrupted")
489 (throw 'error handle))
490 (mm-insert-part part)
491 (unless (condition-case err
493 (gpg-verify message signature mml2015-result-buffer)
494 (mm-set-handle-multipart-parameter
495 mm-security-handle 'gnus-details
496 (with-current-buffer mml2015-result-buffer
499 (mm-set-handle-multipart-parameter
500 mm-security-handle 'gnus-details (mml2015-format-error err))
501 (mm-set-handle-multipart-parameter
502 mm-security-handle 'gnus-info "Error.")
503 (setq info-is-set-p t)
506 (mm-set-handle-multipart-parameter
507 mm-security-handle 'gnus-details "Quit.")
508 (mm-set-handle-multipart-parameter
509 mm-security-handle 'gnus-info "Quit.")
510 (setq info-is-set-p t)
512 (unless info-is-set-p
513 (mm-set-handle-multipart-parameter
514 mm-security-handle 'gnus-info "Failed"))
515 (throw 'error handle)))
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))))
522 (defun mml2015-gpg-clear-verify ()
523 (if (condition-case err
525 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
526 (mm-set-handle-multipart-parameter
527 mm-security-handle 'gnus-details
528 (with-current-buffer mml2015-result-buffer
531 (mm-set-handle-multipart-parameter
532 mm-security-handle 'gnus-details (mml2015-format-error err))
535 (mm-set-handle-multipart-parameter
536 mm-security-handle 'gnus-details "Quit.")
538 (mm-set-handle-multipart-parameter
539 mm-security-handle 'gnus-info
540 (with-current-buffer mml2015-result-buffer
541 (mml2015-gpg-extract-signature-details)))
542 (mm-set-handle-multipart-parameter
543 mm-security-handle 'gnus-info "Failed")))
545 (defun mml2015-gpg-sign (cont)
547 (funcall mml-boundary-function (incf mml-multipart-number)))
548 (text (current-buffer)) signature)
549 (goto-char (point-max))
553 (unless (gpg-sign-detached text (setq signature (current-buffer))
554 mml2015-result-buffer
556 (message-options-get 'message-sender)
557 t t) ; armor & textmode
558 (unless (> (point-max) (point-min))
559 (pop-to-buffer mml2015-result-buffer)
560 (error "Sign error")))
561 (goto-char (point-min))
562 (while (re-search-forward "\r+$" nil t)
563 (replace-match "" t t))
565 (goto-char (point-min))
566 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
568 ;;; FIXME: what is the micalg?
569 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
570 (insert (format "\n--%s\n" boundary))
571 (goto-char (point-max))
572 (insert (format "\n--%s\n" boundary))
573 (insert "Content-Type: application/pgp-signature\n\n")
574 (insert-buffer signature)
575 (goto-char (point-max))
576 (insert (format "--%s--\n" boundary))
577 (goto-char (point-max)))))
579 (defun mml2015-gpg-encrypt (cont &optional sign)
581 (funcall mml-boundary-function (incf mml-multipart-number)))
582 (text (current-buffer))
584 (mm-with-unibyte-current-buffer-mule4
586 ;; set up a function to call the correct gpg encrypt routine
587 ;; with the right arguments. (FIXME: this should be done
589 (flet ((gpg-encrypt-func
590 (sign plaintext ciphertext result recipients &optional
591 passphrase sign-with-key armor textmode)
594 plaintext ciphertext result recipients passphrase
595 sign-with-key armor textmode)
597 plaintext ciphertext result recipients passphrase
599 (unless (gpg-encrypt-func
600 sign ; passed in when using signencrypt
601 text (setq cipher (current-buffer))
602 mml2015-result-buffer
605 (message-options-get 'message-recipients)
606 (message-options-set 'message-recipients
607 (read-string "Recipients: ")))
610 (message-options-get 'message-sender)
611 t t) ; armor & textmode
612 (unless (> (point-max) (point-min))
613 (pop-to-buffer mml2015-result-buffer)
614 (error "Encrypt error"))))
615 (goto-char (point-min))
616 (while (re-search-forward "\r+$" nil t)
617 (replace-match "" t t))
619 (delete-region (point-min) (point-max))
620 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
622 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
623 (insert (format "--%s\n" boundary))
624 (insert "Content-Type: application/pgp-encrypted\n\n")
625 (insert "Version: 1\n\n")
626 (insert (format "--%s\n" boundary))
627 (insert "Content-Type: application/octet-stream\n\n")
628 (insert-buffer cipher)
629 (goto-char (point-max))
630 (insert (format "--%s--\n" boundary))
631 (goto-char (point-max))))))
636 (defvar pgg-errors-buffer)
637 (defvar pgg-output-buffer))
640 (autoload 'pgg-decrypt-region "pgg")
641 (autoload 'pgg-verify-region "pgg")
642 (autoload 'pgg-sign-region "pgg")
643 (autoload 'pgg-encrypt-region "pgg"))
645 (defun mml2015-pgg-decrypt (handle ctl)
647 (let ((pgg-errors-buffer mml2015-result-buffer)
648 child handles result decrypt-status)
649 (unless (setq child (mm-find-part-by-type
651 "application/octet-stream" nil t))
652 (mm-set-handle-multipart-parameter
653 mm-security-handle 'gnus-info "Corrupted")
654 (throw 'error handle))
656 (mm-insert-part child)
657 (if (condition-case err
659 (pgg-decrypt-region (point-min) (point-max))
661 (with-current-buffer mml2015-result-buffer
663 (mm-set-handle-multipart-parameter
664 mm-security-handle 'gnus-details
667 (mm-set-handle-multipart-parameter
668 mm-security-handle 'gnus-details (mml2015-format-error err))
671 (mm-set-handle-multipart-parameter
672 mm-security-handle 'gnus-details "Quit.")
674 (with-current-buffer pgg-output-buffer
675 (goto-char (point-min))
676 (while (search-forward "\r\n" nil t)
677 (replace-match "\n" t t))
678 (setq handles (mm-dissect-buffer t))
679 (mm-destroy-parts handle)
680 (mm-set-handle-multipart-parameter
681 mm-security-handle 'gnus-info "OK")
682 (mm-set-handle-multipart-parameter
683 mm-security-handle 'gnus-details
684 (concat decrypt-status
685 (when (stringp (car handles))
686 "\n" (mm-handle-multipart-ctl-parameter
687 handles 'gnus-details))))
688 (if (listp (car handles))
691 (mm-set-handle-multipart-parameter
692 mm-security-handle 'gnus-info "Failed")
693 (throw 'error handle))))))
695 (defun mml2015-pgg-clear-decrypt ()
696 (let ((pgg-errors-buffer mml2015-result-buffer))
698 (pgg-decrypt-region (point-min) (point-max))
699 (mm-set-handle-multipart-parameter
700 mm-security-handle 'gnus-details
701 (with-current-buffer mml2015-result-buffer
705 (insert-buffer pgg-output-buffer)
706 (goto-char (point-min))
707 (while (search-forward "\r\n" nil t)
708 (replace-match "\n" t t))
709 (mm-set-handle-multipart-parameter
710 mm-security-handle 'gnus-info "OK"))
711 (mm-set-handle-multipart-parameter
712 mm-security-handle 'gnus-info "Failed"))))
714 (defun mml2015-pgg-verify (handle ctl)
715 (let ((pgg-errors-buffer mml2015-result-buffer)
716 signature-file part signature)
717 (if (or (null (setq part (mm-find-raw-part-by-type
718 ctl (or (mm-handle-multipart-ctl-parameter
720 "application/pgp-signature")
722 (null (setq signature (mm-find-part-by-type
723 (cdr handle) "application/pgp-signature" nil t))))
725 (mm-set-handle-multipart-parameter
726 mm-security-handle 'gnus-info "Corrupted")
730 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
731 ;; clearsign use --textmode. The conversion is not necessary.
732 ;; In clearverify, the conversion is not necessary either.
733 (goto-char (point-min))
736 (unless (eq (char-before) ?\r)
740 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
741 (mm-insert-part signature))
742 (if (condition-case err
744 (pgg-verify-region (point-min) (point-max)
746 (goto-char (point-min))
747 (while (search-forward "\r\n" nil t)
748 (replace-match "\n" t t))
749 (mm-set-handle-multipart-parameter
750 mm-security-handle 'gnus-details
751 (concat (with-current-buffer pgg-output-buffer
753 (with-current-buffer pgg-errors-buffer
756 (mm-set-handle-multipart-parameter
757 mm-security-handle 'gnus-details (mml2015-format-error err))
760 (mm-set-handle-multipart-parameter
761 mm-security-handle 'gnus-details "Quit.")
764 (delete-file signature-file)
765 (mm-set-handle-multipart-parameter
766 mm-security-handle 'gnus-info
767 (with-current-buffer pgg-errors-buffer
768 (mml2015-gpg-extract-signature-details))))
769 (delete-file signature-file)
770 (mm-set-handle-multipart-parameter
771 mm-security-handle 'gnus-info "Failed")))))
774 (defun mml2015-pgg-clear-verify ()
775 (let ((pgg-errors-buffer mml2015-result-buffer)
776 (text (current-buffer)))
777 (if (condition-case err
779 (mm-with-unibyte-buffer
781 (pgg-verify-region (point-min) (point-max) nil t))
782 (goto-char (point-min))
783 (while (search-forward "\r\n" nil t)
784 (replace-match "\n" t t))
785 (mm-set-handle-multipart-parameter
786 mm-security-handle 'gnus-details
787 (concat (with-current-buffer pgg-output-buffer
789 (with-current-buffer pgg-errors-buffer
792 (mm-set-handle-multipart-parameter
793 mm-security-handle 'gnus-details (mml2015-format-error err))
796 (mm-set-handle-multipart-parameter
797 mm-security-handle 'gnus-details "Quit.")
799 (mm-set-handle-multipart-parameter
800 mm-security-handle 'gnus-info
801 (with-current-buffer pgg-errors-buffer
802 (mml2015-gpg-extract-signature-details)))
803 (mm-set-handle-multipart-parameter
804 mm-security-handle 'gnus-info "Failed"))))
806 (defun mml2015-pgg-sign (cont)
807 (let ((pgg-errors-buffer mml2015-result-buffer)
808 (boundary (funcall mml-boundary-function (incf mml-multipart-number))))
809 (unless (pgg-sign-region (point-min) (point-max))
810 (pop-to-buffer mml2015-result-buffer)
811 (error "Sign error"))
812 (goto-char (point-min))
813 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
815 ;;; FIXME: what is the micalg?
816 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
817 (insert (format "\n--%s\n" boundary))
818 (goto-char (point-max))
819 (insert (format "\n--%s\n" boundary))
820 (insert "Content-Type: application/pgp-signature\n\n")
821 (insert-buffer pgg-output-buffer)
822 (goto-char (point-max))
823 (insert (format "--%s--\n" boundary))
824 (goto-char (point-max))))
826 (defun mml2015-pgg-encrypt (cont &optional sign)
827 (let ((pgg-errors-buffer mml2015-result-buffer)
828 (boundary (funcall mml-boundary-function (incf mml-multipart-number))))
829 (unless (pgg-encrypt-region (point-min) (point-max)
832 (message-options-get 'message-recipients)
833 (message-options-set 'message-recipients
834 (read-string "Recipients: ")))
837 (pop-to-buffer mml2015-result-buffer)
838 (error "Encrypt error"))
839 (delete-region (point-min) (point-max))
840 (goto-char (point-min))
841 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
843 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
844 (insert (format "--%s\n" boundary))
845 (insert "Content-Type: application/pgp-encrypted\n\n")
846 (insert "Version: 1\n\n")
847 (insert (format "--%s\n" boundary))
848 (insert "Content-Type: application/octet-stream\n\n")
849 (insert-buffer pgg-output-buffer)
850 (goto-char (point-max))
851 (insert (format "--%s--\n" boundary))
852 (goto-char (point-max))))
856 (defun mml2015-clean-buffer ()
857 (if (gnus-buffer-live-p mml2015-result-buffer)
858 (with-current-buffer mml2015-result-buffer
861 (setq mml2015-result-buffer
862 (gnus-get-buffer-create "*MML2015 Result*"))
865 (defsubst mml2015-clear-decrypt-function ()
866 (nth 6 (assq mml2015-use mml2015-function-alist)))
868 (defsubst mml2015-clear-verify-function ()
869 (nth 5 (assq mml2015-use mml2015-function-alist)))
872 (defun mml2015-decrypt (handle ctl)
873 (mml2015-clean-buffer)
874 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
876 (funcall func handle ctl)
880 (defun mml2015-decrypt-test (handle ctl)
884 (defun mml2015-verify (handle ctl)
885 (mml2015-clean-buffer)
886 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
888 (funcall func handle ctl)
892 (defun mml2015-verify-test (handle ctl)
896 (defun mml2015-encrypt (cont &optional sign)
897 (mml2015-clean-buffer)
898 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
900 (funcall func cont sign)
901 (error "Cannot find encrypt function"))))
904 (defun mml2015-sign (cont)
905 (mml2015-clean-buffer)
906 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
909 (error "Cannot find sign function"))))
912 (defun mml2015-self-encrypt ()
913 (mml2015-encrypt nil))
917 ;;; mml2015.el ends here