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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, 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)
59 (epg-check-configuration (epg-configuration))
60 (autoload 'epg-make-context "epg")
64 (autoload 'pgg-decrypt-region "pgg"
65 "PGP decryption of current region." t)
66 (autoload 'pgg-verify-region "pgg"
67 "PGP verification of current region." t)
68 (autoload 'pgg-snarf-keys-region "pgg"
69 "Snarf PGP public keys in current region." t)
71 "Which PGG library to be used.")
73 ;;; @ Internal method for multipart/signed
75 ;;; It is based on RFC 1847 (security-multipart).
77 (defun mime-verify-multipart/signed (entity situation)
78 "Internal method to verify multipart/signed."
80 (nth 1 (mime-entity-children entity)) ; entity-info of signature
81 (list (assq 'mode situation)) ; play-mode
85 ;;; @ internal method for application/pgp
87 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
89 (defun mime-view-application/pgp-with-epg (entity situation)
90 (let* ((p-win (or (get-buffer-window (current-buffer))
91 (get-largest-window)))
93 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
94 (mother (current-buffer))
95 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
96 representation-type message-buf context plain)
97 (set-buffer (setq message-buf (get-buffer-create new-name)))
99 (mime-insert-entity entity)
101 (goto-char (point-min))
102 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
103 (setq context (epg-make-context))
106 (buffer-substring (match-beginning 0)(point-max)))
107 (goto-char (point-min))
111 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
114 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
117 (goto-char (point-min))
118 (while (re-search-forward "^- -" nil t)
120 (setq representation-type (if (mime-entity-cooked-p entity)
123 (goto-char (point-min))
124 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
125 (setq context (epg-make-context))
127 (decode-coding-string
130 (buffer-substring (point-min)(point-max)))
132 (delete-region (point-min)(point-max))
134 (setq representation-type 'binary)
136 (setq major-mode 'mime-show-message-mode)
137 (save-window-excursion
138 (mime-view-buffer nil preview-buffer mother
139 nil representation-type)
140 (make-local-variable 'mime-view-temp-message-buffer)
141 (setq mime-view-temp-message-buffer message-buf))
142 (set-window-buffer p-win preview-buffer)
144 (epg-context-result-for context 'verify))
145 (epa-display-verify-result (epg-context-result-for context 'verify)))))
147 (defun mime-view-application/pgp-with-pgg (entity situation)
148 (let* ((p-win (or (get-buffer-window (current-buffer))
149 (get-largest-window)))
151 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
152 (mother (current-buffer))
153 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
154 representation-type message-buf)
155 (set-buffer (setq message-buf (get-buffer-create new-name)))
157 (mime-insert-entity entity)
159 (goto-char (point-min))
160 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
161 (pgg-verify-region (match-beginning 0)(point-max) nil 'fetch)
162 (goto-char (point-min))
166 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
169 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
172 (goto-char (point-min))
173 (while (re-search-forward "^- -" nil t)
175 (setq representation-type (if (mime-entity-cooked-p entity)
178 (goto-char (point-min))
179 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
180 (pgg-decrypt-region (point-min)(point-max))
181 (delete-region (point-min)(point-max))
182 (insert-buffer pgg-output-buffer)
183 (setq representation-type 'binary)))
184 (setq major-mode 'mime-show-message-mode)
185 (save-window-excursion
186 (mime-view-buffer nil preview-buffer mother
187 nil representation-type)
188 (make-local-variable 'mime-view-temp-message-buffer)
189 (setq mime-view-temp-message-buffer message-buf))
190 (set-window-buffer p-win preview-buffer)))
193 (defun mime-verify-application/*-signature (entity situation)
194 (if (eq mime-pgp-use 'epg)
195 (mime-verify-application/*-signature-with-epg entity situation)
196 (mime-verify-application/*-signature-with-pgg entity situation)))
198 (defun mime-verify-application/*-signature-with-epg (entity situation)
199 (let* ((entity-node-id (mime-entity-node-id entity))
200 (mother (mime-entity-parent entity))
201 (knum (car entity-node-id))
205 (orig-entity (nth onum (mime-entity-children mother)))
206 (protocol (cdr (assoc "protocol" (mime-entity-parameters mother))))
207 (context (epg-make-context
208 (if (equal protocol "application/pgp-signature")
211 "\\`application/\\(x-\\)?pkcs7-signature\\'"
214 (error "Unknown protocol: %s" protocol))))))
215 (epg-verify-string context
216 (mime-entity-content entity)
218 (if (fboundp 'set-buffer-multibyte)
219 (set-buffer-multibyte nil))
220 (mime-insert-entity orig-entity)
221 (goto-char (point-min))
222 (while (search-forward "\n" nil t)
223 (replace-match "\r\n"))
224 (buffer-substring (point-min) (point-max))))
225 (if (epg-context-result-for context 'verify)
226 (epa-display-verify-result (epg-context-result-for context 'verify)))))
228 (defun mime-verify-application/*-signature-with-pgg (entity situation)
229 (let* ((entity-node-id (mime-entity-node-id entity))
230 (mother (mime-entity-parent entity))
231 (knum (car entity-node-id))
235 (orig-entity (nth onum (mime-entity-children mother)))
236 (protocol (cdr (assoc "protocol" (mime-entity-parameters mother))))
237 (sig-file (make-temp-file "tm" nil ".asc")))
238 (unless (equal protocol "application/pgp-signature")
239 (error "Not supported protocol: %s" protocol))
241 (mime-show-echo-buffer)
242 (set-buffer mime-echo-buffer-name)
244 (get-buffer-window mime-echo-buffer-name)
246 (mime-write-entity-content entity sig-file)
249 (mime-insert-entity orig-entity)
250 (goto-char (point-min))
251 (while (progn (end-of-line) (not (eobp)))
254 (pgg-verify-region (point-min)(point-max)
257 (set-buffer mime-echo-buffer-name)
258 (insert-buffer-substring pgg-errors-buffer)))
259 (delete-file sig-file))))
262 ;;; @ Internal method for application/pgp-encrypted
264 ;;; It is based on RFC 2015 (PGP/MIME) and
265 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
267 (defun mime-decrypt-application/pgp-encrypted (entity situation)
268 (let* ((entity-node-id (mime-entity-node-id entity))
269 (mother (mime-entity-parent entity))
270 (knum (car entity-node-id))
274 (orig-entity (nth onum (mime-entity-children mother))))
275 (if (eq mime-pgp-use 'epg)
276 (mime-view-application/pgp-with-epg orig-entity situation)
277 (mime-view-application/pgp-with-pgg orig-entity situation))))
280 ;;; @ Internal method for application/pgp-keys
282 ;;; It is based on RFC 2015 (PGP/MIME) and
283 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
285 (defun mime-add-application/pgp-keys (entity situation)
286 (if (eq mime-pgp-use 'epg)
287 (mime-add-application/pgp-keys-with-epg entity situation)
288 (mime-add-application/pgp-keys-with-pgg entity situation)))
290 (defun mime-add-application/pgp-keys-with-epg (entity situation)
291 (epg-import-keys-from-string (epg-make-context)
292 (mime-entity-content entity)))
294 (defun mime-add-application/pgp-keys-with-pgg (entity situation)
296 (mime-show-echo-buffer)
297 (set-buffer mime-echo-buffer-name)
299 (get-buffer-window mime-echo-buffer-name)
302 (mime-insert-entity-content entity)
303 (mime-decode-region (point-min) (point-max)
304 (cdr (assq 'encoding situation)))
305 (pgg-snarf-keys-region (point-min)(point-max))
307 (set-buffer mime-echo-buffer-name)
308 (insert-buffer-substring pgg-errors-buffer))))
310 ;;; @ Internal method for application/pkcs7-mime
312 ;;; It is based on RFC 2633 (S/MIME version 3).
314 (defun mime-view-application/pkcs7-mime (entity situation)
315 (let* ((p-win (or (get-buffer-window (current-buffer))
316 (get-largest-window)))
318 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
319 (mother (current-buffer))
320 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
321 (context (epg-make-context 'CMS))
323 (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
324 '(enveloped-data signed-data))
325 (set-buffer (setq message-buf (get-buffer-create new-name)))
326 (let ((inhibit-read-only t)
329 (insert (epg-decrypt-string context (mime-entity-content entity))))
330 (setq major-mode 'mime-show-message-mode)
331 (save-window-excursion
332 (mime-view-buffer nil preview-buffer mother
334 (make-local-variable 'mime-view-temp-message-buffer)
335 (setq mime-view-temp-message-buffer message-buf))
336 (set-window-buffer p-win preview-buffer))))
344 (run-hooks 'mime-pgp-load-hook)
346 ;;; mime-pgp.el ends here