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")))
141 (mime-show-echo-buffer)
142 (set-buffer mime-echo-buffer-name)
144 (get-buffer-window mime-echo-buffer-name)
146 (mime-write-entity-content entity sig-file)
149 (mime-insert-entity orig-entity)
150 (goto-char (point-min))
151 (while (progn (end-of-line) (not (eobp)))
154 (pgg-verify-region (point-min)(point-max)
157 (set-buffer mime-echo-buffer-name)
158 (insert-buffer-substring pgg-errors-buffer)))
159 (delete-file sig-file))))
162 ;;; @ Internal method for application/pgp-encrypted
164 ;;; It is based on RFC 2015 (PGP/MIME) and
165 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
167 (defun mime-decrypt-application/pgp-encrypted (entity situation)
168 (let* ((entity-node-id (mime-entity-node-id entity))
169 (mother (mime-entity-parent entity))
170 (knum (car entity-node-id))
174 (orig-entity (nth onum (mime-entity-children mother))))
175 (mime-view-application/pgp orig-entity situation)))
178 ;;; @ Internal method for application/pgp-keys
180 ;;; It is based on RFC 2015 (PGP/MIME) and
181 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
183 (defun mime-add-application/pgp-keys (entity situation)
185 (mime-show-echo-buffer)
186 (set-buffer mime-echo-buffer-name)
188 (get-buffer-window mime-echo-buffer-name)
191 (mime-insert-entity-content entity)
192 (mime-decode-region (point-min) (point-max)
193 (cdr (assq 'encoding situation)))
194 (pgg-snarf-keys-region (point-min)(point-max))
196 (set-buffer mime-echo-buffer-name)
197 (insert-buffer-substring pgg-errors-buffer))))
200 ;;; @ Internal method for application/pkcs7-signature
202 ;;; It is based on the S/MIME user interface in Gnus.
204 (defun mime-verify-application/pkcs7-signature (entity situation)
205 "Internal method to check S/MIME signature."
207 (mime-insert-entity (mime-find-root-entity entity))
208 (let ((good-signature (smime-noverify-buffer))
210 (and (or smime-CA-file smime-CA-directory)
211 (smime-verify-buffer))))
212 (if (not good-signature)
213 ;; we couldn't verify message, fail with openssl output as message
215 (mime-show-echo-buffer)
216 (set-buffer mime-echo-buffer-name)
218 (get-buffer-window mime-echo-buffer-name)
220 (insert-buffer-substring smime-details-buffer))
221 ;; verify mail addresses in mail against those in certificate
222 (when (and (smime-pkcs7-region (point-min)(point-max))
223 (smime-pkcs7-certificates-region (point-min)(point-max)))
226 (nth 1 (std11-extract-address-components
227 (mime-entity-fetch-field
228 (mime-find-root-entity entity) "From"))))
229 (mime-smime-pkcs7-email-buffer (current-buffer))))
230 (message "Sender address forged")
232 (message "Ok (sender authenticated)")
233 (message "Integrity OK (sender unknown)"))))))))
235 (defun mime-smime-pkcs7-email-buffer (buffer)
237 (insert-buffer-substring buffer)
238 (goto-char (point-min))
240 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
241 (if (smime-pkcs7-email-region (point-min)(point))
242 (setq addresses (append (split-string
243 (buffer-substring (point-min)(point))
246 (delete-region (point-min)(point)))
247 (mapcar #'downcase addresses))))
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-buffer))
270 (setq major-mode 'mime-show-message-mode)
271 (save-window-excursion
272 (mime-view-buffer nil preview-buffer mother
274 (make-local-variable 'mime-view-temp-message-buffer)
275 (setq mime-view-temp-message-buffer message-buf))
276 (set-window-buffer p-win preview-buffer))))
284 (run-hooks 'mime-pgp-load-hook)
286 ;;; mime-pgp.el ends here