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