2000-10-29 10:35:08 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 ;; Installation: put the following statements in ~/.gnus:
27 ;;    (require 'mml2015)
28 ;;    (require 'gnus-art)
29 ;;    (mml2015-setup)
30 ;; You may have to make sure that the directory where this file lives
31 ;; is mentioned in `load-path'.
32 ;; 
33 ;; Insert an attribute, postprocess=pgp-sign (or pgp-encrypt), into
34 ;; the mml tag to be signed (or encrypted).
35
36 ;;; Code:
37
38 (eval-when-compile (require 'cl))
39
40 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
41 (defvar mml2015-verify-function 'mailcrypt-verify)
42 (defvar mml2015-encrypt-function 'mml2015-mailcrypt-encrypt)
43 (defvar mml2015-sign-function 'mml2015-mailcrypt-sign)
44
45 ;;;###autoload
46 (defun mml2015-decrypt (handle ctl)
47   (let (child handles result)
48     (unless (setq child (mm-find-part-by-type (cdr handle) 
49                                               "application/octet-stream"))
50       (error "Corrupted pgp-encrypted part."))
51     (with-temp-buffer
52       (mm-insert-part child)
53       (setq result (funcall mml2015-decrypt-function))
54       (unless (car result)
55         (error "Decrypting error."))
56       (setq handles (mm-dissect-buffer t)))
57     (mm-destroy-parts handle)
58     (if (listp (car handles))
59         handles
60       (list handles))))
61
62 (defun mml2015-fix-micalg (alg)
63   (if (and alg (string-match "^pgp-" alg))
64       (substring alg (match-end 0))
65     alg))
66
67 ;;;###autoload
68 (defun mml2015-verify (handle ctl)
69   (let (part)
70     (unless (setq part (mm-find-raw-part-by-type 
71                          ctl "application/pgp-signature" t))
72       (error "Corrupted pgp-signature part."))
73     (with-temp-buffer
74       (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
75       (insert (format "Hash: %s\n\n" 
76                       (or (mml2015-fix-micalg
77                            (mail-content-type-get ctl 'micalg))
78                           "SHA1")))
79       (insert part)
80       (goto-char (point-max))
81       (unless (bolp)
82         (insert "\n"))
83       (unless (setq part (mm-find-part-by-type 
84                            (cdr handle) "application/pgp-signature"))
85         (error "Corrupted pgp-signature part."))
86       (mm-insert-part part)
87       (unless (funcall mml2015-verify-function)
88         (error "Verify error.")))))
89
90 (autoload 'mc-sign-generic "mc-toplev")
91
92 (defun mml2015-mailcrypt-sign (cont)
93   (mc-sign-generic (message-options-get 'message-sender)
94                    nil nil nil nil)
95   (let ((boundary 
96          (funcall mml-boundary-function (incf mml-multipart-number)))
97         (scheme-alist (funcall (or mc-default-scheme 
98                                    (cdr (car mc-schemes)))))
99         hash)
100     (goto-char (point-min))
101     (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
102       (error "Cannot find signed begin line." ))
103     (goto-char (match-beginning 0))
104     (forward-line 1)
105     (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
106       (error "Cannot not find PGP hash." ))
107     (setq hash (match-string 1))
108     (unless (re-search-forward "^$" nil t)
109       (error "Cannot not find PGP message." ))
110     (forward-line 1)
111     (delete-region (point-min) (point))
112     (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
113                     boundary))
114     (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
115                     hash))
116     (insert "\n")
117     (insert (format "--%s\n" boundary))
118     (unless (re-search-forward (cdr (assq 'signed-end-line scheme-alist)))
119       (error "Cannot find signature part." ))
120     (goto-char (match-beginning 0))
121     (unless (re-search-backward "^-+BEGIN" nil t)
122       (error "Cannot find signature part." ))
123     (goto-char (match-beginning 0))
124     (insert (format "--%s\n" boundary))
125     (insert "Content-Type: application/pgp-signature\n\n")
126     (goto-char (point-max))
127     (insert (format "--%s--\n" boundary))
128     (goto-char (point-max))))
129
130 (autoload 'mc-encrypt-generic "mc-toplev")
131
132 (defun mml2015-mailcrypt-encrypt (cont)
133   (mc-encrypt-generic 
134    (or (message-options-get 'message-recipients)
135        (message-options-set 'message-recipients
136                             (mc-cleanup-recipient-headers 
137                              (read-string "Recipients: ")))))
138   (let ((boundary 
139          (funcall mml-boundary-function (incf mml-multipart-number))))
140     (goto-char (point-min))
141     (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
142                     boundary))
143     (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
144     (insert (format "--%s\n" boundary))
145     (insert "Content-Type: application/pgp-encrypted\n\n")
146     (insert "Version: 1\n\n")
147     (insert (format "--%s\n" boundary))
148     (insert "Content-Type: application/octet-stream\n\n")
149     (goto-char (point-max))
150     (insert (format "--%s--\n" boundary))
151     (goto-char (point-max))))
152
153 ;;;###autoload
154 (defun mml2015-encrypt (cont)
155   (funcall mml2015-encrypt-function cont))
156
157 ;;;###autoload
158 (defun mml2015-sign (cont)
159   (funcall mml2015-sign-function cont))
160
161 ;;;###autoload
162 (defun mml2015-setup ()
163   )
164
165 (provide 'mml2015)
166
167 ;;; mml2015.el ends here