46385b14f3fc891dcbe5aaa2076c018dffdce78c
[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   (mc-encrypt-generic 
175    (or (message-options-get 'message-recipients)
176        (message-options-set 'message-recipients
177                             (mc-cleanup-recipient-headers 
178                              (read-string "Recipients: "))))
179    nil nil nil
180    (message-options-get 'message-sender)
181    (or mc-pgp-always-sign
182        (eq t
183            (or (message-options-get 'message-sign-encrypt)
184                (message-options-set 'message-sign-encrypt
185                                     (or (y-or-n-p "Sign the message? ")
186                                         'not))))))
187   (let ((boundary 
188          (funcall mml-boundary-function (incf mml-multipart-number))))
189     (goto-char (point-min))
190     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
191                     boundary))
192     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
193     (insert (format "--%s\n" boundary))
194     (insert "Content-Type: application/pgp-encrypted\n\n")
195     (insert "Version: 1\n\n")
196     (insert (format "--%s\n" boundary))
197     (insert "Content-Type: application/octet-stream\n\n")
198     (goto-char (point-max))
199     (insert (format "--%s--\n" boundary))
200     (goto-char (point-max))))
201
202 ;;; gpg wrapper
203
204 (eval-and-compile
205   (autoload 'gpg-decrypt "gpg")
206   (autoload 'gpg-verify "gpg")
207   (autoload 'gpg-sign-detached "gpg")
208   (autoload 'gpg-sign-encrypt "gpg")
209   (autoload 'gpg-passphrase-read "gpg"))
210
211 (defun mml2015-gpg-passphrase ()
212   (or (message-options-get 'gpg-passphrase)
213       (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
214
215 (defun mml2015-gpg-decrypt-1 ()
216   (let ((cipher (current-buffer)) plain result)
217     (if (with-temp-buffer
218           (prog1
219               (gpg-decrypt cipher (setq plain (current-buffer))  
220                            mml2015-result-buffer nil)
221             (set-buffer cipher)
222             (erase-buffer)
223             (insert-buffer plain)))
224         '(t)
225       ;; Some wrong with the return value, check plain text buffer.
226       (if (> (point-max) (point-min))
227           '(t)
228         (pop-to-buffer mml2015-result-buffer)
229         nil))))
230
231 (defun mml2015-gpg-decrypt (handle ctl)
232   (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
233     (mml2015-mailcrypt-decrypt handle ctl)))
234
235 (defun mml2015-gpg-clear-decrypt ()
236   (let (result)
237     (setq result (mml2015-gpg-decrypt-1))
238     (unless (car result)
239       (error "Decrypting error."))))
240
241 (defun mml2015-gpg-verify (handle ctl)
242   (let (part message signature)
243     (unless (setq part (mm-find-raw-part-by-type 
244                          ctl (or (mail-content-type-get ctl 'protocol)
245                                  "application/pgp-signature")
246                          t))
247       (error "Corrupted pgp-signature part."))
248     (with-temp-buffer
249       (setq message (current-buffer))
250       (insert part)
251       (with-temp-buffer
252         (setq signature (current-buffer))
253         (unless (setq part (mm-find-part-by-type 
254                             (cdr handle) "application/pgp-signature" nil t))
255           (error "Corrupted pgp-signature part."))
256         (mm-insert-part part)
257         (unless (gpg-verify message signature mml2015-result-buffer)
258           (pop-to-buffer mml2015-result-buffer)
259           (error "Verify error.")))))
260   handle)
261
262 (defun mml2015-gpg-sign (cont)
263   (let ((boundary 
264          (funcall mml-boundary-function (incf mml-multipart-number)))
265         (text (current-buffer)) signature)
266     (goto-char (point-max))
267     (unless (bolp)
268       (insert "\n"))
269     (with-temp-buffer
270       (unless (gpg-sign-detached text (setq signature (current-buffer))
271                                  mml2015-result-buffer 
272                                  nil
273                                  (message-options-get 'message-sender)
274                                  t t) ; armor & textmode
275         (unless (> (point-max) (point-min))
276           (pop-to-buffer mml2015-result-buffer)
277           (error "Sign error.")))
278       (set-buffer text)
279       (goto-char (point-min))
280       (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
281                       boundary))
282       ;;; FIXME: what is the micalg?
283       (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
284       (insert (format "\n--%s\n" boundary))
285       (goto-char (point-max))
286       (insert (format "\n--%s\n" boundary))
287       (insert "Content-Type: application/pgp-signature\n\n")
288       (insert-buffer signature)
289       (goto-char (point-max))
290       (insert (format "--%s--\n" boundary))
291       (goto-char (point-max)))))
292
293 (defun mml2015-gpg-encrypt (cont)
294   (let ((boundary 
295          (funcall mml-boundary-function (incf mml-multipart-number)))
296         (text (current-buffer))
297         cipher)
298     (with-temp-buffer
299       (unless (gpg-sign-encrypt 
300                text (setq cipher (current-buffer))
301                mml2015-result-buffer 
302                (split-string
303                 (or 
304                  (message-options-get 'message-recipients)
305                  (message-options-set 'message-recipients
306                                       (read-string "Recipients: ")))
307                 "[ \f\t\n\r\v,]+")
308                nil
309                (message-options-get 'message-sender)
310                t t) ; armor & textmode
311         (unless (> (point-max) (point-min))
312           (pop-to-buffer mml2015-result-buffer)
313           (error "Encrypt error.")))
314       (set-buffer text)
315       (delete-region (point-min) (point-max))
316       (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
317                       boundary))
318       (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
319       (insert (format "--%s\n" boundary))
320       (insert "Content-Type: application/pgp-encrypted\n\n")
321       (insert "Version: 1\n\n")
322       (insert (format "--%s\n" boundary))
323       (insert "Content-Type: application/octet-stream\n\n")
324       (insert-buffer cipher)
325       (goto-char (point-max))
326       (insert (format "--%s--\n" boundary))
327       (goto-char (point-max)))))
328
329 ;;; General wrapper
330
331 (defun mml2015-clean-buffer ()
332   (if (gnus-buffer-live-p mml2015-result-buffer)
333       (with-current-buffer mml2015-result-buffer
334         (erase-buffer)
335         t)
336     (setq mml2015-result-buffer
337           (gnus-get-buffer-create "*MML2015 Result*"))
338     nil))
339
340 (defsubst mml2015-clear-decrypt-function ()
341   (nth 6 (assq mml2015-use mml2015-function-alist)))
342
343 (defsubst mml2015-clear-verify-function ()
344   (nth 5 (assq mml2015-use mml2015-function-alist)))
345
346 ;;;###autoload
347 (defun mml2015-decrypt (handle ctl)
348   (mml2015-clean-buffer)
349   (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
350     (if func
351         (funcall func handle ctl)
352       handle)))
353
354 ;;;###autoload
355 (defun mml2015-decrypt-test (handle ctl)
356   mml2015-use)
357
358 ;;;###autoload
359 (defun mml2015-verify (handle ctl)
360   (mml2015-clean-buffer)
361   (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
362     (if func
363         (funcall func handle ctl)
364       handle)))
365
366 ;;;###autoload
367 (defun mml2015-verify-test (handle ctl)
368   mml2015-use)
369
370 ;;;###autoload
371 (defun mml2015-encrypt (cont)
372   (mml2015-clean-buffer)
373   (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
374     (if func
375         (funcall func cont)
376       (error "Cannot find encrypt function."))))
377
378 ;;;###autoload
379 (defun mml2015-sign (cont)
380   (mml2015-clean-buffer)
381   (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
382     (if func
383         (funcall func cont)
384       (error "Cannot find sign function."))))
385
386 (provide 'mml2015)
387
388 ;;; mml2015.el ends here