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 (upcase (if (string-match "^pgp-" 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)))
367 ;; Some wrong with the return value, check plain text buffer.
368 (if (> (point-max) (point-min))
372 (defun mml2015-gpg-decrypt (handle ctl)
373 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
374 (mml2015-mailcrypt-decrypt handle ctl)))
376 (defun mml2015-gpg-clear-decrypt ()
378 (setq result (mml2015-gpg-decrypt-1))
380 (mm-set-handle-multipart-parameter
381 mm-security-handle 'gnus-info "OK")
382 (mm-set-handle-multipart-parameter
383 mm-security-handle 'gnus-info "Failed"))))
385 (defun mml2015-gpg-pretty-print-fpr (fingerprint)
387 (fpr-length (string-width fingerprint))
390 (setq fingerprint (string-to-list fingerprint))
392 (setq fpr-length (- fpr-length 4))
393 (setq slice (butlast fingerprint fpr-length))
394 (setq fingerprint (nthcdr 4 fingerprint))
395 (setq n-slice (1+ n-slice))
401 (otherwise (concat " " slice))))))
404 (defun mml2015-gpg-extract-signature-details ()
405 (goto-char (point-min))
406 (if (boundp 'gpg-unabbrev-trust-alist)
407 (let* ((signer (and (re-search-forward
408 "^\\[GNUPG:\\] GOODSIG [0-9A-Za-z]* \\(.*\\)$"
411 (fprint (and (re-search-forward
412 "^\\[GNUPG:\\] VALIDSIG \\([0-9a-zA-Z]*\\) "
415 (trust (and (re-search-forward "^\\[GNUPG:\\] \\(TRUST_.*\\)$" nil t)
418 (cdr (assoc (cdr (assoc trust gpg-unabbrev-trust-alist))
419 mml2015-trust-boundaries-alist))))
420 (if (and signer trust fprint)
422 (unless trust-good-enough-p
423 (concat "\nUntrusted, Fingerprint: "
424 (mml2015-gpg-pretty-print-fpr fprint))))
425 "From unknown user"))
426 (if (re-search-forward "^gpg: Good signature from \"\\(.*\\)\"$" nil t)
428 "From unknown user")))
430 (defun mml2015-gpg-verify (handle ctl)
432 (let (part message signature info-is-set-p)
433 (unless (setq part (mm-find-raw-part-by-type
434 ctl (or (mm-handle-multipart-ctl-parameter
436 "application/pgp-signature")
438 (mm-set-handle-multipart-parameter
439 mm-security-handle 'gnus-info "Corrupted")
440 (throw 'error handle))
442 (setq message (current-buffer))
445 (setq signature (current-buffer))
446 (unless (setq part (mm-find-part-by-type
447 (cdr handle) "application/pgp-signature" nil t))
448 (mm-set-handle-multipart-parameter
449 mm-security-handle 'gnus-info "Corrupted")
450 (throw 'error handle))
451 (mm-insert-part part)
452 (unless (condition-case err
454 (gpg-verify message signature mml2015-result-buffer)
455 (mm-set-handle-multipart-parameter
456 mm-security-handle 'gnus-details
457 (with-current-buffer mml2015-result-buffer
460 (mm-set-handle-multipart-parameter
461 mm-security-handle 'gnus-details (mml2015-format-error err))
462 (mm-set-handle-multipart-parameter
463 mm-security-handle 'gnus-info "Error.")
464 (setq info-is-set-p t)
467 (mm-set-handle-multipart-parameter
468 mm-security-handle 'gnus-details "Quit.")
469 (mm-set-handle-multipart-parameter
470 mm-security-handle 'gnus-info "Quit.")
471 (setq info-is-set-p t)
473 (unless info-is-set-p
474 (mm-set-handle-multipart-parameter
475 mm-security-handle 'gnus-info "Failed"))
476 (throw 'error handle)))
477 (mm-set-handle-multipart-parameter
478 mm-security-handle 'gnus-info
479 (with-current-buffer mml2015-result-buffer
480 (mml2015-gpg-extract-signature-details))))
483 (defun mml2015-gpg-clear-verify ()
484 (if (condition-case err
486 (gpg-verify-cleartext (current-buffer) mml2015-result-buffer)
487 (mm-set-handle-multipart-parameter
488 mm-security-handle 'gnus-details
489 (with-current-buffer mml2015-result-buffer
492 (mm-set-handle-multipart-parameter
493 mm-security-handle 'gnus-details (mml2015-format-error err))
496 (mm-set-handle-multipart-parameter
497 mm-security-handle 'gnus-details "Quit.")
499 (mm-set-handle-multipart-parameter
500 mm-security-handle 'gnus-info
501 (with-current-buffer mml2015-result-buffer
502 (mml2015-gpg-extract-signature-details)))
503 (mm-set-handle-multipart-parameter
504 mm-security-handle 'gnus-info "Failed")))
506 (defun mml2015-gpg-sign (cont)
508 (funcall mml-boundary-function (incf mml-multipart-number)))
509 (text (current-buffer)) signature)
510 (goto-char (point-max))
514 (unless (gpg-sign-detached text (setq signature (current-buffer))
515 mml2015-result-buffer
517 (message-options-get 'message-sender)
518 t t) ; armor & textmode
519 (unless (> (point-max) (point-min))
520 (pop-to-buffer mml2015-result-buffer)
521 (error "Sign error")))
522 (goto-char (point-min))
523 (while (re-search-forward "\r+$" nil t)
524 (replace-match "" t t))
526 (goto-char (point-min))
527 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
529 ;;; FIXME: what is the micalg?
530 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
531 (insert (format "\n--%s\n" boundary))
532 (goto-char (point-max))
533 (insert (format "\n--%s\n" boundary))
534 (insert "Content-Type: application/pgp-signature\n\n")
535 (insert-buffer signature)
536 (goto-char (point-max))
537 (insert (format "--%s--\n" boundary))
538 (goto-char (point-max)))))
540 (defun mml2015-gpg-encrypt (cont)
542 (funcall mml-boundary-function (incf mml-multipart-number)))
543 (text (current-buffer))
545 (mm-with-unibyte-current-buffer-mule4
547 (unless (gpg-sign-encrypt
548 text (setq cipher (current-buffer))
549 mml2015-result-buffer
552 (message-options-get 'message-recipients)
553 (message-options-set 'message-recipients
554 (read-string "Recipients: ")))
557 (message-options-get 'message-sender)
558 t t) ; armor & textmode
559 (unless (> (point-max) (point-min))
560 (pop-to-buffer mml2015-result-buffer)
561 (error "Encrypt error")))
562 (goto-char (point-min))
563 (while (re-search-forward "\r+$" nil t)
564 (replace-match "" t t))
566 (delete-region (point-min) (point-max))
567 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
569 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
570 (insert (format "--%s\n" boundary))
571 (insert "Content-Type: application/pgp-encrypted\n\n")
572 (insert "Version: 1\n\n")
573 (insert (format "--%s\n" boundary))
574 (insert "Content-Type: application/octet-stream\n\n")
575 (insert-buffer cipher)
576 (goto-char (point-max))
577 (insert (format "--%s--\n" boundary))
578 (goto-char (point-max))))))
582 (defun mml2015-clean-buffer ()
583 (if (gnus-buffer-live-p mml2015-result-buffer)
584 (with-current-buffer mml2015-result-buffer
587 (setq mml2015-result-buffer
588 (gnus-get-buffer-create "*MML2015 Result*"))
591 (defsubst mml2015-clear-decrypt-function ()
592 (nth 6 (assq mml2015-use mml2015-function-alist)))
594 (defsubst mml2015-clear-verify-function ()
595 (nth 5 (assq mml2015-use mml2015-function-alist)))
598 (defun mml2015-decrypt (handle ctl)
599 (mml2015-clean-buffer)
600 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
602 (funcall func handle ctl)
606 (defun mml2015-decrypt-test (handle ctl)
610 (defun mml2015-verify (handle ctl)
611 (mml2015-clean-buffer)
612 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
614 (funcall func handle ctl)
618 (defun mml2015-verify-test (handle ctl)
622 (defun mml2015-encrypt (cont)
623 (mml2015-clean-buffer)
624 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
627 (error "Cannot find encrypt function"))))
630 (defun mml2015-sign (cont)
631 (mml2015-clean-buffer)
632 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
635 (error "Cannot find sign function"))))
638 (defun mml2015-self-encrypt ()
639 (mml2015-encrypt nil))
643 ;;; mml2015.el ends here