1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000, 2001 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))
35 (defvar mml2015-use (or
39 (and (fboundp 'gpg-sign-detached)
43 (and (fboundp 'mc-encrypt-generic)
44 (fboundp 'mc-sign-generic)
45 (fboundp 'mc-cleanup-recipient-headers)
47 "The package used for PGP/MIME.")
49 ;; Something is not RFC2015.
50 (defvar mml2015-function-alist
51 '((mailcrypt mml2015-mailcrypt-sign
52 mml2015-mailcrypt-encrypt
53 mml2015-mailcrypt-verify
54 mml2015-mailcrypt-decrypt
55 mml2015-mailcrypt-clear-verify
56 mml2015-mailcrypt-clear-decrypt)
61 mml2015-gpg-clear-verify
62 mml2015-gpg-clear-decrypt))
63 "Alist of PGP/MIME functions.")
65 (defvar mml2015-result-buffer nil)
67 (defvar mml2015-trust-boundaries-alist
68 '((trust-undefined . nil)
73 "Trust boundaries for a signer's GnuPG key.
74 This alist contains pairs of the form (trust-symbol . boolean), with
75 symbols that are contained in `gpg-unabbrev-trust-alist'. The boolean
76 specifies whether the given trust value is good enough to be trusted
82 (autoload 'mailcrypt-decrypt "mailcrypt")
83 (autoload 'mailcrypt-verify "mailcrypt")
84 (autoload 'mc-pgp-always-sign "mailcrypt")
85 (autoload 'mc-encrypt-generic "mc-toplev")
86 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
87 (autoload 'mc-sign-generic "mc-toplev"))
90 (defvar mc-default-scheme)
93 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
94 (defvar mml2015-verify-function 'mailcrypt-verify)
96 (defun mml2015-format-error (err)
97 (if (stringp (cadr err))
99 (format "%S" (cdr err))))
101 (defun mml2015-mailcrypt-decrypt (handle ctl)
103 (let (child handles result)
104 (unless (setq child (mm-find-part-by-type
106 "application/octet-stream" nil t))
107 (mm-set-handle-multipart-parameter
108 mm-security-handle 'gnus-info "Corrupted")
109 (throw 'error handle))
111 (mm-insert-part child)
114 (funcall mml2015-decrypt-function)
116 (mm-set-handle-multipart-parameter
117 mm-security-handle 'gnus-details (mml2015-format-error err))
120 (mm-set-handle-multipart-parameter
121 mm-security-handle 'gnus-details "Quit.")
124 (mm-set-handle-multipart-parameter
125 mm-security-handle 'gnus-info "Failed")
126 (throw 'error handle))
127 (setq handles (mm-dissect-buffer t)))
128 (mm-destroy-parts handle)
129 (mm-set-handle-multipart-parameter
130 mm-security-handle 'gnus-info
132 (let ((sig (with-current-buffer mml2015-result-buffer
133 (mml2015-gpg-extract-signature-details))))
134 (concat ", Signer: " sig))))
135 (if (listp (car handles))
139 (defun mml2015-mailcrypt-clear-decrypt ()
143 (funcall mml2015-decrypt-function)
145 (mm-set-handle-multipart-parameter
146 mm-security-handle 'gnus-details (mml2015-format-error err))
149 (mm-set-handle-multipart-parameter
150 mm-security-handle 'gnus-details "Quit.")
153 (mm-set-handle-multipart-parameter
154 mm-security-handle 'gnus-info "OK")
155 (mm-set-handle-multipart-parameter
156 mm-security-handle 'gnus-info "Failed"))))
158 (defun mml2015-fix-micalg (alg)
160 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
161 (upcase (if (string-match "^p[gh]p-" alg)
162 (substring alg (match-end 0))
165 (defun mml2015-mailcrypt-verify (handle ctl)
168 (unless (setq part (mm-find-raw-part-by-type
169 ctl (or (mm-handle-multipart-ctl-parameter
171 "application/pgp-signature")
173 (mm-set-handle-multipart-parameter
174 mm-security-handle 'gnus-info "Corrupted")
175 (throw 'error handle))
177 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
178 (insert (format "Hash: %s\n\n"
179 (or (mml2015-fix-micalg
180 (mm-handle-multipart-ctl-parameter
184 (narrow-to-region (point) (point))
186 (goto-char (point-min))
188 (if (looking-at "^-")
191 (unless (setq part (mm-find-part-by-type
192 (cdr handle) "application/pgp-signature" nil t))
193 (mm-set-handle-multipart-parameter
194 mm-security-handle 'gnus-info "Corrupted")
195 (throw 'error handle))
197 (narrow-to-region (point) (point))
198 (mm-insert-part part)
199 (goto-char (point-min))
200 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
201 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
202 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
203 (replace-match "-----END PGP SIGNATURE-----" t t)))
204 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
205 (unless (condition-case err
207 (funcall mml2015-verify-function)
208 (if (get-buffer " *mailcrypt stderr temp")
209 (mm-set-handle-multipart-parameter
210 mm-security-handle 'gnus-details
211 (with-current-buffer " *mailcrypt stderr temp"
213 (if (get-buffer " *mailcrypt stdout temp")
214 (kill-buffer " *mailcrypt stdout temp"))
215 (if (get-buffer " *mailcrypt stderr temp")
216 (kill-buffer " *mailcrypt stderr temp"))
217 (if (get-buffer " *mailcrypt status temp")
218 (kill-buffer " *mailcrypt status temp"))
219 (if (get-buffer mc-gpg-debug-buffer)
220 (kill-buffer mc-gpg-debug-buffer)))
222 (mm-set-handle-multipart-parameter
223 mm-security-handle 'gnus-details (mml2015-format-error err))
226 (mm-set-handle-multipart-parameter
227 mm-security-handle 'gnus-details "Quit.")
229 (mm-set-handle-multipart-parameter
230 mm-security-handle 'gnus-info "Failed")
231 (throw 'error handle))))
232 (mm-set-handle-multipart-parameter
233 mm-security-handle 'gnus-info "OK")
236 (defun mml2015-mailcrypt-clear-verify ()
237 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
238 (if (condition-case err
240 (funcall mml2015-verify-function)
241 (if (get-buffer " *mailcrypt stderr temp")
242 (mm-set-handle-multipart-parameter
243 mm-security-handle 'gnus-details
244 (with-current-buffer " *mailcrypt stderr temp"
246 (if (get-buffer " *mailcrypt stdout temp")
247 (kill-buffer " *mailcrypt stdout temp"))
248 (if (get-buffer " *mailcrypt stderr temp")
249 (kill-buffer " *mailcrypt stderr temp"))
250 (if (get-buffer " *mailcrypt status temp")
251 (kill-buffer " *mailcrypt status temp"))
252 (if (get-buffer mc-gpg-debug-buffer)
253 (kill-buffer mc-gpg-debug-buffer)))
255 (mm-set-handle-multipart-parameter
256 mm-security-handle 'gnus-details (mml2015-format-error err))
259 (mm-set-handle-multipart-parameter
260 mm-security-handle 'gnus-details "Quit.")
262 (mm-set-handle-multipart-parameter
263 mm-security-handle 'gnus-info "OK")
264 (mm-set-handle-multipart-parameter
265 mm-security-handle 'gnus-info "Failed"))))
267 (defun mml2015-mailcrypt-sign (cont)
268 (mc-sign-generic (message-options-get 'message-sender)
271 (funcall mml-boundary-function (incf mml-multipart-number)))
273 (goto-char (point-min))
274 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
275 (error "Cannot find signed begin line"))
276 (goto-char (match-beginning 0))
278 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
279 (error "Cannot not find PGP hash"))
280 (setq hash (match-string 1))
281 (unless (re-search-forward "^$" nil t)
282 (error "Cannot not find PGP message"))
284 (delete-region (point-min) (point))
285 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
287 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
289 (insert (format "\n--%s\n" boundary))
291 (goto-char (point-max))
292 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
293 (error "Cannot find signature part"))
294 (replace-match "-----END PGP MESSAGE-----" t t)
295 (goto-char (match-beginning 0))
296 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
298 (error "Cannot find signature part"))
299 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
300 (goto-char (match-beginning 0))
302 (narrow-to-region point (point))
304 (while (re-search-forward "^- -" nil t)
305 (replace-match "-" t t))
306 (goto-char (point-max)))
307 (insert (format "--%s\n" boundary))
308 (insert "Content-Type: application/pgp-signature\n\n")
309 (goto-char (point-max))
310 (insert (format "--%s--\n" boundary))
311 (goto-char (point-max))))
313 (defun mml2015-mailcrypt-encrypt (cont)
314 (let ((mc-pgp-always-sign
315 (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-passphrase-read "gpg"))
357 (defun mml2015-gpg-passphrase ()
358 (or (message-options-get 'gpg-passphrase)
359 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
361 (defun mml2015-gpg-decrypt-1 ()
362 (let ((cipher (current-buffer)) plain result)
363 (if (with-temp-buffer
365 (gpg-decrypt cipher (setq plain (current-buffer))
366 mml2015-result-buffer nil)
367 (mm-set-handle-multipart-parameter
368 mm-security-handle 'gnus-details
369 (with-current-buffer mml2015-result-buffer
373 (insert-buffer plain)
374 (goto-char (point-min))
375 (while (search-forward "\r\n" nil t)
376 (replace-match "\n" t t))))
378 ;; Some wrong with the return value, check plain text buffer.
379 (if (> (point-max) (point-min))
383 (defun mml2015-gpg-decrypt (handle ctl)
384 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
385 (mml2015-mailcrypt-decrypt handle ctl)))
387 (defun mml2015-gpg-clear-decrypt ()
389 (setq result (mml2015-gpg-decrypt-1))
391 (mm-set-handle-multipart-parameter
392 mm-security-handle 'gnus-info "OK")
393 (mm-set-handle-multipart-parameter
394 mm-security-handle 'gnus-info "Failed"))))
396 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
398 (fpr-length (string-width fingerprint))
401 (setq fingerprint (string-to-list fingerprint))
403 (setq fpr-length (- fpr-length 4))
404 (setq slice (butlast fingerprint fpr-length))
405 (setq fingerprint (nthcdr 4 fingerprint))
406 (setq n-slice (1+ n-slice))
412 (otherwise (concat " " slice))))))
415 (defun mml2015-gpg-extract-signature-details ()
416 (goto-char (point-min))
417 (if (boundp 'gpg-unabbrev-trust-alist)
418 (let* ((signer (and (re-search-forward
419 "^\\[GNUPG:\\] GOODSIG [0-9A-Za-z]* \\(.*\\)$"
422 (fprint (and (re-search-forward
423 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
426 (trust (and (re-search-forward "^\\[GNUPG:\\] \\(TRUST_.*\\)$" nil t)
429 (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
430 mml2015-trust-boundaries-alist))))
431 (if (and signer trust fprint)
433 (unless trust-good-enough-p
434 (concat "\nUntrusted, Fingerprint: "
435 (mml2015-gpg-pretty-print-fpr fprint))))
436 "From unknown user"))
437 (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
439 "From unknown user")))
441 (defun mml2015-gpg-verify (handle ctl)
443 (let (part message signature info-is-set-p)
444 (unless (setq part (mm-find-raw-part-by-type
445 ctl (or (mm-handle-multipart-ctl-parameter
447 "application/pgp-signature")
449 (mm-set-handle-multipart-parameter
450 mm-security-handle 'gnus-info "Corrupted")
451 (throw 'error handle))
453 (setq message (current-buffer))
455 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
456 ;; clearsign use --textmode. The conversion is not necessary.
457 ;; In clearverify, the conversion is not necessary either.
458 (goto-char (point-min))
461 (unless (eq (char-before) ?\r)
466 (setq signature (current-buffer))
467 (unless (setq part (mm-find-part-by-type
468 (cdr handle) "application/pgp-signature" nil t))
469 (mm-set-handle-multipart-parameter
470 mm-security-handle 'gnus-info "Corrupted")
471 (throw 'error handle))
472 (mm-insert-part part)
473 (unless (condition-case err
475 (gpg-verify message signature mml2015-result-buffer)
476 (mm-set-handle-multipart-parameter
477 mm-security-handle 'gnus-details
478 (with-current-buffer mml2015-result-buffer
481 (mm-set-handle-multipart-parameter
482 mm-security-handle 'gnus-details (mml2015-format-error err))
483 (mm-set-handle-multipart-parameter
484 mm-security-handle 'gnus-info "Error.")
485 (setq info-is-set-p t)
488 (mm-set-handle-multipart-parameter
489 mm-security-handle 'gnus-details "Quit.")
490 (mm-set-handle-multipart-parameter
491 mm-security-handle 'gnus-info "Quit.")
492 (setq info-is-set-p t)
494 (unless info-is-set-p
495 (mm-set-handle-multipart-parameter
496 mm-security-handle 'gnus-info "Failed"))
497 (throw 'error handle)))
498 (mm-set-handle-multipart-parameter
499 mm-security-handle 'gnus-info
500 (with-current-buffer mml2015-result-buffer
501 (mml2015-gpg-extract-signature-details))))
504 (defun mml2015-gpg-clear-verify ()
505 (if (condition-case err
507 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
508 (mm-set-handle-multipart-parameter
509 mm-security-handle 'gnus-details
510 (with-current-buffer mml2015-result-buffer
513 (mm-set-handle-multipart-parameter
514 mm-security-handle 'gnus-details (mml2015-format-error err))
517 (mm-set-handle-multipart-parameter
518 mm-security-handle 'gnus-details "Quit.")
520 (mm-set-handle-multipart-parameter
521 mm-security-handle 'gnus-info
522 (with-current-buffer mml2015-result-buffer
523 (mml2015-gpg-extract-signature-details)))
524 (mm-set-handle-multipart-parameter
525 mm-security-handle 'gnus-info "Failed")))
527 (defun mml2015-gpg-sign (cont)
529 (funcall mml-boundary-function (incf mml-multipart-number)))
530 (text (current-buffer)) signature)
531 (goto-char (point-max))
535 (unless (gpg-sign-detached text (setq signature (current-buffer))
536 mml2015-result-buffer
538 (message-options-get 'message-sender)
539 t t) ; armor & textmode
540 (unless (> (point-max) (point-min))
541 (pop-to-buffer mml2015-result-buffer)
542 (error "Sign error")))
543 (goto-char (point-min))
544 (while (re-search-forward "\r+$" nil t)
545 (replace-match "" t t))
547 (goto-char (point-min))
548 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
550 ;;; FIXME: what is the micalg?
551 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
552 (insert (format "\n--%s\n" boundary))
553 (goto-char (point-max))
554 (insert (format "\n--%s\n" boundary))
555 (insert "Content-Type: application/pgp-signature\n\n")
556 (insert-buffer signature)
557 (goto-char (point-max))
558 (insert (format "--%s--\n" boundary))
559 (goto-char (point-max)))))
561 (defun mml2015-gpg-encrypt (cont)
563 (funcall mml-boundary-function (incf mml-multipart-number)))
564 (text (current-buffer))
566 (mm-with-unibyte-current-buffer-mule4
568 (unless (gpg-sign-encrypt
569 text (setq cipher (current-buffer))
570 mml2015-result-buffer
573 (message-options-get 'message-recipients)
574 (message-options-set 'message-recipients
575 (read-string "Recipients: ")))
578 (message-options-get 'message-sender)
579 t t) ; armor & textmode
580 (unless (> (point-max) (point-min))
581 (pop-to-buffer mml2015-result-buffer)
582 (error "Encrypt error")))
583 (goto-char (point-min))
584 (while (re-search-forward "\r+$" nil t)
585 (replace-match "" t t))
587 (delete-region (point-min) (point-max))
588 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
590 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
591 (insert (format "--%s\n" boundary))
592 (insert "Content-Type: application/pgp-encrypted\n\n")
593 (insert "Version: 1\n\n")
594 (insert (format "--%s\n" boundary))
595 (insert "Content-Type: application/octet-stream\n\n")
596 (insert-buffer cipher)
597 (goto-char (point-max))
598 (insert (format "--%s--\n" boundary))
599 (goto-char (point-max))))))
603 (defun mml2015-clean-buffer ()
604 (if (gnus-buffer-live-p mml2015-result-buffer)
605 (with-current-buffer mml2015-result-buffer
608 (setq mml2015-result-buffer
609 (gnus-get-buffer-create "*MML2015 Result*"))
612 (defsubst mml2015-clear-decrypt-function ()
613 (nth 6 (assq mml2015-use mml2015-function-alist)))
615 (defsubst mml2015-clear-verify-function ()
616 (nth 5 (assq mml2015-use mml2015-function-alist)))
619 (defun mml2015-decrypt (handle ctl)
620 (mml2015-clean-buffer)
621 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
623 (funcall func handle ctl)
627 (defun mml2015-decrypt-test (handle ctl)
631 (defun mml2015-verify (handle ctl)
632 (mml2015-clean-buffer)
633 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
635 (funcall func handle ctl)
639 (defun mml2015-verify-test (handle ctl)
643 (defun mml2015-encrypt (cont)
644 (mml2015-clean-buffer)
645 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
648 (error "Cannot find encrypt function"))))
651 (defun mml2015-sign (cont)
652 (mml2015-clean-buffer)
653 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
656 (error "Cannot find sign function"))))
659 (defun mml2015-self-encrypt ()
660 (mml2015-encrypt nil))
664 ;;; mml2015.el ends here