1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Daiki Ueno <ueno@unixuser.org>
8 ;; Renamed: 1997/2/27 from tm-pgp.el
9 ;; Keywords: PGP, security, MIME, multimedia, mail, news
11 ;; This file is part of SEMI (Secure Emacs MIME Interface).
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.
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.
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.
30 ;; This module is based on
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)
38 ;; [PGP/MIME] RFC 2015: "MIME Security with Pretty Good Privacy
39 ;; (PGP)" by Michael Elkins <elkins@aero.org> (1996/6)
41 ;; [PGP-kazu] draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
42 ;; by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp> (1995/10;
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)
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)
70 ;;; @ Internal method for multipart/signed
72 ;;; It is based on RFC 1847 (security-multipart).
74 (defun mime-verify-multipart/signed (entity situation)
75 "Internal method to verify multipart/signed."
77 (nth 1 (mime-entity-children entity)) ; entity-info of signature
78 (list (assq 'mode situation)) ; play-mode
82 ;;; @ internal method for application/pgp
84 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
86 (defun mime-view-application/pgp (entity situation)
87 (let* ((p-win (or (get-buffer-window (current-buffer))
88 (get-largest-window)))
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)))
96 (mime-insert-entity entity)
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))
105 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
108 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
111 (goto-char (point-min))
112 (while (re-search-forward "^- -" nil t)
114 (setq representation-type (if (mime-entity-cooked-p entity)
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)))
132 ;;; @ Internal method for application/pgp-signature
134 ;;; It is based on RFC 2015 (PGP/MIME) and
135 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
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))
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"))
150 (mime-show-echo-buffer)
151 (set-buffer mime-echo-buffer-name)
153 (get-buffer-window mime-echo-buffer-name)
155 (mime-write-entity-content entity sig-file)
158 (mime-insert-entity orig-entity)
159 (goto-char (point-min))
160 (while (progn (end-of-line) (not (eobp)))
163 (setq status (pgg-verify-region (point-min)(point-max)
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))))
172 ;;; @ Internal method for application/pgp-encrypted
174 ;;; It is based on RFC 2015 (PGP/MIME) and
175 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
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))
184 (orig-entity (nth onum (mime-entity-children mother))))
185 (mime-view-application/pgp orig-entity situation)))
188 ;;; @ Internal method for application/pgp-keys
190 ;;; It is based on RFC 2015 (PGP/MIME) and
191 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
193 (defun mime-add-application/pgp-keys (entity situation)
195 (mime-show-echo-buffer)
196 (set-buffer mime-echo-buffer-name)
198 (get-buffer-window mime-echo-buffer-name)
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))))
206 (set-buffer mime-echo-buffer-name)
207 (insert-buffer-substring (if status pgg-output-buffer
208 pgg-errors-buffer))))))
211 ;;; @ Internal method for application/pkcs7-signature
213 ;;; It is based on RFC 2633 (S/MIME version 3).
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))
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"))
228 (mime-show-echo-buffer)
229 (set-buffer mime-echo-buffer-name)
231 (get-buffer-window mime-echo-buffer-name)
233 (mime-write-entity entity sig-file)
236 (mime-insert-entity orig-entity)
237 (goto-char (point-min))
238 (while (progn (end-of-line) (not (eobp)))
241 (setq status (smime-verify-region (point-min)(point-max)
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))))
250 ;;; @ Internal method for application/pkcs7-mime
252 ;;; It is based on RFC 2633 (S/MIME version 3).
254 (defun mime-view-application/pkcs7-mime (entity situation)
255 (let* ((p-win (or (get-buffer-window (current-buffer))
256 (get-largest-window)))
258 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
259 (mother (current-buffer))
260 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
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)
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
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))))
286 (run-hooks 'mime-pgp-load-hook)
288 ;;; mime-pgp.el ends here