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