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))
34 (defvar mml2015-use (or
38 (and (fboundp 'gpg-sign-detached)
42 (and (fboundp 'mc-encrypt-generic)
43 (fboundp 'mc-sign-generic)
44 (fboundp 'mc-cleanup-recipient-headers)
46 "The package used for PGP/MIME.")
48 ;; Something is not RFC2015.
49 (defvar mml2015-function-alist
50 '((mailcrypt mml2015-mailcrypt-sign
51 mml2015-mailcrypt-encrypt
52 mml2015-mailcrypt-verify
53 mml2015-mailcrypt-decrypt
54 mml2015-mailcrypt-clear-verify
55 mml2015-mailcrypt-clear-decrypt)
60 mml2015-gpg-clear-verify
61 mml2015-gpg-clear-decrypt))
62 "Alist of PGP/MIME functions.")
64 (defvar mml2015-result-buffer nil)
66 (defvar mml2015-trust-boundaries-alist
67 '((trust-undefined . nil)
72 "Trust boundaries for a signer's GnuPG key.
73 This alist contains pairs of the form (trust-symbol . boolean), with
74 symbols that are contained in `gpg-unabbrev-trust-alist'. The boolean
75 specifies whether the given trust value is good enough to be trusted
81 (autoload 'mailcrypt-decrypt "mailcrypt")
82 (autoload 'mailcrypt-verify "mailcrypt")
83 (autoload 'mc-pgp-always-sign "mailcrypt")
84 (autoload 'mc-encrypt-generic "mc-toplev")
85 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
86 (autoload 'mc-sign-generic "mc-toplev"))
89 (defvar mc-default-scheme)
92 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
93 (defvar mml2015-verify-function 'mailcrypt-verify)
95 (defun mml2015-format-error (err)
96 (if (stringp (cadr err))
98 (format "%S" (cdr err))))
100 (defun mml2015-mailcrypt-decrypt (handle ctl)
102 (let (child handles result)
103 (unless (setq child (mm-find-part-by-type
105 "application/octet-stream" nil t))
106 (mm-set-handle-multipart-parameter
107 mm-security-handle 'gnus-info "Corrupted")
108 (throw 'error handle))
110 (mm-insert-part child)
113 (funcall mml2015-decrypt-function)
115 (mm-set-handle-multipart-parameter
116 mm-security-handle 'gnus-details (mml2015-format-error err))
119 (mm-set-handle-multipart-parameter
120 mm-security-handle 'gnus-details "Quit.")
123 (mm-set-handle-multipart-parameter
124 mm-security-handle 'gnus-info "Failed")
125 (throw 'error handle))
126 (setq handles (mm-dissect-buffer t)))
127 (mm-destroy-parts handle)
128 (mm-set-handle-multipart-parameter
129 mm-security-handle 'gnus-info
131 (let ((sig (with-current-buffer mml2015-result-buffer
132 (mml2015-gpg-extract-signature-details))))
133 (concat ", Signer: " sig))))
134 (if (listp (car handles))
138 (defun mml2015-mailcrypt-clear-decrypt ()
142 (funcall mml2015-decrypt-function)
144 (mm-set-handle-multipart-parameter
145 mm-security-handle 'gnus-details (mml2015-format-error err))
148 (mm-set-handle-multipart-parameter
149 mm-security-handle 'gnus-details "Quit.")
152 (mm-set-handle-multipart-parameter
153 mm-security-handle 'gnus-info "OK")
154 (mm-set-handle-multipart-parameter
155 mm-security-handle 'gnus-info "Failed"))))
157 (defun mml2015-fix-micalg (alg)
159 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
160 (upcase (if (string-match "^p[gh]p-" alg)
161 (substring alg (match-end 0))
164 (defun mml2015-mailcrypt-verify (handle ctl)
167 (unless (setq part (mm-find-raw-part-by-type
168 ctl (or (mm-handle-multipart-ctl-parameter
170 "application/pgp-signature")
172 (mm-set-handle-multipart-parameter
173 mm-security-handle 'gnus-info "Corrupted")
174 (throw 'error handle))
176 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
177 (insert (format "Hash: %s\n\n"
178 (or (mml2015-fix-micalg
179 (mm-handle-multipart-ctl-parameter
183 (narrow-to-region (point) (point))
185 (goto-char (point-min))
187 (if (looking-at "^-")
190 (unless (setq part (mm-find-part-by-type
191 (cdr handle) "application/pgp-signature" nil t))
192 (mm-set-handle-multipart-parameter
193 mm-security-handle 'gnus-info "Corrupted")
194 (throw 'error handle))
196 (narrow-to-region (point) (point))
197 (mm-insert-part part)
198 (goto-char (point-min))
199 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
200 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
201 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
202 (replace-match "-----END PGP SIGNATURE-----" t t)))
203 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
204 (unless (condition-case err
206 (funcall mml2015-verify-function)
207 (if (get-buffer " *mailcrypt stderr temp")
208 (mm-set-handle-multipart-parameter
209 mm-security-handle 'gnus-details
210 (with-current-buffer " *mailcrypt stderr temp"
212 (if (get-buffer " *mailcrypt stdout temp")
213 (kill-buffer " *mailcrypt stdout temp"))
214 (if (get-buffer " *mailcrypt stderr temp")
215 (kill-buffer " *mailcrypt stderr temp"))
216 (if (get-buffer " *mailcrypt status temp")
217 (kill-buffer " *mailcrypt status temp"))
218 (if (get-buffer mc-gpg-debug-buffer)
219 (kill-buffer mc-gpg-debug-buffer)))
221 (mm-set-handle-multipart-parameter
222 mm-security-handle 'gnus-details (mml2015-format-error err))
225 (mm-set-handle-multipart-parameter
226 mm-security-handle 'gnus-details "Quit.")
228 (mm-set-handle-multipart-parameter
229 mm-security-handle 'gnus-info "Failed")
230 (throw 'error handle))))
231 (mm-set-handle-multipart-parameter
232 mm-security-handle 'gnus-info "OK")
235 (defun mml2015-mailcrypt-clear-verify ()
236 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
237 (if (condition-case err
239 (funcall mml2015-verify-function)
240 (if (get-buffer " *mailcrypt stderr temp")
241 (mm-set-handle-multipart-parameter
242 mm-security-handle 'gnus-details
243 (with-current-buffer " *mailcrypt stderr temp"
245 (if (get-buffer " *mailcrypt stdout temp")
246 (kill-buffer " *mailcrypt stdout temp"))
247 (if (get-buffer " *mailcrypt stderr temp")
248 (kill-buffer " *mailcrypt stderr temp"))
249 (if (get-buffer " *mailcrypt status temp")
250 (kill-buffer " *mailcrypt status temp"))
251 (if (get-buffer mc-gpg-debug-buffer)
252 (kill-buffer mc-gpg-debug-buffer)))
254 (mm-set-handle-multipart-parameter
255 mm-security-handle 'gnus-details (mml2015-format-error err))
258 (mm-set-handle-multipart-parameter
259 mm-security-handle 'gnus-details "Quit.")
261 (mm-set-handle-multipart-parameter
262 mm-security-handle 'gnus-info "OK")
263 (mm-set-handle-multipart-parameter
264 mm-security-handle 'gnus-info "Failed"))))
266 (defun mml2015-mailcrypt-sign (cont)
267 (mc-sign-generic (message-options-get 'message-sender)
270 (funcall mml-boundary-function (incf mml-multipart-number)))
272 (goto-char (point-min))
273 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
274 (error "Cannot find signed begin line"))
275 (goto-char (match-beginning 0))
277 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
278 (error "Cannot not find PGP hash"))
279 (setq hash (match-string 1))
280 (unless (re-search-forward "^$" nil t)
281 (error "Cannot not find PGP message"))
283 (delete-region (point-min) (point))
284 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
286 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
288 (insert (format "\n--%s\n" boundary))
290 (goto-char (point-max))
291 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
292 (error "Cannot find signature part"))
293 (replace-match "-----END PGP MESSAGE-----" t t)
294 (goto-char (match-beginning 0))
295 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
297 (error "Cannot find signature part"))
298 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
299 (goto-char (match-beginning 0))
301 (narrow-to-region point (point))
303 (while (re-search-forward "^- -" nil t)
304 (replace-match "-" t t))
305 (goto-char (point-max)))
306 (insert (format "--%s\n" boundary))
307 (insert "Content-Type: application/pgp-signature\n\n")
308 (goto-char (point-max))
309 (insert (format "--%s--\n" boundary))
310 (goto-char (point-max))))
312 (defun mml2015-mailcrypt-encrypt (cont &optional sign)
313 (let ((mc-pgp-always-sign
314 (or mc-pgp-always-sign
316 (eq t (or (message-options-get 'message-sign-encrypt)
318 'message-sign-encrypt
319 (or (y-or-n-p "Sign the message? ")
322 (mm-with-unibyte-current-buffer-mule4
324 (or (message-options-get 'message-recipients)
325 (message-options-set 'message-recipients
326 (mc-cleanup-recipient-headers
327 (read-string "Recipients: "))))
329 (message-options-get 'message-sender))))
330 (goto-char (point-min))
331 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
332 (error "Fail to encrypt the message"))
334 (funcall mml-boundary-function (incf mml-multipart-number))))
335 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
337 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
338 (insert (format "--%s\n" boundary))
339 (insert "Content-Type: application/pgp-encrypted\n\n")
340 (insert "Version: 1\n\n")
341 (insert (format "--%s\n" boundary))
342 (insert "Content-Type: application/octet-stream\n\n")
343 (goto-char (point-max))
344 (insert (format "--%s--\n" boundary))
345 (goto-char (point-max))))
350 (autoload 'gpg-decrypt "gpg")
351 (autoload 'gpg-verify "gpg")
352 (autoload 'gpg-verify-cleartext "gpg")
353 (autoload 'gpg-sign-detached "gpg")
354 (autoload 'gpg-sign-encrypt "gpg")
355 (autoload 'gpg-encrypt "gpg")
356 (autoload 'gpg-passphrase-read "gpg"))
358 (defun mml2015-gpg-passphrase ()
359 (or (message-options-get 'gpg-passphrase)
360 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
362 (defun mml2015-gpg-decrypt-1 ()
363 (let ((cipher (current-buffer)) plain result)
364 (if (with-temp-buffer
366 (gpg-decrypt cipher (setq plain (current-buffer))
367 mml2015-result-buffer nil)
368 (mm-set-handle-multipart-parameter
369 mm-security-handle 'gnus-details
370 (with-current-buffer mml2015-result-buffer
374 (insert-buffer plain)
375 (goto-char (point-min))
376 (while (search-forward "\r\n" nil t)
377 (replace-match "\n" t t))))
379 ;; Some wrong with the return value, check plain text buffer.
380 (if (> (point-max) (point-min))
384 (defun mml2015-gpg-decrypt (handle ctl)
385 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
386 (mml2015-mailcrypt-decrypt handle ctl)))
388 (defun mml2015-gpg-clear-decrypt ()
390 (setq result (mml2015-gpg-decrypt-1))
392 (mm-set-handle-multipart-parameter
393 mm-security-handle 'gnus-info "OK")
394 (mm-set-handle-multipart-parameter
395 mm-security-handle 'gnus-info "Failed"))))
397 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
399 (fpr-length (string-width fingerprint))
402 (setq fingerprint (string-to-list fingerprint))
404 (setq fpr-length (- fpr-length 4))
405 (setq slice (butlast fingerprint fpr-length))
406 (setq fingerprint (nthcdr 4 fingerprint))
407 (setq n-slice (1+ n-slice))
413 (otherwise (concat " " slice))))))
416 (defun mml2015-gpg-extract-signature-details ()
417 (goto-char (point-min))
418 (if (boundp 'gpg-unabbrev-trust-alist)
419 (let* ((expired (re-search-forward
420 "^\\[GNUPG:\\] SIGEXPIRED$"
422 (signer (and (re-search-forward
423 "^\\[GNUPG:\\] GOODSIG \\([0-9A-Za-z]*\\) \\(.*\\)$"
425 (cons (match-string 1) (match-string 2))))
426 (fprint (and (re-search-forward
427 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
430 (trust (and (re-search-forward
431 "^\\[GNUPG:\\] \\(TRUST_.*\\)$"
435 (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
436 mml2015-trust-boundaries-alist))))
437 (cond ((and signer fprint)
439 (unless trust-good-enough-p
440 (concat "\nUntrusted, Fingerprint: "
441 (mml2015-gpg-pretty-print-fpr fprint)))
443 (format "\nWARNING: Signature from expired key (%s)"
446 "From unknown user")))
447 (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
449 "From unknown user")))
451 (defun mml2015-gpg-verify (handle ctl)
453 (let (part message signature info-is-set-p)
454 (unless (setq part (mm-find-raw-part-by-type
455 ctl (or (mm-handle-multipart-ctl-parameter
457 "application/pgp-signature")
459 (mm-set-handle-multipart-parameter
460 mm-security-handle 'gnus-info "Corrupted")
461 (throw 'error handle))
463 (setq message (current-buffer))
465 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
466 ;; clearsign use --textmode. The conversion is not necessary.
467 ;; In clearverify, the conversion is not necessary either.
468 (goto-char (point-min))
471 (unless (eq (char-before) ?\r)
476 (setq signature (current-buffer))
477 (unless (setq part (mm-find-part-by-type
478 (cdr handle) "application/pgp-signature" nil t))
479 (mm-set-handle-multipart-parameter
480 mm-security-handle 'gnus-info "Corrupted")
481 (throw 'error handle))
482 (mm-insert-part part)
483 (unless (condition-case err
485 (gpg-verify message signature mml2015-result-buffer)
486 (mm-set-handle-multipart-parameter
487 mm-security-handle 'gnus-details
488 (with-current-buffer mml2015-result-buffer
491 (mm-set-handle-multipart-parameter
492 mm-security-handle 'gnus-details (mml2015-format-error err))
493 (mm-set-handle-multipart-parameter
494 mm-security-handle 'gnus-info "Error.")
495 (setq info-is-set-p t)
498 (mm-set-handle-multipart-parameter
499 mm-security-handle 'gnus-details "Quit.")
500 (mm-set-handle-multipart-parameter
501 mm-security-handle 'gnus-info "Quit.")
502 (setq info-is-set-p t)
504 (unless info-is-set-p
505 (mm-set-handle-multipart-parameter
506 mm-security-handle 'gnus-info "Failed"))
507 (throw 'error handle)))
508 (mm-set-handle-multipart-parameter
509 mm-security-handle 'gnus-info
510 (with-current-buffer mml2015-result-buffer
511 (mml2015-gpg-extract-signature-details))))
514 (defun mml2015-gpg-clear-verify ()
515 (if (condition-case err
517 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
518 (mm-set-handle-multipart-parameter
519 mm-security-handle 'gnus-details
520 (with-current-buffer mml2015-result-buffer
523 (mm-set-handle-multipart-parameter
524 mm-security-handle 'gnus-details (mml2015-format-error err))
527 (mm-set-handle-multipart-parameter
528 mm-security-handle 'gnus-details "Quit.")
530 (mm-set-handle-multipart-parameter
531 mm-security-handle 'gnus-info
532 (with-current-buffer mml2015-result-buffer
533 (mml2015-gpg-extract-signature-details)))
534 (mm-set-handle-multipart-parameter
535 mm-security-handle 'gnus-info "Failed")))
537 (defun mml2015-gpg-sign (cont)
539 (funcall mml-boundary-function (incf mml-multipart-number)))
540 (text (current-buffer)) signature)
541 (goto-char (point-max))
545 (unless (gpg-sign-detached text (setq signature (current-buffer))
546 mml2015-result-buffer
548 (message-options-get 'message-sender)
549 t t) ; armor & textmode
550 (unless (> (point-max) (point-min))
551 (pop-to-buffer mml2015-result-buffer)
552 (error "Sign error")))
553 (goto-char (point-min))
554 (while (re-search-forward "\r+$" nil t)
555 (replace-match "" t t))
557 (goto-char (point-min))
558 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
560 ;;; FIXME: what is the micalg?
561 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
562 (insert (format "\n--%s\n" boundary))
563 (goto-char (point-max))
564 (insert (format "\n--%s\n" boundary))
565 (insert "Content-Type: application/pgp-signature\n\n")
566 (insert-buffer signature)
567 (goto-char (point-max))
568 (insert (format "--%s--\n" boundary))
569 (goto-char (point-max)))))
571 (defun mml2015-gpg-encrypt (cont &optional sign)
573 (funcall mml-boundary-function (incf mml-multipart-number)))
574 (text (current-buffer))
576 (mm-with-unibyte-current-buffer-mule4
578 ;; set up a function to call the correct gpg encrypt routine
579 ;; with the right arguments. (FIXME: this should be done
581 (flet ((gpg-encrypt-func
582 (sign plaintext ciphertext result recipients &optional
583 passphrase sign-with-key armor textmode)
586 plaintext ciphertext result recipients passphrase
587 sign-with-key armor textmode)
589 plaintext ciphertext result recipients passphrase
591 (unless (gpg-encrypt-func
592 sign ; passed in when using signencrypt
593 text (setq cipher (current-buffer))
594 mml2015-result-buffer
597 (message-options-get 'message-recipients)
598 (message-options-set 'message-recipients
599 (read-string "Recipients: ")))
602 (message-options-get 'message-sender)
603 t t) ; armor & textmode
604 (unless (> (point-max) (point-min))
605 (pop-to-buffer mml2015-result-buffer)
606 (error "Encrypt error"))))
607 (goto-char (point-min))
608 (while (re-search-forward "\r+$" nil t)
609 (replace-match "" t t))
611 (delete-region (point-min) (point-max))
612 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
614 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
615 (insert (format "--%s\n" boundary))
616 (insert "Content-Type: application/pgp-encrypted\n\n")
617 (insert "Version: 1\n\n")
618 (insert (format "--%s\n" boundary))
619 (insert "Content-Type: application/octet-stream\n\n")
620 (insert-buffer cipher)
621 (goto-char (point-max))
622 (insert (format "--%s--\n" boundary))
623 (goto-char (point-max))))))
627 (defun mml2015-clean-buffer ()
628 (if (gnus-buffer-live-p mml2015-result-buffer)
629 (with-current-buffer mml2015-result-buffer
632 (setq mml2015-result-buffer
633 (gnus-get-buffer-create "*MML2015 Result*"))
636 (defsubst mml2015-clear-decrypt-function ()
637 (nth 6 (assq mml2015-use mml2015-function-alist)))
639 (defsubst mml2015-clear-verify-function ()
640 (nth 5 (assq mml2015-use mml2015-function-alist)))
643 (defun mml2015-decrypt (handle ctl)
644 (mml2015-clean-buffer)
645 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
647 (funcall func handle ctl)
651 (defun mml2015-decrypt-test (handle ctl)
655 (defun mml2015-verify (handle ctl)
656 (mml2015-clean-buffer)
657 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
659 (funcall func handle ctl)
663 (defun mml2015-verify-test (handle ctl)
667 (defun mml2015-encrypt (cont &optional sign)
668 (mml2015-clean-buffer)
669 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
671 (funcall func cont sign)
672 (error "Cannot find encrypt function"))))
675 (defun mml2015-sign (cont)
676 (mml2015-clean-buffer)
677 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
680 (error "Cannot find sign function"))))
683 (defun mml2015-self-encrypt ()
684 (mml2015-encrypt nil))
688 ;;; mml2015.el ends here