1 ;;; rfc2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (c) 2000 Shenghuo Zhu
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is not (yet) a part of GNU Emacs. Hope it
8 ;; will be a part of oGnus distribution, then GNU Emacs.
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published
12 ;; by the Free Software Foundation; either version 2, or (at your
13 ;; option) any later version.
15 ;; This file is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Installation: put the following statements in ~/.gnus:
29 ;; (require 'gnus-art)
31 ;; You may have to make sure that the directory where this file lives
32 ;; is mentioned in `load-path'.
34 ;; Insert an attribute, postprocess=pgp-sign (or pgp-encrypt), into
35 ;; the mml tag to be signed (or encrypted).
39 (defvar rfc2015-decrypt-function 'mailcrypt-decrypt)
40 (defvar rfc2015-verify-function 'mailcrypt-verify)
42 (defun rfc2015-decrypt (handle)
45 ((setq child (mm-find-part-by-type (cdr handle)
46 "application/octet-stream"))
49 (mm-insert-part child)
50 (setq result (funcall rfc2015-decrypt-function))
52 (error "Decrypting error."))
53 (setq handles (mm-dissect-buffer t)))
54 (setq gnus-article-mime-handles
55 (append (if (listp (car gnus-article-mime-handles))
56 gnus-article-mime-handles
57 (list gnus-article-mime-handles))
58 (if (listp (car handles))
61 (gnus-mime-display-part handles)))
63 (if (y-or-n-p "Corrupted pgp-encrypted part. Abort?" )
64 (error "Corrupted pgp-encrypted part.")
65 (gnus-mime-display-mixed (cdr handle)))))))
67 ;; FIXME: mm-dissect-buffer loses information of micalg and the
68 ;; original header of signed part.
70 (defun rfc2015-verify (handle)
71 (if (y-or-n-p "Verify signed part?" )
72 (let (child result hash)
74 (unless (setq child (mm-find-part-by-type
75 (cdr handle) "application/pgp-signature" t))
76 (error "Corrupted pgp-signature part."))
77 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
78 (insert (format "Hash: %s\n\n" (read-string "Hash: " "SHA1")))
79 (mm-insert-part child)
80 (goto-char (point-max))
83 (unless (setq child (mm-find-part-by-type
84 (cdr handle) "application/pgp-signature"))
85 (error "Corrupted pgp-signature part."))
86 (mm-insert-part child)
87 (setq result (funcall rfc2015-verify-function))
89 (error "Verify error.")))))
90 (gnus-mime-display-part
92 (cdr handle) "application/pgp-signature" t)))
94 (defvar rfc2015-mailcrypt-prefix 0)
96 (defun rfc2015-mailcrypt-sign (cont)
97 (mailcrypt-sign rfc2015-mailcrypt-prefix)
99 (funcall mml-boundary-function (incf mml-multipart-number)))
100 (scheme-alist (funcall (or mc-default-scheme
101 (cdr (car mc-schemes)))))
103 (goto-char (point-min))
104 (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
105 (error "Cannot find signed begin line." ))
106 (goto-char (match-beginning 0))
108 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
109 (error "Cannot not find PGP hash." ))
110 (setq hash (match-string 1))
111 (unless (re-search-forward "^$" nil t)
112 (error "Cannot not find PGP message." ))
114 (delete-region (point-min) (point))
115 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
117 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
120 (insert (format "--%s\n" boundary))
121 (unless (re-search-forward (cdr (assq 'signed-end-line scheme-alist)))
122 (error "Cannot find signature part." ))
123 (goto-char (match-beginning 0))
124 (unless (re-search-backward "^-+BEGIN" nil t)
125 (error "Cannot find signature part." ))
126 (goto-char (match-beginning 0))
127 (insert (format "--%s\n" boundary))
128 (insert "Content-Type: application/pgp-signature\n\n")
129 (goto-char (point-max))
130 (insert (format "--%s--\n" boundary))
131 (goto-char (point-max))))
133 (defun rfc2015-mailcrypt-encrypt (cont)
135 ;; You have to input the receiptant.
136 (mailcrypt-encrypt rfc2015-mailcrypt-prefix)
138 (funcall mml-boundary-function (incf mml-multipart-number))))
139 (goto-char (point-min))
140 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
142 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
143 (insert (format "--%s\n" boundary))
144 (insert "Content-Type: application/pgp-encrypted\n\n")
145 (insert "Version: 1\n\n")
146 (insert (format "--%s\n" boundary))
147 (insert "Content-Type: application/octet-stream\n\n")
148 (goto-char (point-max))
149 (insert (format "--%s--\n" boundary))
150 (goto-char (point-max))))
152 ;; The following code might be moved into mml.el or gnus-art.el.
154 (defvar mml-postprocess-alist
155 '(("pgp-sign" . rfc2015-mailcrypt-sign)
156 ("pgp-encrypt" . rfc2015-mailcrypt-encrypt))
157 "Alist of postprocess functions.")
159 (defun mml-postprocess (cont)
160 (let ((pp (cdr (or (assq 'postprocess cont)
163 (if (and pp (setq item (assoc pp mml-postprocess-alist)))
164 (funcall (cdr item) cont))))
166 (defun rfc2015-setup ()
167 (setq mml-generate-mime-postprocess-function 'mml-postprocess)
168 ; (push '("multipart/signed" . rfc2015-verify)
169 ; gnus-mime-multipart-functions)
170 (push '("multipart/encrypted" . rfc2015-decrypt)
171 gnus-mime-multipart-functions))
173 ;; The following code might be moved into mm-decode.el.
175 (defun mm-find-part-by-type (handles type &optional notp)
179 (not (equal (mm-handle-media-type (car handles)) type))
180 (equal (mm-handle-media-type (car handles)) type))
181 (setq handle (car handles)
183 (setq handles (cdr handles)))
188 ;;; rfc2015.el ends here