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 (beg end cal)
57 (let* ((cnum (mime-raw-point-to-entity-number beg))
58 (p-win (or (get-buffer-window mime-preview-buffer)
59 (get-largest-window)))
60 (new-name (format "%s-%s" (buffer-name) cnum))
61 (the-buf (current-buffer))
62 (mother mime-preview-buffer)
65 (set-buffer (get-buffer-create new-name))
67 (insert-buffer-substring the-buf beg end)
69 (goto-char (point-min))
70 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
72 (funcall (pgp-function 'verify))
73 (goto-char (point-min))
77 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
81 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
85 (goto-char (point-min))
86 (while (re-search-forward "^- -" nil t)
90 (cdr (or (assq mode mime-text-decoder-alist)
91 (assq t mime-text-decoder-alist))))
94 (goto-char (point-min))
95 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
97 (as-binary-process (funcall (pgp-function 'decrypt)))
98 (goto-char (point-min))
99 (delete-region (point-min)
101 (search-forward "\n\n")
103 (setq text-decoder (function mime-text-decode-buffer))
105 (setq major-mode 'mime-show-message-mode)
106 (setq mime-text-decoder text-decoder)
107 (save-window-excursion (mime-view-mode mother))
108 (set-window-buffer p-win mime-preview-buffer)
111 (set-atype 'mime-acting-condition
112 '((type . application)(subtype . pgp)
113 (method . mime-method-for-application/pgp)
116 (set-atype 'mime-acting-condition
117 '((type . text)(subtype . x-pgp)
118 (method . mime-method-for-application/pgp)
122 ;;; @ Internal method for multipart/signed
124 ;;; It is based on RFC 1847 (security-multipart).
126 (defun mime-method-to-verify-multipart/signed (start end cal)
127 "Internal method to verify multipart/signed."
128 (mime-raw-play-entity
129 ;; entity-info of signature
130 (mime-raw-reversed-entity-number-to-entity-info
131 ;; reversed-entity-number of signature
132 (cons 1 (reverse (mime-raw-point-to-entity-number start)))
133 mime-raw-message-info)
134 (cdr (assq 'mode cal)) ; play-mode
137 (set-atype 'mime-acting-condition
138 '((type . multipart)(subtype . signed)
139 (method . mime-method-to-verify-multipart/signed)
143 ;;; @ Internal method for application/pgp-signature
145 ;;; It is based on RFC 2015 (PGP/MIME).
147 (defvar mime-pgp-command "pgp"
148 "*Name of the PGP command.")
150 (defvar mime-pgp-default-language 'en
151 "*Symbol of language for pgp.
152 It should be ISO 639 2 letter language code such as en, ja, ...")
154 (defvar mime-pgp-good-signature-regexp-alist
155 '((en . "Good signature from user.*$"))
156 "Alist of language vs regexp to detect ``Good signature''.")
158 (defvar mime-pgp-key-expected-regexp-alist
159 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
160 "Alist of language vs regexp to detect ``Key expected''.")
162 (defun mime-pgp-check-signature (output-buffer orig-file)
164 (set-buffer output-buffer)
166 (let* ((lang (or mime-pgp-default-language 'en))
167 (status (call-process-region (point-min)(point-max)
169 nil output-buffer nil
170 orig-file (format "+language=%s" lang)))
171 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
174 (set-buffer output-buffer)
175 (goto-char (point-min))
177 (cond ((not (stringp regexp))
178 "Please specify right regexp for specified language")
179 ((re-search-forward regexp nil t)
180 (buffer-substring (match-beginning 0) (match-end 0)))
181 (t "Bad signature")))
184 (defun mime-method-to-verify-application/pgp-signature (beg end cal)
185 "Internal method to check PGP/MIME signature."
186 (let* ((encoding (cdr (assq 'encoding cal)))
187 (cnum (mime-raw-point-to-entity-number beg))
188 (rcnum (reverse cnum))
194 (raw-buf (current-buffer))
195 (oinfo (mime-raw-reversed-entity-number-to-entity-info
196 (cons onum rmcnum) mime-raw-message-info))
198 (basename (expand-file-name "tm" mime-temp-directory))
199 (orig-file (make-temp-name basename))
200 (sig-file (concat orig-file ".sig"))
203 (let ((p-min (mime-entity-info-point-min oinfo))
204 (p-max (mime-entity-info-point-max oinfo))
206 (set-buffer (get-buffer-create mime-temp-buffer-name))
207 (insert-buffer-substring raw-buf p-min p-max)
209 (goto-char (point-min))
210 (while (re-search-forward "\n" nil t)
211 (replace-match "\r\n")
213 (as-binary-output-file (write-region (point-min)(point-max) orig-file))
214 (kill-buffer (current-buffer))
216 (save-excursion (mime-show-echo-buffer))
218 (let ((p-min (save-excursion
220 (and (search-forward "\n\n")
223 (set-buffer (setq kbuf (get-buffer-create mime-temp-buffer-name)))
224 (insert-buffer-substring raw-buf p-min end)
226 (mime-decode-region (point-min)(point-max) encoding)
227 (as-binary-output-file (write-region (point-min)(point-max) sig-file))
228 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
231 (set-buffer mime-echo-buffer-name)
232 (goto-char (point-min))
233 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
234 mime-pgp-key-expected-regexp-alist))))
235 (cond ((not (stringp regexp))
237 "Please specify right regexp for specified language")
239 ((re-search-forward regexp nil t)
241 (concat "0x" (buffer-substring-no-properties
247 (format "Key %s not found; attempt to fetch? " pgp-id))
250 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
251 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
254 (let ((other-window-scroll-buffer mime-echo-buffer-name))
255 (scroll-other-window 8)
258 (delete-file orig-file)
259 (delete-file sig-file)
262 (set-atype 'mime-acting-condition
263 '((type . application)(subtype . pgp-signature)
264 (method . mime-method-to-verify-application/pgp-signature)
268 ;;; @ Internal method for application/pgp-encrypted
270 ;;; It is based on RFC 2015 (PGP/MIME).
272 (defun mime-method-to-decrypt-application/pgp-encrypted (beg end cal)
273 (let* ((cnum (mime-raw-point-to-entity-number beg))
274 (rcnum (reverse cnum))
280 (oinfo (mime-raw-reversed-entity-number-to-entity-info
281 (cons onum rmcnum) mime-raw-message-info))
282 (obeg (mime-entity-info-point-min oinfo))
283 (oend (mime-entity-info-point-max oinfo))
285 (mime-method-for-application/pgp obeg oend cal)
288 (set-atype 'mime-acting-condition
289 '((type . application)(subtype . pgp-encrypted)
290 (method . mime-method-to-decrypt-application/pgp-encrypted)
294 ;;; @ Internal method for application/pgp-keys
296 ;;; It is based on RFC 2015 (PGP/MIME).
298 (defun mime-method-to-add-application/pgp-keys (beg end cal)
299 (let* ((cnum (mime-raw-point-to-entity-number beg))
300 (new-name (format "%s-%s" (buffer-name) cnum))
301 (encoding (cdr (assq 'encoding cal)))
303 (setq str (buffer-substring beg end))
304 (switch-to-buffer new-name)
305 (setq buffer-read-only nil)
308 (goto-char (point-min))
309 (if (re-search-forward "^\n" nil t)
310 (delete-region (point-min) (match-end 0))
312 (mime-decode-region (point-min)(point-max) encoding)
313 (funcall (pgp-function 'snarf-keys))
314 (kill-buffer (current-buffer))
317 (set-atype 'mime-acting-condition
318 '((type . application)(subtype . pgp-keys)
319 (method . mime-method-to-add-application/pgp-keys)
328 (run-hooks 'mime-pgp-load-hook)
330 ;;; mime-pgp.el ends here