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.1 1997-02-27 08:10:24 tmorioka Exp $
9 ;; Keywords: mail, news, MIME, multimedia, PGP, security
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 (cur-buf (current-buffer))
54 (p-win (or (get-buffer-window mime::article/preview-buffer)
55 (get-largest-window)))
56 (new-name (format "%s-%s" (buffer-name) cnum))
57 (mother mime::article/preview-buffer)
60 (str (buffer-substring beg end))
62 (set-buffer (get-buffer-create new-name))
66 (goto-char (point-min))
67 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
69 (funcall (pgp-function 'verify))
70 (goto-char (point-min))
74 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
78 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
82 (goto-char (point-min))
83 (while (re-search-forward "^- -" nil t)
88 (cdr (assq mode mime-viewer/code-converter-alist))
89 (function mime-viewer/default-code-convert-region)))
92 (goto-char (point-min))
93 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
95 (as-binary-process (funcall (pgp-function 'decrypt)))
96 (goto-char (point-min))
97 (delete-region (point-min)
99 (search-forward "\n\n")
101 (setq code-converter (function mime-charset/decode-buffer))
103 (setq major-mode 'mime/show-message-mode)
104 (setq mime::article/code-converter code-converter)
105 (save-window-excursion (mime/viewer-mode mother))
106 (set-window-buffer p-win mime::article/preview-buffer)
109 (set-atype 'mime/content-decoding-condition
110 '((type . "application/pgp")
111 (method . mime-article/view-application/pgp)
114 (set-atype 'mime/content-decoding-condition
115 '((type . "text/x-pgp")
116 (method . mime-article/view-application/pgp)
120 ;;; @ Internal method for application/pgp-signature
122 ;;; It is based on RFC 2015.
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::article/call-pgp-to-check-signature (output-buffer orig-file)
138 (set-buffer output-buffer)
141 (let* ((lang (or mime-pgp-default-language 'en))
143 (call-process-region (point-min)(point-max)
144 "pgp" nil output-buffer nil orig-file
145 (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))
164 (defun mime-article/check-pgp-signature (beg end cal)
165 (let* ((encoding (cdr (assq 'encoding cal)))
166 (cnum (mime-article/point-content-number beg))
167 (rcnum (reverse cnum))
173 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
174 mime::article/content-info))
176 (basename (expand-file-name "tm" mime/tmp-dir))
177 (orig-file (make-temp-name basename))
178 (sig-file (concat orig-file ".sig"))
181 (setq str (buffer-substring
182 (mime::content-info/point-min oinfo)
183 (mime::content-info/point-max oinfo)
185 (set-buffer (get-buffer-create mime/temp-buffer-name))
187 (goto-char (point-min))
188 (while (re-search-forward "\n" nil t)
189 (replace-match "\r\n")
191 (as-binary-output-file (write-file orig-file))
192 (kill-buffer (current-buffer))
195 (mime-article/show-output-buffer)
198 (setq str (buffer-substring
201 (and (search-forward "\n\n")
204 (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
206 (mime-decode-region (point-min)(point-max) encoding)
207 (as-binary-output-file (write-file sig-file))
208 (or (mime::article/call-pgp-to-check-signature
209 mime/output-buffer-name orig-file)
212 (set-buffer mime/output-buffer-name)
213 (goto-char (point-min))
214 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
215 mime-pgp-key-expected-regexp-alist))))
216 (cond ((not (stringp regexp))
218 "Please specify right regexp for specified language")
220 ((re-search-forward regexp nil t)
222 (concat "0x" (buffer-substring-no-properties
228 (format "Key %s not found; attempt to fetch? " pgp-id))
231 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
232 (mime::article/call-pgp-to-check-signature
233 mime/output-buffer-name orig-file)
236 (let ((other-window-scroll-buffer mime/output-buffer-name))
237 (scroll-other-window 8)
240 (delete-file orig-file)
241 (delete-file sig-file)
244 (set-atype 'mime/content-decoding-condition
245 '((type . "application/pgp-signature")
246 (method . mime-article/check-pgp-signature)
250 ;;; @ Internal method for application/pgp-encrypted
252 ;;; It is based on RFC 2015.
254 (defun mime-article/decrypt-pgp (beg end cal)
255 (let* ((cnum (mime-article/point-content-number beg))
256 (rcnum (reverse cnum))
262 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
263 mime::article/content-info))
264 (obeg (mime::content-info/point-min oinfo))
265 (oend (mime::content-info/point-max oinfo))
267 (mime-article/view-application/pgp obeg oend cal)
270 (set-atype 'mime/content-decoding-condition
271 '((type . "application/pgp-encrypted")
272 (method . mime-article/decrypt-pgp)
276 ;;; @ Internal method for application/pgp-keys
278 ;;; It is based on RFC 2015.
280 (defun mime-article/add-pgp-keys (beg end cal)
281 (let* ((cnum (mime-article/point-content-number beg))
282 (cur-buf (current-buffer))
283 (new-name (format "%s-%s" (buffer-name) cnum))
284 (mother mime::article/preview-buffer)
285 (charset (cdr (assoc "charset" cal)))
286 (encoding (cdr (assq 'encoding cal)))
289 (setq str (buffer-substring beg end))
290 (switch-to-buffer new-name)
291 (setq buffer-read-only nil)
294 (goto-char (point-min))
295 (if (re-search-forward "^\n" nil t)
296 (delete-region (point-min) (match-end 0))
298 (mime-decode-region (point-min)(point-max) encoding)
299 (funcall (pgp-function 'snarf-keys))
300 (kill-buffer (current-buffer))
303 (set-atype 'mime/content-decoding-condition
304 '((type . "application/pgp-keys")
305 (method . mime-article/add-pgp-keys)
314 (run-hooks 'mime-pgp-load-hook)
316 ;;; mime-pgp.el ends here