7bfdb614c49deaf7c2d281a1f243e59012f8e69d
[elisp/semi.git] / mime-pgp.el
1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;      Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Created: 1995/12/7
8 ;;      Renamed: 1997/2/27 from tm-pgp.el
9 ;; Keywords: PGP, security, MIME, multimedia, mail, news
10
11 ;; This file is part of SEMI (Secure Emacs MIME Interface).
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;    This module is based on
31
32 ;;      [security-multipart] RFC 1847: "Security Multiparts for MIME:
33 ;;          Multipart/Signed and Multipart/Encrypted" by
34 ;;          Jim Galvin <galvin@tis.com>, Sandy Murphy <sandy@tis.com>,
35 ;;          Steve Crocker <crocker@cybercash.com> and
36 ;;          Ned Freed <ned@innosoft.com> (1995/10)
37
38 ;;      [PGP/MIME] RFC 2015: "MIME Security with Pretty Good Privacy
39 ;;          (PGP)" by Michael Elkins <elkins@aero.org> (1996/6)
40
41 ;;      [PGP-kazu] draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
42 ;;          by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp> (1995/10;
43 ;;          expired)
44
45 ;;      [OpenPGP/MIME] draft-yamamoto-openpgp-mime-00.txt: "MIME
46 ;;          Security with OpenPGP (OpenPGP/MIME)" by Kazuhiko YAMAMOTO
47 ;;          <kazu@iijlab.net> (1998/1)
48
49 ;;; Code:
50
51 (require 'mime-play)
52 (require 'pgg-def)
53
54 (autoload 'pgg-decrypt-region "pgg"
55   "PGP decryption of current region." t)
56 (autoload 'pgg-verify-region "pgg"
57   "PGP verification of current region." t)
58 (autoload 'pgg-snarf-keys-region "pgg"
59   "Snarf PGP public keys in current region." t)
60 (autoload 'smime-decrypt-region "smime"
61   "S/MIME decryption of current region.")
62 (autoload 'smime-verify-region "smime"
63   "S/MIME verification of current region.")
64
65
66 ;;; @ Internal method for multipart/signed
67 ;;;
68 ;;; It is based on RFC 1847 (security-multipart).
69
70 (defun mime-verify-multipart/signed (entity situation)
71   "Internal method to verify multipart/signed."
72   (mime-play-entity
73    (nth 1 (mime-entity-children entity)) ; entity-info of signature
74    (list (assq 'mode situation)) ; play-mode
75    ))
76
77
78 ;;; @ internal method for application/pgp
79 ;;;
80 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
81
82 (defun mime-view-application/pgp (entity situation)
83   (let* ((p-win (or (get-buffer-window (current-buffer))
84                     (get-largest-window)))
85          (new-name
86           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
87          (mother (current-buffer))
88          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
89          representation-type)
90     (set-buffer (get-buffer-create new-name))
91     (erase-buffer)
92     (mime-insert-entity entity)
93     (cond ((progn
94              (goto-char (point-min))
95              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
96            (pgg-verify-region (match-beginning 0)(point-max) nil 'fetch)
97            (goto-char (point-min))
98            (delete-region
99             (point-min)
100             (and
101              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
102              (match-end 0)))
103            (delete-region
104             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
105                  (match-beginning 0))
106             (point-max))
107            (goto-char (point-min))
108            (while (re-search-forward "^- -" nil t)
109              (replace-match "-"))
110            (setq representation-type (if (mime-entity-cooked-p entity)
111                                          'cooked)))
112           ((progn
113              (goto-char (point-min))
114              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
115            (pgg-decrypt-region (point-min)(point-max))
116            (delete-region (point-min)(point-max))
117            (insert-buffer pgg-output-buffer)
118            (setq representation-type 'binary)))
119     (setq major-mode 'mime-show-message-mode)
120     (save-window-excursion (mime-view-buffer nil preview-buffer mother
121                                              nil representation-type))
122     (set-window-buffer p-win preview-buffer)))
123
124
125 ;;; @ Internal method for application/pgp-signature
126 ;;;
127 ;;; It is based on RFC 2015 (PGP/MIME) and
128 ;;; draft-yamamoto-openpgp-mime-00.txt (OpenPGP/MIME).
129
130 (defun mime-verify-application/pgp-signature (entity situation)
131   "Internal method to check PGP/MIME signature."
132   (let* ((entity-node-id (mime-entity-node-id entity))
133          (mother (mime-entity-parent entity))
134          (knum (car entity-node-id))
135          (onum (if (> knum 0)
136                    (1- knum)
137                  (1+ knum)))
138          (orig-entity (nth onum (mime-entity-children mother)))
139          (basename (expand-file-name "tm" temporary-file-directory))
140          (sig-file (concat (make-temp-name basename) ".asc"))
141          status)
142     (save-excursion 
143       (mime-show-echo-buffer)
144       (set-buffer mime-echo-buffer-name)
145       (set-window-start 
146        (get-buffer-window mime-echo-buffer-name)
147        (point-max)))
148     (mime-write-entity-content entity sig-file)
149     (unwind-protect
150         (with-temp-buffer
151           (mime-insert-entity orig-entity)
152           (goto-char (point-min))
153           (while (progn (end-of-line) (not (eobp)))
154             (insert "\r")
155             (forward-line 1))
156           (setq status (pgg-verify-region (point-min)(point-max) 
157                                           sig-file 'fetch))
158           (save-excursion 
159             (set-buffer mime-echo-buffer-name)
160             (insert-buffer-substring (if status pgg-output-buffer
161                                        pgg-errors-buffer))))
162       (delete-file sig-file))))
163
164
165 ;;; @ Internal method for application/pgp-encrypted
166 ;;;
167 ;;; It is based on RFC 2015 (PGP/MIME) and
168 ;;; draft-yamamoto-openpgp-mime-00.txt (OpenPGP/MIME).
169
170 (defun mime-decrypt-application/pgp-encrypted (entity situation)
171   (let* ((entity-node-id (mime-entity-node-id entity))
172          (mother (mime-entity-parent entity))
173          (knum (car entity-node-id))
174          (onum (if (> knum 0)
175                    (1- knum)
176                  (1+ knum)))
177          (orig-entity (nth onum (mime-entity-children mother))))
178     (mime-view-application/pgp orig-entity situation)))
179
180
181 ;;; @ Internal method for application/pgp-keys
182 ;;;
183 ;;; It is based on RFC 2015 (PGP/MIME) and
184 ;;; draft-yamamoto-openpgp-mime-00.txt (OpenPGP/MIME).
185
186 (defun mime-add-application/pgp-keys (entity situation)
187   (save-excursion 
188     (mime-show-echo-buffer)
189     (set-buffer mime-echo-buffer-name)
190     (set-window-start 
191      (get-buffer-window mime-echo-buffer-name)
192      (point-max)))
193   (with-temp-buffer
194     (mime-insert-entity-content entity)
195     (mime-decode-region (point-min) (point-max)
196                         (cdr (assq 'encoding situation)))
197     (let ((status (pgg-snarf-keys-region (point-min)(point-max))))
198       (save-excursion 
199         (set-buffer mime-echo-buffer-name)
200         (insert-buffer-substring (if status pgg-output-buffer
201                                    pgg-errors-buffer))))))
202
203
204 ;;; @ Internal method for application/pkcs7-signature
205 ;;;
206 ;;; It is based on RFC 2633 (S/MIME version 3).
207
208 (defun mime-verify-application/pkcs7-signature (entity situation)
209   "Internal method to check S/MIME signature."
210   (let* ((entity-node-id (mime-entity-node-id entity))
211          (mother (mime-entity-parent entity))
212          (knum (car entity-node-id))
213          (onum (if (> knum 0)
214                    (1- knum)
215                  (1+ knum)))
216          (orig-entity (nth onum (mime-entity-children mother)))
217          (basename (expand-file-name "tm" temporary-file-directory))
218          (sig-file (concat (make-temp-name basename) ".asc"))
219          status)
220     (save-excursion 
221       (mime-show-echo-buffer)
222       (set-buffer mime-echo-buffer-name)
223       (set-window-start 
224        (get-buffer-window mime-echo-buffer-name)
225        (point-max)))
226     (mime-write-entity entity sig-file)
227     (unwind-protect
228         (with-temp-buffer
229           (mime-insert-entity orig-entity)
230           (goto-char (point-min))
231           (while (progn (end-of-line) (not (eobp)))
232             (insert "\r")
233             (forward-line 1))
234           (setq status (smime-verify-region (point-min)(point-max) 
235                                             sig-file))
236           (save-excursion 
237             (set-buffer mime-echo-buffer-name)
238             (insert-buffer-substring (if status smime-output-buffer
239                                        smime-errors-buffer))))
240       (delete-file sig-file))))
241
242
243 ;;; @ Internal method for application/pkcs7-mime
244 ;;;
245 ;;; It is based on RFC 2633 (S/MIME version 3).
246
247 (defun mime-view-application/pkcs7-mime (entity situation)
248   (let* ((p-win (or (get-buffer-window (current-buffer))
249                     (get-largest-window)))
250          (new-name
251           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
252          (mother (current-buffer))
253          (preview-buffer (concat "*Preview-" (buffer-name) "*")))
254     (when (memq (or (cdr (assq 'smime-type situation)) enveloped-data)
255                 '(enveloped-data signed-data))
256       (set-buffer (get-buffer-create new-name))
257       (let ((inhibit-read-only t)
258             buffer-read-only)
259         (erase-buffer)
260         (mime-insert-entity entity)
261         (smime-decrypt-region (point-min)(point-max))
262         (delete-region (point-min)(point-max))
263         (insert-buffer smime-output-buffer))
264       (setq major-mode 'mime-show-message-mode)
265       (save-window-excursion (mime-view-buffer nil preview-buffer mother
266                                                nil 'binary))
267       (set-window-buffer p-win preview-buffer))))
268
269
270 ;;; @ end
271 ;;;
272
273 (provide 'mime-pgp)
274
275 (run-hooks 'mime-pgp-load-hook)
276
277 ;;; mime-pgp.el ends here