Update copyright header.
[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,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, 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 (defvar smime-output-buffer)
65 (defvar smime-errors-buffer)
66
67
68 ;;; @ Internal method for multipart/signed
69 ;;;
70 ;;; It is based on RFC 1847 (security-multipart).
71
72 (defun mime-verify-multipart/signed (entity situation)
73   "Internal method to verify multipart/signed."
74   (mime-play-entity
75    (nth 1 (mime-entity-children entity)) ; entity-info of signature
76    (list (assq 'mode situation)) ; play-mode
77    ))
78
79
80 ;;; @ internal method for application/pgp
81 ;;;
82 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
83
84 (defun mime-view-application/pgp (entity situation)
85   (let* ((p-win (or (get-buffer-window (current-buffer))
86                     (get-largest-window)))
87          (new-name
88           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
89          (mother (current-buffer))
90          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
91          representation-type message-buf)
92     (set-buffer (setq message-buf (get-buffer-create new-name)))
93     (erase-buffer)
94     (mime-insert-entity entity)
95     (cond ((progn
96              (goto-char (point-min))
97              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
98            (pgg-verify-region (match-beginning 0)(point-max) nil 'fetch)
99            (goto-char (point-min))
100            (delete-region
101             (point-min)
102             (and
103              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
104              (match-end 0)))
105            (delete-region
106             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
107                  (match-beginning 0))
108             (point-max))
109            (goto-char (point-min))
110            (while (re-search-forward "^- -" nil t)
111              (replace-match "-"))
112            (setq representation-type (if (mime-entity-cooked-p entity)
113                                          'cooked)))
114           ((progn
115              (goto-char (point-min))
116              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
117            (pgg-decrypt-region (point-min)(point-max))
118            (delete-region (point-min)(point-max))
119            (insert-buffer pgg-output-buffer)
120            (setq representation-type 'binary)))
121     (setq major-mode 'mime-show-message-mode)
122     (save-window-excursion
123       (mime-view-buffer nil preview-buffer mother
124                         nil representation-type)
125       (make-local-variable 'mime-view-temp-message-buffer)
126       (setq mime-view-temp-message-buffer message-buf))
127     (set-window-buffer p-win preview-buffer)))
128
129
130 ;;; @ Internal method for application/pgp-signature
131 ;;;
132 ;;; It is based on RFC 2015 (PGP/MIME) and
133 ;;; draft-yamamoto-openpgp-mime-00.txt (OpenPGP/MIME).
134
135 (defun mime-verify-application/pgp-signature (entity situation)
136   "Internal method to check PGP/MIME signature."
137   (let* ((entity-node-id (mime-entity-node-id entity))
138          (mother (mime-entity-parent entity))
139          (knum (car entity-node-id))
140          (onum (if (> knum 0)
141                    (1- knum)
142                  (1+ knum)))
143          (orig-entity (nth onum (mime-entity-children mother)))
144          (sig-file (make-temp-file "tm" nil ".asc")))
145     (save-excursion 
146       (mime-show-echo-buffer)
147       (set-buffer mime-echo-buffer-name)
148       (set-window-start 
149        (get-buffer-window mime-echo-buffer-name)
150        (point-max)))
151     (mime-write-entity-content entity sig-file)
152     (unwind-protect
153         (with-temp-buffer
154           (mime-insert-entity orig-entity)
155           (goto-char (point-min))
156           (while (progn (end-of-line) (not (eobp)))
157             (insert "\r")
158             (forward-line 1))
159           (pgg-verify-region (point-min)(point-max) 
160                              sig-file 'fetch)
161           (save-excursion 
162             (set-buffer mime-echo-buffer-name)
163             (insert-buffer-substring pgg-errors-buffer)))
164       (delete-file sig-file))))
165
166
167 ;;; @ Internal method for application/pgp-encrypted
168 ;;;
169 ;;; It is based on RFC 2015 (PGP/MIME) and
170 ;;; draft-yamamoto-openpgp-mime-00.txt (OpenPGP/MIME).
171
172 (defun mime-decrypt-application/pgp-encrypted (entity situation)
173   (let* ((entity-node-id (mime-entity-node-id entity))
174          (mother (mime-entity-parent entity))
175          (knum (car entity-node-id))
176          (onum (if (> knum 0)
177                    (1- knum)
178                  (1+ knum)))
179          (orig-entity (nth onum (mime-entity-children mother))))
180     (mime-view-application/pgp orig-entity situation)))
181
182
183 ;;; @ Internal method for application/pgp-keys
184 ;;;
185 ;;; It is based on RFC 2015 (PGP/MIME) and
186 ;;; draft-yamamoto-openpgp-mime-00.txt (OpenPGP/MIME).
187
188 (defun mime-add-application/pgp-keys (entity situation)
189   (save-excursion 
190     (mime-show-echo-buffer)
191     (set-buffer mime-echo-buffer-name)
192     (set-window-start 
193      (get-buffer-window mime-echo-buffer-name)
194      (point-max)))
195   (with-temp-buffer
196     (mime-insert-entity-content entity)
197     (mime-decode-region (point-min) (point-max)
198                         (cdr (assq 'encoding situation)))
199     (pgg-snarf-keys-region (point-min)(point-max))
200     (save-excursion 
201       (set-buffer mime-echo-buffer-name)
202       (insert-buffer-substring pgg-errors-buffer))))
203
204
205 ;;; @ Internal method for application/pkcs7-signature
206 ;;;
207 ;;; It is based on RFC 2633 (S/MIME version 3).
208
209 (defun mime-verify-application/pkcs7-signature (entity situation)
210   "Internal method to check S/MIME signature."
211   (let* ((entity-node-id (mime-entity-node-id entity))
212          (mother (mime-entity-parent entity))
213          (knum (car entity-node-id))
214          (onum (if (> knum 0)
215                    (1- knum)
216                  (1+ knum)))
217          (orig-entity (nth onum (mime-entity-children mother)))
218          (sig-file (make-temp-file "tm" nil ".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          message-buf)
255     (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
256                 '(enveloped-data signed-data))
257       (set-buffer (setq message-buf (get-buffer-create new-name)))
258       (let ((inhibit-read-only t)
259             buffer-read-only)
260         (erase-buffer)
261         (mime-insert-entity entity)
262         (smime-decrypt-region (point-min)(point-max))
263         (delete-region (point-min)(point-max))
264         (insert-buffer smime-output-buffer))
265       (setq major-mode 'mime-show-message-mode)
266       (save-window-excursion
267         (mime-view-buffer nil preview-buffer mother
268                           nil 'binary)
269         (make-local-variable 'mime-view-temp-message-buffer)
270         (setq mime-view-temp-message-buffer message-buf))
271       (set-window-buffer p-win preview-buffer))))
272
273 (defun mime-display-multipart/pgp-encrypted (entity situation)
274    (if pgg-decrypt-automatically
275        (let ((pgp-begin (point)))
276          (mime-insert-entity entity)
277          (goto-char pgp-begin)
278          (when (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
279            (pgg-decrypt-region pgp-begin (point-max))
280            (delete-region pgp-begin (point-max))
281            (mime-display-entity
282             (mime-parse-buffer pgg-output-buffer))))
283      (mime-display-multipart/mixed entity situation)))
284
285 ;;; @ end
286 ;;;
287
288 (provide 'mime-pgp)
289
290 (run-hooks 'mime-pgp-load-hook)
291
292 ;;; mime-pgp.el ends here