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.9 1997-03-15 21:20:02 morioka Exp $
9 ;; Keywords: PGP, security, MIME, multimedia, mail, news
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
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-article/view-application/pgp (beg end cal)
52 (let* ((cnum (mime-article/point-content-number beg))
53 (p-win (or (get-buffer-window mime::article/preview-buffer)
54 (get-largest-window)))
55 (new-name (format "%s-%s" (buffer-name) cnum))
56 (mother mime::article/preview-buffer)
59 (str (buffer-substring beg end))
61 (set-buffer (get-buffer-create new-name))
65 (goto-char (point-min))
66 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
68 (funcall (pgp-function 'verify))
69 (goto-char (point-min))
73 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
77 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
81 (goto-char (point-min))
82 (while (re-search-forward "^- -" nil t)
86 (cdr (or (assq mode mime-text-decoder-alist)
87 (assq t mime-text-decoder-alist))))
90 (goto-char (point-min))
91 (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 code-converter (function mime-charset/decode-buffer))
101 (setq major-mode 'mime/show-message-mode)
102 (setq mime::article/code-converter code-converter)
103 (save-window-excursion (mime-view-mode mother))
104 (set-window-buffer p-win mime::article/preview-buffer)
107 (set-atype 'mime/content-decoding-condition
108 '((type . "application/pgp")
109 (method . mime-article/view-application/pgp)
112 (set-atype 'mime/content-decoding-condition
113 '((type . "text/x-pgp")
114 (method . mime-article/view-application/pgp)
118 ;;; @ Internal method for application/pgp-signature
120 ;;; It is based on RFC 2015.
122 (defvar mime-pgp-command "pgp"
123 "*Name of the PGP command.")
125 (defvar mime-pgp-default-language 'en
126 "*Symbol of language for pgp.
127 It should be ISO 639 2 letter language code such as en, ja, ...")
129 (defvar mime-pgp-good-signature-regexp-alist
130 '((en . "Good signature from user.*$"))
131 "Alist of language vs regexp to detect ``Good signature''.")
133 (defvar mime-pgp-key-expected-regexp-alist
134 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
135 "Alist of language vs regexp to detect ``Key expected''.")
137 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
139 (set-buffer output-buffer)
142 (let* ((lang (or mime-pgp-default-language 'en))
143 (status (call-process-region (point-min)(point-max)
145 nil output-buffer nil
146 orig-file (format "+language=%s" lang)))
147 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist)))
151 (set-buffer output-buffer)
152 (goto-char (point-min))
154 (cond ((not (stringp regexp))
155 "Please specify right regexp for specified language")
156 ((re-search-forward regexp nil t)
157 (buffer-substring (match-beginning 0) (match-end 0))
163 (defun mime-article/check-pgp-signature (beg end cal)
164 (let* ((encoding (cdr (assq 'encoding cal)))
165 (cnum (mime-article/point-content-number beg))
166 (rcnum (reverse cnum))
172 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
173 mime::article/content-info))
175 (basename (expand-file-name "tm" mime-temp-directory))
176 (orig-file (make-temp-name basename))
177 (sig-file (concat orig-file ".sig"))
180 (setq str (buffer-substring
181 (mime::content-info/point-min oinfo)
182 (mime::content-info/point-max oinfo)
184 (set-buffer (get-buffer-create mime/temp-buffer-name))
186 (goto-char (point-min))
187 (while (re-search-forward "\n" nil t)
188 (replace-match "\r\n")
190 (as-binary-output-file (write-file orig-file))
191 (kill-buffer (current-buffer))
194 (mime-article/show-output-buffer)
197 (setq str (buffer-substring
200 (and (search-forward "\n\n")
203 (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
205 (mime-decode-region (point-min)(point-max) encoding)
206 (as-binary-output-file (write-file sig-file))
207 (or (mime::article/call-pgp-to-check-signature
208 mime/output-buffer-name orig-file)
211 (set-buffer mime/output-buffer-name)
212 (goto-char (point-min))
213 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
214 mime-pgp-key-expected-regexp-alist))))
215 (cond ((not (stringp regexp))
217 "Please specify right regexp for specified language")
219 ((re-search-forward regexp nil t)
221 (concat "0x" (buffer-substring-no-properties
227 (format "Key %s not found; attempt to fetch? " pgp-id))
230 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
231 (mime::article/call-pgp-to-check-signature
232 mime/output-buffer-name orig-file)
235 (let ((other-window-scroll-buffer mime/output-buffer-name))
236 (scroll-other-window 8)
239 (delete-file orig-file)
240 (delete-file sig-file)
243 (set-atype 'mime/content-decoding-condition
244 '((type . "application/pgp-signature")
245 (method . mime-article/check-pgp-signature)
249 ;;; @ Internal method for application/pgp-encrypted
251 ;;; It is based on RFC 2015.
253 (defun mime-article/decrypt-pgp (beg end cal)
254 (let* ((cnum (mime-article/point-content-number beg))
255 (rcnum (reverse cnum))
261 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
262 mime::article/content-info))
263 (obeg (mime::content-info/point-min oinfo))
264 (oend (mime::content-info/point-max oinfo))
266 (mime-article/view-application/pgp obeg oend cal)
269 (set-atype 'mime/content-decoding-condition
270 '((type . "application/pgp-encrypted")
271 (method . mime-article/decrypt-pgp)
275 ;;; @ Internal method for application/pgp-keys
277 ;;; It is based on RFC 2015.
279 (defun mime-article/add-pgp-keys (beg end cal)
280 (let* ((cnum (mime-article/point-content-number beg))
281 (new-name (format "%s-%s" (buffer-name) cnum))
282 (encoding (cdr (assq 'encoding cal)))
284 (setq str (buffer-substring beg end))
285 (switch-to-buffer new-name)
286 (setq buffer-read-only nil)
289 (goto-char (point-min))
290 (if (re-search-forward "^\n" nil t)
291 (delete-region (point-min) (match-end 0))
293 (mime-decode-region (point-min)(point-max) encoding)
294 (funcall (pgp-function 'snarf-keys))
295 (kill-buffer (current-buffer))
298 (set-atype 'mime/content-decoding-condition
299 '((type . "application/pgp-keys")
300 (method . mime-article/add-pgp-keys)
309 (run-hooks 'mime-pgp-load-hook)
311 ;;; mime-pgp.el ends here