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))
449 (setq signature (current-buffer))
450 (unless (setq part (mm-find-part-by-type
451 (cdr handle) "application/pgp-signature" nil t))
452 (mm-set-handle-multipart-parameter
453 mm-security-handle 'gnus-info "Corrupted")
454 (throw 'error handle))
455 (mm-insert-part part)
456 (unless (condition-case err
458 (gpg-verify message signature mml2015-result-buffer)
459 (mm-set-handle-multipart-parameter
460 mm-security-handle 'gnus-details
461 (with-current-buffer mml2015-result-buffer
464 (mm-set-handle-multipart-parameter
465 mm-security-handle 'gnus-details (mml2015-format-error err))
466 (mm-set-handle-multipart-parameter
467 mm-security-handle 'gnus-info "Error.")
468 (setq info-is-set-p t)
471 (mm-set-handle-multipart-parameter
472 mm-security-handle 'gnus-details "Quit.")
473 (mm-set-handle-multipart-parameter
474 mm-security-handle 'gnus-info "Quit.")
475 (setq info-is-set-p t)
477 (unless info-is-set-p
478 (mm-set-handle-multipart-parameter
479 mm-security-handle 'gnus-info "Failed"))
480 (throw 'error handle)))
481 (mm-set-handle-multipart-parameter
482 mm-security-handle 'gnus-info
483 (with-current-buffer mml2015-result-buffer
484 (mml2015-gpg-extract-signature-details))))
487 (defun mml2015-gpg-clear-verify ()
488 (if (condition-case err
490 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
491 (mm-set-handle-multipart-parameter
492 mm-security-handle 'gnus-details
493 (with-current-buffer mml2015-result-buffer
496 (mm-set-handle-multipart-parameter
497 mm-security-handle 'gnus-details (mml2015-format-error err))
500 (mm-set-handle-multipart-parameter
501 mm-security-handle 'gnus-details "Quit.")
503 (mm-set-handle-multipart-parameter
504 mm-security-handle 'gnus-info
505 (with-current-buffer mml2015-result-buffer
506 (mml2015-gpg-extract-signature-details)))
507 (mm-set-handle-multipart-parameter
508 mm-security-handle 'gnus-info "Failed")))
510 (defun mml2015-gpg-sign (cont)
512 (funcall mml-boundary-function (incf mml-multipart-number)))
513 (text (current-buffer)) signature)
514 (goto-char (point-max))
518 (unless (gpg-sign-detached text (setq signature (current-buffer))
519 mml2015-result-buffer
521 (message-options-get 'message-sender)
522 t t) ; armor & textmode
523 (unless (> (point-max) (point-min))
524 (pop-to-buffer mml2015-result-buffer)
525 (error "Sign error")))
526 (goto-char (point-min))
527 (while (re-search-forward "\r+$" nil t)
528 (replace-match "" t t))
530 (goto-char (point-min))
531 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
533 ;;; FIXME: what is the micalg?
534 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
535 (insert (format "\n--%s\n" boundary))
536 (goto-char (point-max))
537 (insert (format "\n--%s\n" boundary))
538 (insert "Content-Type: application/pgp-signature\n\n")
539 (insert-buffer signature)
540 (goto-char (point-max))
541 (insert (format "--%s--\n" boundary))
542 (goto-char (point-max)))))
544 (defun mml2015-gpg-encrypt (cont)
546 (funcall mml-boundary-function (incf mml-multipart-number)))
547 (text (current-buffer))
549 (mm-with-unibyte-current-buffer-mule4
551 (unless (gpg-sign-encrypt
552 text (setq cipher (current-buffer))
553 mml2015-result-buffer
556 (message-options-get 'message-recipients)
557 (message-options-set 'message-recipients
558 (read-string "Recipients: ")))
561 (message-options-get 'message-sender)
562 t t) ; armor & textmode
563 (unless (> (point-max) (point-min))
564 (pop-to-buffer mml2015-result-buffer)
565 (error "Encrypt error")))
566 (goto-char (point-min))
567 (while (re-search-forward "\r+$" nil t)
568 (replace-match "" t t))
570 (delete-region (point-min) (point-max))
571 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
573 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
574 (insert (format "--%s\n" boundary))
575 (insert "Content-Type: application/pgp-encrypted\n\n")
576 (insert "Version: 1\n\n")
577 (insert (format "--%s\n" boundary))
578 (insert "Content-Type: application/octet-stream\n\n")
579 (insert-buffer cipher)
580 (goto-char (point-max))
581 (insert (format "--%s--\n" boundary))
582 (goto-char (point-max))))))
586 (defun mml2015-clean-buffer ()
587 (if (gnus-buffer-live-p mml2015-result-buffer)
588 (with-current-buffer mml2015-result-buffer
591 (setq mml2015-result-buffer
592 (gnus-get-buffer-create "*MML2015 Result*"))
595 (defsubst mml2015-clear-decrypt-function ()
596 (nth 6 (assq mml2015-use mml2015-function-alist)))
598 (defsubst mml2015-clear-verify-function ()
599 (nth 5 (assq mml2015-use mml2015-function-alist)))
602 (defun mml2015-decrypt (handle ctl)
603 (mml2015-clean-buffer)
604 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
606 (funcall func handle ctl)
610 (defun mml2015-decrypt-test (handle ctl)
614 (defun mml2015-verify (handle ctl)
615 (mml2015-clean-buffer)
616 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
618 (funcall func handle ctl)
622 (defun mml2015-verify-test (handle ctl)
626 (defun mml2015-encrypt (cont)
627 (mml2015-clean-buffer)
628 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
631 (error "Cannot find encrypt function"))))
634 (defun mml2015-sign (cont)
635 (mml2015-clean-buffer)
636 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
639 (error "Cannot find sign function"))))
642 (defun mml2015-self-encrypt ()
643 (mml2015-encrypt nil))
647 ;;; mml2015.el ends here