2000-11-12 08:52:46 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         (unless (car result)
98           (mm-set-handle-multipart-parameter 
99            mm-security-handle 'gnus-info "Failed")
100           (throw 'error handle))
101         (setq handles (mm-dissect-buffer t)))
102       (mm-destroy-parts handle)
103       (mm-set-handle-multipart-parameter 
104        mm-security-handle 'gnus-info "OK")
105       (if (listp (car handles))
106           handles
107         (list handles)))))
108
109 (defun mml2015-mailcrypt-clear-decrypt ()
110   (let (result)
111     (setq result 
112           (condition-case err
113               (funcall mml2015-decrypt-function)
114             (error 
115              (mm-set-handle-multipart-parameter 
116               mm-security-handle 'gnus-details (cadr err)) 
117              nil)))
118     (if (car result)
119         (mm-set-handle-multipart-parameter 
120          mm-security-handle 'gnus-info "OK")
121       (mm-set-handle-multipart-parameter 
122        mm-security-handle 'gnus-info "Failed"))))
123
124 (defun mml2015-fix-micalg (alg)
125   (upcase
126    (if (and alg (string-match "^pgp-" alg))
127        (substring alg (match-end 0))
128      alg)))
129
130 (defun mml2015-mailcrypt-verify (handle ctl)
131   (catch 'error
132     (let (part)
133       (unless (setq part (mm-find-raw-part-by-type 
134                           ctl (or (mail-content-type-get ctl 'protocol)
135                                   "application/pgp-signature")
136                           t))
137         (mm-set-handle-multipart-parameter 
138          mm-security-handle 'gnus-info "Corrupted")
139         (throw 'error handle))
140       (with-temp-buffer
141         (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
142         (insert (format "Hash: %s\n\n" 
143                         (or (mml2015-fix-micalg
144                              (mail-content-type-get ctl 'micalg))
145                             "SHA1")))
146         (save-restriction
147           (narrow-to-region (point) (point))
148           (insert part "\n")
149           (goto-char (point-min))
150           (while (not (eobp))
151             (if (looking-at "^-")
152                 (insert "- "))
153             (forward-line)))
154         (unless (setq part (mm-find-part-by-type 
155                             (cdr handle) "application/pgp-signature" nil t))
156           (mm-set-handle-multipart-parameter 
157            mm-security-handle 'gnus-info "Corrupted")
158           (throw 'error handle))
159         (save-restriction
160           (narrow-to-region (point) (point))
161           (mm-insert-part part)
162           (goto-char (point-min))
163           (if (re-search-forward "^-----\\([^-]+\\)-----$" nil t)
164               (replace-match "BEGIN PGP SIGNATURE" t t nil 1))
165           (if (re-search-forward "^-----\\([^-]+\\)-----$" nil t)
166               (replace-match "END PGP SIGNATURE" t t nil 1)))
167         (unless (condition-case err
168                     (funcall mml2015-verify-function)
169                   (error 
170                    (mm-set-handle-multipart-parameter 
171                     mm-security-handle 'gnus-details (cadr err)) 
172                    nil))
173           (mm-set-handle-multipart-parameter 
174            mm-security-handle 'gnus-info "Failed")
175           (throw 'error handle)))
176       (mm-set-handle-multipart-parameter 
177        mm-security-handle 'gnus-info "OK")
178       handle)))
179
180 (defun mml2015-mailcrypt-clear-verify ()
181   (if (condition-case err
182           (funcall mml2015-verify-function)
183         (error 
184          (mm-set-handle-multipart-parameter 
185           mm-security-handle 'gnus-details (cadr err)) 
186          nil))
187       (mm-set-handle-multipart-parameter 
188        mm-security-handle 'gnus-info "OK")
189     (mm-set-handle-multipart-parameter 
190      mm-security-handle 'gnus-info "Failed")))
191
192 (defun mml2015-mailcrypt-sign (cont)
193   (mc-sign-generic (message-options-get 'message-sender)
194                    nil nil nil nil)
195   (let ((boundary 
196          (funcall mml-boundary-function (incf mml-multipart-number)))
197         hash)
198     (goto-char (point-min))
199     (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
200       (error "Cannot find signed begin line." ))
201     (goto-char (match-beginning 0))
202     (forward-line 1)
203     (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
204       (error "Cannot not find PGP hash." ))
205     (setq hash (match-string 1))
206     (unless (re-search-forward "^$" nil t)
207       (error "Cannot not find PGP message." ))
208     (forward-line 1)
209     (delete-region (point-min) (point))
210     (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
211                     boundary))
212     (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
213                     (downcase hash)))
214     (insert (format "\n--%s\n" boundary))
215     (goto-char (point-max))
216     (unless (re-search-backward "^-----END PGP \\(SIGNATURE\\)-----\r?$" nil t)
217       (error "Cannot find signature part." ))
218     (replace-match "MESSAGE" t t nil 1)
219     (goto-char (match-beginning 0))
220     (unless (re-search-backward "^-----BEGIN PGP \\(SIGNATURE\\)-----\r?$" 
221                                 nil t)
222       (error "Cannot find signature part." ))
223     (replace-match "MESSAGE" t t nil 1)
224     (goto-char (match-beginning 0))
225     (insert (format "--%s\n" boundary))
226     (insert "Content-Type: application/pgp-signature\n\n")
227     (goto-char (point-max))
228     (insert (format "--%s--\n" boundary))
229     (goto-char (point-max))))
230
231 (defun mml2015-mailcrypt-encrypt (cont)
232   (let ((mc-pgp-always-sign
233          (or mc-pgp-always-sign
234              (eq t (or (message-options-get 'message-sign-encrypt)
235                        (message-options-set 
236                         'message-sign-encrypt
237                         (or (y-or-n-p "Sign the message? ")
238                             'not))))
239              'never)))
240     (mc-encrypt-generic 
241      (or (message-options-get 'message-recipients)
242          (message-options-set 'message-recipients
243                               (mc-cleanup-recipient-headers 
244                                (read-string "Recipients: "))))
245      nil nil nil
246      (message-options-get 'message-sender)))
247   (let ((boundary 
248          (funcall mml-boundary-function (incf mml-multipart-number))))
249     (goto-char (point-min))
250     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
251                     boundary))
252     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
253     (insert (format "--%s\n" boundary))
254     (insert "Content-Type: application/pgp-encrypted\n\n")
255     (insert "Version: 1\n\n")
256     (insert (format "--%s\n" boundary))
257     (insert "Content-Type: application/octet-stream\n\n")
258     (goto-char (point-max))
259     (insert (format "--%s--\n" boundary))
260     (goto-char (point-max))))
261
262 ;;; gpg wrapper
263
264 (eval-and-compile
265   (autoload 'gpg-decrypt "gpg")
266   (autoload 'gpg-verify "gpg")
267   (autoload 'gpg-sign-detached "gpg")
268   (autoload 'gpg-sign-encrypt "gpg")
269   (autoload 'gpg-passphrase-read "gpg"))
270
271 (defun mml2015-gpg-passphrase ()
272   (or (message-options-get 'gpg-passphrase)
273       (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
274
275 (defun mml2015-gpg-decrypt-1 ()
276   (let ((cipher (current-buffer)) plain result)
277     (if (with-temp-buffer
278           (prog1
279               (gpg-decrypt cipher (setq plain (current-buffer))  
280                            mml2015-result-buffer nil)
281             (set-buffer cipher)
282             (erase-buffer)
283             (insert-buffer plain)))
284         '(t)
285       ;; Some wrong with the return value, check plain text buffer.
286       (if (> (point-max) (point-min))
287           '(t)
288         (mm-set-handle-multipart-parameter 
289          mm-security-handle 'gnus-details 
290          (with-current-buffer mml2015-result-buffer
291            (buffer-string)))
292         nil))))
293
294 (defun mml2015-gpg-decrypt (handle ctl)
295   (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
296     (mml2015-mailcrypt-decrypt handle ctl)))
297
298 (defun mml2015-gpg-clear-decrypt ()
299   (let (result)
300     (setq result (mml2015-gpg-decrypt-1))
301     (if (car result)
302         (mm-set-handle-multipart-parameter 
303          mm-security-handle 'gnus-info "OK")
304       (mm-set-handle-multipart-parameter 
305        mm-security-handle 'gnus-info "Failed"))))
306
307 (defun mml2015-gpg-verify (handle ctl)
308   (catch 'error
309     (let (part message signature)
310       (unless (setq part (mm-find-raw-part-by-type 
311                           ctl (or (mail-content-type-get ctl 'protocol)
312                                   "application/pgp-signature")
313                           t))
314         (mm-set-handle-multipart-parameter 
315          mm-security-handle 'gnus-info "Corrupted")
316         (throw 'error handle))
317       (with-temp-buffer
318         (setq message (current-buffer))
319         (insert part)
320         (with-temp-buffer
321           (setq signature (current-buffer))
322           (unless (setq part (mm-find-part-by-type 
323                               (cdr handle) "application/pgp-signature" nil t))
324             (mm-set-handle-multipart-parameter 
325              mm-security-handle 'gnus-info "Corrupted")
326             (throw 'error handle))
327           (mm-insert-part part)
328           (unless (gpg-verify message signature mml2015-result-buffer)
329             (mm-set-handle-multipart-parameter 
330              mm-security-handle 'gnus-details 
331              (with-current-buffer mml2015-result-buffer
332                (buffer-string)))
333             (mm-set-handle-multipart-parameter 
334              mm-security-handle 'gnus-info "Failed")
335             (throw 'error handle)))
336         (mm-set-handle-multipart-parameter 
337          mm-security-handle 'gnus-info "OK"))
338       handle)))
339
340 (defun mml2015-gpg-sign (cont)
341   (let ((boundary 
342          (funcall mml-boundary-function (incf mml-multipart-number)))
343         (text (current-buffer)) signature)
344     (goto-char (point-max))
345     (unless (bolp)
346       (insert "\n"))
347     (with-temp-buffer
348       (unless (gpg-sign-detached text (setq signature (current-buffer))
349                                  mml2015-result-buffer 
350                                  nil
351                                  (message-options-get 'message-sender)
352                                  t t) ; armor & textmode
353         (unless (> (point-max) (point-min))
354           (pop-to-buffer mml2015-result-buffer)
355           (error "Sign error.")))
356       (set-buffer text)
357       (goto-char (point-min))
358       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
359                       boundary))
360       ;;; FIXME: what is the micalg?
361       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
362       (insert (format "\n--%s\n" boundary))
363       (goto-char (point-max))
364       (insert (format "\n--%s\n" boundary))
365       (insert "Content-Type: application/pgp-signature\n\n")
366       (insert-buffer signature)
367       (goto-char (point-max))
368       (insert (format "--%s--\n" boundary))
369       (goto-char (point-max)))))
370
371 (defun mml2015-gpg-encrypt (cont)
372   (let ((boundary 
373          (funcall mml-boundary-function (incf mml-multipart-number)))
374         (text (current-buffer))
375         cipher)
376     (with-temp-buffer
377       (unless (gpg-sign-encrypt 
378                text (setq cipher (current-buffer))
379                mml2015-result-buffer 
380                (split-string
381                 (or 
382                  (message-options-get 'message-recipients)
383                  (message-options-set 'message-recipients
384                                       (read-string "Recipients: ")))
385                 "[ \f\t\n\r\v,]+")
386                nil
387                (message-options-get 'message-sender)
388                t t) ; armor & textmode
389         (unless (> (point-max) (point-min))
390           (pop-to-buffer mml2015-result-buffer)
391           (error "Encrypt error.")))
392       (set-buffer text)
393       (delete-region (point-min) (point-max))
394       (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
395                       boundary))
396       (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
397       (insert (format "--%s\n" boundary))
398       (insert "Content-Type: application/pgp-encrypted\n\n")
399       (insert "Version: 1\n\n")
400       (insert (format "--%s\n" boundary))
401       (insert "Content-Type: application/octet-stream\n\n")
402       (insert-buffer cipher)
403       (goto-char (point-max))
404       (insert (format "--%s--\n" boundary))
405       (goto-char (point-max)))))
406
407 ;;; General wrapper
408
409 (defun mml2015-clean-buffer ()
410   (if (gnus-buffer-live-p mml2015-result-buffer)
411       (with-current-buffer mml2015-result-buffer
412         (erase-buffer)
413         t)
414     (setq mml2015-result-buffer
415           (gnus-get-buffer-create "*MML2015 Result*"))
416     nil))
417
418 (defsubst mml2015-clear-decrypt-function ()
419   (nth 6 (assq mml2015-use mml2015-function-alist)))
420
421 (defsubst mml2015-clear-verify-function ()
422   (nth 5 (assq mml2015-use mml2015-function-alist)))
423
424 ;;;###autoload
425 (defun mml2015-decrypt (handle ctl)
426   (mml2015-clean-buffer)
427   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
428     (if func
429         (funcall func handle ctl)
430       handle)))
431
432 ;;;###autoload
433 (defun mml2015-decrypt-test (handle ctl)
434   mml2015-use)
435
436 ;;;###autoload
437 (defun mml2015-verify (handle ctl)
438   (mml2015-clean-buffer)
439   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
440     (if func
441         (funcall func handle ctl)
442       handle)))
443
444 ;;;###autoload
445 (defun mml2015-verify-test (handle ctl)
446   mml2015-use)
447
448 ;;;###autoload
449 (defun mml2015-encrypt (cont)
450   (mml2015-clean-buffer)
451   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
452     (if func
453         (funcall func cont)
454       (error "Cannot find encrypt function."))))
455
456 ;;;###autoload
457 (defun mml2015-sign (cont)
458   (mml2015-clean-buffer)
459   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
460     (if func
461         (funcall func cont)
462       (error "Cannot find sign function."))))
463
464 ;;;###autoload
465 (defun mml2015-self-encrypt ()
466   (mml2015-encrypt nil))
467
468 (provide 'mml2015)
469
470 ;;; mml2015.el ends here