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;
44 ;; PGP/MIME and PGP-kazu may be contrary to each other. You should
45 ;; decide which you support (Maybe you should not use PGP-kazu).
52 ;;; @ internal method for application/pgp
54 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
56 (defun mime-method-for-application/pgp (start end cal)
57 (let* ((entity-number (mime-raw-point-to-entity-number start))
58 (p-win (or (get-buffer-window mime-preview-buffer)
59 (get-largest-window)))
60 (new-name (format "%s-%s" (buffer-name) entity-number))
61 (the-buf (current-buffer))
62 (mother mime-preview-buffer)
65 (set-buffer (get-buffer-create new-name))
67 (insert-buffer-substring the-buf start end)
69 (goto-char (point-min))
70 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
71 (funcall (pgp-function 'verify))
72 (goto-char (point-min))
76 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
79 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
82 (goto-char (point-min))
83 (while (re-search-forward "^- -" nil t)
87 (cdr (or (assq mode mime-text-decoder-alist)
88 (assq t mime-text-decoder-alist))))
91 (goto-char (point-min))
92 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
93 (as-binary-process (funcall (pgp-function 'decrypt)))
94 (goto-char (point-min))
95 (delete-region (point-min)
97 (search-forward "\n\n")
99 (setq text-decoder (function mime-text-decode-buffer))
101 (setq major-mode 'mime-show-message-mode)
102 (setq mime-text-decoder text-decoder)
103 (save-window-excursion (mime-view-mode mother))
104 (set-window-buffer p-win mime-preview-buffer)
107 ;; (ctree-set-calist-strictly
108 ;; 'mime-preview-condition '((type . application)(subtype . pgp)
109 ;; (message-button . visible)))
111 ;; (ctree-set-calist-strictly
112 ;; 'mime-acting-condition '((type . application)(subtype . pgp)
113 ;; (method . mime-method-for-application/pgp)))
115 ;; (ctree-set-calist-strictly
116 ;; 'mime-acting-condition '((type . text)(subtype . x-pgp)
117 ;; (method . mime-method-for-application/pgp)))
120 ;;; @ Internal method for multipart/signed
122 ;;; It is based on RFC 1847 (security-multipart).
124 (defun mime-method-to-verify-multipart/signed (start end cal)
125 "Internal method to verify multipart/signed."
126 (mime-raw-play-entity
127 ;; entity-info of signature
128 (mime-raw-find-entity-from-node-id
129 ;; entity-node-id of signature
130 (cons 1 (mime-raw-point-to-entity-node-id start)))
131 (cdr (assq 'mode cal)) ; play-mode
134 ;; (ctree-set-calist-strictly
135 ;; 'mime-acting-condition
136 ;; '((type . multipart)(subtype . signed)
137 ;; (method . mime-method-to-verify-multipart/signed)))
140 ;;; @ Internal method for application/pgp-signature
142 ;;; It is based on RFC 2015 (PGP/MIME).
144 (defvar mime-pgp-command "pgp"
145 "*Name of the PGP command.")
147 (defvar mime-pgp-default-language 'en
148 "*Symbol of language for pgp.
149 It should be ISO 639 2 letter language code such as en, ja, ...")
151 (defvar mime-pgp-good-signature-regexp-alist
152 '((en . "Good signature from user.*$"))
153 "Alist of language vs regexp to detect ``Good signature''.")
155 (defvar mime-pgp-key-expected-regexp-alist
156 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
157 "Alist of language vs regexp to detect ``Key expected''.")
159 (defun mime-pgp-check-signature (output-buffer orig-file)
161 (set-buffer output-buffer)
163 (let* ((lang (or mime-pgp-default-language 'en))
164 (status (call-process-region (point-min)(point-max)
166 nil output-buffer nil
167 orig-file (format "+language=%s" lang)))
168 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
171 (set-buffer output-buffer)
172 (goto-char (point-min))
174 (cond ((not (stringp regexp))
175 "Please specify right regexp for specified language")
176 ((re-search-forward regexp nil t)
177 (buffer-substring (match-beginning 0) (match-end 0)))
178 (t "Bad signature")))
181 (defun mime-method-to-verify-application/pgp-signature (start end cal)
182 "Internal method to check PGP/MIME signature."
183 (let* ((encoding (cdr (assq 'encoding cal)))
184 (entity-node-id (mime-raw-point-to-entity-node-id start))
185 (mother-node-id (cdr entity-node-id))
186 (knum (car entity-node-id))
190 (oinfo (mime-raw-find-entity-from-node-id
191 (cons onum mother-node-id) mime-raw-message-info))
192 (basename (expand-file-name "tm" mime-temp-directory))
193 (orig-file (make-temp-name basename))
194 (sig-file (concat orig-file ".sig"))
196 (mime-raw-write-region (mime-entity-point-min oinfo)
197 (mime-entity-point-max oinfo)
199 (save-excursion (mime-show-echo-buffer))
200 (mime-write-decoded-region (save-excursion
202 (and (search-forward "\n\n")
204 ) end sig-file encoding)
205 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
208 (set-buffer mime-echo-buffer-name)
209 (goto-char (point-min))
210 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
211 mime-pgp-key-expected-regexp-alist))))
212 (cond ((not (stringp regexp))
214 "Please specify right regexp for specified language")
216 ((re-search-forward regexp nil t)
218 (concat "0x" (buffer-substring-no-properties
224 (format "Key %s not found; attempt to fetch? " pgp-id))
227 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
228 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
231 (let ((other-window-scroll-buffer mime-echo-buffer-name))
232 (scroll-other-window 8)
234 (delete-file orig-file)
235 (delete-file sig-file)
238 ;; (ctree-set-calist-strictly
239 ;; 'mime-acting-condition
240 ;; '((type . application)(subtype . pgp-signature)
241 ;; (method . mime-method-to-verify-application/pgp-signature)))
244 ;;; @ Internal method for application/pgp-encrypted
246 ;;; It is based on RFC 2015 (PGP/MIME).
248 (defun mime-method-to-decrypt-application/pgp-encrypted (start end cal)
249 (let* ((entity-node-id (mime-raw-point-to-entity-node-id start))
250 (mother-node-id (cdr entity-node-id))
251 (knum (car entity-node-id))
255 (oinfo (mime-raw-find-entity-from-node-id
256 (cons onum mother-node-id) mime-raw-message-info))
257 (obeg (mime-entity-point-min oinfo))
258 (oend (mime-entity-point-max oinfo))
260 (mime-method-for-application/pgp obeg oend cal)
263 ;; (ctree-set-calist-strictly
264 ;; 'mime-acting-condition
265 ;; '((type . application)(subtype . pgp-encrypted)
266 ;; (method . mime-method-to-decrypt-application/pgp-encrypted)))
269 ;;; @ Internal method for application/pgp-keys
271 ;;; It is based on RFC 2015 (PGP/MIME).
273 (defun mime-method-to-add-application/pgp-keys (start end cal)
274 (let* ((entity-number (mime-raw-point-to-entity-number start))
275 (new-name (format "%s-%s" (buffer-name) entity-number))
276 (encoding (cdr (assq 'encoding cal)))
278 (setq str (buffer-substring start end))
279 (switch-to-buffer new-name)
280 (setq buffer-read-only nil)
283 (goto-char (point-min))
284 (if (re-search-forward "^\n" nil t)
285 (delete-region (point-min) (match-end 0))
287 (mime-decode-region (point-min)(point-max) encoding)
288 (funcall (pgp-function 'snarf-keys))
289 (kill-buffer (current-buffer))
292 ;; (ctree-set-calist-strictly
293 ;; 'mime-acting-condition
294 ;; '((type . application)(subtype . pgp-keys)
295 ;; (method . mime-method-to-add-application/pgp-keys)))
303 (run-hooks 'mime-pgp-load-hook)
305 ;;; mime-pgp.el ends here