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))
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 "OK")
130 (if (listp (car handles))
134 (defun mml2015-mailcrypt-clear-decrypt ()
138 (funcall mml2015-decrypt-function)
140 (mm-set-handle-multipart-parameter
141 mm-security-handle 'gnus-details (mml2015-format-error err))
144 (mm-set-handle-multipart-parameter
145 mm-security-handle 'gnus-details "Quit.")
148 (mm-set-handle-multipart-parameter
149 mm-security-handle 'gnus-info "OK")
150 (mm-set-handle-multipart-parameter
151 mm-security-handle 'gnus-info "Failed"))))
153 (defun mml2015-fix-micalg (alg)
155 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
156 (upcase (if (string-match "^p[gh]p-" alg)
157 (substring alg (match-end 0))
160 (defun mml2015-mailcrypt-verify (handle ctl)
163 (unless (setq part (mm-find-raw-part-by-type
164 ctl (or (mm-handle-multipart-ctl-parameter
166 "application/pgp-signature")
168 (mm-set-handle-multipart-parameter
169 mm-security-handle 'gnus-info "Corrupted")
170 (throw 'error handle))
172 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
173 (insert (format "Hash: %s\n\n"
174 (or (mml2015-fix-micalg
175 (mm-handle-multipart-ctl-parameter
179 (narrow-to-region (point) (point))
181 (goto-char (point-min))
183 (if (looking-at "^-")
186 (unless (setq part (mm-find-part-by-type
187 (cdr handle) "application/pgp-signature" nil t))
188 (mm-set-handle-multipart-parameter
189 mm-security-handle 'gnus-info "Corrupted")
190 (throw 'error handle))
192 (narrow-to-region (point) (point))
193 (mm-insert-part part)
194 (goto-char (point-min))
195 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
196 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
197 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
198 (replace-match "-----END PGP SIGNATURE-----" t t)))
199 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
200 (unless (condition-case err
202 (funcall mml2015-verify-function)
203 (if (get-buffer " *mailcrypt stderr temp")
204 (mm-set-handle-multipart-parameter
205 mm-security-handle 'gnus-details
206 (with-current-buffer " *mailcrypt stderr temp"
208 (if (get-buffer " *mailcrypt stdout temp")
209 (kill-buffer " *mailcrypt stdout temp"))
210 (if (get-buffer " *mailcrypt stderr temp")
211 (kill-buffer " *mailcrypt stderr temp"))
212 (if (get-buffer " *mailcrypt status temp")
213 (kill-buffer " *mailcrypt status temp"))
214 (if (get-buffer mc-gpg-debug-buffer)
215 (kill-buffer mc-gpg-debug-buffer)))
217 (mm-set-handle-multipart-parameter
218 mm-security-handle 'gnus-details (mml2015-format-error err))
221 (mm-set-handle-multipart-parameter
222 mm-security-handle 'gnus-details "Quit.")
224 (mm-set-handle-multipart-parameter
225 mm-security-handle 'gnus-info "Failed")
226 (throw 'error handle))))
227 (mm-set-handle-multipart-parameter
228 mm-security-handle 'gnus-info "OK")
231 (defun mml2015-mailcrypt-clear-verify ()
232 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
233 (if (condition-case err
235 (funcall mml2015-verify-function)
236 (if (get-buffer " *mailcrypt stderr temp")
237 (mm-set-handle-multipart-parameter
238 mm-security-handle 'gnus-details
239 (with-current-buffer " *mailcrypt stderr temp"
241 (if (get-buffer " *mailcrypt stdout temp")
242 (kill-buffer " *mailcrypt stdout temp"))
243 (if (get-buffer " *mailcrypt stderr temp")
244 (kill-buffer " *mailcrypt stderr temp"))
245 (if (get-buffer " *mailcrypt status temp")
246 (kill-buffer " *mailcrypt status temp"))
247 (if (get-buffer mc-gpg-debug-buffer)
248 (kill-buffer mc-gpg-debug-buffer)))
250 (mm-set-handle-multipart-parameter
251 mm-security-handle 'gnus-details (mml2015-format-error err))
254 (mm-set-handle-multipart-parameter
255 mm-security-handle 'gnus-details "Quit.")
257 (mm-set-handle-multipart-parameter
258 mm-security-handle 'gnus-info "OK")
259 (mm-set-handle-multipart-parameter
260 mm-security-handle 'gnus-info "Failed"))))
262 (defun mml2015-mailcrypt-sign (cont)
263 (mc-sign-generic (message-options-get 'message-sender)
266 (funcall mml-boundary-function (incf mml-multipart-number)))
268 (goto-char (point-min))
269 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
270 (error "Cannot find signed begin line"))
271 (goto-char (match-beginning 0))
273 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
274 (error "Cannot not find PGP hash"))
275 (setq hash (match-string 1))
276 (unless (re-search-forward "^$" nil t)
277 (error "Cannot not find PGP message"))
279 (delete-region (point-min) (point))
280 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
282 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
284 (insert (format "\n--%s\n" boundary))
286 (goto-char (point-max))
287 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
288 (error "Cannot find signature part"))
289 (replace-match "-----END PGP MESSAGE-----" t t)
290 (goto-char (match-beginning 0))
291 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
293 (error "Cannot find signature part"))
294 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
295 (goto-char (match-beginning 0))
297 (narrow-to-region point (point))
299 (while (re-search-forward "^- -" nil t)
300 (replace-match "-" t t))
301 (goto-char (point-max)))
302 (insert (format "--%s\n" boundary))
303 (insert "Content-Type: application/pgp-signature\n\n")
304 (goto-char (point-max))
305 (insert (format "--%s--\n" boundary))
306 (goto-char (point-max))))
308 (defun mml2015-mailcrypt-encrypt (cont)
309 (let ((mc-pgp-always-sign
310 (or mc-pgp-always-sign
311 (eq t (or (message-options-get 'message-sign-encrypt)
313 'message-sign-encrypt
314 (or (y-or-n-p "Sign the message? ")
317 (mm-with-unibyte-current-buffer-mule4
319 (or (message-options-get 'message-recipients)
320 (message-options-set 'message-recipients
321 (mc-cleanup-recipient-headers
322 (read-string "Recipients: "))))
324 (message-options-get 'message-sender))))
325 (goto-char (point-min))
326 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
327 (error "Fail to encrypt the message"))
329 (funcall mml-boundary-function (incf mml-multipart-number))))
330 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
332 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
333 (insert (format "--%s\n" boundary))
334 (insert "Content-Type: application/pgp-encrypted\n\n")
335 (insert "Version: 1\n\n")
336 (insert (format "--%s\n" boundary))
337 (insert "Content-Type: application/octet-stream\n\n")
338 (goto-char (point-max))
339 (insert (format "--%s--\n" boundary))
340 (goto-char (point-max))))
345 (autoload 'gpg-decrypt "gpg")
346 (autoload 'gpg-verify "gpg")
347 (autoload 'gpg-verify-cleartext "gpg")
348 (autoload 'gpg-sign-detached "gpg")
349 (autoload 'gpg-sign-encrypt "gpg")
350 (autoload 'gpg-passphrase-read "gpg"))
352 (defun mml2015-gpg-passphrase ()
353 (or (message-options-get 'gpg-passphrase)
354 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
356 (defun mml2015-gpg-decrypt-1 ()
357 (let ((cipher (current-buffer)) plain result)
358 (if (with-temp-buffer
360 (gpg-decrypt cipher (setq plain (current-buffer))
361 mml2015-result-buffer nil)
362 (mm-set-handle-multipart-parameter
363 mm-security-handle 'gnus-details
364 (with-current-buffer mml2015-result-buffer
368 (insert-buffer plain)
369 (goto-char (point-min))
370 (while (search-forward "\r\n" nil t)
371 (replace-match "\n" t t))))
373 ;; Some wrong with the return value, check plain text buffer.
374 (if (> (point-max) (point-min))
378 (defun mml2015-gpg-decrypt (handle ctl)
379 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
380 (mml2015-mailcrypt-decrypt handle ctl)))
382 (defun mml2015-gpg-clear-decrypt ()
384 (setq result (mml2015-gpg-decrypt-1))
386 (mm-set-handle-multipart-parameter
387 mm-security-handle 'gnus-info "OK")
388 (mm-set-handle-multipart-parameter
389 mm-security-handle 'gnus-info "Failed"))))
391 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
393 (fpr-length (string-width fingerprint))
396 (setq fingerprint (string-to-list fingerprint))
398 (setq fpr-length (- fpr-length 4))
399 (setq slice (butlast fingerprint fpr-length))
400 (setq fingerprint (nthcdr 4 fingerprint))
401 (setq n-slice (1+ n-slice))
407 (otherwise (concat " " slice))))))
410 (defun mml2015-gpg-extract-signature-details ()
411 (goto-char (point-min))
412 (if (boundp 'gpg-unabbrev-trust-alist)
413 (let* ((signer (and (re-search-forward
414 "^\\[GNUPG:\\] GOODSIG [0-9A-Za-z]* \\(.*\\)$"
417 (fprint (and (re-search-forward
418 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
421 (trust (and (re-search-forward "^\\[GNUPG:\\] \\(TRUST_.*\\)$" nil t)
424 (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
425 mml2015-trust-boundaries-alist))))
426 (if (and signer trust fprint)
428 (unless trust-good-enough-p
429 (concat "\nUntrusted, Fingerprint: "
430 (mml2015-gpg-pretty-print-fpr fprint))))
431 "From unknown user"))
432 (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
434 "From unknown user")))
436 (defun mml2015-gpg-verify (handle ctl)
438 (let (part message signature info-is-set-p)
439 (unless (setq part (mm-find-raw-part-by-type
440 ctl (or (mm-handle-multipart-ctl-parameter
442 "application/pgp-signature")
444 (mm-set-handle-multipart-parameter
445 mm-security-handle 'gnus-info "Corrupted")
446 (throw 'error handle))
448 (setq message (current-buffer))
450 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
451 ;; clearsign use --textmode. The conversion is not necessary.
452 ;; In clearverify, the conversion is not necessary either.
453 (goto-char (point-min))
456 (unless (eq (char-before) ?\r)
461 (setq signature (current-buffer))
462 (unless (setq part (mm-find-part-by-type
463 (cdr handle) "application/pgp-signature" nil t))
464 (mm-set-handle-multipart-parameter
465 mm-security-handle 'gnus-info "Corrupted")
466 (throw 'error handle))
467 (mm-insert-part part)
468 (unless (condition-case err
470 (gpg-verify message signature mml2015-result-buffer)
471 (mm-set-handle-multipart-parameter
472 mm-security-handle 'gnus-details
473 (with-current-buffer mml2015-result-buffer
476 (mm-set-handle-multipart-parameter
477 mm-security-handle 'gnus-details (mml2015-format-error err))
478 (mm-set-handle-multipart-parameter
479 mm-security-handle 'gnus-info "Error.")
480 (setq info-is-set-p t)
483 (mm-set-handle-multipart-parameter
484 mm-security-handle 'gnus-details "Quit.")
485 (mm-set-handle-multipart-parameter
486 mm-security-handle 'gnus-info "Quit.")
487 (setq info-is-set-p t)
489 (unless info-is-set-p
490 (mm-set-handle-multipart-parameter
491 mm-security-handle 'gnus-info "Failed"))
492 (throw 'error handle)))
493 (mm-set-handle-multipart-parameter
494 mm-security-handle 'gnus-info
495 (with-current-buffer mml2015-result-buffer
496 (mml2015-gpg-extract-signature-details))))
499 (defun mml2015-gpg-clear-verify ()
500 (if (condition-case err
502 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
503 (mm-set-handle-multipart-parameter
504 mm-security-handle 'gnus-details
505 (with-current-buffer mml2015-result-buffer
508 (mm-set-handle-multipart-parameter
509 mm-security-handle 'gnus-details (mml2015-format-error err))
512 (mm-set-handle-multipart-parameter
513 mm-security-handle 'gnus-details "Quit.")
515 (mm-set-handle-multipart-parameter
516 mm-security-handle 'gnus-info
517 (with-current-buffer mml2015-result-buffer
518 (mml2015-gpg-extract-signature-details)))
519 (mm-set-handle-multipart-parameter
520 mm-security-handle 'gnus-info "Failed")))
522 (defun mml2015-gpg-sign (cont)
524 (funcall mml-boundary-function (incf mml-multipart-number)))
525 (text (current-buffer)) signature)
526 (goto-char (point-max))
530 (unless (gpg-sign-detached text (setq signature (current-buffer))
531 mml2015-result-buffer
533 (message-options-get 'message-sender)
534 t t) ; armor & textmode
535 (unless (> (point-max) (point-min))
536 (pop-to-buffer mml2015-result-buffer)
537 (error "Sign error")))
538 (goto-char (point-min))
539 (while (re-search-forward "\r+$" nil t)
540 (replace-match "" t t))
542 (goto-char (point-min))
543 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
545 ;;; FIXME: what is the micalg?
546 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
547 (insert (format "\n--%s\n" boundary))
548 (goto-char (point-max))
549 (insert (format "\n--%s\n" boundary))
550 (insert "Content-Type: application/pgp-signature\n\n")
551 (insert-buffer signature)
552 (goto-char (point-max))
553 (insert (format "--%s--\n" boundary))
554 (goto-char (point-max)))))
556 (defun mml2015-gpg-encrypt (cont)
558 (funcall mml-boundary-function (incf mml-multipart-number)))
559 (text (current-buffer))
561 (mm-with-unibyte-current-buffer-mule4
563 (unless (gpg-sign-encrypt
564 text (setq cipher (current-buffer))
565 mml2015-result-buffer
568 (message-options-get 'message-recipients)
569 (message-options-set 'message-recipients
570 (read-string "Recipients: ")))
573 (message-options-get 'message-sender)
574 t t) ; armor & textmode
575 (unless (> (point-max) (point-min))
576 (pop-to-buffer mml2015-result-buffer)
577 (error "Encrypt error")))
578 (goto-char (point-min))
579 (while (re-search-forward "\r+$" nil t)
580 (replace-match "" t t))
582 (delete-region (point-min) (point-max))
583 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
585 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
586 (insert (format "--%s\n" boundary))
587 (insert "Content-Type: application/pgp-encrypted\n\n")
588 (insert "Version: 1\n\n")
589 (insert (format "--%s\n" boundary))
590 (insert "Content-Type: application/octet-stream\n\n")
591 (insert-buffer cipher)
592 (goto-char (point-max))
593 (insert (format "--%s--\n" boundary))
594 (goto-char (point-max))))))
598 (defun mml2015-clean-buffer ()
599 (if (gnus-buffer-live-p mml2015-result-buffer)
600 (with-current-buffer mml2015-result-buffer
603 (setq mml2015-result-buffer
604 (gnus-get-buffer-create "*MML2015 Result*"))
607 (defsubst mml2015-clear-decrypt-function ()
608 (nth 6 (assq mml2015-use mml2015-function-alist)))
610 (defsubst mml2015-clear-verify-function ()
611 (nth 5 (assq mml2015-use mml2015-function-alist)))
614 (defun mml2015-decrypt (handle ctl)
615 (mml2015-clean-buffer)
616 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
618 (funcall func handle ctl)
622 (defun mml2015-decrypt-test (handle ctl)
626 (defun mml2015-verify (handle ctl)
627 (mml2015-clean-buffer)
628 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
630 (funcall func handle ctl)
634 (defun mml2015-verify-test (handle ctl)
638 (defun mml2015-encrypt (cont)
639 (mml2015-clean-buffer)
640 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
643 (error "Cannot find encrypt function"))))
646 (defun mml2015-sign (cont)
647 (mml2015-clean-buffer)
648 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
651 (error "Cannot find sign function"))))
654 (defun mml2015-self-encrypt ()
655 (mml2015-encrypt nil))
659 ;;; mml2015.el ends here