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)
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)
63 ;;; @ Internal method for multipart/signed
65 ;;; It is based on RFC 1847 (security-multipart).
67 (defun mime-verify-multipart/signed (entity situation)
68 "Internal method to verify multipart/signed."
70 (nth 1 (mime-entity-children entity)) ; entity-info of signature
71 (list (assq 'mode situation)) ; play-mode
75 ;;; @ internal method for application/pgp
77 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
79 (defun mime-view-application/pgp (entity situation)
80 (let* ((p-win (or (get-buffer-window (current-buffer))
81 (get-largest-window)))
83 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
84 (mother (current-buffer))
85 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
86 representation-type message-buf)
87 (set-buffer (setq message-buf (get-buffer-create new-name)))
89 (mime-insert-entity entity)
91 (goto-char (point-min))
92 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
93 (pgg-verify-region (match-beginning 0)(point-max) nil 'fetch)
94 (goto-char (point-min))
98 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
101 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
104 (goto-char (point-min))
105 (while (re-search-forward "^- -" nil t)
107 (setq representation-type (if (mime-entity-cooked-p entity)
110 (goto-char (point-min))
111 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
112 (pgg-decrypt-region (point-min)(point-max))
113 (delete-region (point-min)(point-max))
114 (insert-buffer pgg-output-buffer)
115 (setq representation-type 'binary)))
116 (setq major-mode 'mime-show-message-mode)
117 (save-window-excursion
118 (mime-view-buffer nil preview-buffer mother
119 nil representation-type)
120 (make-local-variable 'mime-view-temp-message-buffer)
121 (setq mime-view-temp-message-buffer message-buf))
122 (set-window-buffer p-win preview-buffer)))
125 ;;; @ Internal method for application/pgp-signature
127 ;;; It is based on RFC 2015 (PGP/MIME) and
128 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
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))
138 (orig-entity (nth onum (mime-entity-children mother)))
139 (sig-file (make-temp-file "tm" nil ".asc"))
142 (mime-show-echo-buffer)
143 (set-buffer mime-echo-buffer-name)
145 (get-buffer-window mime-echo-buffer-name)
147 (mime-write-entity-content entity sig-file)
150 (mime-insert-entity orig-entity)
151 (goto-char (point-min))
152 (while (progn (end-of-line) (not (eobp)))
155 (setq status (pgg-verify-region (point-min)(point-max)
158 (set-buffer mime-echo-buffer-name)
159 (insert-buffer-substring (if status pgg-output-buffer
160 pgg-errors-buffer))))
161 (delete-file sig-file))))
164 ;;; @ Internal method for application/pgp-encrypted
166 ;;; It is based on RFC 2015 (PGP/MIME) and
167 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
169 (defun mime-decrypt-application/pgp-encrypted (entity situation)
170 (let* ((entity-node-id (mime-entity-node-id entity))
171 (mother (mime-entity-parent entity))
172 (knum (car entity-node-id))
176 (orig-entity (nth onum (mime-entity-children mother))))
177 (mime-view-application/pgp orig-entity situation)))
180 ;;; @ Internal method for application/pgp-keys
182 ;;; It is based on RFC 2015 (PGP/MIME) and
183 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
185 (defun mime-add-application/pgp-keys (entity situation)
187 (mime-show-echo-buffer)
188 (set-buffer mime-echo-buffer-name)
190 (get-buffer-window mime-echo-buffer-name)
193 (mime-insert-entity-content entity)
194 (mime-decode-region (point-min) (point-max)
195 (cdr (assq 'encoding situation)))
196 (let ((status (pgg-snarf-keys-region (point-min)(point-max))))
198 (set-buffer mime-echo-buffer-name)
199 (insert-buffer-substring (if status pgg-output-buffer
200 pgg-errors-buffer))))))
203 ;;; @ Internal method for application/pkcs7-signature
205 ;;; It is based on the S/MIME user interface in Gnus.
207 (defun mime-verify-application/pkcs7-signature (entity situation)
208 "Internal method to check S/MIME signature."
210 (mime-insert-entity (mime-find-root-entity entity))
211 (let ((good-signature (smime-noverify-buffer))
213 (and (or smime-CA-file smime-CA-directory)
214 (smime-verify-buffer))))
215 (if (not good-signature)
216 ;; we couldn't verify message, fail with openssl output as message
218 (mime-show-echo-buffer)
219 (set-buffer mime-echo-buffer-name)
221 (get-buffer-window mime-echo-buffer-name)
223 (insert-buffer-substring smime-details-buffer))
224 ;; verify mail addresses in mail against those in certificate
225 (when (and (smime-pkcs7-region (point-min)(point-max))
226 (smime-pkcs7-certificates-region (point-min)(point-max)))
229 (nth 1 (std11-extract-address-components
230 (mime-entity-fetch-field
231 (mime-find-root-entity entity) "From"))))
232 (mime-smime-pkcs7-email-buffer (current-buffer))))
233 (message "Sender address forged")
235 (message "Ok (sender authenticated)")
236 (message "Integrity OK (sender unknown)"))))))))
238 (defun mime-smime-pkcs7-email-buffer (buffer)
240 (insert-buffer-substring buffer)
241 (goto-char (point-min))
243 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
244 (if (smime-pkcs7-email-region (point-min)(point))
245 (setq addresses (append (split-string
246 (buffer-substring (point-min)(point))
249 (delete-region (point-min)(point)))
250 (mapcar #'downcase addresses))))
253 ;;; @ Internal method for application/pkcs7-mime
255 ;;; It is based on RFC 2633 (S/MIME version 3).
257 (defun mime-view-application/pkcs7-mime (entity situation)
258 (let* ((p-win (or (get-buffer-window (current-buffer))
259 (get-largest-window)))
261 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
262 (mother (current-buffer))
263 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
265 (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
266 '(enveloped-data signed-data))
267 (set-buffer (setq message-buf (get-buffer-create new-name)))
268 (let ((inhibit-read-only t)
271 (mime-insert-entity entity)
272 (smime-decrypt-buffer))
273 (setq major-mode 'mime-show-message-mode)
274 (save-window-excursion
275 (mime-view-buffer nil preview-buffer mother
277 (make-local-variable 'mime-view-temp-message-buffer)
278 (setq mime-view-temp-message-buffer message-buf))
279 (set-window-buffer p-win preview-buffer))))
287 (run-hooks 'mime-pgp-load-hook)
289 ;;; mime-pgp.el ends here