2000-11-13 16:09:09 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / 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 "^-----\\([^-]+\\)-----$" nil t)
172               (replace-match "BEGIN PGP SIGNATURE" t t nil 1))
173           (if (re-search-forward "^-----\\([^-]+\\)-----$" nil t)
174               (replace-match "END PGP SIGNATURE" t t nil 1)))
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)
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     (goto-char (point-max))
232     (unless (re-search-backward "^-----END PGP \\(SIGNATURE\\)-----\r?$" nil t)
233       (error "Cannot find signature part." ))
234     (replace-match "MESSAGE" t t nil 1)
235     (goto-char (match-beginning 0))
236     (unless (re-search-backward "^-----BEGIN PGP \\(SIGNATURE\\)-----\r?$" 
237                                 nil t)
238       (error "Cannot find signature part." ))
239     (replace-match "MESSAGE" t t nil 1)
240     (goto-char (match-beginning 0))
241     (insert (format "--%s\n" boundary))
242     (insert "Content-Type: application/pgp-signature\n\n")
243     (goto-char (point-max))
244     (insert (format "--%s--\n" boundary))
245     (goto-char (point-max))))
246
247 (defun mml2015-mailcrypt-encrypt (cont)
248   (let ((mc-pgp-always-sign
249          (or mc-pgp-always-sign
250              (eq t (or (message-options-get 'message-sign-encrypt)
251                        (message-options-set 
252                         'message-sign-encrypt
253                         (or (y-or-n-p "Sign the message? ")
254                             'not))))
255              'never)))
256     (mc-encrypt-generic 
257      (or (message-options-get 'message-recipients)
258          (message-options-set 'message-recipients
259                               (mc-cleanup-recipient-headers 
260                                (read-string "Recipients: "))))
261      nil nil nil
262      (message-options-get 'message-sender)))
263   (let ((boundary 
264          (funcall mml-boundary-function (incf mml-multipart-number))))
265     (goto-char (point-min))
266     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
267                     boundary))
268     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
269     (insert (format "--%s\n" boundary))
270     (insert "Content-Type: application/pgp-encrypted\n\n")
271     (insert "Version: 1\n\n")
272     (insert (format "--%s\n" boundary))
273     (insert "Content-Type: application/octet-stream\n\n")
274     (goto-char (point-max))
275     (insert (format "--%s--\n" boundary))
276     (goto-char (point-max))))
277
278 ;;; gpg wrapper
279
280 (eval-and-compile
281   (autoload 'gpg-decrypt "gpg")
282   (autoload 'gpg-verify "gpg")
283   (autoload 'gpg-sign-detached "gpg")
284   (autoload 'gpg-sign-encrypt "gpg")
285   (autoload 'gpg-passphrase-read "gpg"))
286
287 (defun mml2015-gpg-passphrase ()
288   (or (message-options-get 'gpg-passphrase)
289       (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
290
291 (defun mml2015-gpg-decrypt-1 ()
292   (let ((cipher (current-buffer)) plain result)
293     (if (with-temp-buffer
294           (prog1
295               (gpg-decrypt cipher (setq plain (current-buffer))  
296                            mml2015-result-buffer nil)
297             (set-buffer cipher)
298             (erase-buffer)
299             (insert-buffer plain)))
300         '(t)
301       ;; Some wrong with the return value, check plain text buffer.
302       (if (> (point-max) (point-min))
303           '(t)
304         (mm-set-handle-multipart-parameter 
305          mm-security-handle 'gnus-details 
306          (with-current-buffer mml2015-result-buffer
307            (buffer-string)))
308         nil))))
309
310 (defun mml2015-gpg-decrypt (handle ctl)
311   (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
312     (mml2015-mailcrypt-decrypt handle ctl)))
313
314 (defun mml2015-gpg-clear-decrypt ()
315   (let (result)
316     (setq result (mml2015-gpg-decrypt-1))
317     (if (car result)
318         (mm-set-handle-multipart-parameter 
319          mm-security-handle 'gnus-info "OK")
320       (mm-set-handle-multipart-parameter 
321        mm-security-handle 'gnus-info "Failed"))))
322
323 (defun mml2015-gpg-verify (handle ctl)
324   (catch 'error
325     (let (part message signature)
326       (unless (setq part (mm-find-raw-part-by-type 
327                           ctl (or (mail-content-type-get ctl 'protocol)
328                                   "application/pgp-signature")
329                           t))
330         (mm-set-handle-multipart-parameter 
331          mm-security-handle 'gnus-info "Corrupted")
332         (throw 'error handle))
333       (with-temp-buffer
334         (setq message (current-buffer))
335         (insert part)
336         (with-temp-buffer
337           (setq signature (current-buffer))
338           (unless (setq part (mm-find-part-by-type 
339                               (cdr handle) "application/pgp-signature" nil t))
340             (mm-set-handle-multipart-parameter 
341              mm-security-handle 'gnus-info "Corrupted")
342             (throw 'error handle))
343           (mm-insert-part part)
344           (unless (condition-case err
345                       (gpg-verify message signature mml2015-result-buffer)
346                     (error 
347                      (mm-set-handle-multipart-parameter 
348                       mm-security-handle 'gnus-details (cadr err)) 
349                      nil)
350                     (quit
351                      (mm-set-handle-multipart-parameter 
352                       mm-security-handle 'gnus-details "Quit.") 
353                      nil))
354             (mm-set-handle-multipart-parameter 
355              mm-security-handle 'gnus-details 
356              (with-current-buffer mml2015-result-buffer
357                (buffer-string)))
358             (mm-set-handle-multipart-parameter 
359              mm-security-handle 'gnus-info "Failed")
360             (throw 'error handle)))
361         (mm-set-handle-multipart-parameter 
362          mm-security-handle 'gnus-info "OK"))
363       handle)))
364
365 (defun mml2015-gpg-sign (cont)
366   (let ((boundary 
367          (funcall mml-boundary-function (incf mml-multipart-number)))
368         (text (current-buffer)) signature)
369     (goto-char (point-max))
370     (unless (bolp)
371       (insert "\n"))
372     (with-temp-buffer
373       (unless (gpg-sign-detached text (setq signature (current-buffer))
374                                  mml2015-result-buffer 
375                                  nil
376                                  (message-options-get 'message-sender)
377                                  t t) ; armor & textmode
378         (unless (> (point-max) (point-min))
379           (pop-to-buffer mml2015-result-buffer)
380           (error "Sign error.")))
381       (set-buffer text)
382       (goto-char (point-min))
383       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
384                       boundary))
385       ;;; FIXME: what is the micalg?
386       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
387       (insert (format "\n--%s\n" boundary))
388       (goto-char (point-max))
389       (insert (format "\n--%s\n" boundary))
390       (insert "Content-Type: application/pgp-signature\n\n")
391       (insert-buffer signature)
392       (goto-char (point-max))
393       (insert (format "--%s--\n" boundary))
394       (goto-char (point-max)))))
395
396 (defun mml2015-gpg-encrypt (cont)
397   (let ((boundary 
398          (funcall mml-boundary-function (incf mml-multipart-number)))
399         (text (current-buffer))
400         cipher)
401     (with-temp-buffer
402       (unless (gpg-sign-encrypt 
403                text (setq cipher (current-buffer))
404                mml2015-result-buffer 
405                (split-string
406                 (or 
407                  (message-options-get 'message-recipients)
408                  (message-options-set 'message-recipients
409                                       (read-string "Recipients: ")))
410                 "[ \f\t\n\r\v,]+")
411                nil
412                (message-options-get 'message-sender)
413                t t) ; armor & textmode
414         (unless (> (point-max) (point-min))
415           (pop-to-buffer mml2015-result-buffer)
416           (error "Encrypt error.")))
417       (set-buffer text)
418       (delete-region (point-min) (point-max))
419       (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
420                       boundary))
421       (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
422       (insert (format "--%s\n" boundary))
423       (insert "Content-Type: application/pgp-encrypted\n\n")
424       (insert "Version: 1\n\n")
425       (insert (format "--%s\n" boundary))
426       (insert "Content-Type: application/octet-stream\n\n")
427       (insert-buffer cipher)
428       (goto-char (point-max))
429       (insert (format "--%s--\n" boundary))
430       (goto-char (point-max)))))
431
432 ;;; General wrapper
433
434 (defun mml2015-clean-buffer ()
435   (if (gnus-buffer-live-p mml2015-result-buffer)
436       (with-current-buffer mml2015-result-buffer
437         (erase-buffer)
438         t)
439     (setq mml2015-result-buffer
440           (gnus-get-buffer-create "*MML2015 Result*"))
441     nil))
442
443 (defsubst mml2015-clear-decrypt-function ()
444   (nth 6 (assq mml2015-use mml2015-function-alist)))
445
446 (defsubst mml2015-clear-verify-function ()
447   (nth 5 (assq mml2015-use mml2015-function-alist)))
448
449 ;;;###autoload
450 (defun mml2015-decrypt (handle ctl)
451   (mml2015-clean-buffer)
452   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
453     (if func
454         (funcall func handle ctl)
455       handle)))
456
457 ;;;###autoload
458 (defun mml2015-decrypt-test (handle ctl)
459   mml2015-use)
460
461 ;;;###autoload
462 (defun mml2015-verify (handle ctl)
463   (mml2015-clean-buffer)
464   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
465     (if func
466         (funcall func handle ctl)
467       handle)))
468
469 ;;;###autoload
470 (defun mml2015-verify-test (handle ctl)
471   mml2015-use)
472
473 ;;;###autoload
474 (defun mml2015-encrypt (cont)
475   (mml2015-clean-buffer)
476   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
477     (if func
478         (funcall func cont)
479       (error "Cannot find encrypt function."))))
480
481 ;;;###autoload
482 (defun mml2015-sign (cont)
483   (mml2015-clean-buffer)
484   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
485     (if func
486         (funcall func cont)
487       (error "Cannot find sign function."))))
488
489 ;;;###autoload
490 (defun mml2015-self-encrypt ()
491   (mml2015-encrypt nil))
492
493 (provide 'mml2015)
494
495 ;;; mml2015.el ends here