1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
3 ;; Copyright (C) 1995,1996,1997,1998,1999 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
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;
52 ;;; @ Internal method for multipart/signed
54 ;;; It is based on RFC 1847 (security-multipart).
56 (defun mime-verify-multipart/signed (entity situation)
57 "Internal method to verify multipart/signed."
59 (nth 1 (mime-entity-children entity)) ; entity-info of signature
60 (list (assq 'mode situation)) ; play-mode
64 ;;; @ internal method for application/pgp
66 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
68 (defun mime-view-application/pgp (entity situation)
69 (let* ((p-win (or (get-buffer-window (current-buffer))
70 (get-largest-window)))
72 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
73 (mother (current-buffer))
74 (preview-buffer (concat "*Preview-" (buffer-name) "*"))
76 (set-buffer (get-buffer-create new-name))
78 (mime-insert-entity entity)
80 (goto-char (point-min))
81 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
82 (funcall (pgp-function 'verify)
83 (point-min)(point-max))
84 (goto-char (point-min))
88 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
91 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
94 (goto-char (point-min))
95 (while (re-search-forward "^- -" nil t)
98 (setq representation-type (if (mime-entity-cooked-p entity)
102 (goto-char (point-min))
103 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
104 (funcall (pgp-function 'decrypt)
105 (point-min)(point-max))
106 (delete-region (point-min)(point-max))
107 (insert-buffer pgg-output-buffer)
108 (setq representation-type 'binary)
110 (setq major-mode 'mime-show-message-mode)
111 (save-window-excursion (mime-view-buffer nil preview-buffer mother
112 nil representation-type))
113 (set-window-buffer p-win preview-buffer)
117 ;;; @ Internal method for application/pgp-signature
119 ;;; It is based on RFC 2015 (PGP/MIME).
121 (defvar mime-pgp-command "pgp"
122 "*Name of the PGP command.")
124 (defvar mime-pgp-default-language 'en
125 "*Symbol of language for pgp.
126 It should be ISO 639 2 letter language code such as en, ja, ...")
128 (defvar mime-pgp-good-signature-regexp-alist
129 '((en . "Good signature from user.*$"))
130 "Alist of language vs regexp to detect ``Good signature''.")
132 (defvar mime-pgp-key-expected-regexp-alist
133 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
134 "Alist of language vs regexp to detect ``Key expected''.")
136 (defun mime-pgp-check-signature (output-buffer sig-file orig-file)
138 (set-buffer output-buffer)
140 (let* ((lang (or mime-pgp-default-language 'en))
141 (status (call-process-region (point-min)(point-max)
143 nil output-buffer nil
144 sig-file orig-file (format "+language=%s" lang)))
145 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
148 (set-buffer output-buffer)
149 (goto-char (point-min))
151 (cond ((not (stringp regexp))
152 "Please specify right regexp for specified language")
153 ((re-search-forward regexp nil t)
154 (buffer-substring (match-beginning 0) (match-end 0)))
155 (t "Bad signature")))
158 (defun mime-verify-application/pgp-signature (entity situation)
159 "Internal method to check PGP/MIME signature."
160 (let* ((entity-node-id (mime-entity-node-id entity))
161 (mother (mime-entity-parent entity))
162 (knum (car entity-node-id))
166 (orig-entity (nth onum (mime-entity-children mother)))
167 (basename (expand-file-name "tm" temporary-file-directory))
168 (sig-file (concat (make-temp-name basename) ".asc"))
170 (save-excursion (mime-show-echo-buffer))
171 (mime-write-entity-content entity sig-file)
173 (mime-insert-entity orig-entity)
174 (goto-char (point-min))
175 (while (progn (end-of-line) (not (eobp)))
178 (let ((pgg-output-buffer mime-echo-buffer-name))
179 (funcall (pgp-function 'verify)
180 (point-min)(point-max) sig-file)))
181 (delete-file sig-file)
184 (defun mime-display-application/pgp-signature (entity situation)
186 (cdr (assq 2 (pgg-parse-armor (mime-entity-content entity)))))
190 (int-to-string (cdr (assq 'version packet)))
193 (cdr (assq 'signature-type packet))
195 (if (setq field (cdr (assq 'hash-algorithm packet)))
196 (concat "hash algorithm: " (symbol-name field) "\n")
198 (if (setq field (cdr (assq 'public-key-algorithm packet)))
199 (concat "public key algorithm: " (symbol-name field) "\n")
201 (if (setq field (cdr (assq 'key-identifier packet)))
202 (concat "key identifier: " field "\n")
204 (if (setq field (cdr (assq 'creation-time packet)))
205 (concat "creation time: " (current-time-string field) "\n")
207 (if (setq field (cdr (assq 'signature-expiry packet)))
208 (concat "signature exipiration time: "
209 (current-time-string field) "\n")
211 (if (setq field (cdr (assq 'key-expiry packet)))
212 (concat "key exipiration time: " (current-time-string field) "\n")
214 (if (setq field (cdr (assq 'trust-level packet)))
215 (concat "trust level: " (int-to-string field) "\n")
217 (if (setq field (cdr (assq 'preferred-symmetric-key-algorithm packet)))
218 (concat "preferred symmetric algorithm: "
219 (symbol-name field) "\n")
221 (if (setq field (cdr (assq 'preferred-hash-algorithm packet)))
222 (concat "preferred hash algorithm: "
223 (symbol-name field) "\n")
225 (if (setq field (cdr (assq 'exportability packet)))
226 (concat "signature exportable: "
227 (if (< 0 field) "yes" "no") "\n")
229 (if (setq field (cdr (assq 'revocability packet)))
230 (concat "signature revocable: "
231 (if (< 0 field) "yes" "no") "\n")
233 (if (setq field (cdr (assq 'policy-url packet)))
234 (concat "policy URL: " field "\n")
238 (function (lambda (nn)
239 (and (eq (car nn) 'notation) nn)))
241 (concat "notations:\n"
242 (mapconcat (lambda (nn)
243 (concat " " (cadr nn) ": " (cddr nn)))
247 (mime-add-url-buttons)
248 (run-hooks 'mime-display-application/pgp-signature-hook)
252 ;;; @ Internal method for application/pgp-encrypted
254 ;;; It is based on RFC 2015 (PGP/MIME).
256 (defun mime-decrypt-application/pgp-encrypted (entity situation)
257 (let* ((entity-node-id (mime-entity-node-id entity))
258 (mother (mime-entity-parent entity))
259 (knum (car entity-node-id))
263 (orig-entity (nth onum (mime-entity-children mother))))
264 (mime-view-application/pgp orig-entity situation)
267 (defun mime-display-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 (packet (cdr (assq 1 (pgg-parse-armor
276 (mime-entity-content orig-entity))))))
279 (int-to-string (cdr (assq 'version packet)))
281 "public key identifier: "
282 (cdr (assq 'public-key-identifier packet))
284 "public key algorithm: "
285 (symbol-name (cdr (assq 'public-key-algorithm packet)))
287 (run-hooks 'mime-display-application/pgp-encrypted-hook)
290 ;;; @ Internal method for application/pgp-keys
292 ;;; It is based on RFC 2015 (PGP/MIME).
294 (defun mime-add-application/pgp-keys (entity situation)
295 (let* ((start (mime-entity-header-start-point entity))
296 (end (mime-entity-body-end-point entity))
297 (entity-number (mime-entity-number entity))
298 (new-name (format "%s-%s" (buffer-name) entity-number))
299 (encoding (cdr (assq 'encoding situation)))
301 (setq str (buffer-substring start end))
302 (switch-to-buffer new-name)
303 (setq buffer-read-only nil)
306 (goto-char (point-min))
307 (if (re-search-forward "^\n" nil t)
308 (delete-region (point-min) (match-end 0))
310 (mime-decode-region (point-min)(point-max) encoding)
311 (funcall (pgp-function 'snarf-keys)
312 (point-min)(point-max))
313 (kill-buffer (current-buffer))
316 (defun mime-display-application/pgp-keys (entity situation)
318 (cdr (assq 6 (pgg-parse-armor (mime-entity-content entity)))))
322 (int-to-string (cdr (assq 'version packet)))
325 (current-time-string (cdr (assq 'creation-time packet)))
327 "public key algorithm: "
328 (symbol-name (cdr (assq 'public-key-algorithm packet)))
330 (if (setq field (cdr (assq 'key-expiry packet)))
331 (concat "key exipiration time: " (current-time-string field) "\n")
333 (run-hooks 'mime-display-application/pgp-keys-hook)
342 (run-hooks 'mime-pgp-load-hook)
344 ;;; mime-pgp.el ends here