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 "OK")
131 (if (listp (car handles))
135 (defun mml2015-mailcrypt-clear-decrypt ()
139 (funcall mml2015-decrypt-function)
141 (mm-set-handle-multipart-parameter
142 mm-security-handle 'gnus-details (mml2015-format-error err))
145 (mm-set-handle-multipart-parameter
146 mm-security-handle 'gnus-details "Quit.")
149 (mm-set-handle-multipart-parameter
150 mm-security-handle 'gnus-info "OK")
151 (mm-set-handle-multipart-parameter
152 mm-security-handle 'gnus-info "Failed"))))
154 (defun mml2015-fix-micalg (alg)
156 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
157 (upcase (if (string-match "^p[gh]p-" alg)
158 (substring alg (match-end 0))
161 (defun mml2015-mailcrypt-verify (handle ctl)
164 (unless (setq part (mm-find-raw-part-by-type
165 ctl (or (mm-handle-multipart-ctl-parameter
167 "application/pgp-signature")
169 (mm-set-handle-multipart-parameter
170 mm-security-handle 'gnus-info "Corrupted")
171 (throw 'error handle))
173 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
174 (insert (format "Hash: %s\n\n"
175 (or (mml2015-fix-micalg
176 (mm-handle-multipart-ctl-parameter
180 (narrow-to-region (point) (point))
182 (goto-char (point-min))
184 (if (looking-at "^-")
187 (unless (setq part (mm-find-part-by-type
188 (cdr handle) "application/pgp-signature" nil t))
189 (mm-set-handle-multipart-parameter
190 mm-security-handle 'gnus-info "Corrupted")
191 (throw 'error handle))
193 (narrow-to-region (point) (point))
194 (mm-insert-part part)
195 (goto-char (point-min))
196 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
197 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
198 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
199 (replace-match "-----END PGP SIGNATURE-----" t t)))
200 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
201 (unless (condition-case err
203 (funcall mml2015-verify-function)
204 (if (get-buffer " *mailcrypt stderr temp")
205 (mm-set-handle-multipart-parameter
206 mm-security-handle 'gnus-details
207 (with-current-buffer " *mailcrypt stderr temp"
209 (if (get-buffer " *mailcrypt stdout temp")
210 (kill-buffer " *mailcrypt stdout temp"))
211 (if (get-buffer " *mailcrypt stderr temp")
212 (kill-buffer " *mailcrypt stderr temp"))
213 (if (get-buffer " *mailcrypt status temp")
214 (kill-buffer " *mailcrypt status temp"))
215 (if (get-buffer mc-gpg-debug-buffer)
216 (kill-buffer mc-gpg-debug-buffer)))
218 (mm-set-handle-multipart-parameter
219 mm-security-handle 'gnus-details (mml2015-format-error err))
222 (mm-set-handle-multipart-parameter
223 mm-security-handle 'gnus-details "Quit.")
225 (mm-set-handle-multipart-parameter
226 mm-security-handle 'gnus-info "Failed")
227 (throw 'error handle))))
228 (mm-set-handle-multipart-parameter
229 mm-security-handle 'gnus-info "OK")
232 (defun mml2015-mailcrypt-clear-verify ()
233 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
234 (if (condition-case err
236 (funcall mml2015-verify-function)
237 (if (get-buffer " *mailcrypt stderr temp")
238 (mm-set-handle-multipart-parameter
239 mm-security-handle 'gnus-details
240 (with-current-buffer " *mailcrypt stderr temp"
242 (if (get-buffer " *mailcrypt stdout temp")
243 (kill-buffer " *mailcrypt stdout temp"))
244 (if (get-buffer " *mailcrypt stderr temp")
245 (kill-buffer " *mailcrypt stderr temp"))
246 (if (get-buffer " *mailcrypt status temp")
247 (kill-buffer " *mailcrypt status temp"))
248 (if (get-buffer mc-gpg-debug-buffer)
249 (kill-buffer mc-gpg-debug-buffer)))
251 (mm-set-handle-multipart-parameter
252 mm-security-handle 'gnus-details (mml2015-format-error err))
255 (mm-set-handle-multipart-parameter
256 mm-security-handle 'gnus-details "Quit.")
258 (mm-set-handle-multipart-parameter
259 mm-security-handle 'gnus-info "OK")
260 (mm-set-handle-multipart-parameter
261 mm-security-handle 'gnus-info "Failed"))))
263 (defun mml2015-mailcrypt-sign (cont)
264 (mc-sign-generic (message-options-get 'message-sender)
267 (funcall mml-boundary-function (incf mml-multipart-number)))
269 (goto-char (point-min))
270 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
271 (error "Cannot find signed begin line"))
272 (goto-char (match-beginning 0))
274 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
275 (error "Cannot not find PGP hash"))
276 (setq hash (match-string 1))
277 (unless (re-search-forward "^$" nil t)
278 (error "Cannot not find PGP message"))
280 (delete-region (point-min) (point))
281 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
283 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
285 (insert (format "\n--%s\n" boundary))
287 (goto-char (point-max))
288 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
289 (error "Cannot find signature part"))
290 (replace-match "-----END PGP MESSAGE-----" t t)
291 (goto-char (match-beginning 0))
292 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
294 (error "Cannot find signature part"))
295 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
296 (goto-char (match-beginning 0))
298 (narrow-to-region point (point))
300 (while (re-search-forward "^- -" nil t)
301 (replace-match "-" t t))
302 (goto-char (point-max)))
303 (insert (format "--%s\n" boundary))
304 (insert "Content-Type: application/pgp-signature\n\n")
305 (goto-char (point-max))
306 (insert (format "--%s--\n" boundary))
307 (goto-char (point-max))))
309 (defun mml2015-mailcrypt-encrypt (cont)
310 (let ((mc-pgp-always-sign
311 (or mc-pgp-always-sign
312 (eq t (or (message-options-get 'message-sign-encrypt)
314 'message-sign-encrypt
315 (or (y-or-n-p "Sign the message? ")
318 (mm-with-unibyte-current-buffer-mule4
320 (or (message-options-get 'message-recipients)
321 (message-options-set 'message-recipients
322 (mc-cleanup-recipient-headers
323 (read-string "Recipients: "))))
325 (message-options-get 'message-sender))))
326 (goto-char (point-min))
327 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
328 (error "Fail to encrypt the message"))
330 (funcall mml-boundary-function (incf mml-multipart-number))))
331 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
333 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
334 (insert (format "--%s\n" boundary))
335 (insert "Content-Type: application/pgp-encrypted\n\n")
336 (insert "Version: 1\n\n")
337 (insert (format "--%s\n" boundary))
338 (insert "Content-Type: application/octet-stream\n\n")
339 (goto-char (point-max))
340 (insert (format "--%s--\n" boundary))
341 (goto-char (point-max))))
346 (autoload 'gpg-decrypt "gpg")
347 (autoload 'gpg-verify "gpg")
348 (autoload 'gpg-verify-cleartext "gpg")
349 (autoload 'gpg-sign-detached "gpg")
350 (autoload 'gpg-sign-encrypt "gpg")
351 (autoload 'gpg-passphrase-read "gpg"))
353 (defun mml2015-gpg-passphrase ()
354 (or (message-options-get 'gpg-passphrase)
355 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
357 (defun mml2015-gpg-decrypt-1 ()
358 (let ((cipher (current-buffer)) plain result)
359 (if (with-temp-buffer
361 (gpg-decrypt cipher (setq plain (current-buffer))
362 mml2015-result-buffer nil)
363 (mm-set-handle-multipart-parameter
364 mm-security-handle 'gnus-details
365 (with-current-buffer mml2015-result-buffer
369 (insert-buffer plain)
370 (goto-char (point-min))
371 (while (search-forward "\r\n" nil t)
372 (replace-match "\n" t t))))
374 ;; Some wrong with the return value, check plain text buffer.
375 (if (> (point-max) (point-min))
379 (defun mml2015-gpg-decrypt (handle ctl)
380 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
381 (mml2015-mailcrypt-decrypt handle ctl)))
383 (defun mml2015-gpg-clear-decrypt ()
385 (setq result (mml2015-gpg-decrypt-1))
387 (mm-set-handle-multipart-parameter
388 mm-security-handle 'gnus-info "OK")
389 (mm-set-handle-multipart-parameter
390 mm-security-handle 'gnus-info "Failed"))))
392 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
394 (fpr-length (string-width fingerprint))
397 (setq fingerprint (string-to-list fingerprint))
399 (setq fpr-length (- fpr-length 4))
400 (setq slice (butlast fingerprint fpr-length))
401 (setq fingerprint (nthcdr 4 fingerprint))
402 (setq n-slice (1+ n-slice))
408 (otherwise (concat " " slice))))))
411 (defun mml2015-gpg-extract-signature-details ()
412 (goto-char (point-min))
413 (if (boundp 'gpg-unabbrev-trust-alist)
414 (let* ((signer (and (re-search-forward
415 "^\\[GNUPG:\\] GOODSIG [0-9A-Za-z]* \\(.*\\)$"
418 (fprint (and (re-search-forward
419 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
422 (trust (and (re-search-forward "^\\[GNUPG:\\] \\(TRUST_.*\\)$" nil t)
425 (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
426 mml2015-trust-boundaries-alist))))
427 (if (and signer trust fprint)
429 (unless trust-good-enough-p
430 (concat "\nUntrusted, Fingerprint: "
431 (mml2015-gpg-pretty-print-fpr fprint))))
432 "From unknown user"))
433 (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
435 "From unknown user")))
437 (defun mml2015-gpg-verify (handle ctl)
439 (let (part message signature info-is-set-p)
440 (unless (setq part (mm-find-raw-part-by-type
441 ctl (or (mm-handle-multipart-ctl-parameter
443 "application/pgp-signature")
445 (mm-set-handle-multipart-parameter
446 mm-security-handle 'gnus-info "Corrupted")
447 (throw 'error handle))
449 (setq message (current-buffer))
451 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
452 ;; clearsign use --textmode. The conversion is not necessary.
453 ;; In clearverify, the conversion is not necessary either.
454 (goto-char (point-min))
457 (unless (eq (char-before) ?\r)
462 (setq signature (current-buffer))
463 (unless (setq part (mm-find-part-by-type
464 (cdr handle) "application/pgp-signature" nil t))
465 (mm-set-handle-multipart-parameter
466 mm-security-handle 'gnus-info "Corrupted")
467 (throw 'error handle))
468 (mm-insert-part part)
469 (unless (condition-case err
471 (gpg-verify message signature mml2015-result-buffer)
472 (mm-set-handle-multipart-parameter
473 mm-security-handle 'gnus-details
474 (with-current-buffer mml2015-result-buffer
477 (mm-set-handle-multipart-parameter
478 mm-security-handle 'gnus-details (mml2015-format-error err))
479 (mm-set-handle-multipart-parameter
480 mm-security-handle 'gnus-info "Error.")
481 (setq info-is-set-p t)
484 (mm-set-handle-multipart-parameter
485 mm-security-handle 'gnus-details "Quit.")
486 (mm-set-handle-multipart-parameter
487 mm-security-handle 'gnus-info "Quit.")
488 (setq info-is-set-p t)
490 (unless info-is-set-p
491 (mm-set-handle-multipart-parameter
492 mm-security-handle 'gnus-info "Failed"))
493 (throw 'error handle)))
494 (mm-set-handle-multipart-parameter
495 mm-security-handle 'gnus-info
496 (with-current-buffer mml2015-result-buffer
497 (mml2015-gpg-extract-signature-details))))
500 (defun mml2015-gpg-clear-verify ()
501 (if (condition-case err
503 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
504 (mm-set-handle-multipart-parameter
505 mm-security-handle 'gnus-details
506 (with-current-buffer mml2015-result-buffer
509 (mm-set-handle-multipart-parameter
510 mm-security-handle 'gnus-details (mml2015-format-error err))
513 (mm-set-handle-multipart-parameter
514 mm-security-handle 'gnus-details "Quit.")
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)))
520 (mm-set-handle-multipart-parameter
521 mm-security-handle 'gnus-info "Failed")))
523 (defun mml2015-gpg-sign (cont)
525 (funcall mml-boundary-function (incf mml-multipart-number)))
526 (text (current-buffer)) signature)
527 (goto-char (point-max))
531 (unless (gpg-sign-detached text (setq signature (current-buffer))
532 mml2015-result-buffer
534 (message-options-get 'message-sender)
535 t t) ; armor & textmode
536 (unless (> (point-max) (point-min))
537 (pop-to-buffer mml2015-result-buffer)
538 (error "Sign error")))
539 (goto-char (point-min))
540 (while (re-search-forward "\r+$" nil t)
541 (replace-match "" t t))
543 (goto-char (point-min))
544 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
546 ;;; FIXME: what is the micalg?
547 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
548 (insert (format "\n--%s\n" boundary))
549 (goto-char (point-max))
550 (insert (format "\n--%s\n" boundary))
551 (insert "Content-Type: application/pgp-signature\n\n")
552 (insert-buffer signature)
553 (goto-char (point-max))
554 (insert (format "--%s--\n" boundary))
555 (goto-char (point-max)))))
557 (defun mml2015-gpg-encrypt (cont)
559 (funcall mml-boundary-function (incf mml-multipart-number)))
560 (text (current-buffer))
562 (mm-with-unibyte-current-buffer-mule4
564 (unless (gpg-sign-encrypt
565 text (setq cipher (current-buffer))
566 mml2015-result-buffer
569 (message-options-get 'message-recipients)
570 (message-options-set 'message-recipients
571 (read-string "Recipients: ")))
574 (message-options-get 'message-sender)
575 t t) ; armor & textmode
576 (unless (> (point-max) (point-min))
577 (pop-to-buffer mml2015-result-buffer)
578 (error "Encrypt error")))
579 (goto-char (point-min))
580 (while (re-search-forward "\r+$" nil t)
581 (replace-match "" t t))
583 (delete-region (point-min) (point-max))
584 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
586 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
587 (insert (format "--%s\n" boundary))
588 (insert "Content-Type: application/pgp-encrypted\n\n")
589 (insert "Version: 1\n\n")
590 (insert (format "--%s\n" boundary))
591 (insert "Content-Type: application/octet-stream\n\n")
592 (insert-buffer cipher)
593 (goto-char (point-max))
594 (insert (format "--%s--\n" boundary))
595 (goto-char (point-max))))))
599 (defun mml2015-clean-buffer ()
600 (if (gnus-buffer-live-p mml2015-result-buffer)
601 (with-current-buffer mml2015-result-buffer
604 (setq mml2015-result-buffer
605 (gnus-get-buffer-create "*MML2015 Result*"))
608 (defsubst mml2015-clear-decrypt-function ()
609 (nth 6 (assq mml2015-use mml2015-function-alist)))
611 (defsubst mml2015-clear-verify-function ()
612 (nth 5 (assq mml2015-use mml2015-function-alist)))
615 (defun mml2015-decrypt (handle ctl)
616 (mml2015-clean-buffer)
617 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
619 (funcall func handle ctl)
623 (defun mml2015-decrypt-test (handle ctl)
627 (defun mml2015-verify (handle ctl)
628 (mml2015-clean-buffer)
629 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
631 (funcall func handle ctl)
635 (defun mml2015-verify-test (handle ctl)
639 (defun mml2015-encrypt (cont)
640 (mml2015-clean-buffer)
641 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
644 (error "Cannot find encrypt function"))))
647 (defun mml2015-sign (cont)
648 (mml2015-clean-buffer)
649 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
652 (error "Cannot find sign function"))))
655 (defun mml2015-self-encrypt ()
656 (mml2015-encrypt nil))
660 ;;; mml2015.el ends here