2 ;;; tm-pgp.el --- tm-view internal methods for PGP.
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995,1996 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
11 ;;; $Id: tm-pgp.el,v 7.19 1996/07/18 12:20:32 morioka Exp $
12 ;;; Keywords: mail, news, MIME, multimedia, PGP, security
14 ;;; This file is part of tm (Tools for MIME).
16 ;;; This program is free software; you can redistribute it and/or
17 ;;; modify it under the terms of the GNU General Public License as
18 ;;; published by the Free Software Foundation; either version 2, or
19 ;;; (at your option) any later version.
21 ;;; This program is distributed in the hope that it will be useful,
22 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 ;;; General Public License for more details.
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with This program. If not, write to the Free Software
28 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 ;;; This module is based on 2 drafts about PGP MIME integration:
32 ;;; - draft-elkins-pem-pgp-02.txt
33 ;;; ``MIME Security with Pretty Good Privacy (PGP)''
34 ;;; by Michael Elkins <elkins@aero.org> (1995/11)
35 ;;; - draft-kazu-pgp-mime-00.txt
36 ;;; ``PGP MIME Integration''
37 ;;; by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp> (1995/10)
38 ;;; These drafts may be contrary to each other. You should decide
39 ;;; which you support.
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 (new-name (format "%s-%s" (buffer-name) cnum))
55 (mother mime::article/preview-buffer)
58 (setq str (buffer-substring beg end))
59 (switch-to-buffer new-name)
63 (goto-char (point-min))
64 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
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)
85 (cdr (assq mode mime-viewer/code-converter-alist))
86 (function mime-viewer/default-code-convert-region)))
89 (goto-char (point-min))
90 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
92 (as-binary-process (mc-decrypt))
93 (goto-char (point-min))
94 (delete-region (point-min)
96 (search-forward "\n\n")
98 (setq code-converter (function mime-charset/decode-buffer))
100 (setq major-mode 'mime/show-message-mode)
101 (setq mime::article/code-converter code-converter)
102 (mime/viewer-mode mother)
105 (set-atype 'mime/content-decoding-condition
106 '((type . "application/pgp")
107 (method . mime-article/view-application/pgp)
110 (set-atype 'mime/content-decoding-condition
111 '((type . "text/x-pgp")
112 (method . mime-article/view-application/pgp)
116 ;;; @ Internal method for application/pgp-signature
118 ;;; It is based on draft-elkins-pem-pgp-02.txt
120 (defvar tm-pgp::good-signature-regexp "Good signature from user.*$")
122 (defvar tm-pgp::key-expected-regexp
123 "Key matching expected Key ID \\(\\S +\\) not found")
125 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
127 (set-buffer output-buffer)
131 (call-process-region (point-min)(point-max)
132 "pgp" nil output-buffer nil orig-file)))
135 (set-buffer output-buffer)
136 (goto-char (point-min))
138 (if (re-search-forward tm-pgp::good-signature-regexp nil t)
139 (buffer-substring (match-beginning 0) (match-end 0))
144 (defun mime-article/check-pgp-signature (beg end cal)
145 (let* ((encoding (cdr (assq 'encoding cal)))
146 (cnum (mime-article/point-content-number beg))
147 (rcnum (reverse cnum))
153 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
154 mime::article/content-info))
156 (basename (expand-file-name "tm" mime/tmp-dir))
157 (orig-file (make-temp-name basename))
158 (sig-file (concat orig-file ".sig"))
161 (setq str (buffer-substring
162 (mime::content-info/point-min oinfo)
163 (mime::content-info/point-max oinfo)
165 (set-buffer (get-buffer-create mime/temp-buffer-name))
167 (goto-char (point-min))
168 (while (re-search-forward "\n" nil t)
169 (replace-match "\r\n")
171 (let ((mc-flag nil) ; for Mule
172 (file-coding-system *noconv*)
173 kanji-flag ; for NEmacs
174 (emx-binary-mode t) ; for OS/2
175 jka-compr-compression-info-list ; for jka-compr
176 jam-zcat-filename-list ; for jam-zcat
177 require-final-newline)
178 (write-file orig-file)
180 (kill-buffer (current-buffer))
183 (mime-article/show-output-buffer)
186 (setq str (buffer-substring
189 (and (search-forward "\n\n")
192 (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
194 (mime-decode-region (point-min)(point-max) encoding)
195 (let ((mc-flag nil) ; for Mule
196 (file-coding-system *noconv*)
197 kanji-flag ; for NEmacs
198 (emx-binary-mode t) ; for OS/2
199 jka-compr-compression-info-list ; for jka-compr
200 jam-zcat-filename-list ; for jam-zcat
201 require-final-newline)
202 (write-file sig-file)
204 ;;(get-buffer-create mime/output-buffer-name)
205 (or (mime::article/call-pgp-to-check-signature
206 mime/output-buffer-name orig-file)
209 (set-buffer mime/output-buffer-name)
210 (goto-char (point-min))
211 (if (re-search-forward tm-pgp::key-expected-regexp nil t)
213 (concat "0x" (buffer-substring-no-properties
218 (format "Key %s not found; attempt to fetch? " pgp-id))
219 (mc-pgp-fetch-key (cons nil pgp-id))
220 (mime::article/call-pgp-to-check-signature
221 mime/output-buffer-name orig-file)
223 (let ((other-window-scroll-buffer mime/output-buffer-name))
224 (scroll-other-window 8)
227 (delete-file orig-file)
228 (delete-file sig-file)
231 (set-atype 'mime/content-decoding-condition
232 '((type . "application/pgp-signature")
233 (method . mime-article/check-pgp-signature)
237 ;;; @ Internal method for application/pgp-encrypted
239 ;;; It is based on draft-elkins-pem-pgp-02.txt
241 (defun mime-article/decrypt-pgp (beg end cal)
242 (let* ((cnum (mime-article/point-content-number beg))
243 (rcnum (reverse cnum))
249 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
250 mime::article/content-info))
251 (obeg (mime::content-info/point-min oinfo))
252 (oend (mime::content-info/point-max oinfo))
254 (mime-article/view-application/pgp obeg oend cal)
257 (set-atype 'mime/content-decoding-condition
258 '((type . "application/pgp-encrypted")
259 (method . mime-article/decrypt-pgp)
263 ;;; @ Internal method for application/pgp-keys
265 ;;; It is based on draft-elkins-pem-pgp-02.txt
267 (autoload 'mc-snarf-keys "mc-toplev")
269 (defun mime-article/add-pgp-keys (beg end cal)
270 (let* ((cnum (mime-article/point-content-number beg))
271 (cur-buf (current-buffer))
272 (new-name (format "%s-%s" (buffer-name) cnum))
273 (mother mime::article/preview-buffer)
274 (charset (cdr (assoc "charset" cal)))
275 (encoding (cdr (assq 'encoding cal)))
278 (setq str (buffer-substring beg end))
279 (switch-to-buffer new-name)
280 (setq buffer-read-only nil)
283 (goto-char (point-min))
284 (if (re-search-forward "^\n" nil t)
285 (delete-region (point-min) (match-end 0))
287 (mime-decode-region (point-min)(point-max) encoding)
289 (kill-buffer (current-buffer))
292 (set-atype 'mime/content-decoding-condition
293 '((type . "application/pgp-keys")
294 (method . mime-article/add-pgp-keys)
303 ;;; tm-pgp.el ends here