1 ;;; rfc2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (c) 2000 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is a part of GNU Emacs.
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.
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.
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.
26 ;; Installation: put the following statement in ~/.gnus:
28 ;; You may have to make sure that the directory where this file lives
29 ;; is mentioned in `load-path'.
31 ;; Insert an attribute, postprocess=pgp-sign (or pgp-encrypt), into
32 ;; the mml tag to be signed (or encrypted).
36 (defvar rfc2015-decrypt-function 'mailcrypt-decrypt)
37 (defvar rfc2015-verify-function 'mailcrypt-verify)
39 (defun rfc2015-decrypt (handle)
42 ((setq child (mm-find-part-by-type (cdr handle)
43 "application/octet-stream"))
46 (mm-insert-part child)
47 (setq result (funcall rfc2015-decrypt-function))
49 (error "Decrypting error."))
50 (setq handles (mm-dissect-buffer t)))
51 (setq gnus-article-mime-handles
52 (append (if (listp (car gnus-article-mime-handles))
53 gnus-article-mime-handles
54 (list gnus-article-mime-handles))
55 (if (listp (car handles))
58 (gnus-mime-display-part handles)))
60 (if (y-or-n-p "Corrupted pgp-encrypted part. Abort?" )
61 (error "Corrupted pgp-encrypted part.")
62 (gnus-mime-display-mixed (cdr handle)))))))
64 ;; FIXME: mm-dissect-buffer loses information of micalg and the
65 ;; original header of signed part.
67 (defun rfc2015-verify (handle)
68 (if (y-or-n-p "Verify signed part?" )
69 (let (child result hash)
71 (unless (setq child (mm-find-part-by-type
72 (cdr handle) "application/pgp-signature" t))
73 (error "Corrupted pgp-signature part."))
74 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
75 (insert (format "Hash: %s\n\n" (read-string "Hash: " "SHA1")))
76 (mm-insert-part child)
77 (goto-char (point-max))
80 (unless (setq child (mm-find-part-by-type
81 (cdr handle) "application/pgp-signature"))
82 (error "Corrupted pgp-signature part."))
83 (mm-insert-part child)
84 (setq result (funcall rfc2015-verify-function))
86 (error "Verify error.")))))
87 (gnus-mime-display-part
89 (cdr handle) "application/pgp-signature" t)))
91 (defvar rfc2015-mailcrypt-prefix 0)
93 (defun rfc2015-mailcrypt-sign (cont)
94 (mailcrypt-sign rfc2015-mailcrypt-prefix)
96 (funcall mml-boundary-function (incf mml-multipart-number)))
97 (scheme-alist (funcall (or mc-default-scheme
98 (cdr (car mc-schemes)))))
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))
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." ))
111 (delete-region (point-min) (point))
112 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
114 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\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))))
130 (defun rfc2015-mailcrypt-encrypt (cont)
132 ;; You have to input the receiptant.
133 (mailcrypt-encrypt rfc2015-mailcrypt-prefix)
135 (funcall mml-boundary-function (incf mml-multipart-number))))
136 (goto-char (point-min))
137 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
139 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
140 (insert (format "--%s\n" boundary))
141 (insert "Content-Type: application/pgp-encrypted\n\n")
142 (insert "Version: 1\n\n")
143 (insert (format "--%s\n" boundary))
144 (insert "Content-Type: application/octet-stream\n\n")
145 (goto-char (point-max))
146 (insert (format "--%s--\n" boundary))
147 (goto-char (point-max))))
149 ;; The following code might be moved into mml.el or gnus-art.el.
151 (defvar mml-postprocess-alist
152 '(("pgp-sign" . rfc2015-mailcrypt-sign)
153 ("pgp-encrypt" . rfc2015-mailcrypt-encrypt))
154 "Alist of postprocess functions.")
156 (defun mml-postprocess (cont)
157 (let ((pp (cdr (or (assq 'postprocess cont)
160 (if (and pp (setq item (assoc pp mml-postprocess-alist)))
161 (funcall (cdr item) cont))))
163 (defun rfc2015-setup ()
164 (setq mml-generate-mime-postprocess-function 'mml-postprocess)
165 ; (push '("multipart/signed" . rfc2015-verify)
166 ; gnus-mime-multipart-functions)
167 (push '("multipart/encrypted" . rfc2015-decrypt)
168 gnus-mime-multipart-functions))
170 ;; The following code might be moved into mm-decode.el.
172 (defun mm-find-part-by-type (handles type &optional notp)
176 (not (equal (mm-handle-media-type (car handles)) type))
177 (equal (mm-handle-media-type (car handles)) type))
178 (setq handle (car handles)
180 (setq handles (cdr handles)))
185 ;;; rfc2015.el ends here