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))
29 (eval-when-compile (require 'gnus-clfns))
32 (defvar mml2015-use (or
36 (and (fboundp 'gpg-sign-detached)
40 (and (fboundp 'mc-encrypt-generic)
41 (fboundp 'mc-sign-generic)
42 (fboundp 'mc-cleanup-recipient-headers)
44 "The package used for PGP/MIME.")
46 ;; Something is not RFC2015.
47 (defvar mml2015-function-alist
48 '((mailcrypt mml2015-mailcrypt-sign
49 mml2015-mailcrypt-encrypt
50 mml2015-mailcrypt-verify
51 mml2015-mailcrypt-decrypt
52 mml2015-mailcrypt-clear-verify
53 mml2015-mailcrypt-clear-decrypt)
58 mml2015-gpg-clear-verify
59 mml2015-gpg-clear-decrypt))
60 "Alist of PGP/MIME functions.")
62 (defvar mml2015-result-buffer nil)
64 (defvar mml2015-trust-boundaries-alist
65 '((trust-undefined . nil)
70 "Trust boundaries for a signer's GnuPG key.
71 This alist contains pairs of the form (trust-symbol . boolean), with
72 symbols that are contained in `gpg-unabbrev-trust-alist'. The boolean
73 specifies whether the given trust value is good enough to be trusted
79 (autoload 'mailcrypt-decrypt "mailcrypt")
80 (autoload 'mailcrypt-verify "mailcrypt")
81 (autoload 'mc-pgp-always-sign "mailcrypt")
82 (autoload 'mc-encrypt-generic "mc-toplev")
83 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
84 (autoload 'mc-sign-generic "mc-toplev"))
87 (defvar mc-default-scheme)
90 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
91 (defvar mml2015-verify-function 'mailcrypt-verify)
93 (defun mml2015-format-error (err)
94 (if (stringp (cadr err))
96 (format "%S" (cdr err))))
98 (defun mml2015-mailcrypt-decrypt (handle ctl)
100 (let (child handles result)
101 (unless (setq child (mm-find-part-by-type
103 "application/octet-stream" nil t))
104 (mm-set-handle-multipart-parameter
105 mm-security-handle 'gnus-info "Corrupted")
106 (throw 'error handle))
108 (mm-insert-part child)
111 (funcall mml2015-decrypt-function)
113 (mm-set-handle-multipart-parameter
114 mm-security-handle 'gnus-details (mml2015-format-error err))
117 (mm-set-handle-multipart-parameter
118 mm-security-handle 'gnus-details "Quit.")
121 (mm-set-handle-multipart-parameter
122 mm-security-handle 'gnus-info "Failed")
123 (throw 'error handle))
124 (setq handles (mm-dissect-buffer t)))
125 (mm-destroy-parts handle)
126 (mm-set-handle-multipart-parameter
127 mm-security-handle 'gnus-info "OK")
128 (if (listp (car handles))
132 (defun mml2015-mailcrypt-clear-decrypt ()
136 (funcall mml2015-decrypt-function)
138 (mm-set-handle-multipart-parameter
139 mm-security-handle 'gnus-details (mml2015-format-error err))
142 (mm-set-handle-multipart-parameter
143 mm-security-handle 'gnus-details "Quit.")
146 (mm-set-handle-multipart-parameter
147 mm-security-handle 'gnus-info "OK")
148 (mm-set-handle-multipart-parameter
149 mm-security-handle 'gnus-info "Failed"))))
151 (defun mml2015-fix-micalg (alg)
153 ;; Mutt/1.2.5i has seen sending micalg=php-sha1
154 (upcase (if (string-match "^p[gh]p-" alg)
155 (substring alg (match-end 0))
158 (defun mml2015-mailcrypt-verify (handle ctl)
161 (unless (setq part (mm-find-raw-part-by-type
162 ctl (or (mm-handle-multipart-ctl-parameter
164 "application/pgp-signature")
166 (mm-set-handle-multipart-parameter
167 mm-security-handle 'gnus-info "Corrupted")
168 (throw 'error handle))
170 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
171 (insert (format "Hash: %s\n\n"
172 (or (mml2015-fix-micalg
173 (mm-handle-multipart-ctl-parameter
177 (narrow-to-region (point) (point))
179 (goto-char (point-min))
181 (if (looking-at "^-")
184 (unless (setq part (mm-find-part-by-type
185 (cdr handle) "application/pgp-signature" nil t))
186 (mm-set-handle-multipart-parameter
187 mm-security-handle 'gnus-info "Corrupted")
188 (throw 'error handle))
190 (narrow-to-region (point) (point))
191 (mm-insert-part part)
192 (goto-char (point-min))
193 (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
194 (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
195 (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
196 (replace-match "-----END PGP SIGNATURE-----" t t)))
197 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
198 (unless (condition-case err
200 (funcall mml2015-verify-function)
201 (if (get-buffer " *mailcrypt stderr temp")
202 (mm-set-handle-multipart-parameter
203 mm-security-handle 'gnus-details
204 (with-current-buffer " *mailcrypt stderr temp"
206 (if (get-buffer " *mailcrypt stdout temp")
207 (kill-buffer " *mailcrypt stdout temp"))
208 (if (get-buffer " *mailcrypt stderr temp")
209 (kill-buffer " *mailcrypt stderr temp"))
210 (if (get-buffer " *mailcrypt status temp")
211 (kill-buffer " *mailcrypt status temp"))
212 (if (get-buffer mc-gpg-debug-buffer)
213 (kill-buffer mc-gpg-debug-buffer)))
215 (mm-set-handle-multipart-parameter
216 mm-security-handle 'gnus-details (mml2015-format-error err))
219 (mm-set-handle-multipart-parameter
220 mm-security-handle 'gnus-details "Quit.")
222 (mm-set-handle-multipart-parameter
223 mm-security-handle 'gnus-info "Failed")
224 (throw 'error handle))))
225 (mm-set-handle-multipart-parameter
226 mm-security-handle 'gnus-info "OK")
229 (defun mml2015-mailcrypt-clear-verify ()
230 (let ((mc-gpg-debug-buffer (get-buffer-create " *gnus gpg debug*")))
231 (if (condition-case err
233 (funcall mml2015-verify-function)
234 (if (get-buffer " *mailcrypt stderr temp")
235 (mm-set-handle-multipart-parameter
236 mm-security-handle 'gnus-details
237 (with-current-buffer " *mailcrypt stderr temp"
239 (if (get-buffer " *mailcrypt stdout temp")
240 (kill-buffer " *mailcrypt stdout temp"))
241 (if (get-buffer " *mailcrypt stderr temp")
242 (kill-buffer " *mailcrypt stderr temp"))
243 (if (get-buffer " *mailcrypt status temp")
244 (kill-buffer " *mailcrypt status temp"))
245 (if (get-buffer mc-gpg-debug-buffer)
246 (kill-buffer mc-gpg-debug-buffer)))
248 (mm-set-handle-multipart-parameter
249 mm-security-handle 'gnus-details (mml2015-format-error err))
252 (mm-set-handle-multipart-parameter
253 mm-security-handle 'gnus-details "Quit.")
255 (mm-set-handle-multipart-parameter
256 mm-security-handle 'gnus-info "OK")
257 (mm-set-handle-multipart-parameter
258 mm-security-handle 'gnus-info "Failed"))))
260 (defun mml2015-mailcrypt-sign (cont)
261 (mc-sign-generic (message-options-get 'message-sender)
264 (funcall mml-boundary-function (incf mml-multipart-number)))
266 (goto-char (point-min))
267 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
268 (error "Cannot find signed begin line"))
269 (goto-char (match-beginning 0))
271 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
272 (error "Cannot not find PGP hash"))
273 (setq hash (match-string 1))
274 (unless (re-search-forward "^$" nil t)
275 (error "Cannot not find PGP message"))
277 (delete-region (point-min) (point))
278 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
280 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
282 (insert (format "\n--%s\n" boundary))
284 (goto-char (point-max))
285 (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
286 (error "Cannot find signature part"))
287 (replace-match "-----END PGP MESSAGE-----" t t)
288 (goto-char (match-beginning 0))
289 (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$"
291 (error "Cannot find signature part"))
292 (replace-match "-----BEGIN PGP MESSAGE-----" t t)
293 (goto-char (match-beginning 0))
295 (narrow-to-region point (point))
297 (while (re-search-forward "^- -" nil t)
298 (replace-match "-" t t))
299 (goto-char (point-max)))
300 (insert (format "--%s\n" boundary))
301 (insert "Content-Type: application/pgp-signature\n\n")
302 (goto-char (point-max))
303 (insert (format "--%s--\n" boundary))
304 (goto-char (point-max))))
306 (defun mml2015-mailcrypt-encrypt (cont)
307 (let ((mc-pgp-always-sign
308 (or mc-pgp-always-sign
309 (eq t (or (message-options-get 'message-sign-encrypt)
311 'message-sign-encrypt
312 (or (y-or-n-p "Sign the message? ")
315 (mm-with-unibyte-current-buffer-mule4
317 (or (message-options-get 'message-recipients)
318 (message-options-set 'message-recipients
319 (mc-cleanup-recipient-headers
320 (read-string "Recipients: "))))
322 (message-options-get 'message-sender))))
323 (goto-char (point-min))
324 (unless (looking-at "-----BEGIN PGP MESSAGE-----")
325 (error "Fail to encrypt the message"))
327 (funcall mml-boundary-function (incf mml-multipart-number))))
328 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
330 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
331 (insert (format "--%s\n" boundary))
332 (insert "Content-Type: application/pgp-encrypted\n\n")
333 (insert "Version: 1\n\n")
334 (insert (format "--%s\n" boundary))
335 (insert "Content-Type: application/octet-stream\n\n")
336 (goto-char (point-max))
337 (insert (format "--%s--\n" boundary))
338 (goto-char (point-max))))
343 (autoload 'gpg-decrypt "gpg")
344 (autoload 'gpg-verify "gpg")
345 (autoload 'gpg-verify-cleartext "gpg")
346 (autoload 'gpg-sign-detached "gpg")
347 (autoload 'gpg-sign-encrypt "gpg")
348 (autoload 'gpg-passphrase-read "gpg"))
350 (defun mml2015-gpg-passphrase ()
351 (or (message-options-get 'gpg-passphrase)
352 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
354 (defun mml2015-gpg-decrypt-1 ()
355 (let ((cipher (current-buffer)) plain result)
356 (if (with-temp-buffer
358 (gpg-decrypt cipher (setq plain (current-buffer))
359 mml2015-result-buffer nil)
360 (mm-set-handle-multipart-parameter
361 mm-security-handle 'gnus-details
362 (with-current-buffer mml2015-result-buffer
366 (insert-buffer plain)
367 (goto-char (point-min))
368 (while (search-forward "\r\n" nil t)
369 (replace-match "\n" t t))))
371 ;; Some wrong with the return value, check plain text buffer.
372 (if (> (point-max) (point-min))
376 (defun mml2015-gpg-decrypt (handle ctl)
377 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
378 (mml2015-mailcrypt-decrypt handle ctl)))
380 (defun mml2015-gpg-clear-decrypt ()
382 (setq result (mml2015-gpg-decrypt-1))
384 (mm-set-handle-multipart-parameter
385 mm-security-handle 'gnus-info "OK")
386 (mm-set-handle-multipart-parameter
387 mm-security-handle 'gnus-info "Failed"))))
389 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
391 (fpr-length (string-width fingerprint))
394 (setq fingerprint (string-to-list fingerprint))
396 (setq fpr-length (- fpr-length 4))
397 (setq slice (butlast fingerprint fpr-length))
398 (setq fingerprint (nthcdr 4 fingerprint))
399 (setq n-slice (1+ n-slice))
405 (otherwise (concat " " slice))))))
408 (defun mml2015-gpg-extract-signature-details ()
409 (goto-char (point-min))
410 (if (boundp 'gpg-unabbrev-trust-alist)
411 (let* ((signer (and (re-search-forward
412 "^\\[GNUPG:\\] GOODSIG [0-9A-Za-z]* \\(.*\\)$"
415 (fprint (and (re-search-forward
416 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
419 (trust (and (re-search-forward "^\\[GNUPG:\\] \\(TRUST_.*\\)$" nil t)
422 (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
423 mml2015-trust-boundaries-alist))))
424 (if (and signer trust fprint)
426 (unless trust-good-enough-p
427 (concat "\nUntrusted, Fingerprint: "
428 (mml2015-gpg-pretty-print-fpr fprint))))
429 "From unknown user"))
430 (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
432 "From unknown user")))
434 (defun mml2015-gpg-verify (handle ctl)
436 (let (part message signature info-is-set-p)
437 (unless (setq part (mm-find-raw-part-by-type
438 ctl (or (mm-handle-multipart-ctl-parameter
440 "application/pgp-signature")
442 (mm-set-handle-multipart-parameter
443 mm-security-handle 'gnus-info "Corrupted")
444 (throw 'error handle))
446 (setq message (current-buffer))
448 ;; Convert <LF> to <CR><LF> in verify mode. Sign and
449 ;; clearsign use --textmode. The conversion is not necessary.
450 ;; In clearverify, the conversion is not necessary either.
451 (goto-char (point-min))
454 (unless (eq (char-before) ?\r)
459 (setq signature (current-buffer))
460 (unless (setq part (mm-find-part-by-type
461 (cdr handle) "application/pgp-signature" nil t))
462 (mm-set-handle-multipart-parameter
463 mm-security-handle 'gnus-info "Corrupted")
464 (throw 'error handle))
465 (mm-insert-part part)
466 (unless (condition-case err
468 (gpg-verify message signature mml2015-result-buffer)
469 (mm-set-handle-multipart-parameter
470 mm-security-handle 'gnus-details
471 (with-current-buffer mml2015-result-buffer
474 (mm-set-handle-multipart-parameter
475 mm-security-handle 'gnus-details (mml2015-format-error err))
476 (mm-set-handle-multipart-parameter
477 mm-security-handle 'gnus-info "Error.")
478 (setq info-is-set-p t)
481 (mm-set-handle-multipart-parameter
482 mm-security-handle 'gnus-details "Quit.")
483 (mm-set-handle-multipart-parameter
484 mm-security-handle 'gnus-info "Quit.")
485 (setq info-is-set-p t)
487 (unless info-is-set-p
488 (mm-set-handle-multipart-parameter
489 mm-security-handle 'gnus-info "Failed"))
490 (throw 'error handle)))
491 (mm-set-handle-multipart-parameter
492 mm-security-handle 'gnus-info
493 (with-current-buffer mml2015-result-buffer
494 (mml2015-gpg-extract-signature-details))))
497 (defun mml2015-gpg-clear-verify ()
498 (if (condition-case err
500 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
501 (mm-set-handle-multipart-parameter
502 mm-security-handle 'gnus-details
503 (with-current-buffer mml2015-result-buffer
506 (mm-set-handle-multipart-parameter
507 mm-security-handle 'gnus-details (mml2015-format-error err))
510 (mm-set-handle-multipart-parameter
511 mm-security-handle 'gnus-details "Quit.")
513 (mm-set-handle-multipart-parameter
514 mm-security-handle 'gnus-info
515 (with-current-buffer mml2015-result-buffer
516 (mml2015-gpg-extract-signature-details)))
517 (mm-set-handle-multipart-parameter
518 mm-security-handle 'gnus-info "Failed")))
520 (defun mml2015-gpg-sign (cont)
522 (funcall mml-boundary-function (incf mml-multipart-number)))
523 (text (current-buffer)) signature)
524 (goto-char (point-max))
528 (unless (gpg-sign-detached text (setq signature (current-buffer))
529 mml2015-result-buffer
531 (message-options-get 'message-sender)
532 t t) ; armor & textmode
533 (unless (> (point-max) (point-min))
534 (pop-to-buffer mml2015-result-buffer)
535 (error "Sign error")))
536 (goto-char (point-min))
537 (while (re-search-forward "\r+$" nil t)
538 (replace-match "" t t))
540 (goto-char (point-min))
541 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
543 ;;; FIXME: what is the micalg?
544 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
545 (insert (format "\n--%s\n" boundary))
546 (goto-char (point-max))
547 (insert (format "\n--%s\n" boundary))
548 (insert "Content-Type: application/pgp-signature\n\n")
549 (insert-buffer signature)
550 (goto-char (point-max))
551 (insert (format "--%s--\n" boundary))
552 (goto-char (point-max)))))
554 (defun mml2015-gpg-encrypt (cont)
556 (funcall mml-boundary-function (incf mml-multipart-number)))
557 (text (current-buffer))
559 (mm-with-unibyte-current-buffer-mule4
561 (unless (gpg-sign-encrypt
562 text (setq cipher (current-buffer))
563 mml2015-result-buffer
566 (message-options-get 'message-recipients)
567 (message-options-set 'message-recipients
568 (read-string "Recipients: ")))
571 (message-options-get 'message-sender)
572 t t) ; armor & textmode
573 (unless (> (point-max) (point-min))
574 (pop-to-buffer mml2015-result-buffer)
575 (error "Encrypt error")))
576 (goto-char (point-min))
577 (while (re-search-forward "\r+$" nil t)
578 (replace-match "" t t))
580 (delete-region (point-min) (point-max))
581 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
583 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
584 (insert (format "--%s\n" boundary))
585 (insert "Content-Type: application/pgp-encrypted\n\n")
586 (insert "Version: 1\n\n")
587 (insert (format "--%s\n" boundary))
588 (insert "Content-Type: application/octet-stream\n\n")
589 (insert-buffer cipher)
590 (goto-char (point-max))
591 (insert (format "--%s--\n" boundary))
592 (goto-char (point-max))))))
596 (defun mml2015-clean-buffer ()
597 (if (gnus-buffer-live-p mml2015-result-buffer)
598 (with-current-buffer mml2015-result-buffer
601 (setq mml2015-result-buffer
602 (gnus-get-buffer-create "*MML2015 Result*"))
605 (defsubst mml2015-clear-decrypt-function ()
606 (nth 6 (assq mml2015-use mml2015-function-alist)))
608 (defsubst mml2015-clear-verify-function ()
609 (nth 5 (assq mml2015-use mml2015-function-alist)))
612 (defun mml2015-decrypt (handle ctl)
613 (mml2015-clean-buffer)
614 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
616 (funcall func handle ctl)
620 (defun mml2015-decrypt-test (handle ctl)
624 (defun mml2015-verify (handle ctl)
625 (mml2015-clean-buffer)
626 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
628 (funcall func handle ctl)
632 (defun mml2015-verify-test (handle ctl)
636 (defun mml2015-encrypt (cont)
637 (mml2015-clean-buffer)
638 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
641 (error "Cannot find encrypt function"))))
644 (defun mml2015-sign (cont)
645 (mml2015-clean-buffer)
646 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
649 (error "Cannot find sign function"))))
652 (defun mml2015-self-encrypt ()
653 (mml2015-encrypt nil))
657 ;;; mml2015.el ends here