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 (set-atype 'mime-acting-condition
108 '((type . application)(subtype . pgp)
109 (method . mime-method-for-application/pgp)))
111 (set-atype 'mime-acting-condition
112 '((type . text)(subtype . x-pgp)
113 (method . mime-method-for-application/pgp)))
116 ;;; @ Internal method for multipart/signed
118 ;;; It is based on RFC 1847 (security-multipart).
120 (defun mime-method-to-verify-multipart/signed (start end cal)
121 "Internal method to verify multipart/signed."
122 (mime-raw-play-entity
123 ;; entity-info of signature
124 (mime-raw-reversed-entity-number-to-entity-info
125 ;; reversed-entity-number of signature
126 (cons 1 (reverse (mime-raw-point-to-entity-number start)))
127 mime-raw-message-info)
128 (cdr (assq 'mode cal)) ; play-mode
131 (set-atype 'mime-acting-condition
132 '((type . multipart)(subtype . signed)
133 (method . mime-method-to-verify-multipart/signed)))
136 ;;; @ Internal method for application/pgp-signature
138 ;;; It is based on RFC 2015 (PGP/MIME).
140 (defvar mime-pgp-command "pgp"
141 "*Name of the PGP command.")
143 (defvar mime-pgp-default-language 'en
144 "*Symbol of language for pgp.
145 It should be ISO 639 2 letter language code such as en, ja, ...")
147 (defvar mime-pgp-good-signature-regexp-alist
148 '((en . "Good signature from user.*$"))
149 "Alist of language vs regexp to detect ``Good signature''.")
151 (defvar mime-pgp-key-expected-regexp-alist
152 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
153 "Alist of language vs regexp to detect ``Key expected''.")
155 (defun mime-pgp-check-signature (output-buffer orig-file)
157 (set-buffer output-buffer)
159 (let* ((lang (or mime-pgp-default-language 'en))
160 (status (call-process-region (point-min)(point-max)
162 nil output-buffer nil
163 orig-file (format "+language=%s" lang)))
164 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
167 (set-buffer output-buffer)
168 (goto-char (point-min))
170 (cond ((not (stringp regexp))
171 "Please specify right regexp for specified language")
172 ((re-search-forward regexp nil t)
173 (buffer-substring (match-beginning 0) (match-end 0)))
174 (t "Bad signature")))
177 (defun mime-method-to-verify-application/pgp-signature (start end cal)
178 "Internal method to check PGP/MIME signature."
179 (let* ((encoding (cdr (assq 'encoding cal)))
180 (entity-number (mime-raw-point-to-entity-number start))
181 (reversed-entity-number (reverse entity-number))
182 (rmcnum (cdr reversed-entity-number))
183 (knum (car reversed-entity-number))
187 (raw-buf (current-buffer))
188 (oinfo (mime-raw-reversed-entity-number-to-entity-info
189 (cons onum rmcnum) mime-raw-message-info))
191 (basename (expand-file-name "tm" mime-temp-directory))
192 (orig-file (make-temp-name basename))
193 (sig-file (concat orig-file ".sig"))
196 (let ((p-min (mime-entity-info-point-min oinfo))
197 (p-max (mime-entity-info-point-max oinfo))
199 (set-buffer (get-buffer-create mime-temp-buffer-name))
200 (insert-buffer-substring raw-buf p-min p-max)
202 (goto-char (point-min))
203 (while (re-search-forward "\n" nil t)
204 (replace-match "\r\n")
206 (as-binary-output-file (write-region (point-min)(point-max) orig-file))
207 (kill-buffer (current-buffer))
209 (save-excursion (mime-show-echo-buffer))
211 (let ((p-min (save-excursion
213 (and (search-forward "\n\n")
216 (set-buffer (setq kbuf (get-buffer-create mime-temp-buffer-name)))
217 (insert-buffer-substring raw-buf p-min end)
219 (mime-decode-region (point-min)(point-max) encoding)
220 (as-binary-output-file (write-region (point-min)(point-max) sig-file))
221 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
224 (set-buffer mime-echo-buffer-name)
225 (goto-char (point-min))
226 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
227 mime-pgp-key-expected-regexp-alist))))
228 (cond ((not (stringp regexp))
230 "Please specify right regexp for specified language")
232 ((re-search-forward regexp nil t)
234 (concat "0x" (buffer-substring-no-properties
240 (format "Key %s not found; attempt to fetch? " pgp-id))
243 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
244 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
247 (let ((other-window-scroll-buffer mime-echo-buffer-name))
248 (scroll-other-window 8)
251 (delete-file orig-file)
252 (delete-file sig-file)
255 (set-atype 'mime-acting-condition
256 '((type . application)(subtype . pgp-signature)
257 (method . mime-method-to-verify-application/pgp-signature)))
260 ;;; @ Internal method for application/pgp-encrypted
262 ;;; It is based on RFC 2015 (PGP/MIME).
264 (defun mime-method-to-decrypt-application/pgp-encrypted (start end cal)
265 (let* ((entity-number (mime-raw-point-to-entity-number start))
266 (reversed-entity-number (reverse entity-number))
267 (rmcnum (cdr reversed-entity-number))
268 (knum (car reversed-entity-number))
272 (oinfo (mime-raw-reversed-entity-number-to-entity-info
273 (cons onum rmcnum) mime-raw-message-info))
274 (obeg (mime-entity-info-point-min oinfo))
275 (oend (mime-entity-info-point-max oinfo))
277 (mime-method-for-application/pgp obeg oend cal)
280 (set-atype 'mime-acting-condition
281 '((type . application)(subtype . pgp-encrypted)
282 (method . mime-method-to-decrypt-application/pgp-encrypted)))
285 ;;; @ Internal method for application/pgp-keys
287 ;;; It is based on RFC 2015 (PGP/MIME).
289 (defun mime-method-to-add-application/pgp-keys (start end cal)
290 (let* ((entity-number (mime-raw-point-to-entity-number start))
291 (new-name (format "%s-%s" (buffer-name) entity-number))
292 (encoding (cdr (assq 'encoding cal)))
294 (setq str (buffer-substring start end))
295 (switch-to-buffer new-name)
296 (setq buffer-read-only nil)
299 (goto-char (point-min))
300 (if (re-search-forward "^\n" nil t)
301 (delete-region (point-min) (match-end 0))
303 (mime-decode-region (point-min)(point-max) encoding)
304 (funcall (pgp-function 'snarf-keys))
305 (kill-buffer (current-buffer))
308 (set-atype 'mime-acting-condition
309 '((type . application)(subtype . pgp-keys)
310 (method . mime-method-to-add-application/pgp-keys)))
318 (run-hooks 'mime-pgp-load-hook)
320 ;;; mime-pgp.el ends here