1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
3 ;; Copyright (C) 1995,1996,1997 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Renamed: 1997/2/27 from tm-pgp.el
8 ;; Version: $Id: mime-pgp.el,v 0.28 1997-11-20 13:24:20 morioka Exp $
9 ;; Keywords: PGP, security, MIME, multimedia, mail, news
11 ;; This file is part of SEMI (Secure Emacs MIME Interface).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This module is based on 2 drafts about PGP MIME integration:
32 ;; - RFC 2015: "MIME Security with Pretty Good Privacy (PGP)"
33 ;; by Michael Elkins <elkins@aero.org> (1996/6)
35 ;; - draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
36 ;; by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp>
39 ;; These drafts may be contrary to each other. You should decide
40 ;; which you support. (Maybe you should use PGP/MIME)
47 ;;; @ internal method for application/pgp
49 ;;; It is based on draft-kazu-pgp-mime-00.txt
51 (defun mime-process-application/pgp (beg end cal)
52 (let* ((cnum (mime-article/point-content-number beg))
53 (p-win (or (get-buffer-window mime-view-buffer)
54 (get-largest-window)))
55 (new-name (format "%s-%s" (buffer-name) cnum))
56 (the-buf (current-buffer))
57 (mother mime-view-buffer)
60 (set-buffer (get-buffer-create new-name))
62 (insert-buffer-substring the-buf beg end)
64 (goto-char (point-min))
65 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
67 (funcall (pgp-function 'verify))
68 (goto-char (point-min))
72 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
76 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
80 (goto-char (point-min))
81 (while (re-search-forward "^- -" nil t)
85 (cdr (or (assq mode mime-text-decoder-alist)
86 (assq t mime-text-decoder-alist))))
89 (goto-char (point-min))
90 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
92 (as-binary-process (funcall (pgp-function 'decrypt)))
93 (goto-char (point-min))
94 (delete-region (point-min)
96 (search-forward "\n\n")
98 (setq text-decoder (function mime-text-decode-buffer))
100 (setq major-mode 'mime-show-message-mode)
101 (setq mime-text-decoder text-decoder)
102 (save-window-excursion (mime-view-mode mother))
103 (set-window-buffer p-win mime-view-buffer)
106 (set-atype 'mime-acting-condition
107 '((type . "application/pgp")
108 (method . mime-process-application/pgp)
111 (set-atype 'mime-acting-condition
112 '((type . "text/x-pgp")
113 (method . mime-process-application/pgp)
117 ;;; @ Internal method for application/pgp-signature
119 ;;; It is based on RFC 2015.
121 (defvar mime-pgp-command "pgp"
122 "*Name of the PGP command.")
124 (defvar mime-pgp-default-language 'en
125 "*Symbol of language for pgp.
126 It should be ISO 639 2 letter language code such as en, ja, ...")
128 (defvar mime-pgp-good-signature-regexp-alist
129 '((en . "Good signature from user.*$"))
130 "Alist of language vs regexp to detect ``Good signature''.")
132 (defvar mime-pgp-key-expected-regexp-alist
133 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
134 "Alist of language vs regexp to detect ``Key expected''.")
136 (defun mime-pgp-check-signature (output-buffer orig-file)
138 (set-buffer output-buffer)
140 (let* ((lang (or mime-pgp-default-language 'en))
141 (status (call-process-region (point-min)(point-max)
143 nil output-buffer nil
144 orig-file (format "+language=%s" lang)))
145 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
148 (set-buffer output-buffer)
149 (goto-char (point-min))
151 (cond ((not (stringp regexp))
152 "Please specify right regexp for specified language")
153 ((re-search-forward regexp nil t)
154 (buffer-substring (match-beginning 0) (match-end 0)))
155 (t "Bad signature")))
158 (defun mime-pgp-check-application/pgp-signature (beg end cal)
159 "Internal method to check PGP/MIME signature."
160 (let* ((encoding (cdr (assq 'encoding cal)))
161 (cnum (mime-article/point-content-number beg))
162 (rcnum (reverse cnum))
168 (raw-buf (current-buffer))
169 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
170 mime-raw-content-info))
172 (basename (expand-file-name "tm" mime-temp-directory))
173 (orig-file (make-temp-name basename))
174 (sig-file (concat orig-file ".sig"))
177 (let ((p-min (mime-entity-info-point-min oinfo))
178 (p-max (mime-entity-info-point-max oinfo))
180 (set-buffer (get-buffer-create mime-temp-buffer-name))
181 (insert-buffer-substring raw-buf p-min p-max)
183 (goto-char (point-min))
184 (while (re-search-forward "\n" nil t)
185 (replace-match "\r\n")
187 (as-binary-output-file (write-region (point-min)(point-max) orig-file))
188 (kill-buffer (current-buffer))
190 (save-excursion (mime-show-echo-buffer))
192 (let ((p-min (save-excursion
194 (and (search-forward "\n\n")
197 (set-buffer (setq kbuf (get-buffer-create mime-temp-buffer-name)))
198 (insert-buffer-substring raw-buf p-min end)
200 (mime-decode-region (point-min)(point-max) encoding)
201 (as-binary-output-file (write-region (point-min)(point-max) sig-file))
202 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
205 (set-buffer mime-echo-buffer-name)
206 (goto-char (point-min))
207 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
208 mime-pgp-key-expected-regexp-alist))))
209 (cond ((not (stringp regexp))
211 "Please specify right regexp for specified language")
213 ((re-search-forward regexp nil t)
215 (concat "0x" (buffer-substring-no-properties
221 (format "Key %s not found; attempt to fetch? " pgp-id))
224 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
225 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
228 (let ((other-window-scroll-buffer mime-echo-buffer-name))
229 (scroll-other-window 8)
232 (delete-file orig-file)
233 (delete-file sig-file)
236 (set-atype 'mime-acting-condition
237 '((type . "application/pgp-signature")
238 (method . mime-pgp-check-application/pgp-signature)
242 ;;; @ Internal method for application/pgp-encrypted
244 ;;; It is based on RFC 2015.
246 (defun mime-pgp-decrypt-application/pgp-encrypted (beg end cal)
247 (let* ((cnum (mime-article/point-content-number beg))
248 (rcnum (reverse cnum))
254 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
255 mime-raw-content-info))
256 (obeg (mime-entity-info-point-min oinfo))
257 (oend (mime-entity-info-point-max oinfo))
259 (mime-process-application/pgp obeg oend cal)
262 (set-atype 'mime-acting-condition
263 '((type . "application/pgp-encrypted")
264 (method . mime-pgp-decrypt-application/pgp-encrypted)
268 ;;; @ Internal method for application/pgp-keys
270 ;;; It is based on RFC 2015.
272 (defun mime-pgp-add-keys (beg end cal)
273 (let* ((cnum (mime-article/point-content-number beg))
274 (new-name (format "%s-%s" (buffer-name) cnum))
275 (encoding (cdr (assq 'encoding cal)))
277 (setq str (buffer-substring beg end))
278 (switch-to-buffer new-name)
279 (setq buffer-read-only nil)
282 (goto-char (point-min))
283 (if (re-search-forward "^\n" nil t)
284 (delete-region (point-min) (match-end 0))
286 (mime-decode-region (point-min)(point-max) encoding)
287 (funcall (pgp-function 'snarf-keys))
288 (kill-buffer (current-buffer))
291 (set-atype 'mime-acting-condition
292 '((type . "application/pgp-keys")
293 (method . mime-pgp-add-keys)
302 (run-hooks 'mime-pgp-load-hook)
304 ;;; mime-pgp.el ends here