1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: PGP MIME MML
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; RFC 2015 is updated by RFC 3156, this file should be compatible
33 (eval-when-compile (require 'cl
))
38 (defvar mml2015-use
(or
42 (and (fboundp 'pgg-sign-region
)
47 (and (fboundp 'gpg-sign-detached
)
51 (and (fboundp 'mc-encrypt-generic
)
52 (fboundp 'mc-sign-generic
)
53 (fboundp 'mc-cleanup-recipient-headers
)
55 "The package used for PGP/MIME.")
57 ;; Something is not RFC2015.
58 (defvar mml2015-function-alist
59 '((mailcrypt mml2015-mailcrypt-sign
60 mml2015-mailcrypt-encrypt
61 mml2015-mailcrypt-verify
62 mml2015-mailcrypt-decrypt
63 mml2015-mailcrypt-clear-verify
64 mml2015-mailcrypt-clear-decrypt
)
69 mml2015-gpg-clear-verify
70 mml2015-gpg-clear-decrypt
)
75 mml2015-pgg-clear-verify
76 mml2015-pgg-clear-decrypt
))
77 "Alist of PGP/MIME functions.")
79 (defvar mml2015-result-buffer nil
)
81 (defcustom mml2015-unabbrev-trust-alist
82 '(("TRUST_UNDEFINED" . nil
)
84 ("TRUST_MARGINAL" . t
)
86 ("TRUST_ULTIMATE" . t
))
87 "Map GnuPG trust output values to a boolean saying if you trust the key."
90 :type
'(repeat (cons (regexp :tag
"GnuPG output regexp")
91 (boolean :tag
"Trust key"))))
96 (autoload 'mailcrypt-decrypt
"mailcrypt")
97 (autoload 'mailcrypt-verify
"mailcrypt")
98 (autoload 'mc-pgp-always-sign
"mailcrypt")
99 (autoload 'mc-encrypt-generic
"mc-toplev")
100 (autoload 'mc-cleanup-recipient-headers
"mc-toplev")
101 (autoload 'mc-sign-generic
"mc-toplev"))
104 (defvar mc-default-scheme
)
107 (defvar mml2015-decrypt-function
'mailcrypt-decrypt
)
108 (defvar mml2015-verify-function
'mailcrypt-verify
)
110 (defun mml2015-format-error (err)
111 (if (stringp (cadr err
))
113 (format "%S" (cdr err
))))
115 (defun mml2015-mailcrypt-decrypt (handle ctl
)
117 (let (child handles result
)
118 (unless (setq child
(mm-find-part-by-type
120 "application/octet-stream" nil t
))
121 (mm-set-handle-multipart-parameter
122 mm-security-handle
'gnus-info
"Corrupted")
123 (throw 'error handle
))
125 (mm-insert-part child
)
128 (funcall mml2015-decrypt-function
)
130 (mm-set-handle-multipart-parameter
131 mm-security-handle
'gnus-details
(mml2015-format-error err
))
134 (mm-set-handle-multipart-parameter
135 mm-security-handle
'gnus-details
"Quit.")
138 (mm-set-handle-multipart-parameter
139 mm-security-handle
'gnus-info
"Failed")
140 (throw 'error handle
))
141 (setq handles
(mm-dissect-buffer t
)))
142 (mm-destroy-parts handle
)
143 (mm-set-handle-multipart-parameter
144 mm-security-handle
'gnus-info
146 (let ((sig (with-current-buffer mml2015-result-buffer
147 (mml2015-gpg-extract-signature-details))))
148 (concat ", Signer: " sig
))))
149 (if (listp (car handles
))
153 (defun mml2015-mailcrypt-clear-decrypt ()
157 (funcall mml2015-decrypt-function
)
159 (mm-set-handle-multipart-parameter
160 mm-security-handle
'gnus-details
(mml2015-format-error err
))
163 (mm-set-handle-multipart-parameter
164 mm-security-handle
'gnus-details
"Quit.")
167 (mm-set-handle-multipart-parameter
168 mm-security-handle
'gnus-info
"OK")
169 (mm-set-handle-multipart-parameter
170 mm-security-handle
'gnus-info
"Failed"))))
172 (defun mml2015-fix-micalg (alg)
174 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
175 (upcase (if (string-match "^p[gh]p-" alg
)
176 (substring alg
(match-end 0))
179 (defun mml2015-mailcrypt-verify (handle ctl
)
182 (unless (setq part
(mm-find-raw-part-by-type
183 ctl
(or (mm-handle-multipart-ctl-parameter
185 "application/pgp-signature")
187 (mm-set-handle-multipart-parameter
188 mm-security-handle
'gnus-info
"Corrupted")
189 (throw 'error handle
))
191 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
192 (insert (format "Hash: %s\n\n"
193 (or (mml2015-fix-micalg
194 (mm-handle-multipart-ctl-parameter
198 (narrow-to-region (point) (point))
200 (goto-char (point-min))
202 (if (looking-at "^-")
205 (unless (setq part
(mm-find-part-by-type
206 (cdr handle
) "application/pgp-signature" nil t
))
207 (mm-set-handle-multipart-parameter
208 mm-security-handle
'gnus-info
"Corrupted")
209 (throw 'error handle
))
211 (narrow-to-region (point) (point))
212 (mm-insert-part part
)
213 (goto-char (point-min))
214 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t
)
215 (replace-match "-----BEGIN PGP SIGNATURE-----" t t
))
216 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t
)
217 (replace-match "-----END PGP SIGNATURE-----" t t
)))
218 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
219 (unless (condition-case err
221 (funcall mml2015-verify-function
)
222 (if (get-buffer " *mailcrypt stderr temp")
223 (mm-set-handle-multipart-parameter
224 mm-security-handle
'gnus-details
225 (with-current-buffer " *mailcrypt stderr temp"
227 (if (get-buffer " *mailcrypt stdout temp")
228 (kill-buffer " *mailcrypt stdout temp"))
229 (if (get-buffer " *mailcrypt stderr temp")
230 (kill-buffer " *mailcrypt stderr temp"))
231 (if (get-buffer " *mailcrypt status temp")
232 (kill-buffer " *mailcrypt status temp"))
233 (if (get-buffer mc-gpg-debug-buffer
)
234 (kill-buffer mc-gpg-debug-buffer
)))
236 (mm-set-handle-multipart-parameter
237 mm-security-handle
'gnus-details
(mml2015-format-error err
))
240 (mm-set-handle-multipart-parameter
241 mm-security-handle
'gnus-details
"Quit.")
243 (mm-set-handle-multipart-parameter
244 mm-security-handle
'gnus-info
"Failed")
245 (throw 'error handle
))))
246 (mm-set-handle-multipart-parameter
247 mm-security-handle
'gnus-info
"OK")
250 (defun mml2015-mailcrypt-clear-verify ()
251 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
252 (if (condition-case err
254 (funcall mml2015-verify-function
)
255 (if (get-buffer " *mailcrypt stderr temp")
256 (mm-set-handle-multipart-parameter
257 mm-security-handle
'gnus-details
258 (with-current-buffer " *mailcrypt stderr temp"
260 (if (get-buffer " *mailcrypt stdout temp")
261 (kill-buffer " *mailcrypt stdout temp"))
262 (if (get-buffer " *mailcrypt stderr temp")
263 (kill-buffer " *mailcrypt stderr temp"))
264 (if (get-buffer " *mailcrypt status temp")
265 (kill-buffer " *mailcrypt status temp"))
266 (if (get-buffer mc-gpg-debug-buffer
)
267 (kill-buffer mc-gpg-debug-buffer
)))
269 (mm-set-handle-multipart-parameter
270 mm-security-handle
'gnus-details
(mml2015-format-error err
))
273 (mm-set-handle-multipart-parameter
274 mm-security-handle
'gnus-details
"Quit.")
276 (mm-set-handle-multipart-parameter
277 mm-security-handle
'gnus-info
"OK")
278 (mm-set-handle-multipart-parameter
279 mm-security-handle
'gnus-info
"Failed"))))
281 (defun mml2015-mailcrypt-sign (cont)
282 (mc-sign-generic (message-options-get 'message-sender
)
284 (let ((boundary (mml-compute-boundary cont
))
286 (goto-char (point-min))
287 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t
)
288 (error "Cannot find signed begin line"))
289 (goto-char (match-beginning 0))
291 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
292 (error "Cannot not find PGP hash"))
293 (setq hash
(match-string 1))
294 (unless (re-search-forward "^$" nil t
)
295 (error "Cannot not find PGP message"))
297 (delete-region (point-min) (point))
298 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
300 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
302 (insert (format "\n--%s\n" boundary
))
304 (goto-char (point-max))
305 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t
)
306 (error "Cannot find signature part"))
307 (replace-match "-----END PGP MESSAGE-----" t t
)
308 (goto-char (match-beginning 0))
309 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
311 (error "Cannot find signature part"))
312 (replace-match "-----BEGIN PGP MESSAGE-----" t t
)
313 (goto-char (match-beginning 0))
315 (narrow-to-region point
(point))
317 (while (re-search-forward "^- -" nil t
)
318 (replace-match "-" t t
))
319 (goto-char (point-max)))
320 (insert (format "--%s\n" boundary
))
321 (insert "Content-Type: application/pgp-signature\n\n")
322 (goto-char (point-max))
323 (insert (format "--%s--\n" boundary
))
324 (goto-char (point-max))))
326 (defun mml2015-mailcrypt-encrypt (cont &optional sign
)
327 (let ((mc-pgp-always-sign
328 (or mc-pgp-always-sign
330 (eq t
(or (message-options-get 'message-sign-encrypt
)
332 'message-sign-encrypt
333 (or (y-or-n-p "Sign the message? ")
336 (mm-with-unibyte-current-buffer
338 (or (message-options-get 'message-recipients
)
339 (message-options-set 'message-recipients
340 (mc-cleanup-recipient-headers
341 (read-string "Recipients: "))))
343 (message-options-get 'message-sender
))))
344 (goto-char (point-min))
345 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
346 (error "Fail to encrypt the message"))
347 (let ((boundary (mml-compute-boundary cont
)))
348 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
350 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
351 (insert (format "--%s\n" boundary
))
352 (insert "Content-Type: application/pgp-encrypted\n\n")
353 (insert "Version: 1\n\n")
354 (insert (format "--%s\n" boundary
))
355 (insert "Content-Type: application/octet-stream\n\n")
356 (goto-char (point-max))
357 (insert (format "--%s--\n" boundary
))
358 (goto-char (point-max))))
363 (autoload 'gpg-decrypt
"gpg")
364 (autoload 'gpg-verify
"gpg")
365 (autoload 'gpg-verify-cleartext
"gpg")
366 (autoload 'gpg-sign-detached
"gpg")
367 (autoload 'gpg-sign-encrypt
"gpg")
368 (autoload 'gpg-encrypt
"gpg")
369 (autoload 'gpg-passphrase-read
"gpg"))
371 (defun mml2015-gpg-passphrase ()
372 (or (message-options-get 'gpg-passphrase
)
373 (message-options-set 'gpg-passphrase
(gpg-passphrase-read))))
375 (defun mml2015-gpg-decrypt-1 ()
376 (let ((cipher (current-buffer)) plain result
)
377 (if (with-temp-buffer
379 (gpg-decrypt cipher
(setq plain
(current-buffer))
380 mml2015-result-buffer nil
)
381 (mm-set-handle-multipart-parameter
382 mm-security-handle
'gnus-details
383 (with-current-buffer mml2015-result-buffer
387 (insert-buffer-substring plain
)
388 (goto-char (point-min))
389 (while (search-forward "\r\n" nil t
)
390 (replace-match "\n" t t
))))
392 ;; Some wrong with the return value, check plain text buffer.
393 (if (> (point-max) (point-min))
397 (defun mml2015-gpg-decrypt (handle ctl
)
398 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1
))
399 (mml2015-mailcrypt-decrypt handle ctl
)))
401 (defun mml2015-gpg-clear-decrypt ()
403 (setq result
(mml2015-gpg-decrypt-1))
405 (mm-set-handle-multipart-parameter
406 mm-security-handle
'gnus-info
"OK")
407 (mm-set-handle-multipart-parameter
408 mm-security-handle
'gnus-info
"Failed"))))
410 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
412 (fpr-length (string-width fingerprint
))
415 (setq fingerprint
(string-to-list fingerprint
))
417 (setq fpr-length
(- fpr-length
4))
418 (setq slice
(butlast fingerprint fpr-length
))
419 (setq fingerprint
(nthcdr 4 fingerprint
))
420 (setq n-slice
(1+ n-slice
))
426 (otherwise (concat " " slice
))))))
429 (defun mml2015-gpg-extract-signature-details ()
430 (goto-char (point-min))
431 (let* ((expired (re-search-forward
432 "^\\[GNUPG:\\] SIGEXPIRED$"
434 (signer (and (re-search-forward
435 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
437 (cons (match-string 1) (match-string 2))))
438 (fprint (and (re-search-forward
439 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
442 (trust (and (re-search-forward
443 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
447 (cdr (assoc trust mml2015-unabbrev-trust-alist
))))
448 (cond ((and signer fprint
)
450 (unless trust-good-enough-p
451 (concat "\nUntrusted, Fingerprint: "
452 (mml2015-gpg-pretty-print-fpr fprint
)))
454 (format "\nWARNING: Signature from expired key (%s)"
457 "^\\(gpg: \\)?Good signature from \"\\(.*\\)\"$" nil t
)
460 "From unknown user"))))
462 (defun mml2015-gpg-verify (handle ctl
)
464 (let (part message signature info-is-set-p
)
465 (unless (setq part
(mm-find-raw-part-by-type
466 ctl
(or (mm-handle-multipart-ctl-parameter
468 "application/pgp-signature")
470 (mm-set-handle-multipart-parameter
471 mm-security-handle
'gnus-info
"Corrupted")
472 (throw 'error handle
))
474 (setq message
(current-buffer))
476 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
477 ;; clearsign use --textmode. The conversion is not necessary.
478 ;; In clearverify, the conversion is not necessary either.
479 (goto-char (point-min))
482 (unless (eq (char-before) ?
\r)
487 (setq signature
(current-buffer))
488 (unless (setq part
(mm-find-part-by-type
489 (cdr handle
) "application/pgp-signature" nil t
))
490 (mm-set-handle-multipart-parameter
491 mm-security-handle
'gnus-info
"Corrupted")
492 (throw 'error handle
))
493 (mm-insert-part part
)
494 (unless (condition-case err
496 (gpg-verify message signature mml2015-result-buffer
)
497 (mm-set-handle-multipart-parameter
498 mm-security-handle
'gnus-details
499 (with-current-buffer mml2015-result-buffer
502 (mm-set-handle-multipart-parameter
503 mm-security-handle
'gnus-details
(mml2015-format-error err
))
504 (mm-set-handle-multipart-parameter
505 mm-security-handle
'gnus-info
"Error.")
506 (setq info-is-set-p t
)
509 (mm-set-handle-multipart-parameter
510 mm-security-handle
'gnus-details
"Quit.")
511 (mm-set-handle-multipart-parameter
512 mm-security-handle
'gnus-info
"Quit.")
513 (setq info-is-set-p t
)
515 (unless info-is-set-p
516 (mm-set-handle-multipart-parameter
517 mm-security-handle
'gnus-info
"Failed"))
518 (throw 'error handle
)))
519 (mm-set-handle-multipart-parameter
520 mm-security-handle
'gnus-info
521 (with-current-buffer mml2015-result-buffer
522 (mml2015-gpg-extract-signature-details))))
525 (defun mml2015-gpg-clear-verify ()
526 (if (condition-case err
528 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer
)
529 (mm-set-handle-multipart-parameter
530 mm-security-handle
'gnus-details
531 (with-current-buffer mml2015-result-buffer
534 (mm-set-handle-multipart-parameter
535 mm-security-handle
'gnus-details
(mml2015-format-error err
))
538 (mm-set-handle-multipart-parameter
539 mm-security-handle
'gnus-details
"Quit.")
541 (mm-set-handle-multipart-parameter
542 mm-security-handle
'gnus-info
543 (with-current-buffer mml2015-result-buffer
544 (mml2015-gpg-extract-signature-details)))
545 (mm-set-handle-multipart-parameter
546 mm-security-handle
'gnus-info
"Failed")))
548 (defun mml2015-gpg-sign (cont)
549 (let ((boundary (mml-compute-boundary cont
))
550 (text (current-buffer)) signature
)
551 (goto-char (point-max))
555 (unless (gpg-sign-detached text
(setq signature
(current-buffer))
556 mml2015-result-buffer
558 (message-options-get 'message-sender
)
559 t t
) ; armor & textmode
560 (unless (> (point-max) (point-min))
561 (pop-to-buffer mml2015-result-buffer
)
562 (error "Sign error")))
563 (goto-char (point-min))
564 (while (re-search-forward "\r+$" nil t
)
565 (replace-match "" t t
))
567 (goto-char (point-min))
568 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
570 ;;; FIXME: what is the micalg?
571 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
572 (insert (format "\n--%s\n" boundary
))
573 (goto-char (point-max))
574 (insert (format "\n--%s\n" boundary
))
575 (insert "Content-Type: application/pgp-signature\n\n")
576 (insert-buffer-substring signature
)
577 (goto-char (point-max))
578 (insert (format "--%s--\n" boundary
))
579 (goto-char (point-max)))))
581 (defun mml2015-gpg-encrypt (cont &optional sign
)
582 (let ((boundary (mml-compute-boundary cont
))
583 (text (current-buffer))
585 (mm-with-unibyte-current-buffer
587 ;; set up a function to call the correct gpg encrypt routine
588 ;; with the right arguments. (FIXME: this should be done
590 (flet ((gpg-encrypt-func
591 (sign plaintext ciphertext result recipients
&optional
592 passphrase sign-with-key armor textmode
)
595 plaintext ciphertext result recipients passphrase
596 sign-with-key armor textmode
)
598 plaintext ciphertext result recipients passphrase
600 (unless (gpg-encrypt-func
601 sign
; passed in when using signencrypt
602 text
(setq cipher
(current-buffer))
603 mml2015-result-buffer
606 (message-options-get 'message-recipients
)
607 (message-options-set 'message-recipients
608 (read-string "Recipients: ")))
611 (message-options-get 'message-sender
)
612 t t
) ; armor & textmode
613 (unless (> (point-max) (point-min))
614 (pop-to-buffer mml2015-result-buffer
)
615 (error "Encrypt error"))))
616 (goto-char (point-min))
617 (while (re-search-forward "\r+$" nil t
)
618 (replace-match "" t t
))
620 (delete-region (point-min) (point-max))
621 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
623 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
624 (insert (format "--%s\n" boundary
))
625 (insert "Content-Type: application/pgp-encrypted\n\n")
626 (insert "Version: 1\n\n")
627 (insert (format "--%s\n" boundary
))
628 (insert "Content-Type: application/octet-stream\n\n")
629 (insert-buffer-substring cipher
)
630 (goto-char (point-max))
631 (insert (format "--%s--\n" boundary
))
632 (goto-char (point-max))))))
637 (defvar pgg-default-user-id
)
638 (defvar pgg-errors-buffer
)
639 (defvar pgg-output-buffer
))
642 (autoload 'pgg-decrypt-region
"pgg")
643 (autoload 'pgg-verify-region
"pgg")
644 (autoload 'pgg-sign-region
"pgg")
645 (autoload 'pgg-encrypt-region
"pgg"))
647 (defun mml2015-pgg-decrypt (handle ctl
)
649 (let ((pgg-errors-buffer mml2015-result-buffer
)
650 child handles result decrypt-status
)
651 (unless (setq child
(mm-find-part-by-type
653 "application/octet-stream" nil t
))
654 (mm-set-handle-multipart-parameter
655 mm-security-handle
'gnus-info
"Corrupted")
656 (throw 'error handle
))
658 (mm-insert-part child
)
659 (if (condition-case err
661 (pgg-decrypt-region (point-min) (point-max))
663 (with-current-buffer mml2015-result-buffer
665 (mm-set-handle-multipart-parameter
666 mm-security-handle
'gnus-details
669 (mm-set-handle-multipart-parameter
670 mm-security-handle
'gnus-details
(mml2015-format-error err
))
673 (mm-set-handle-multipart-parameter
674 mm-security-handle
'gnus-details
"Quit.")
676 (with-current-buffer pgg-output-buffer
677 (goto-char (point-min))
678 (while (search-forward "\r\n" nil t
)
679 (replace-match "\n" t t
))
680 (setq handles
(mm-dissect-buffer t
))
681 (mm-destroy-parts handle
)
682 (mm-set-handle-multipart-parameter
683 mm-security-handle
'gnus-info
"OK")
684 (mm-set-handle-multipart-parameter
685 mm-security-handle
'gnus-details
686 (concat decrypt-status
687 (when (stringp (car handles
))
688 "\n" (mm-handle-multipart-ctl-parameter
689 handles
'gnus-details
))))
690 (if (listp (car handles
))
693 (mm-set-handle-multipart-parameter
694 mm-security-handle
'gnus-info
"Failed")
695 (throw 'error handle
))))))
697 (defun mml2015-pgg-clear-decrypt ()
698 (let ((pgg-errors-buffer mml2015-result-buffer
))
700 (pgg-decrypt-region (point-min) (point-max))
701 (mm-set-handle-multipart-parameter
702 mm-security-handle
'gnus-details
703 (with-current-buffer mml2015-result-buffer
707 (insert-buffer-substring pgg-output-buffer
)
708 (goto-char (point-min))
709 (while (search-forward "\r\n" nil t
)
710 (replace-match "\n" t t
))
711 (mm-set-handle-multipart-parameter
712 mm-security-handle
'gnus-info
"OK"))
713 (mm-set-handle-multipart-parameter
714 mm-security-handle
'gnus-info
"Failed"))))
716 (defun mml2015-pgg-verify (handle ctl
)
717 (let ((pgg-errors-buffer mml2015-result-buffer
)
718 signature-file part signature
)
719 (if (or (null (setq part
(mm-find-raw-part-by-type
720 ctl
(or (mm-handle-multipart-ctl-parameter
722 "application/pgp-signature")
724 (null (setq signature
(mm-find-part-by-type
725 (cdr handle
) "application/pgp-signature" nil t
))))
727 (mm-set-handle-multipart-parameter
728 mm-security-handle
'gnus-info
"Corrupted")
732 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
733 ;; clearsign use --textmode. The conversion is not necessary.
734 ;; In clearverify, the conversion is not necessary either.
735 (goto-char (point-min))
738 (unless (eq (char-before) ?
\r)
742 (with-temp-file (setq signature-file
(mm-make-temp-file "pgg"))
743 (mm-insert-part signature
))
744 (if (condition-case err
746 (pgg-verify-region (point-min) (point-max)
748 (goto-char (point-min))
749 (while (search-forward "\r\n" nil t
)
750 (replace-match "\n" t t
))
751 (mm-set-handle-multipart-parameter
752 mm-security-handle
'gnus-details
753 (concat (with-current-buffer pgg-output-buffer
755 (with-current-buffer pgg-errors-buffer
758 (mm-set-handle-multipart-parameter
759 mm-security-handle
'gnus-details
(mml2015-format-error err
))
762 (mm-set-handle-multipart-parameter
763 mm-security-handle
'gnus-details
"Quit.")
766 (delete-file signature-file
)
767 (mm-set-handle-multipart-parameter
768 mm-security-handle
'gnus-info
769 (with-current-buffer pgg-errors-buffer
770 (mml2015-gpg-extract-signature-details))))
771 (delete-file signature-file
)
772 (mm-set-handle-multipart-parameter
773 mm-security-handle
'gnus-info
"Failed")))))
776 (defun mml2015-pgg-clear-verify ()
777 (let ((pgg-errors-buffer mml2015-result-buffer
)
778 (text (buffer-string))
779 (coding-system buffer-file-coding-system
))
780 (if (condition-case err
782 (mm-with-unibyte-buffer
783 (insert (encode-coding-string text coding-system
))
784 (pgg-verify-region (point-min) (point-max) nil t
))
785 (goto-char (point-min))
786 (while (search-forward "\r\n" nil t
)
787 (replace-match "\n" t t
))
788 (mm-set-handle-multipart-parameter
789 mm-security-handle
'gnus-details
790 (concat (with-current-buffer pgg-output-buffer
792 (with-current-buffer pgg-errors-buffer
795 (mm-set-handle-multipart-parameter
796 mm-security-handle
'gnus-details
(mml2015-format-error err
))
799 (mm-set-handle-multipart-parameter
800 mm-security-handle
'gnus-details
"Quit.")
802 (mm-set-handle-multipart-parameter
803 mm-security-handle
'gnus-info
804 (with-current-buffer pgg-errors-buffer
805 (mml2015-gpg-extract-signature-details)))
806 (mm-set-handle-multipart-parameter
807 mm-security-handle
'gnus-info
"Failed"))))
809 (defun mml2015-pgg-sign (cont)
810 (let ((pgg-errors-buffer mml2015-result-buffer
)
811 (boundary (mml-compute-boundary cont
))
812 (pgg-default-user-id (or (message-options-get 'mml-sender
)
813 pgg-default-user-id
)))
814 (unless (pgg-sign-region (point-min) (point-max))
815 (pop-to-buffer mml2015-result-buffer
)
816 (error "Sign error"))
817 (goto-char (point-min))
818 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
820 ;;; FIXME: what is the micalg?
821 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
822 (insert (format "\n--%s\n" boundary
))
823 (goto-char (point-max))
824 (insert (format "\n--%s\n" boundary
))
825 (insert "Content-Type: application/pgp-signature\n\n")
826 (insert-buffer-substring pgg-output-buffer
)
827 (goto-char (point-max))
828 (insert (format "--%s--\n" boundary
))
829 (goto-char (point-max))))
831 (defun mml2015-pgg-encrypt (cont &optional sign
)
832 (let ((pgg-errors-buffer mml2015-result-buffer
)
833 (boundary (mml-compute-boundary cont
)))
834 (unless (pgg-encrypt-region (point-min) (point-max)
837 (message-options-get 'message-recipients
)
838 (message-options-set 'message-recipients
839 (read-string "Recipients: ")))
842 (pop-to-buffer mml2015-result-buffer
)
843 (error "Encrypt error"))
844 (delete-region (point-min) (point-max))
845 (goto-char (point-min))
846 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
848 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
849 (insert (format "--%s\n" boundary
))
850 (insert "Content-Type: application/pgp-encrypted\n\n")
851 (insert "Version: 1\n\n")
852 (insert (format "--%s\n" boundary
))
853 (insert "Content-Type: application/octet-stream\n\n")
854 (insert-buffer-substring pgg-output-buffer
)
855 (goto-char (point-max))
856 (insert (format "--%s--\n" boundary
))
857 (goto-char (point-max))))
861 (defun mml2015-clean-buffer ()
862 (if (gnus-buffer-live-p mml2015-result-buffer
)
863 (with-current-buffer mml2015-result-buffer
866 (setq mml2015-result-buffer
867 (gnus-get-buffer-create "*MML2015 Result*"))
870 (defsubst mml2015-clear-decrypt-function
()
871 (nth 6 (assq mml2015-use mml2015-function-alist
)))
873 (defsubst mml2015-clear-verify-function
()
874 (nth 5 (assq mml2015-use mml2015-function-alist
)))
877 (defun mml2015-decrypt (handle ctl
)
878 (mml2015-clean-buffer)
879 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist
))))
881 (funcall func handle ctl
)
885 (defun mml2015-decrypt-test (handle ctl
)
889 (defun mml2015-verify (handle ctl
)
890 (mml2015-clean-buffer)
891 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist
))))
893 (funcall func handle ctl
)
897 (defun mml2015-verify-test (handle ctl
)
901 (defun mml2015-encrypt (cont &optional sign
)
902 (mml2015-clean-buffer)
903 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist
))))
905 (funcall func cont sign
)
906 (error "Cannot find encrypt function"))))
909 (defun mml2015-sign (cont)
910 (mml2015-clean-buffer)
911 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist
))))
914 (error "Cannot find sign function"))))
917 (defun mml2015-self-encrypt ()
918 (mml2015-encrypt nil
))
922 ;;; arch-tag: b04701d5-0b09-44d8-bed8-de901bf435f2
923 ;;; mml2015.el ends here