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 2 drafts about PGP MIME integration:
31 ;; - RFC 2015: "MIME Security with Pretty Good Privacy (PGP)"
32 ;; by Michael Elkins <elkins@aero.org> (1996/6)
34 ;; - draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
35 ;; by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp>
38 ;; These drafts may be contrary to each other. You should decide
39 ;; which you support. (Maybe you should use PGP/MIME)
46 ;;; @ internal method for application/pgp
48 ;;; It is based on draft-kazu-pgp-mime-00.txt
50 (defun mime-method-for-application/pgp (beg end cal)
51 (let* ((cnum (mime-raw-point-content-number beg))
52 (p-win (or (get-buffer-window mime-preview-buffer)
53 (get-largest-window)))
54 (new-name (format "%s-%s" (buffer-name) cnum))
55 (the-buf (current-buffer))
56 (mother mime-preview-buffer)
59 (set-buffer (get-buffer-create new-name))
61 (insert-buffer-substring the-buf beg end)
63 (goto-char (point-min))
64 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
66 (funcall (pgp-function 'verify))
67 (goto-char (point-min))
71 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
75 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
79 (goto-char (point-min))
80 (while (re-search-forward "^- -" nil t)
84 (cdr (or (assq mode mime-text-decoder-alist)
85 (assq t mime-text-decoder-alist))))
88 (goto-char (point-min))
89 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
91 (as-binary-process (funcall (pgp-function 'decrypt)))
92 (goto-char (point-min))
93 (delete-region (point-min)
95 (search-forward "\n\n")
97 (setq text-decoder (function mime-text-decode-buffer))
99 (setq major-mode 'mime-show-message-mode)
100 (setq mime-text-decoder text-decoder)
101 (save-window-excursion (mime-view-mode mother))
102 (set-window-buffer p-win mime-preview-buffer)
105 (set-atype 'mime-acting-condition
106 '((type . application)(subtype . pgp)
107 (method . mime-method-for-application/pgp)
110 (set-atype 'mime-acting-condition
111 '((type . text)(subtype . x-pgp)
112 (method . mime-method-for-application/pgp)
116 ;;; @ Internal method for multipart/signed
119 (defun mime-method-to-verify-multipart/signed (beg end cal)
120 "Internal method to check multipart/signed."
121 (let* ((rcnum (reverse (mime-raw-point-content-number beg)))
122 (oinfo (mime-raw-rcnum-to-cinfo (cons '1 rcnum)
123 mime-raw-entity-info))
125 (mime-playback-entity oinfo (cdr (assq 'mode cal)))
128 (set-atype 'mime-acting-condition
129 '((type . multipart)(subtype . signed)
130 (method . mime-method-to-verify-multipart/signed)
134 ;;; @ Internal method for application/pgp-signature
136 ;;; It is based on RFC 2015.
138 (defvar mime-pgp-command "pgp"
139 "*Name of the PGP command.")
141 (defvar mime-pgp-default-language 'en
142 "*Symbol of language for pgp.
143 It should be ISO 639 2 letter language code such as en, ja, ...")
145 (defvar mime-pgp-good-signature-regexp-alist
146 '((en . "Good signature from user.*$"))
147 "Alist of language vs regexp to detect ``Good signature''.")
149 (defvar mime-pgp-key-expected-regexp-alist
150 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
151 "Alist of language vs regexp to detect ``Key expected''.")
153 (defun mime-pgp-check-signature (output-buffer orig-file)
155 (set-buffer output-buffer)
157 (let* ((lang (or mime-pgp-default-language 'en))
158 (status (call-process-region (point-min)(point-max)
160 nil output-buffer nil
161 orig-file (format "+language=%s" lang)))
162 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
165 (set-buffer output-buffer)
166 (goto-char (point-min))
168 (cond ((not (stringp regexp))
169 "Please specify right regexp for specified language")
170 ((re-search-forward regexp nil t)
171 (buffer-substring (match-beginning 0) (match-end 0)))
172 (t "Bad signature")))
175 (defun mime-method-to-verify-application/pgp-signature (beg end cal)
176 "Internal method to check PGP/MIME signature."
177 (let* ((encoding (cdr (assq 'encoding cal)))
178 (cnum (mime-raw-point-content-number beg))
179 (rcnum (reverse cnum))
185 (raw-buf (current-buffer))
186 (oinfo (mime-raw-rcnum-to-cinfo (cons onum rmcnum)
187 mime-raw-entity-info))
189 (basename (expand-file-name "tm" mime-temp-directory))
190 (orig-file (make-temp-name basename))
191 (sig-file (concat orig-file ".sig"))
194 (let ((p-min (mime-entity-info-point-min oinfo))
195 (p-max (mime-entity-info-point-max oinfo))
197 (set-buffer (get-buffer-create mime-temp-buffer-name))
198 (insert-buffer-substring raw-buf p-min p-max)
200 (goto-char (point-min))
201 (while (re-search-forward "\n" nil t)
202 (replace-match "\r\n")
204 (as-binary-output-file (write-region (point-min)(point-max) orig-file))
205 (kill-buffer (current-buffer))
207 (save-excursion (mime-show-echo-buffer))
209 (let ((p-min (save-excursion
211 (and (search-forward "\n\n")
214 (set-buffer (setq kbuf (get-buffer-create mime-temp-buffer-name)))
215 (insert-buffer-substring raw-buf p-min end)
217 (mime-decode-region (point-min)(point-max) encoding)
218 (as-binary-output-file (write-region (point-min)(point-max) sig-file))
219 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
222 (set-buffer mime-echo-buffer-name)
223 (goto-char (point-min))
224 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
225 mime-pgp-key-expected-regexp-alist))))
226 (cond ((not (stringp regexp))
228 "Please specify right regexp for specified language")
230 ((re-search-forward regexp nil t)
232 (concat "0x" (buffer-substring-no-properties
238 (format "Key %s not found; attempt to fetch? " pgp-id))
241 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
242 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
245 (let ((other-window-scroll-buffer mime-echo-buffer-name))
246 (scroll-other-window 8)
249 (delete-file orig-file)
250 (delete-file sig-file)
253 (set-atype 'mime-acting-condition
254 '((type . application)(subtype . pgp-signature)
255 (method . mime-method-to-verify-application/pgp-signature)
259 ;;; @ Internal method for application/pgp-encrypted
261 ;;; It is based on RFC 2015.
263 (defun mime-method-to-decrypt-application/pgp-encrypted (beg end cal)
264 (let* ((cnum (mime-raw-point-content-number beg))
265 (rcnum (reverse cnum))
271 (oinfo (mime-raw-rcnum-to-cinfo (cons onum rmcnum)
272 mime-raw-entity-info))
273 (obeg (mime-entity-info-point-min oinfo))
274 (oend (mime-entity-info-point-max oinfo))
276 (mime-method-for-application/pgp obeg oend cal)
279 (set-atype 'mime-acting-condition
280 '((type . application)(subtype . pgp-encrypted)
281 (method . mime-method-to-decrypt-application/pgp-encrypted)
285 ;;; @ Internal method for application/pgp-keys
287 ;;; It is based on RFC 2015.
289 (defun mime-method-to-add-application/pgp-keys (beg end cal)
290 (let* ((cnum (mime-raw-point-content-number beg))
291 (new-name (format "%s-%s" (buffer-name) cnum))
292 (encoding (cdr (assq 'encoding cal)))
294 (setq str (buffer-substring beg 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)
319 (run-hooks 'mime-pgp-load-hook)
321 ;;; mime-pgp.el ends here