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