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