Add MIME security button.
[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         (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          (buffer-string mml2015-result-buffer))
277         nil))))
278
279 (defun mml2015-gpg-decrypt (handle ctl)
280   (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
281     (mml2015-mailcrypt-decrypt handle ctl)))
282
283 (defun mml2015-gpg-clear-decrypt ()
284   (let (result)
285     (setq result (mml2015-gpg-decrypt-1))
286     (if (car result)
287         (mm-set-handle-multipart-parameter 
288          mm-security-handle 'gnus-info "OK")
289       (mm-set-handle-multipart-parameter 
290        mm-security-handle 'gnus-info "Failed"))))
291
292 (defun mml2015-gpg-verify (handle ctl)
293   (catch 'error
294     (let (part message signature)
295       (unless (setq part (mm-find-raw-part-by-type 
296                           ctl (or (mail-content-type-get ctl 'protocol)
297                                   "application/pgp-signature")
298                           t))
299         (mm-set-handle-multipart-parameter 
300          mm-security-handle 'gnus-info "Corrupted")
301         (throw 'error handle))
302       (with-temp-buffer
303         (setq message (current-buffer))
304         (insert part)
305         (with-temp-buffer
306           (setq signature (current-buffer))
307           (unless (setq part (mm-find-part-by-type 
308                               (cdr handle) "application/pgp-signature" nil t))
309             (mm-set-handle-multipart-parameter 
310              mm-security-handle 'gnus-info "Corrupted")
311             (throw 'error handle))
312           (mm-insert-part part)
313           (unless (gpg-verify message signature mml2015-result-buffer)
314             (mm-set-handle-multipart-parameter 
315              mm-security-handle 'gnus-details 
316              (buffer-string mml2015-result-buffer))
317             (mm-set-handle-multipart-parameter 
318              mm-security-handle 'gnus-info "Failed")
319             (throw 'error handle))))
320       handle)))
321
322 (defun mml2015-gpg-sign (cont)
323   (let ((boundary 
324          (funcall mml-boundary-function (incf mml-multipart-number)))
325         (text (current-buffer)) signature)
326     (goto-char (point-max))
327     (unless (bolp)
328       (insert "\n"))
329     (with-temp-buffer
330       (unless (gpg-sign-detached text (setq signature (current-buffer))
331                                  mml2015-result-buffer 
332                                  nil
333                                  (message-options-get 'message-sender)
334                                  t t) ; armor & textmode
335         (unless (> (point-max) (point-min))
336           (pop-to-buffer mml2015-result-buffer)
337           (error "Sign error.")))
338       (set-buffer text)
339       (goto-char (point-min))
340       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
341                       boundary))
342       ;;; FIXME: what is the micalg?
343       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
344       (insert (format "\n--%s\n" boundary))
345       (goto-char (point-max))
346       (insert (format "\n--%s\n" boundary))
347       (insert "Content-Type: application/pgp-signature\n\n")
348       (insert-buffer signature)
349       (goto-char (point-max))
350       (insert (format "--%s--\n" boundary))
351       (goto-char (point-max)))))
352
353 (defun mml2015-gpg-encrypt (cont)
354   (let ((boundary 
355          (funcall mml-boundary-function (incf mml-multipart-number)))
356         (text (current-buffer))
357         cipher)
358     (with-temp-buffer
359       (unless (gpg-sign-encrypt 
360                text (setq cipher (current-buffer))
361                mml2015-result-buffer 
362                (split-string
363                 (or 
364                  (message-options-get 'message-recipients)
365                  (message-options-set 'message-recipients
366                                       (read-string "Recipients: ")))
367                 "[ \f\t\n\r\v,]+")
368                nil
369                (message-options-get 'message-sender)
370                t t) ; armor & textmode
371         (unless (> (point-max) (point-min))
372           (pop-to-buffer mml2015-result-buffer)
373           (error "Encrypt error.")))
374       (set-buffer text)
375       (delete-region (point-min) (point-max))
376       (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
377                       boundary))
378       (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
379       (insert (format "--%s\n" boundary))
380       (insert "Content-Type: application/pgp-encrypted\n\n")
381       (insert "Version: 1\n\n")
382       (insert (format "--%s\n" boundary))
383       (insert "Content-Type: application/octet-stream\n\n")
384       (insert-buffer cipher)
385       (goto-char (point-max))
386       (insert (format "--%s--\n" boundary))
387       (goto-char (point-max)))))
388
389 ;;; General wrapper
390
391 (defun mml2015-clean-buffer ()
392   (if (gnus-buffer-live-p mml2015-result-buffer)
393       (with-current-buffer mml2015-result-buffer
394         (erase-buffer)
395         t)
396     (setq mml2015-result-buffer
397           (gnus-get-buffer-create "*MML2015 Result*"))
398     nil))
399
400 (defsubst mml2015-clear-decrypt-function ()
401   (nth 6 (assq mml2015-use mml2015-function-alist)))
402
403 (defsubst mml2015-clear-verify-function ()
404   (nth 5 (assq mml2015-use mml2015-function-alist)))
405
406 ;;;###autoload
407 (defun mml2015-decrypt (handle ctl)
408   (mml2015-clean-buffer)
409   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
410     (if func
411         (funcall func handle ctl)
412       handle)))
413
414 ;;;###autoload
415 (defun mml2015-decrypt-test (handle ctl)
416   mml2015-use)
417
418 ;;;###autoload
419 (defun mml2015-verify (handle ctl)
420   (mml2015-clean-buffer)
421   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
422     (if func
423         (funcall func handle ctl)
424       handle)))
425
426 ;;;###autoload
427 (defun mml2015-verify-test (handle ctl)
428   mml2015-use)
429
430 ;;;###autoload
431 (defun mml2015-encrypt (cont)
432   (mml2015-clean-buffer)
433   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
434     (if func
435         (funcall func cont)
436       (error "Cannot find encrypt function."))))
437
438 ;;;###autoload
439 (defun mml2015-sign (cont)
440   (mml2015-clean-buffer)
441   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
442     (if func
443         (funcall func cont)
444       (error "Cannot find sign function."))))
445
446 ;;;###autoload
447 (defun mml2015-self-encrypt ()
448   (mml2015-encrypt nil))
449
450 (provide 'mml2015)
451
452 ;;; mml2015.el ends here