* mime-vcard.el: New module.
[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@unixuser.org>
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-ietf-openpgp-mime-02.txt: "MIME
46 ;;          Security with OpenPGP" by
47 ;;          John W. Noerenberg II <jwn2@qualcomm.com>,
48 ;;          Dave Del Torto <ddt@cryptorights.org> and
49 ;;          Michael Elkins <michael_elkins@nai.com> (2000/8)
50
51 ;;; Code:
52
53 (require 'mime-play)
54 (require 'pgg-def)
55
56 (autoload 'pgg-decrypt-region "pgg"
57   "PGP decryption of current region." t)
58 (autoload 'pgg-verify-region "pgg"
59   "PGP verification of current region." t)
60 (autoload 'pgg-snarf-keys-region "pgg"
61   "Snarf PGP public keys in current region." t)
62 (autoload 'smime-decrypt-region "smime"
63   "S/MIME decryption of current region.")
64 (autoload 'smime-verify-region "smime"
65   "S/MIME verification of current region.")
66 (defvar smime-output-buffer)
67 (defvar smime-errors-buffer)
68
69
70 ;;; @ Internal method for multipart/signed
71 ;;;
72 ;;; It is based on RFC 1847 (security-multipart).
73
74 (defun mime-verify-multipart/signed (entity situation)
75   "Internal method to verify multipart/signed."
76   (mime-play-entity
77    (nth 1 (mime-entity-children entity)) ; entity-info of signature
78    (list (assq 'mode situation)) ; play-mode
79    ))
80
81
82 ;;; @ internal method for application/pgp
83 ;;;
84 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
85
86 (defun mime-view-application/pgp (entity situation)
87   (let* ((p-win (or (get-buffer-window (current-buffer))
88                     (get-largest-window)))
89          (new-name
90           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
91          (mother (current-buffer))
92          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
93          representation-type message-buf)
94     (set-buffer (setq message-buf (get-buffer-create new-name)))
95     (erase-buffer)
96     (mime-insert-entity entity)
97     (cond ((progn
98              (goto-char (point-min))
99              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
100            (pgg-verify-region (match-beginning 0)(point-max) nil 'fetch)
101            (goto-char (point-min))
102            (delete-region
103             (point-min)
104             (and
105              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
106              (match-end 0)))
107            (delete-region
108             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
109                  (match-beginning 0))
110             (point-max))
111            (goto-char (point-min))
112            (while (re-search-forward "^- -" nil t)
113              (replace-match "-"))
114            (setq representation-type (if (mime-entity-cooked-p entity)
115                                          'cooked)))
116           ((progn
117              (goto-char (point-min))
118              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
119            (pgg-decrypt-region (point-min)(point-max))
120            (delete-region (point-min)(point-max))
121            (insert-buffer pgg-output-buffer)
122            (setq representation-type 'binary)))
123     (setq major-mode 'mime-show-message-mode)
124     (save-window-excursion
125       (mime-view-buffer nil preview-buffer mother
126                         nil representation-type)
127       (make-local-variable 'mime-view-temp-message-buffer)
128       (setq mime-view-temp-message-buffer message-buf))
129     (set-window-buffer p-win preview-buffer)))
130
131
132 ;;; @ Internal method for application/pgp-signature
133 ;;;
134 ;;; It is based on RFC 2015 (PGP/MIME) and
135 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
136
137 (defun mime-verify-application/pgp-signature (entity situation)
138   "Internal method to check PGP/MIME signature."
139   (let* ((entity-node-id (mime-entity-node-id entity))
140          (mother (mime-entity-parent entity))
141          (knum (car entity-node-id))
142          (onum (if (> knum 0)
143                    (1- knum)
144                  (1+ knum)))
145          (orig-entity (nth onum (mime-entity-children mother)))
146          (basename (expand-file-name "tm" temporary-file-directory))
147          (sig-file (concat (make-temp-name basename) ".asc"))
148          status)
149     (save-excursion 
150       (mime-show-echo-buffer)
151       (set-buffer mime-echo-buffer-name)
152       (set-window-start 
153        (get-buffer-window mime-echo-buffer-name)
154        (point-max)))
155     (mime-write-entity-content entity sig-file)
156     (unwind-protect
157         (with-temp-buffer
158           (mime-insert-entity orig-entity)
159           (goto-char (point-min))
160           (while (progn (end-of-line) (not (eobp)))
161             (insert "\r")
162             (forward-line 1))
163           (setq status (pgg-verify-region (point-min)(point-max) 
164                                           sig-file 'fetch))
165           (save-excursion 
166             (set-buffer mime-echo-buffer-name)
167             (insert-buffer-substring (if status pgg-output-buffer
168                                        pgg-errors-buffer))))
169       (delete-file sig-file))))
170
171
172 ;;; @ Internal method for application/pgp-encrypted
173 ;;;
174 ;;; It is based on RFC 2015 (PGP/MIME) and
175 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
176
177 (defun mime-decrypt-application/pgp-encrypted (entity situation)
178   (let* ((entity-node-id (mime-entity-node-id entity))
179          (mother (mime-entity-parent entity))
180          (knum (car entity-node-id))
181          (onum (if (> knum 0)
182                    (1- knum)
183                  (1+ knum)))
184          (orig-entity (nth onum (mime-entity-children mother))))
185     (mime-view-application/pgp orig-entity situation)))
186
187
188 ;;; @ Internal method for application/pgp-keys
189 ;;;
190 ;;; It is based on RFC 2015 (PGP/MIME) and
191 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
192
193 (defun mime-add-application/pgp-keys (entity situation)
194   (save-excursion 
195     (mime-show-echo-buffer)
196     (set-buffer mime-echo-buffer-name)
197     (set-window-start 
198      (get-buffer-window mime-echo-buffer-name)
199      (point-max)))
200   (with-temp-buffer
201     (mime-insert-entity-content entity)
202     (mime-decode-region (point-min) (point-max)
203                         (cdr (assq 'encoding situation)))
204     (let ((status (pgg-snarf-keys-region (point-min)(point-max))))
205       (save-excursion 
206         (set-buffer mime-echo-buffer-name)
207         (insert-buffer-substring (if status pgg-output-buffer
208                                    pgg-errors-buffer))))))
209
210
211 ;;; @ Internal method for application/pkcs7-signature
212 ;;;
213 ;;; It is based on RFC 2633 (S/MIME version 3).
214
215 (defun mime-verify-application/pkcs7-signature (entity situation)
216   "Internal method to check S/MIME signature."
217   (let* ((entity-node-id (mime-entity-node-id entity))
218          (mother (mime-entity-parent entity))
219          (knum (car entity-node-id))
220          (onum (if (> knum 0)
221                    (1- knum)
222                  (1+ knum)))
223          (orig-entity (nth onum (mime-entity-children mother)))
224          (basename (expand-file-name "tm" temporary-file-directory))
225          (sig-file (concat (make-temp-name basename) ".asc"))
226          status)
227     (save-excursion 
228       (mime-show-echo-buffer)
229       (set-buffer mime-echo-buffer-name)
230       (set-window-start 
231        (get-buffer-window mime-echo-buffer-name)
232        (point-max)))
233     (mime-write-entity entity sig-file)
234     (unwind-protect
235         (with-temp-buffer
236           (mime-insert-entity orig-entity)
237           (goto-char (point-min))
238           (while (progn (end-of-line) (not (eobp)))
239             (insert "\r")
240             (forward-line 1))
241           (setq status (smime-verify-region (point-min)(point-max) 
242                                             sig-file))
243           (save-excursion 
244             (set-buffer mime-echo-buffer-name)
245             (insert-buffer-substring (if status smime-output-buffer
246                                        smime-errors-buffer))))
247       (delete-file sig-file))))
248
249
250 ;;; @ Internal method for application/pkcs7-mime
251 ;;;
252 ;;; It is based on RFC 2633 (S/MIME version 3).
253
254 (defun mime-view-application/pkcs7-mime (entity situation)
255   (let* ((p-win (or (get-buffer-window (current-buffer))
256                     (get-largest-window)))
257          (new-name
258           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
259          (mother (current-buffer))
260          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
261          message-buf)
262     (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
263                 '(enveloped-data signed-data))
264       (set-buffer (setq message-buf (get-buffer-create new-name)))
265       (let ((inhibit-read-only t)
266             buffer-read-only)
267         (erase-buffer)
268         (mime-insert-entity entity)
269         (smime-decrypt-region (point-min)(point-max))
270         (delete-region (point-min)(point-max))
271         (insert-buffer smime-output-buffer))
272       (setq major-mode 'mime-show-message-mode)
273       (save-window-excursion
274         (mime-view-buffer nil preview-buffer mother
275                           nil 'binary)
276         (make-local-variable 'mime-view-temp-message-buffer)
277         (setq mime-view-temp-message-buffer message-buf))
278       (set-window-buffer p-win preview-buffer))))
279
280
281 ;;; @ end
282 ;;;
283
284 (provide 'mime-pgp)
285
286 (run-hooks 'mime-pgp-load-hook)
287
288 ;;; mime-pgp.el ends here