1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Renamed: 1997/2/27 from tm-pgp.el
8 ;; Keywords: PGP, security, MIME, multimedia, mail, news
10 ;; This file is part of SEMI (Secure Emacs MIME Interface).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This module is based on
31 ;; [security-multipart] RFC 1847: "Security Multiparts for MIME:
32 ;; Multipart/Signed and Multipart/Encrypted" by
33 ;; Jim Galvin <galvin@tis.com>, Sandy Murphy <sandy@tis.com>,
34 ;; Steve Crocker <crocker@cybercash.com> and
35 ;; Ned Freed <ned@innosoft.com> (1995/10)
37 ;; [PGP/MIME] RFC 2015: "MIME Security with Pretty Good Privacy
38 ;; (PGP)" by Michael Elkins <elkins@aero.org> (1996/6)
40 ;; [PGP-kazu] draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
41 ;; by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp> (1995/10;
49 ;;; @ Internal method for multipart/signed
51 ;;; It is based on RFC 1847 (security-multipart).
53 (defun mime-verify-multipart/signed (entity situation)
54 "Internal method to verify multipart/signed."
56 (nth 1 (mime-entity-children entity)) ; entity-info of signature
57 (cdr (assq 'mode situation)) ; play-mode
61 ;;; @ internal method for application/pgp
63 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
65 (defun mime-view-application/pgp (entity situation)
66 (let* ((start (mime-entity-point-min entity))
67 (end (mime-entity-point-max entity))
68 (entity-number (mime-raw-point-to-entity-number start))
69 (p-win (or (get-buffer-window mime-preview-buffer)
70 (get-largest-window)))
71 (new-name (format "%s-%s" (buffer-name) entity-number))
72 (the-buf (current-buffer))
73 (mother mime-preview-buffer)
76 (set-buffer (get-buffer-create new-name))
78 (insert-buffer-substring the-buf start end)
80 (goto-char (point-min))
81 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
82 (funcall (pgp-function 'verify))
83 (goto-char (point-min))
87 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
90 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
93 (goto-char (point-min))
94 (while (re-search-forward "^- -" nil t)
97 (setq representation-type
98 (cdr (or (assq mode mime-raw-representation-type-alist)
99 (assq t mime-raw-representation-type-alist))))
102 (goto-char (point-min))
103 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
104 (as-binary-process (funcall (pgp-function 'decrypt)))
105 (goto-char (point-min))
106 (delete-region (point-min)
108 (search-forward "\n\n")
110 (setq representation-type 'binary)
112 (setq major-mode 'mime-show-message-mode)
113 (setq mime-raw-representation-type representation-type)
114 (save-window-excursion (mime-view-mode mother))
115 (set-window-buffer p-win mime-preview-buffer)
119 ;;; @ Internal method for application/pgp-signature
121 ;;; It is based on RFC 2015 (PGP/MIME).
123 (defvar mime-pgp-command "pgp"
124 "*Name of the PGP command.")
126 (defvar mime-pgp-default-language 'en
127 "*Symbol of language for pgp.
128 It should be ISO 639 2 letter language code such as en, ja, ...")
130 (defvar mime-pgp-good-signature-regexp-alist
131 '((en . "Good signature from user.*$"))
132 "Alist of language vs regexp to detect ``Good signature''.")
134 (defvar mime-pgp-key-expected-regexp-alist
135 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
136 "Alist of language vs regexp to detect ``Key expected''.")
138 (defun mime-pgp-check-signature (output-buffer orig-file)
140 (set-buffer output-buffer)
142 (let* ((lang (or mime-pgp-default-language 'en))
143 (status (call-process-region (point-min)(point-max)
145 nil output-buffer nil
146 orig-file (format "+language=%s" lang)))
147 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
150 (set-buffer output-buffer)
151 (goto-char (point-min))
153 (cond ((not (stringp regexp))
154 "Please specify right regexp for specified language")
155 ((re-search-forward regexp nil t)
156 (buffer-substring (match-beginning 0) (match-end 0)))
157 (t "Bad signature")))
160 (defun mime-verify-application/pgp-signature (entity situation)
161 "Internal method to check PGP/MIME signature."
162 (let* ((start (mime-entity-point-min entity))
163 (end (mime-entity-point-max entity))
164 (encoding (cdr (assq 'encoding situation)))
165 (entity-node-id (mime-raw-point-to-entity-node-id start))
166 (mother-node-id (cdr entity-node-id))
167 (knum (car entity-node-id))
171 (oinfo (mime-raw-find-entity-from-node-id
172 (cons onum mother-node-id) mime-message-structure))
173 (basename (expand-file-name "tm" mime-temp-directory))
174 (orig-file (make-temp-name basename))
175 (sig-file (concat orig-file ".sig"))
177 (mime-raw-write-region (mime-entity-point-min oinfo)
178 (mime-entity-point-max oinfo)
180 (save-excursion (mime-show-echo-buffer))
181 (mime-write-decoded-region (save-excursion
183 (and (search-forward "\n\n")
185 ) end sig-file encoding)
186 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
189 (set-buffer mime-echo-buffer-name)
190 (goto-char (point-min))
191 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
192 mime-pgp-key-expected-regexp-alist))))
193 (cond ((not (stringp regexp))
195 "Please specify right regexp for specified language")
197 ((re-search-forward regexp nil t)
199 (concat "0x" (buffer-substring-no-properties
205 (format "Key %s not found; attempt to fetch? " pgp-id))
208 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
209 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
212 (let ((other-window-scroll-buffer mime-echo-buffer-name))
213 (scroll-other-window 8)
215 (delete-file orig-file)
216 (delete-file sig-file)
220 ;;; @ Internal method for application/pgp-encrypted
222 ;;; It is based on RFC 2015 (PGP/MIME).
224 (defun mime-decrypt-application/pgp-encrypted (entity situation)
225 (let* ((entity-node-id (mime-entity-node-id entity))
226 (mother-node-id (cdr entity-node-id))
227 (knum (car entity-node-id))
231 (oinfo (mime-raw-find-entity-from-node-id
232 (cons onum mother-node-id) mime-message-structure)))
233 (mime-view-application/pgp oinfo situation)
237 ;;; @ Internal method for application/pgp-keys
239 ;;; It is based on RFC 2015 (PGP/MIME).
241 (defun mime-add-application/pgp-keys (entity situation)
242 (let* ((start (mime-entity-point-min entity))
243 (end (mime-entity-point-max entity))
244 (entity-number (mime-raw-point-to-entity-number start))
245 (new-name (format "%s-%s" (buffer-name) entity-number))
246 (encoding (cdr (assq 'encoding situation)))
248 (setq str (buffer-substring start end))
249 (switch-to-buffer new-name)
250 (setq buffer-read-only nil)
253 (goto-char (point-min))
254 (if (re-search-forward "^\n" nil t)
255 (delete-region (point-min) (match-end 0))
257 (mime-decode-region (point-min)(point-max) encoding)
258 (funcall (pgp-function 'snarf-keys))
259 (kill-buffer (current-buffer))
268 (run-hooks 'mime-pgp-load-hook)
270 ;;; mime-pgp.el ends here