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.
28 (eval-when-compile (require 'cl))
31 (defvar mml2015-use (or
35 (and (fboundp 'gpg-sign-detached)
39 (and (fboundp 'mc-encrypt-generic)
40 (fboundp 'mc-sign-generic)
41 (fboundp 'mc-cleanup-recipient-headers)
43 "The package used for PGP/MIME.")
45 ;; Something is not RFC2015.
46 (defvar mml2015-function-alist
47 '((mailcrypt mml2015-mailcrypt-sign
48 mml2015-mailcrypt-encrypt
49 mml2015-mailcrypt-verify
50 mml2015-mailcrypt-decrypt
51 mml2015-mailcrypt-clear-verify
52 mml2015-mailcrypt-clear-decrypt)
57 mml2015-gpg-clear-verify
58 mml2015-gpg-clear-decrypt))
59 "Alist of PGP/MIME functions.")
61 (defvar mml2015-result-buffer nil)
63 (defvar mml2015-trust-boundaries-alist
64 '((trust-undefined . nil)
69 "Trust boundaries for a signer's GnuPG key.
70 This alist contains pairs of the form (trust-symbol . boolean), with
71 symbols that are contained in `gpg-unabbrev-trust-alist'. The boolean
72 specifies whether the given trust value is good enough to be trusted
78 (autoload 'mailcrypt-decrypt "mailcrypt")
79 (autoload 'mailcrypt-verify "mailcrypt")
80 (autoload 'mc-pgp-always-sign "mailcrypt")
81 (autoload 'mc-encrypt-generic "mc-toplev")
82 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
83 (autoload 'mc-sign-generic "mc-toplev"))
86 (defvar mc-default-scheme)
89 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
90 (defvar mml2015-verify-function 'mailcrypt-verify)
92 (defun mml2015-format-error (err)
93 (if (stringp (cadr err))
95 (format "%S" (cdr err))))
97 (defun mml2015-mailcrypt-decrypt (handle ctl)
99 (let (child handles result)
100 (unless (setq child (mm-find-part-by-type
102 "application/octet-stream" nil t))
103 (mm-set-handle-multipart-parameter
104 mm-security-handle 'gnus-info "Corrupted")
105 (throw 'error handle))
107 (mm-insert-part child)
110 (funcall mml2015-decrypt-function)
112 (mm-set-handle-multipart-parameter
113 mm-security-handle 'gnus-details (mml2015-format-error err))
116 (mm-set-handle-multipart-parameter
117 mm-security-handle 'gnus-details "Quit.")
120 (mm-set-handle-multipart-parameter
121 mm-security-handle 'gnus-info "Failed")
122 (throw 'error handle))
123 (setq handles (mm-dissect-buffer t)))
124 (mm-destroy-parts handle)
125 (mm-set-handle-multipart-parameter
126 mm-security-handle 'gnus-info "OK")
127 (if (listp (car handles))
131 (defun mml2015-mailcrypt-clear-decrypt ()
135 (funcall mml2015-decrypt-function)
137 (mm-set-handle-multipart-parameter
138 mm-security-handle 'gnus-details (mml2015-format-error err))
141 (mm-set-handle-multipart-parameter
142 mm-security-handle 'gnus-details "Quit.")
145 (mm-set-handle-multipart-parameter
146 mm-security-handle 'gnus-info "OK")
147 (mm-set-handle-multipart-parameter
148 mm-security-handle 'gnus-info "Failed"))))
150 (defun mml2015-fix-micalg (alg)
152 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
153 (upcase (if (string-match "^p[gh]p-" alg)
154 (substring alg (match-end 0))
157 (defun mml2015-mailcrypt-verify (handle ctl)
160 (unless (setq part (mm-find-raw-part-by-type
161 ctl (or (mm-handle-multipart-ctl-parameter
163 "application/pgp-signature")
165 (mm-set-handle-multipart-parameter
166 mm-security-handle 'gnus-info "Corrupted")
167 (throw 'error handle))
169 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
170 (insert (format "Hash: %s\n\n"
171 (or (mml2015-fix-micalg
172 (mm-handle-multipart-ctl-parameter
176 (narrow-to-region (point) (point))
178 (goto-char (point-min))
180 (if (looking-at "^-")
183 (unless (setq part (mm-find-part-by-type
184 (cdr handle) "application/pgp-signature" nil t))
185 (mm-set-handle-multipart-parameter
186 mm-security-handle 'gnus-info "Corrupted")
187 (throw 'error handle))
189 (narrow-to-region (point) (point))
190 (mm-insert-part part)
191 (goto-char (point-min))
192 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
193 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
194 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
195 (replace-match "-----END PGP SIGNATURE-----" t t)))
196 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
197 (unless (condition-case err
199 (funcall mml2015-verify-function)
200 (if (get-buffer " *mailcrypt stderr temp")
201 (mm-set-handle-multipart-parameter
202 mm-security-handle 'gnus-details
203 (with-current-buffer " *mailcrypt stderr temp"
205 (if (get-buffer " *mailcrypt stdout temp")
206 (kill-buffer " *mailcrypt stdout temp"))
207 (if (get-buffer " *mailcrypt stderr temp")
208 (kill-buffer " *mailcrypt stderr temp"))
209 (if (get-buffer " *mailcrypt status temp")
210 (kill-buffer " *mailcrypt status temp"))
211 (if (get-buffer mc-gpg-debug-buffer)
212 (kill-buffer mc-gpg-debug-buffer)))
214 (mm-set-handle-multipart-parameter
215 mm-security-handle 'gnus-details (mml2015-format-error err))
218 (mm-set-handle-multipart-parameter
219 mm-security-handle 'gnus-details "Quit.")
221 (mm-set-handle-multipart-parameter
222 mm-security-handle 'gnus-info "Failed")
223 (throw 'error handle))))
224 (mm-set-handle-multipart-parameter
225 mm-security-handle 'gnus-info "OK")
228 (defun mml2015-mailcrypt-clear-verify ()
229 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
230 (if (condition-case err
232 (funcall mml2015-verify-function)
233 (if (get-buffer " *mailcrypt stderr temp")
234 (mm-set-handle-multipart-parameter
235 mm-security-handle 'gnus-details
236 (with-current-buffer " *mailcrypt stderr temp"
238 (if (get-buffer " *mailcrypt stdout temp")
239 (kill-buffer " *mailcrypt stdout temp"))
240 (if (get-buffer " *mailcrypt stderr temp")
241 (kill-buffer " *mailcrypt stderr temp"))
242 (if (get-buffer " *mailcrypt status temp")
243 (kill-buffer " *mailcrypt status temp"))
244 (if (get-buffer mc-gpg-debug-buffer)
245 (kill-buffer mc-gpg-debug-buffer)))
247 (mm-set-handle-multipart-parameter
248 mm-security-handle 'gnus-details (mml2015-format-error err))
251 (mm-set-handle-multipart-parameter
252 mm-security-handle 'gnus-details "Quit.")
254 (mm-set-handle-multipart-parameter
255 mm-security-handle 'gnus-info "OK")
256 (mm-set-handle-multipart-parameter
257 mm-security-handle 'gnus-info "Failed"))))
259 (defun mml2015-mailcrypt-sign (cont)
260 (mc-sign-generic (message-options-get 'message-sender)
263 (funcall mml-boundary-function (incf mml-multipart-number)))
265 (goto-char (point-min))
266 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
267 (error "Cannot find signed begin line"))
268 (goto-char (match-beginning 0))
270 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
271 (error "Cannot not find PGP hash"))
272 (setq hash (match-string 1))
273 (unless (re-search-forward "^$" nil t)
274 (error "Cannot not find PGP message"))
276 (delete-region (point-min) (point))
277 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
279 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
281 (insert (format "\n--%s\n" boundary))
283 (goto-char (point-max))
284 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
285 (error "Cannot find signature part"))
286 (replace-match "-----END PGP MESSAGE-----" t t)
287 (goto-char (match-beginning 0))
288 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
290 (error "Cannot find signature part"))
291 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
292 (goto-char (match-beginning 0))
294 (narrow-to-region point (point))
296 (while (re-search-forward "^- -" nil t)
297 (replace-match "-" t t))
298 (goto-char (point-max)))
299 (insert (format "--%s\n" boundary))
300 (insert "Content-Type: application/pgp-signature\n\n")
301 (goto-char (point-max))
302 (insert (format "--%s--\n" boundary))
303 (goto-char (point-max))))
305 (defun mml2015-mailcrypt-encrypt (cont)
306 (let ((mc-pgp-always-sign
307 (or mc-pgp-always-sign
308 (eq t (or (message-options-get 'message-sign-encrypt)
310 'message-sign-encrypt
311 (or (y-or-n-p "Sign the message? ")
314 (mm-with-unibyte-current-buffer-mule4
316 (or (message-options-get 'message-recipients)
317 (message-options-set 'message-recipients
318 (mc-cleanup-recipient-headers
319 (read-string "Recipients: "))))
321 (message-options-get 'message-sender))))
322 (goto-char (point-min))
323 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
324 (error "Fail to encrypt the message"))
326 (funcall mml-boundary-function (incf mml-multipart-number))))
327 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
329 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
330 (insert (format "--%s\n" boundary))
331 (insert "Content-Type: application/pgp-encrypted\n\n")
332 (insert "Version: 1\n\n")
333 (insert (format "--%s\n" boundary))
334 (insert "Content-Type: application/octet-stream\n\n")
335 (goto-char (point-max))
336 (insert (format "--%s--\n" boundary))
337 (goto-char (point-max))))
342 (autoload 'gpg-decrypt "gpg")
343 (autoload 'gpg-verify "gpg")
344 (autoload 'gpg-verify-cleartext "gpg")
345 (autoload 'gpg-sign-detached "gpg")
346 (autoload 'gpg-sign-encrypt "gpg")
347 (autoload 'gpg-passphrase-read "gpg"))
349 (defun mml2015-gpg-passphrase ()
350 (or (message-options-get 'gpg-passphrase)
351 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
353 (defun mml2015-gpg-decrypt-1 ()
354 (let ((cipher (current-buffer)) plain result)
355 (if (with-temp-buffer
357 (gpg-decrypt cipher (setq plain (current-buffer))
358 mml2015-result-buffer nil)
359 (mm-set-handle-multipart-parameter
360 mm-security-handle 'gnus-details
361 (with-current-buffer mml2015-result-buffer
365 (insert-buffer plain)
366 (goto-char (point-min))
367 (while (search-forward "\r\n" nil t)
368 (replace-match "\n" t t))))
370 ;; Some wrong with the return value, check plain text buffer.
371 (if (> (point-max) (point-min))
375 (defun mml2015-gpg-decrypt (handle ctl)
376 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
377 (mml2015-mailcrypt-decrypt handle ctl)))
379 (defun mml2015-gpg-clear-decrypt ()
381 (setq result (mml2015-gpg-decrypt-1))
383 (mm-set-handle-multipart-parameter
384 mm-security-handle 'gnus-info "OK")
385 (mm-set-handle-multipart-parameter
386 mm-security-handle 'gnus-info "Failed"))))
388 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
390 (fpr-length (string-width fingerprint))
393 (setq fingerprint (string-to-list fingerprint))
395 (setq fpr-length (- fpr-length 4))
396 (setq slice (butlast fingerprint fpr-length))
397 (setq fingerprint (nthcdr 4 fingerprint))
398 (setq n-slice (1+ n-slice))
404 (otherwise (concat " " slice))))))
407 (defun mml2015-gpg-extract-signature-details ()
408 (goto-char (point-min))
409 (if (boundp 'gpg-unabbrev-trust-alist)
410 (let* ((signer (and (re-search-forward
411 "^\\[GNUPG:\\] GOODSIG [0-9A-Za-z]* \\(.*\\)$"
414 (fprint (and (re-search-forward
415 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
418 (trust (and (re-search-forward "^\\[GNUPG:\\] \\(TRUST_.*\\)$" nil t)
421 (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
422 mml2015-trust-boundaries-alist))))
423 (if (and signer trust fprint)
425 (unless trust-good-enough-p
426 (concat "\nUntrusted, Fingerprint: "
427 (mml2015-gpg-pretty-print-fpr fprint))))
428 "From unknown user"))
429 (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
431 "From unknown user")))
433 (defun mml2015-gpg-verify (handle ctl)
435 (let (part message signature info-is-set-p)
436 (unless (setq part (mm-find-raw-part-by-type
437 ctl (or (mm-handle-multipart-ctl-parameter
439 "application/pgp-signature")
441 (mm-set-handle-multipart-parameter
442 mm-security-handle 'gnus-info "Corrupted")
443 (throw 'error handle))
445 (setq message (current-buffer))
447 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
448 ;; clearsign use --textmode. The conversion is not necessary.
449 ;; In clearverify, the conversion is not necessary either.
450 (goto-char (point-min))
453 (unless (eq (char-before) ?\r)
458 (setq signature (current-buffer))
459 (unless (setq part (mm-find-part-by-type
460 (cdr handle) "application/pgp-signature" nil t))
461 (mm-set-handle-multipart-parameter
462 mm-security-handle 'gnus-info "Corrupted")
463 (throw 'error handle))
464 (mm-insert-part part)
465 (unless (condition-case err
467 (gpg-verify message signature mml2015-result-buffer)
468 (mm-set-handle-multipart-parameter
469 mm-security-handle 'gnus-details
470 (with-current-buffer mml2015-result-buffer
473 (mm-set-handle-multipart-parameter
474 mm-security-handle 'gnus-details (mml2015-format-error err))
475 (mm-set-handle-multipart-parameter
476 mm-security-handle 'gnus-info "Error.")
477 (setq info-is-set-p t)
480 (mm-set-handle-multipart-parameter
481 mm-security-handle 'gnus-details "Quit.")
482 (mm-set-handle-multipart-parameter
483 mm-security-handle 'gnus-info "Quit.")
484 (setq info-is-set-p t)
486 (unless info-is-set-p
487 (mm-set-handle-multipart-parameter
488 mm-security-handle 'gnus-info "Failed"))
489 (throw 'error handle)))
490 (mm-set-handle-multipart-parameter
491 mm-security-handle 'gnus-info
492 (with-current-buffer mml2015-result-buffer
493 (mml2015-gpg-extract-signature-details))))
496 (defun mml2015-gpg-clear-verify ()
497 (if (condition-case err
499 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
500 (mm-set-handle-multipart-parameter
501 mm-security-handle 'gnus-details
502 (with-current-buffer mml2015-result-buffer
505 (mm-set-handle-multipart-parameter
506 mm-security-handle 'gnus-details (mml2015-format-error err))
509 (mm-set-handle-multipart-parameter
510 mm-security-handle 'gnus-details "Quit.")
512 (mm-set-handle-multipart-parameter
513 mm-security-handle 'gnus-info
514 (with-current-buffer mml2015-result-buffer
515 (mml2015-gpg-extract-signature-details)))
516 (mm-set-handle-multipart-parameter
517 mm-security-handle 'gnus-info "Failed")))
519 (defun mml2015-gpg-sign (cont)
521 (funcall mml-boundary-function (incf mml-multipart-number)))
522 (text (current-buffer)) signature)
523 (goto-char (point-max))
527 (unless (gpg-sign-detached text (setq signature (current-buffer))
528 mml2015-result-buffer
530 (message-options-get 'message-sender)
531 t t) ; armor & textmode
532 (unless (> (point-max) (point-min))
533 (pop-to-buffer mml2015-result-buffer)
534 (error "Sign error")))
535 (goto-char (point-min))
536 (while (re-search-forward "\r+$" nil t)
537 (replace-match "" t t))
539 (goto-char (point-min))
540 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
542 ;;; FIXME: what is the micalg?
543 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
544 (insert (format "\n--%s\n" boundary))
545 (goto-char (point-max))
546 (insert (format "\n--%s\n" boundary))
547 (insert "Content-Type: application/pgp-signature\n\n")
548 (insert-buffer signature)
549 (goto-char (point-max))
550 (insert (format "--%s--\n" boundary))
551 (goto-char (point-max)))))
553 (defun mml2015-gpg-encrypt (cont)
555 (funcall mml-boundary-function (incf mml-multipart-number)))
556 (text (current-buffer))
558 (mm-with-unibyte-current-buffer-mule4
560 (unless (gpg-sign-encrypt
561 text (setq cipher (current-buffer))
562 mml2015-result-buffer
565 (message-options-get 'message-recipients)
566 (message-options-set 'message-recipients
567 (read-string "Recipients: ")))
570 (message-options-get 'message-sender)
571 t t) ; armor & textmode
572 (unless (> (point-max) (point-min))
573 (pop-to-buffer mml2015-result-buffer)
574 (error "Encrypt error")))
575 (goto-char (point-min))
576 (while (re-search-forward "\r+$" nil t)
577 (replace-match "" t t))
579 (delete-region (point-min) (point-max))
580 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
582 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
583 (insert (format "--%s\n" boundary))
584 (insert "Content-Type: application/pgp-encrypted\n\n")
585 (insert "Version: 1\n\n")
586 (insert (format "--%s\n" boundary))
587 (insert "Content-Type: application/octet-stream\n\n")
588 (insert-buffer cipher)
589 (goto-char (point-max))
590 (insert (format "--%s--\n" boundary))
591 (goto-char (point-max))))))
595 (defun mml2015-clean-buffer ()
596 (if (gnus-buffer-live-p mml2015-result-buffer)
597 (with-current-buffer mml2015-result-buffer
600 (setq mml2015-result-buffer
601 (gnus-get-buffer-create "*MML2015 Result*"))
604 (defsubst mml2015-clear-decrypt-function ()
605 (nth 6 (assq mml2015-use mml2015-function-alist)))
607 (defsubst mml2015-clear-verify-function ()
608 (nth 5 (assq mml2015-use mml2015-function-alist)))
611 (defun mml2015-decrypt (handle ctl)
612 (mml2015-clean-buffer)
613 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
615 (funcall func handle ctl)
619 (defun mml2015-decrypt-test (handle ctl)
623 (defun mml2015-verify (handle ctl)
624 (mml2015-clean-buffer)
625 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
627 (funcall func handle ctl)
631 (defun mml2015-verify-test (handle ctl)
635 (defun mml2015-encrypt (cont)
636 (mml2015-clean-buffer)
637 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
640 (error "Cannot find encrypt function"))))
643 (defun mml2015-sign (cont)
644 (mml2015-clean-buffer)
645 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
648 (error "Cannot find sign function"))))
651 (defun mml2015-self-encrypt ()
652 (mml2015-encrypt nil))
656 ;;; mml2015.el ends here