1 ;;; tm-pgp.el --- tm-view internal methods for PGP.
3 ;; Copyright (C) 1995,1996,1997 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Version: $Id: tm-pgp.el,v 7.33 1997/02/13 14:51:04 morioka Exp $
8 ;; Keywords: mail, news, MIME, multimedia, PGP, security
10 ;; This file is part of tm (Tools for MIME).
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-article/view-application/pgp (beg end cal)
51 (let* ((cnum (mime-article/point-content-number beg))
52 (cur-buf (current-buffer))
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)
87 (cdr (assq mode mime-viewer/code-converter-alist))
88 (function mime-viewer/default-code-convert-region)))
91 (goto-char (point-min))
92 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
94 (as-binary-process (funcall (pgp-function 'decrypt)))
95 (goto-char (point-min))
96 (delete-region (point-min)
98 (search-forward "\n\n")
100 (setq code-converter (function mime-charset/decode-buffer))
102 (setq major-mode 'mime/show-message-mode)
103 (setq mime::article/code-converter code-converter)
104 (save-window-excursion (mime/viewer-mode mother))
105 (set-window-buffer p-win mime::article/preview-buffer)
108 (set-atype 'mime/content-decoding-condition
109 '((type . "application/pgp")
110 (method . mime-article/view-application/pgp)
113 (set-atype 'mime/content-decoding-condition
114 '((type . "text/x-pgp")
115 (method . mime-article/view-application/pgp)
119 ;;; @ Internal method for application/pgp-signature
121 ;;; It is based on RFC 2015.
123 (defvar tm-pgp::default-language 'en
124 "*Symbol of language for pgp.
125 It should be ISO 639 2 letter language code such as en, ja, ...")
127 (defvar tm-pgp::good-signature-regexp-alist
128 '((en . "Good signature from user.*$"))
129 "Alist of language vs regexp to detect ``Good signature''.")
131 (defvar tm-pgp::key-expected-regexp-alist
132 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
133 "Alist of language vs regexp to detect ``Key expected''.")
135 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
137 (set-buffer output-buffer)
140 (let* ((lang (or tm-pgp::default-language 'en))
142 (call-process-region (point-min)(point-max)
143 "pgp" nil output-buffer nil orig-file
144 (format "+language=%s" lang)
146 (regexp (cdr (assq lang tm-pgp::good-signature-regexp-alist)))
150 (set-buffer output-buffer)
151 (goto-char (point-min))
153 (cond ((not (stringp regexp))
154 "Please specify right regexp for specified language")
155 ((re-search-forward regexp nil t)
156 (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/tmp-dir))
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 tm-pgp::default-language 'en)
214 tm-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 (cur-buf (current-buffer))
282 (new-name (format "%s-%s" (buffer-name) cnum))
283 (mother mime::article/preview-buffer)
284 (charset (cdr (assoc "charset" cal)))
285 (encoding (cdr (assq 'encoding cal)))
288 (setq str (buffer-substring beg end))
289 (switch-to-buffer new-name)
290 (setq buffer-read-only nil)
293 (goto-char (point-min))
294 (if (re-search-forward "^\n" nil t)
295 (delete-region (point-min) (match-end 0))
297 (mime-decode-region (point-min)(point-max) encoding)
298 (funcall (pgp-function 'snarf-keys))
299 (kill-buffer (current-buffer))
302 (set-atype 'mime/content-decoding-condition
303 '((type . "application/pgp-keys")
304 (method . mime-article/add-pgp-keys)
313 (run-hooks 'tm-pgp-load-hook)
315 ;;; tm-pgp.el ends here