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-buffer "smime"
63 "S/MIME decryption of current region.")
64 (autoload 'smime-verify-buffer "smime"
65 "Verify integrity of S/MIME message in BUFFER.")
66 (autoload 'smime-noverify-buffer "smime"
67 "Verify integrity of S/MIME message in BUFFER.")
68 (autoload 'smime-pkcs7-region "smime"
69 "Convert S/MIME message into a PKCS7 message.")
70 (autoload 'smime-pkcs7-certificates-region "smime"
71 "Extract any certificates enclosed in PKCS7 message.")
72 (autoload 'smime-pkcs7-email-region "smime"
73 "Get email addresses contained in certificate.")
74 (defvar smime-details-buffer)
75 (defvar smime-CA-file)
76 (defvar smime-CA-directory)
78 ;;; @ Internal method for multipart/signed
80 ;;; It is based on RFC 1847 (security-multipart).
82 (defun mime-verify-multipart/signed (entity situation)
83 "Internal method to verify multipart/signed."
85 (nth 1 (mime-entity-children entity)) ; entity-info of signature
86 (list (assq 'mode situation)) ; play-mode
90 ;;; @ internal method for application/pgp
92 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
94 (defun mime-view-application/pgp (entity situation)
95 (let* ((p-win (or (get-buffer-window (current-buffer))
96 (get-largest-window)))
98 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
99 (mother (current-buffer))
100 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
101 representation-type message-buf)
102 (set-buffer (setq message-buf (get-buffer-create new-name)))
104 (mime-insert-entity entity)
106 (goto-char (point-min))
107 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
108 (pgg-verify-region (match-beginning 0)(point-max) nil 'fetch)
109 (goto-char (point-min))
113 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
116 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
119 (goto-char (point-min))
120 (while (re-search-forward "^- -" nil t)
122 (setq representation-type (if (mime-entity-cooked-p entity)
125 (goto-char (point-min))
126 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
127 (pgg-decrypt-region (point-min)(point-max))
128 (delete-region (point-min)(point-max))
129 (insert-buffer pgg-output-buffer)
130 (setq representation-type 'binary)))
131 (setq major-mode 'mime-show-message-mode)
132 (save-window-excursion
133 (mime-view-buffer nil preview-buffer mother
134 nil representation-type)
135 (make-local-variable 'mime-view-temp-message-buffer)
136 (setq mime-view-temp-message-buffer message-buf))
137 (set-window-buffer p-win preview-buffer)))
140 ;;; @ Internal method for application/pgp-signature
142 ;;; It is based on RFC 2015 (PGP/MIME) and
143 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
145 (defun mime-verify-application/pgp-signature (entity situation)
146 "Internal method to check PGP/MIME signature."
147 (let* ((entity-node-id (mime-entity-node-id entity))
148 (mother (mime-entity-parent entity))
149 (knum (car entity-node-id))
153 (orig-entity (nth onum (mime-entity-children mother)))
154 (basename (expand-file-name "tm" temporary-file-directory))
155 (sig-file (concat (make-temp-name basename) ".asc"))
158 (mime-show-echo-buffer)
159 (set-buffer mime-echo-buffer-name)
161 (get-buffer-window mime-echo-buffer-name)
163 (mime-write-entity-content entity sig-file)
166 (mime-insert-entity orig-entity)
167 (goto-char (point-min))
168 (while (progn (end-of-line) (not (eobp)))
171 (setq status (pgg-verify-region (point-min)(point-max)
174 (set-buffer mime-echo-buffer-name)
175 (insert-buffer-substring (if status pgg-output-buffer
176 pgg-errors-buffer))))
177 (delete-file sig-file))))
180 ;;; @ Internal method for application/pgp-encrypted
182 ;;; It is based on RFC 2015 (PGP/MIME) and
183 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
185 (defun mime-decrypt-application/pgp-encrypted (entity situation)
186 (let* ((entity-node-id (mime-entity-node-id entity))
187 (mother (mime-entity-parent entity))
188 (knum (car entity-node-id))
192 (orig-entity (nth onum (mime-entity-children mother))))
193 (mime-view-application/pgp orig-entity situation)))
196 ;;; @ Internal method for application/pgp-keys
198 ;;; It is based on RFC 2015 (PGP/MIME) and
199 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
201 (defun mime-add-application/pgp-keys (entity situation)
203 (mime-show-echo-buffer)
204 (set-buffer mime-echo-buffer-name)
206 (get-buffer-window mime-echo-buffer-name)
209 (mime-insert-entity-content entity)
210 (mime-decode-region (point-min) (point-max)
211 (cdr (assq 'encoding situation)))
212 (let ((status (pgg-snarf-keys-region (point-min)(point-max))))
214 (set-buffer mime-echo-buffer-name)
215 (insert-buffer-substring (if status pgg-output-buffer
216 pgg-errors-buffer))))))
219 ;;; @ Internal method for application/pkcs7-signature
221 ;;; It is based on the S/MIME user interface in Gnus.
223 (defun mime-verify-application/pkcs7-signature (entity situation)
224 "Internal method to check S/MIME signature."
226 (mime-insert-entity (mime-find-root-entity entity))
227 (let ((good-signature (smime-noverify-buffer))
229 (and (or smime-CA-file smime-CA-directory)
230 (smime-verify-buffer))))
231 (if (not good-signature)
232 ;; we couldn't verify message, fail with openssl output as message
234 (mime-show-echo-buffer)
235 (set-buffer mime-echo-buffer-name)
237 (get-buffer-window mime-echo-buffer-name)
239 (insert-buffer-substring smime-details-buffer))
240 ;; verify mail addresses in mail against those in certificate
241 (when (and (smime-pkcs7-region (point-min)(point-max))
242 (smime-pkcs7-certificates-region (point-min)(point-max)))
245 (nth 1 (std11-extract-address-components
246 (mime-entity-fetch-field
247 (mime-find-root-entity entity) "From"))))
248 (mime-smime-pkcs7-email-buffer (current-buffer))))
249 (message "Sender address forged")
251 (message "Ok (sender authenticated)")
252 (message "Integrity OK (sender unknown)"))))))))
254 (defun mime-smime-pkcs7-email-buffer (buffer)
256 (insert-buffer-substring buffer)
257 (goto-char (point-min))
259 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
260 (if (smime-pkcs7-email-region (point-min)(point))
261 (setq addresses (append (split-string
262 (buffer-substring (point-min)(point))
265 (delete-region (point-min)(point)))
266 (mapcar #'downcase addresses))))
269 ;;; @ Internal method for application/pkcs7-mime
271 ;;; It is based on RFC 2633 (S/MIME version 3).
273 (defun mime-view-application/pkcs7-mime (entity situation)
274 (let* ((p-win (or (get-buffer-window (current-buffer))
275 (get-largest-window)))
277 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
278 (mother (current-buffer))
279 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
281 (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
282 '(enveloped-data signed-data))
283 (set-buffer (setq message-buf (get-buffer-create new-name)))
284 (let ((inhibit-read-only t)
287 (mime-insert-entity entity)
288 (smime-decrypt-buffer))
289 (setq major-mode 'mime-show-message-mode)
290 (save-window-excursion
291 (mime-view-buffer nil preview-buffer mother
293 (make-local-variable 'mime-view-temp-message-buffer)
294 (setq mime-view-temp-message-buffer message-buf))
295 (set-window-buffer p-win preview-buffer))))
303 (run-hooks 'mime-pgp-load-hook)
305 ;;; mime-pgp.el ends here