1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000, 2001, 2002, 2003 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))
32 (eval-when-compile (require 'gnus-clfns))
37 (defvar mml2015-use (or
41 (and (fboundp 'pgg-sign-region)
46 (and (fboundp 'gpg-sign-detached)
50 (and (fboundp 'mc-encrypt-generic)
51 (fboundp 'mc-sign-generic)
52 (fboundp 'mc-cleanup-recipient-headers)
54 "The package used for PGP/MIME.")
56 ;; Something is not RFC2015.
57 (defvar mml2015-function-alist
58 '((mailcrypt mml2015-mailcrypt-sign
59 mml2015-mailcrypt-encrypt
60 mml2015-mailcrypt-verify
61 mml2015-mailcrypt-decrypt
62 mml2015-mailcrypt-clear-verify
63 mml2015-mailcrypt-clear-decrypt)
68 mml2015-gpg-clear-verify
69 mml2015-gpg-clear-decrypt)
74 mml2015-pgg-clear-verify
75 mml2015-pgg-clear-decrypt))
76 "Alist of PGP/MIME functions.")
78 (defvar mml2015-result-buffer nil)
80 (defcustom mml2015-unabbrev-trust-alist
81 '(("TRUST_UNDEFINED" . nil)
83 ("TRUST_MARGINAL" . t)
85 ("TRUST_ULTIMATE" . t))
86 "Map GnuPG trust output values to a boolean saying if you trust the key."
87 :type '(repeat (cons (regexp :tag "GnuPG output regexp")
88 (boolean :tag "Trust key"))))
93 (autoload 'mailcrypt-decrypt "mailcrypt")
94 (autoload 'mailcrypt-verify "mailcrypt")
95 (autoload 'mc-pgp-always-sign "mailcrypt")
96 (autoload 'mc-encrypt-generic "mc-toplev")
97 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
98 (autoload 'mc-sign-generic "mc-toplev"))
101 (defvar mc-default-scheme)
104 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
105 (defvar mml2015-verify-function 'mailcrypt-verify)
107 (defun mml2015-format-error (err)
108 (if (stringp (cadr err))
110 (format "%S" (cdr err))))
112 (defun mml2015-mailcrypt-decrypt (handle ctl)
114 (let (child handles result)
115 (unless (setq child (mm-find-part-by-type
117 "application/octet-stream" nil t))
118 (mm-set-handle-multipart-parameter
119 mm-security-handle 'gnus-info "Corrupted")
120 (throw 'error handle))
122 (mm-insert-part child)
125 (funcall mml2015-decrypt-function)
127 (mm-set-handle-multipart-parameter
128 mm-security-handle 'gnus-details (mml2015-format-error err))
131 (mm-set-handle-multipart-parameter
132 mm-security-handle 'gnus-details "Quit.")
135 (mm-set-handle-multipart-parameter
136 mm-security-handle 'gnus-info "Failed")
137 (throw 'error handle))
138 (setq handles (mm-dissect-buffer t)))
139 (mm-destroy-parts handle)
140 (mm-set-handle-multipart-parameter
141 mm-security-handle 'gnus-info
143 (let ((sig (with-current-buffer mml2015-result-buffer
144 (mml2015-gpg-extract-signature-details))))
145 (concat ", Signer: " sig))))
146 (if (listp (car handles))
150 (defun mml2015-mailcrypt-clear-decrypt ()
154 (funcall mml2015-decrypt-function)
156 (mm-set-handle-multipart-parameter
157 mm-security-handle 'gnus-details (mml2015-format-error err))
160 (mm-set-handle-multipart-parameter
161 mm-security-handle 'gnus-details "Quit.")
164 (mm-set-handle-multipart-parameter
165 mm-security-handle 'gnus-info "OK")
166 (mm-set-handle-multipart-parameter
167 mm-security-handle 'gnus-info "Failed"))))
169 (defun mml2015-fix-micalg (alg)
171 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
172 (upcase (if (string-match "^p[gh]p-" alg)
173 (substring alg (match-end 0))
176 (defun mml2015-mailcrypt-verify (handle ctl)
179 (unless (setq part (mm-find-raw-part-by-type
180 ctl (or (mm-handle-multipart-ctl-parameter
182 "application/pgp-signature")
184 (mm-set-handle-multipart-parameter
185 mm-security-handle 'gnus-info "Corrupted")
186 (throw 'error handle))
188 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
189 (insert (format "Hash: %s\n\n"
190 (or (mml2015-fix-micalg
191 (mm-handle-multipart-ctl-parameter
195 (narrow-to-region (point) (point))
197 (goto-char (point-min))
199 (if (looking-at "^-")
202 (unless (setq part (mm-find-part-by-type
203 (cdr handle) "application/pgp-signature" nil t))
204 (mm-set-handle-multipart-parameter
205 mm-security-handle 'gnus-info "Corrupted")
206 (throw 'error handle))
208 (narrow-to-region (point) (point))
209 (mm-insert-part part)
210 (goto-char (point-min))
211 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
212 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
213 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
214 (replace-match "-----END PGP SIGNATURE-----" t t)))
215 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
216 (unless (condition-case err
218 (funcall mml2015-verify-function)
219 (if (get-buffer " *mailcrypt stderr temp")
220 (mm-set-handle-multipart-parameter
221 mm-security-handle 'gnus-details
222 (with-current-buffer " *mailcrypt stderr temp"
224 (if (get-buffer " *mailcrypt stdout temp")
225 (kill-buffer " *mailcrypt stdout temp"))
226 (if (get-buffer " *mailcrypt stderr temp")
227 (kill-buffer " *mailcrypt stderr temp"))
228 (if (get-buffer " *mailcrypt status temp")
229 (kill-buffer " *mailcrypt status temp"))
230 (if (get-buffer mc-gpg-debug-buffer)
231 (kill-buffer mc-gpg-debug-buffer)))
233 (mm-set-handle-multipart-parameter
234 mm-security-handle 'gnus-details (mml2015-format-error err))
237 (mm-set-handle-multipart-parameter
238 mm-security-handle 'gnus-details "Quit.")
240 (mm-set-handle-multipart-parameter
241 mm-security-handle 'gnus-info "Failed")
242 (throw 'error handle))))
243 (mm-set-handle-multipart-parameter
244 mm-security-handle 'gnus-info "OK")
247 (defun mml2015-mailcrypt-clear-verify ()
248 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
249 (if (condition-case err
251 (funcall mml2015-verify-function)
252 (if (get-buffer " *mailcrypt stderr temp")
253 (mm-set-handle-multipart-parameter
254 mm-security-handle 'gnus-details
255 (with-current-buffer " *mailcrypt stderr temp"
257 (if (get-buffer " *mailcrypt stdout temp")
258 (kill-buffer " *mailcrypt stdout temp"))
259 (if (get-buffer " *mailcrypt stderr temp")
260 (kill-buffer " *mailcrypt stderr temp"))
261 (if (get-buffer " *mailcrypt status temp")
262 (kill-buffer " *mailcrypt status temp"))
263 (if (get-buffer mc-gpg-debug-buffer)
264 (kill-buffer mc-gpg-debug-buffer)))
266 (mm-set-handle-multipart-parameter
267 mm-security-handle 'gnus-details (mml2015-format-error err))
270 (mm-set-handle-multipart-parameter
271 mm-security-handle 'gnus-details "Quit.")
273 (mm-set-handle-multipart-parameter
274 mm-security-handle 'gnus-info "OK")
275 (mm-set-handle-multipart-parameter
276 mm-security-handle 'gnus-info "Failed"))))
278 (defun mml2015-mailcrypt-sign (cont)
279 (mc-sign-generic (message-options-get 'message-sender)
281 (let ((boundary (mml-compute-boundary cont))
283 (goto-char (point-min))
284 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
285 (error "Cannot find signed begin line"))
286 (goto-char (match-beginning 0))
288 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
289 (error "Cannot not find PGP hash"))
290 (setq hash (match-string 1))
291 (unless (re-search-forward "^$" nil t)
292 (error "Cannot not find PGP message"))
294 (delete-region (point-min) (point))
295 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
297 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
299 (insert (format "\n--%s\n" boundary))
301 (goto-char (point-max))
302 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
303 (error "Cannot find signature part"))
304 (replace-match "-----END PGP MESSAGE-----" t t)
305 (goto-char (match-beginning 0))
306 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
308 (error "Cannot find signature part"))
309 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
310 (goto-char (match-beginning 0))
312 (narrow-to-region point (point))
314 (while (re-search-forward "^- -" nil t)
315 (replace-match "-" t t))
316 (goto-char (point-max)))
317 (insert (format "--%s\n" boundary))
318 (insert "Content-Type: application/pgp-signature\n\n")
319 (goto-char (point-max))
320 (insert (format "--%s--\n" boundary))
321 (goto-char (point-max))))
323 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
324 (let ((mc-pgp-always-sign
325 (or mc-pgp-always-sign
327 (eq t (or (message-options-get 'message-sign-encrypt)
329 'message-sign-encrypt
330 (or (y-or-n-p "Sign the message? ")
333 (mm-with-unibyte-current-buffer
335 (or (message-options-get 'message-recipients)
336 (message-options-set 'message-recipients
337 (mc-cleanup-recipient-headers
338 (read-string "Recipients: "))))
340 (message-options-get 'message-sender))))
341 (goto-char (point-min))
342 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
343 (error "Fail to encrypt the message"))
344 (let ((boundary (mml-compute-boundary cont)))
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-substring 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)
546 (let ((boundary (mml-compute-boundary cont))
547 (text (current-buffer)) signature)
548 (goto-char (point-max))
552 (unless (gpg-sign-detached text (setq signature (current-buffer))
553 mml2015-result-buffer
555 (message-options-get 'message-sender)
556 t t) ; armor & textmode
557 (unless (> (point-max) (point-min))
558 (pop-to-buffer mml2015-result-buffer)
559 (error "Sign error")))
560 (goto-char (point-min))
561 (while (re-search-forward "\r+$" nil t)
562 (replace-match "" t t))
564 (goto-char (point-min))
565 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
567 ;;; FIXME: what is the micalg?
568 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
569 (insert (format "\n--%s\n" boundary))
570 (goto-char (point-max))
571 (insert (format "\n--%s\n" boundary))
572 (insert "Content-Type: application/pgp-signature\n\n")
573 (insert-buffer-substring signature)
574 (goto-char (point-max))
575 (insert (format "--%s--\n" boundary))
576 (goto-char (point-max)))))
578 (defun mml2015-gpg-encrypt (cont &optional sign)
579 (let ((boundary (mml-compute-boundary cont))
580 (text (current-buffer))
582 (mm-with-unibyte-current-buffer
584 ;; set up a function to call the correct gpg encrypt routine
585 ;; with the right arguments. (FIXME: this should be done
587 (flet ((gpg-encrypt-func
588 (sign plaintext ciphertext result recipients &optional
589 passphrase sign-with-key armor textmode)
592 plaintext ciphertext result recipients passphrase
593 sign-with-key armor textmode)
595 plaintext ciphertext result recipients passphrase
597 (unless (gpg-encrypt-func
598 sign ; passed in when using signencrypt
599 text (setq cipher (current-buffer))
600 mml2015-result-buffer
603 (message-options-get 'message-recipients)
604 (message-options-set 'message-recipients
605 (read-string "Recipients: ")))
608 (message-options-get 'message-sender)
609 t t) ; armor & textmode
610 (unless (> (point-max) (point-min))
611 (pop-to-buffer mml2015-result-buffer)
612 (error "Encrypt error"))))
613 (goto-char (point-min))
614 (while (re-search-forward "\r+$" nil t)
615 (replace-match "" t t))
617 (delete-region (point-min) (point-max))
618 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
620 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
621 (insert (format "--%s\n" boundary))
622 (insert "Content-Type: application/pgp-encrypted\n\n")
623 (insert "Version: 1\n\n")
624 (insert (format "--%s\n" boundary))
625 (insert "Content-Type: application/octet-stream\n\n")
626 (insert-buffer-substring cipher)
627 (goto-char (point-max))
628 (insert (format "--%s--\n" boundary))
629 (goto-char (point-max))))))
634 (defvar pgg-errors-buffer)
635 (defvar pgg-output-buffer))
638 (autoload 'pgg-decrypt-region "pgg")
639 (autoload 'pgg-verify-region "pgg")
640 (autoload 'pgg-sign-region "pgg")
641 (autoload 'pgg-encrypt-region "pgg"))
643 (defun mml2015-pgg-decrypt (handle ctl)
645 (let ((pgg-errors-buffer mml2015-result-buffer)
646 child handles result decrypt-status)
647 (unless (setq child (mm-find-part-by-type
649 "application/octet-stream" nil t))
650 (mm-set-handle-multipart-parameter
651 mm-security-handle 'gnus-info "Corrupted")
652 (throw 'error handle))
654 (mm-insert-part child)
655 (if (condition-case err
657 (pgg-decrypt-region (point-min) (point-max))
659 (with-current-buffer mml2015-result-buffer
661 (mm-set-handle-multipart-parameter
662 mm-security-handle 'gnus-details
665 (mm-set-handle-multipart-parameter
666 mm-security-handle 'gnus-details (mml2015-format-error err))
669 (mm-set-handle-multipart-parameter
670 mm-security-handle 'gnus-details "Quit.")
672 (with-current-buffer pgg-output-buffer
673 (goto-char (point-min))
674 (while (search-forward "\r\n" nil t)
675 (replace-match "\n" t t))
676 (setq handles (mm-dissect-buffer t))
677 (mm-destroy-parts handle)
678 (mm-set-handle-multipart-parameter
679 mm-security-handle 'gnus-info "OK")
680 (mm-set-handle-multipart-parameter
681 mm-security-handle 'gnus-details
682 (concat decrypt-status
683 (when (stringp (car handles))
684 "\n" (mm-handle-multipart-ctl-parameter
685 handles 'gnus-details))))
686 (if (listp (car handles))
689 (mm-set-handle-multipart-parameter
690 mm-security-handle 'gnus-info "Failed")
691 (throw 'error handle))))))
693 (defun mml2015-pgg-clear-decrypt ()
694 (let ((pgg-errors-buffer mml2015-result-buffer))
696 (pgg-decrypt-region (point-min) (point-max))
697 (mm-set-handle-multipart-parameter
698 mm-security-handle 'gnus-details
699 (with-current-buffer mml2015-result-buffer
703 (insert-buffer-substring pgg-output-buffer)
704 (goto-char (point-min))
705 (while (search-forward "\r\n" nil t)
706 (replace-match "\n" t t))
707 (mm-set-handle-multipart-parameter
708 mm-security-handle 'gnus-info "OK"))
709 (mm-set-handle-multipart-parameter
710 mm-security-handle 'gnus-info "Failed"))))
712 (defun mml2015-pgg-verify (handle ctl)
713 (let ((pgg-errors-buffer mml2015-result-buffer)
714 signature-file part signature)
715 (if (or (null (setq part (mm-find-raw-part-by-type
716 ctl (or (mm-handle-multipart-ctl-parameter
718 "application/pgp-signature")
720 (null (setq signature (mm-find-part-by-type
721 (cdr handle) "application/pgp-signature" nil t))))
723 (mm-set-handle-multipart-parameter
724 mm-security-handle 'gnus-info "Corrupted")
728 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
729 ;; clearsign use --textmode. The conversion is not necessary.
730 ;; In clearverify, the conversion is not necessary either.
731 (goto-char (point-min))
734 (unless (eq (char-before) ?\r)
738 (with-temp-file (setq signature-file (mm-make-temp-file "pgg"))
739 (mm-insert-part signature))
740 (if (condition-case err
742 (pgg-verify-region (point-min) (point-max)
744 (goto-char (point-min))
745 (while (search-forward "\r\n" nil t)
746 (replace-match "\n" t t))
747 (mm-set-handle-multipart-parameter
748 mm-security-handle 'gnus-details
749 (concat (with-current-buffer pgg-output-buffer
751 (with-current-buffer pgg-errors-buffer
754 (mm-set-handle-multipart-parameter
755 mm-security-handle 'gnus-details (mml2015-format-error err))
758 (mm-set-handle-multipart-parameter
759 mm-security-handle 'gnus-details "Quit.")
762 (delete-file signature-file)
763 (mm-set-handle-multipart-parameter
764 mm-security-handle 'gnus-info
765 (with-current-buffer pgg-errors-buffer
766 (mml2015-gpg-extract-signature-details))))
767 (delete-file signature-file)
768 (mm-set-handle-multipart-parameter
769 mm-security-handle 'gnus-info "Failed")))))
772 (defun mml2015-pgg-clear-verify ()
773 (let ((pgg-errors-buffer mml2015-result-buffer)
774 (text (buffer-string))
775 (coding-system buffer-file-coding-system))
776 (if (condition-case err
778 (mm-with-unibyte-buffer
779 (insert (encode-coding-string text coding-system))
780 (pgg-verify-region (point-min) (point-max) nil t))
781 (goto-char (point-min))
782 (while (search-forward "\r\n" nil t)
783 (replace-match "\n" t t))
784 (mm-set-handle-multipart-parameter
785 mm-security-handle 'gnus-details
786 (concat (with-current-buffer pgg-output-buffer
788 (with-current-buffer pgg-errors-buffer
791 (mm-set-handle-multipart-parameter
792 mm-security-handle 'gnus-details (mml2015-format-error err))
795 (mm-set-handle-multipart-parameter
796 mm-security-handle 'gnus-details "Quit.")
798 (mm-set-handle-multipart-parameter
799 mm-security-handle 'gnus-info
800 (with-current-buffer pgg-errors-buffer
801 (mml2015-gpg-extract-signature-details)))
802 (mm-set-handle-multipart-parameter
803 mm-security-handle 'gnus-info "Failed"))))
805 (defun mml2015-pgg-sign (cont)
806 (let ((pgg-errors-buffer mml2015-result-buffer)
807 (boundary (mml-compute-boundary cont))
808 (pgg-default-user-id (or (message-options-get 'mml-sender)
809 pgg-default-user-id)))
810 (unless (pgg-sign-region (point-min) (point-max))
811 (pop-to-buffer mml2015-result-buffer)
812 (error "Sign error"))
813 (goto-char (point-min))
814 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
816 ;;; FIXME: what is the micalg?
817 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
818 (insert (format "\n--%s\n" boundary))
819 (goto-char (point-max))
820 (insert (format "\n--%s\n" boundary))
821 (insert "Content-Type: application/pgp-signature\n\n")
822 (insert-buffer-substring pgg-output-buffer)
823 (goto-char (point-max))
824 (insert (format "--%s--\n" boundary))
825 (goto-char (point-max))))
827 (defun mml2015-pgg-encrypt (cont &optional sign)
828 (let ((pgg-errors-buffer mml2015-result-buffer)
829 (boundary (mml-compute-boundary cont)))
830 (unless (pgg-encrypt-region (point-min) (point-max)
833 (message-options-get 'message-recipients)
834 (message-options-set 'message-recipients
835 (read-string "Recipients: ")))
838 (pop-to-buffer mml2015-result-buffer)
839 (error "Encrypt error"))
840 (delete-region (point-min) (point-max))
841 (goto-char (point-min))
842 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
844 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
845 (insert (format "--%s\n" boundary))
846 (insert "Content-Type: application/pgp-encrypted\n\n")
847 (insert "Version: 1\n\n")
848 (insert (format "--%s\n" boundary))
849 (insert "Content-Type: application/octet-stream\n\n")
850 (insert-buffer-substring pgg-output-buffer)
851 (goto-char (point-max))
852 (insert (format "--%s--\n" boundary))
853 (goto-char (point-max))))
857 (defun mml2015-clean-buffer ()
858 (if (gnus-buffer-live-p mml2015-result-buffer)
859 (with-current-buffer mml2015-result-buffer
862 (setq mml2015-result-buffer
863 (gnus-get-buffer-create "*MML2015 Result*"))
866 (defsubst mml2015-clear-decrypt-function ()
867 (nth 6 (assq mml2015-use mml2015-function-alist)))
869 (defsubst mml2015-clear-verify-function ()
870 (nth 5 (assq mml2015-use mml2015-function-alist)))
873 (defun mml2015-decrypt (handle ctl)
874 (mml2015-clean-buffer)
875 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
877 (funcall func handle ctl)
881 (defun mml2015-decrypt-test (handle ctl)
885 (defun mml2015-verify (handle ctl)
886 (mml2015-clean-buffer)
887 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
889 (funcall func handle ctl)
893 (defun mml2015-verify-test (handle ctl)
897 (defun mml2015-encrypt (cont &optional sign)
898 (mml2015-clean-buffer)
899 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
901 (funcall func cont sign)
902 (error "Cannot find encrypt function"))))
905 (defun mml2015-sign (cont)
906 (mml2015-clean-buffer)
907 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
910 (error "Cannot find sign function"))))
913 (defun mml2015-self-encrypt ()
914 (mml2015-encrypt nil))
918 ;;; mml2015.el ends here