Synch with Gnus.
[elisp/gnus.git-] / lisp / mml2015.el
1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: PGP MIME MML
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29 (require 'mm-decode)
30
31 (defvar mml2015-use (or (progn (ignore-errors
32                                  (load "mc-toplev"))
33                                (and (fboundp 'mc-encrypt-generic)
34                                     (fboundp 'mc-sign-generic)
35                                     (fboundp 'mc-cleanup-recipient-headers)
36                                     'mailcrypt))
37                         (progn
38                           (ignore-errors
39                             (require 'gpg))
40                           (and (fboundp 'gpg-sign-detached)
41                                'gpg)))
42   "The package used for PGP/MIME.")
43
44 ;; Something is not RFC2015.
45 (defvar mml2015-function-alist
46   '((mailcrypt mml2015-mailcrypt-sign
47                mml2015-mailcrypt-encrypt
48                mml2015-mailcrypt-verify
49                mml2015-mailcrypt-decrypt
50                mml2015-mailcrypt-clear-verify
51                mml2015-mailcrypt-clear-decrypt) 
52     (gpg mml2015-gpg-sign
53          mml2015-gpg-encrypt
54          mml2015-gpg-verify
55          mml2015-gpg-decrypt
56          nil
57          mml2015-gpg-clear-decrypt))
58   "Alist of PGP/MIME functions.")
59
60 (defvar mml2015-result-buffer nil)
61
62 ;;; mailcrypt wrapper
63
64 (eval-and-compile
65   (autoload 'mailcrypt-decrypt "mailcrypt")
66   (autoload 'mailcrypt-verify "mailcrypt")
67   (autoload 'mc-pgp-always-sign "mailcrypt")
68   (autoload 'mc-encrypt-generic "mc-toplev")
69   (autoload 'mc-cleanup-recipient-headers "mc-toplev")
70   (autoload 'mc-sign-generic "mc-toplev"))
71
72 (eval-when-compile
73   (defvar mc-default-scheme)
74   (defvar mc-schemes))
75
76 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
77 (defvar mml2015-verify-function 'mailcrypt-verify)
78
79 (defun mml2015-mailcrypt-decrypt (handle ctl)
80   (catch 'error
81     (let (child handles result)
82       (unless (setq child (mm-find-part-by-type 
83                            (cdr handle) 
84                            "application/octet-stream" nil t))
85         (mm-set-handle-multipart-parameter 
86          mm-security-handle 'gnus-info "Corrupted")
87         (throw 'error handle))
88       (with-temp-buffer
89         (mm-insert-part child)
90         (setq result 
91               (condition-case err
92                   (funcall mml2015-decrypt-function)
93                 (error 
94                  (mm-set-handle-multipart-parameter 
95                   mm-security-handle 'gnus-details (cadr err)) 
96                  nil)
97                 (quit
98                  (mm-set-handle-multipart-parameter 
99                   mm-security-handle 'gnus-details "Quit.") 
100                  nil)))
101         (unless (car result)
102           (mm-set-handle-multipart-parameter 
103            mm-security-handle 'gnus-info "Failed")
104           (throw 'error handle))
105         (setq handles (mm-dissect-buffer t)))
106       (mm-destroy-parts handle)
107       (mm-set-handle-multipart-parameter 
108        mm-security-handle 'gnus-info "OK")
109       (if (listp (car handles))
110           handles
111         (list handles)))))
112
113 (defun mml2015-mailcrypt-clear-decrypt ()
114   (let (result)
115     (setq result 
116           (condition-case err
117               (funcall mml2015-decrypt-function)
118             (error 
119              (mm-set-handle-multipart-parameter 
120               mm-security-handle 'gnus-details (cadr err)) 
121              nil)
122             (quit
123              (mm-set-handle-multipart-parameter 
124               mm-security-handle 'gnus-details "Quit.") 
125              nil)))
126     (if (car result)
127         (mm-set-handle-multipart-parameter 
128          mm-security-handle 'gnus-info "OK")
129       (mm-set-handle-multipart-parameter 
130        mm-security-handle 'gnus-info "Failed"))))
131
132 (defun mml2015-fix-micalg (alg)
133   (upcase
134    (if (and alg (string-match "^pgp-" alg))
135        (substring alg (match-end 0))
136      alg)))
137
138 (defun mml2015-mailcrypt-verify (handle ctl)
139   (catch 'error
140     (let (part)
141       (unless (setq part (mm-find-raw-part-by-type 
142                           ctl (or (mail-content-type-get ctl 'protocol)
143                                   "application/pgp-signature")
144                           t))
145         (mm-set-handle-multipart-parameter 
146          mm-security-handle 'gnus-info "Corrupted")
147         (throw 'error handle))
148       (with-temp-buffer
149         (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
150         (insert (format "Hash: %s\n\n" 
151                         (or (mml2015-fix-micalg
152                              (mail-content-type-get ctl 'micalg))
153                             "SHA1")))
154         (save-restriction
155           (narrow-to-region (point) (point))
156           (insert part "\n")
157           (goto-char (point-min))
158           (while (not (eobp))
159             (if (looking-at "^-")
160                 (insert "- "))
161             (forward-line)))
162         (unless (setq part (mm-find-part-by-type 
163                             (cdr handle) "application/pgp-signature" nil t))
164           (mm-set-handle-multipart-parameter 
165            mm-security-handle 'gnus-info "Corrupted")
166           (throw 'error handle))
167         (save-restriction
168           (narrow-to-region (point) (point))
169           (mm-insert-part part)
170           (goto-char (point-min))
171           (if (re-search-forward "^-----BEGIN PGP [^-]+-----\r?$" nil t)
172               (replace-match "-----BEGIN PGP SIGNATURE-----" t t))
173           (if (re-search-forward "^-----END PGP [^-]+-----\r?$" nil t)
174               (replace-match "-----END PGP SIGNATURE-----" t t)))
175         (unless (condition-case err
176                     (funcall mml2015-verify-function)
177                   (error 
178                    (mm-set-handle-multipart-parameter 
179                     mm-security-handle 'gnus-details (cadr err)) 
180                    nil)
181                   (quit
182                    (mm-set-handle-multipart-parameter 
183                     mm-security-handle 'gnus-details "Quit.") 
184                    nil))
185           (mm-set-handle-multipart-parameter 
186            mm-security-handle 'gnus-info "Failed")
187           (throw 'error handle)))
188       (mm-set-handle-multipart-parameter 
189        mm-security-handle 'gnus-info "OK")
190       handle)))
191
192 (defun mml2015-mailcrypt-clear-verify ()
193   (if (condition-case err
194           (funcall mml2015-verify-function)
195         (error 
196          (mm-set-handle-multipart-parameter 
197           mm-security-handle 'gnus-details (cadr err)) 
198          nil)
199         (quit
200          (mm-set-handle-multipart-parameter 
201           mm-security-handle 'gnus-details "Quit.") 
202          nil))
203       (mm-set-handle-multipart-parameter 
204        mm-security-handle 'gnus-info "OK")
205     (mm-set-handle-multipart-parameter 
206      mm-security-handle 'gnus-info "Failed")))
207
208 (defun mml2015-mailcrypt-sign (cont)
209   (mc-sign-generic (message-options-get 'message-sender)
210                    nil nil nil nil)
211   (let ((boundary 
212          (funcall mml-boundary-function (incf mml-multipart-number)))
213         hash point)
214     (goto-char (point-min))
215     (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
216       (error "Cannot find signed begin line." ))
217     (goto-char (match-beginning 0))
218     (forward-line 1)
219     (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
220       (error "Cannot not find PGP hash." ))
221     (setq hash (match-string 1))
222     (unless (re-search-forward "^$" nil t)
223       (error "Cannot not find PGP message." ))
224     (forward-line 1)
225     (delete-region (point-min) (point))
226     (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
227                     boundary))
228     (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
229                     (downcase hash)))
230     (insert (format "\n--%s\n" boundary))
231     (setq point (point))
232     (goto-char (point-max))
233     (unless (re-search-backward "^-----END PGP SIGNATURE-----\r?$" nil t)
234       (error "Cannot find signature part." ))
235     (replace-match "-----END PGP MESSAGE-----" t t)
236     (goto-char (match-beginning 0))
237     (unless (re-search-backward "^-----BEGIN PGP SIGNATURE-----\r?$" 
238                                 nil t)
239       (error "Cannot find signature part." ))
240     (replace-match "-----BEGIN PGP MESSAGE-----" t t)
241     (goto-char (match-beginning 0))
242     (save-restriction
243       (narrow-to-region point (point))
244       (goto-char point)
245       (while (re-search-forward "^- -" nil t)
246         (replace-match "-" t t))
247       (goto-char (point-max)))
248     (insert (format "--%s\n" boundary))
249     (insert "Content-Type: application/pgp-signature\n\n")
250     (goto-char (point-max))
251     (insert (format "--%s--\n" boundary))
252     (goto-char (point-max))))
253
254 (defun mml2015-mailcrypt-encrypt (cont)
255   (let ((mc-pgp-always-sign
256          (or mc-pgp-always-sign
257              (eq t (or (message-options-get 'message-sign-encrypt)
258                        (message-options-set 
259                         'message-sign-encrypt
260                         (or (y-or-n-p "Sign the message? ")
261                             'not))))
262              'never)))
263     (mc-encrypt-generic 
264      (or (message-options-get 'message-recipients)
265          (message-options-set 'message-recipients
266                               (mc-cleanup-recipient-headers 
267                                (read-string "Recipients: "))))
268      nil nil nil
269      (message-options-get 'message-sender)))
270   (let ((boundary 
271          (funcall mml-boundary-function (incf mml-multipart-number))))
272     (goto-char (point-min))
273     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
274                     boundary))
275     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
276     (insert (format "--%s\n" boundary))
277     (insert "Content-Type: application/pgp-encrypted\n\n")
278     (insert "Version: 1\n\n")
279     (insert (format "--%s\n" boundary))
280     (insert "Content-Type: application/octet-stream\n\n")
281     (goto-char (point-max))
282     (insert (format "--%s--\n" boundary))
283     (goto-char (point-max))))
284
285 ;;; gpg wrapper
286
287 (eval-and-compile
288   (autoload 'gpg-decrypt "gpg")
289   (autoload 'gpg-verify "gpg")
290   (autoload 'gpg-sign-detached "gpg")
291   (autoload 'gpg-sign-encrypt "gpg")
292   (autoload 'gpg-passphrase-read "gpg"))
293
294 (defun mml2015-gpg-passphrase ()
295   (or (message-options-get 'gpg-passphrase)
296       (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
297
298 (defun mml2015-gpg-decrypt-1 ()
299   (let ((cipher (current-buffer)) plain result)
300     (if (with-temp-buffer
301           (prog1
302               (gpg-decrypt cipher (setq plain (current-buffer))  
303                            mml2015-result-buffer nil)
304             (set-buffer cipher)
305             (erase-buffer)
306             (insert-buffer plain)))
307         '(t)
308       ;; Some wrong with the return value, check plain text buffer.
309       (if (> (point-max) (point-min))
310           '(t)
311         (mm-set-handle-multipart-parameter 
312          mm-security-handle 'gnus-details 
313          (with-current-buffer mml2015-result-buffer
314            (buffer-string)))
315         nil))))
316
317 (defun mml2015-gpg-decrypt (handle ctl)
318   (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
319     (mml2015-mailcrypt-decrypt handle ctl)))
320
321 (defun mml2015-gpg-clear-decrypt ()
322   (let (result)
323     (setq result (mml2015-gpg-decrypt-1))
324     (if (car result)
325         (mm-set-handle-multipart-parameter 
326          mm-security-handle 'gnus-info "OK")
327       (mm-set-handle-multipart-parameter 
328        mm-security-handle 'gnus-info "Failed"))))
329
330 (defun mml2015-gpg-verify (handle ctl)
331   (catch 'error
332     (let (part message signature)
333       (unless (setq part (mm-find-raw-part-by-type 
334                           ctl (or (mail-content-type-get ctl 'protocol)
335                                   "application/pgp-signature")
336                           t))
337         (mm-set-handle-multipart-parameter 
338          mm-security-handle 'gnus-info "Corrupted")
339         (throw 'error handle))
340       (with-temp-buffer
341         (setq message (current-buffer))
342         (insert part)
343         (with-temp-buffer
344           (setq signature (current-buffer))
345           (unless (setq part (mm-find-part-by-type 
346                               (cdr handle) "application/pgp-signature" nil t))
347             (mm-set-handle-multipart-parameter 
348              mm-security-handle 'gnus-info "Corrupted")
349             (throw 'error handle))
350           (mm-insert-part part)
351           (unless (condition-case err
352                       (gpg-verify message signature mml2015-result-buffer)
353                     (error 
354                      (mm-set-handle-multipart-parameter 
355                       mm-security-handle 'gnus-details (cadr err)) 
356                      nil)
357                     (quit
358                      (mm-set-handle-multipart-parameter 
359                       mm-security-handle 'gnus-details "Quit.") 
360                      nil))
361             (mm-set-handle-multipart-parameter 
362              mm-security-handle 'gnus-details 
363              (with-current-buffer mml2015-result-buffer
364                (buffer-string)))
365             (mm-set-handle-multipart-parameter 
366              mm-security-handle 'gnus-info "Failed")
367             (throw 'error handle)))
368         (mm-set-handle-multipart-parameter 
369          mm-security-handle 'gnus-info "OK"))
370       handle)))
371
372 (defun mml2015-gpg-sign (cont)
373   (let ((boundary 
374          (funcall mml-boundary-function (incf mml-multipart-number)))
375         (text (current-buffer)) signature)
376     (goto-char (point-max))
377     (unless (bolp)
378       (insert "\n"))
379     (with-temp-buffer
380       (unless (gpg-sign-detached text (setq signature (current-buffer))
381                                  mml2015-result-buffer 
382                                  nil
383                                  (message-options-get 'message-sender)
384                                  t t) ; armor & textmode
385         (unless (> (point-max) (point-min))
386           (pop-to-buffer mml2015-result-buffer)
387           (error "Sign error.")))
388       (set-buffer text)
389       (goto-char (point-min))
390       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
391                       boundary))
392       ;;; FIXME: what is the micalg?
393       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
394       (insert (format "\n--%s\n" boundary))
395       (goto-char (point-max))
396       (insert (format "\n--%s\n" boundary))
397       (insert "Content-Type: application/pgp-signature\n\n")
398       (insert-buffer signature)
399       (goto-char (point-max))
400       (insert (format "--%s--\n" boundary))
401       (goto-char (point-max)))))
402
403 (defun mml2015-gpg-encrypt (cont)
404   (let ((boundary 
405          (funcall mml-boundary-function (incf mml-multipart-number)))
406         (text (current-buffer))
407         cipher)
408     (with-temp-buffer
409       (unless (gpg-sign-encrypt 
410                text (setq cipher (current-buffer))
411                mml2015-result-buffer 
412                (split-string
413                 (or 
414                  (message-options-get 'message-recipients)
415                  (message-options-set 'message-recipients
416                                       (read-string "Recipients: ")))
417                 "[ \f\t\n\r\v,]+")
418                nil
419                (message-options-get 'message-sender)
420                t t) ; armor & textmode
421         (unless (> (point-max) (point-min))
422           (pop-to-buffer mml2015-result-buffer)
423           (error "Encrypt error.")))
424       (set-buffer text)
425       (delete-region (point-min) (point-max))
426       (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
427                       boundary))
428       (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
429       (insert (format "--%s\n" boundary))
430       (insert "Content-Type: application/pgp-encrypted\n\n")
431       (insert "Version: 1\n\n")
432       (insert (format "--%s\n" boundary))
433       (insert "Content-Type: application/octet-stream\n\n")
434       (insert-buffer cipher)
435       (goto-char (point-max))
436       (insert (format "--%s--\n" boundary))
437       (goto-char (point-max)))))
438
439 ;;; General wrapper
440
441 (defun mml2015-clean-buffer ()
442   (if (gnus-buffer-live-p mml2015-result-buffer)
443       (with-current-buffer mml2015-result-buffer
444         (erase-buffer)
445         t)
446     (setq mml2015-result-buffer
447           (gnus-get-buffer-create "*MML2015 Result*"))
448     nil))
449
450 (defsubst mml2015-clear-decrypt-function ()
451   (nth 6 (assq mml2015-use mml2015-function-alist)))
452
453 (defsubst mml2015-clear-verify-function ()
454   (nth 5 (assq mml2015-use mml2015-function-alist)))
455
456 ;;;###autoload
457 (defun mml2015-decrypt (handle ctl)
458   (mml2015-clean-buffer)
459   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
460     (if func
461         (funcall func handle ctl)
462       handle)))
463
464 ;;;###autoload
465 (defun mml2015-decrypt-test (handle ctl)
466   mml2015-use)
467
468 ;;;###autoload
469 (defun mml2015-verify (handle ctl)
470   (mml2015-clean-buffer)
471   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
472     (if func
473         (funcall func handle ctl)
474       handle)))
475
476 ;;;###autoload
477 (defun mml2015-verify-test (handle ctl)
478   mml2015-use)
479
480 ;;;###autoload
481 (defun mml2015-encrypt (cont)
482   (mml2015-clean-buffer)
483   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
484     (if func
485         (funcall func cont)
486       (error "Cannot find encrypt function."))))
487
488 ;;;###autoload
489 (defun mml2015-sign (cont)
490   (mml2015-clean-buffer)
491   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
492     (if func
493         (funcall func cont)
494       (error "Cannot find sign function."))))
495
496 ;;;###autoload
497 (defun mml2015-self-encrypt ()
498   (mml2015-encrypt nil))
499
500 (provide 'mml2015)
501
502 ;;; mml2015.el ends here