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.15 1996/05/27 08:16:38 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 (let ((program-coding-system-alist
93 (if (boundp 'program-coding-system-alist)
94 (cons '((nil . ".*pgp.*") *noconv* . *noconv*)
95 program-coding-system-alist))
99 (goto-char (point-min))
100 (delete-region (point-min)
102 (search-forward "\n\n")
104 (setq code-converter (function mime-charset-decode-region))
106 (setq major-mode 'mime/show-message-mode)
107 (setq mime::article/code-converter code-converter)
108 (mime/viewer-mode mother)
111 (set-atype 'mime/content-decoding-condition
112 '((type . "application/pgp")
113 (method . mime-article/view-application/pgp)
116 (set-atype 'mime/content-decoding-condition
117 '((type . "text/x-pgp")
118 (method . mime-article/view-application/pgp)
122 ;;; @ Internal method for application/pgp-signature
124 ;;; It is based on draft-elkins-pem-pgp-02.txt
126 (defvar tm-pgp::good-signature-regexp "Good signature from user.*$")
128 (defvar tm-pgp::key-expected-regexp
129 "Key matching expected Key ID \\(\\S +\\) not found")
131 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
133 (set-buffer output-buffer)
137 (call-process-region (point-min)(point-max)
138 "pgp" nil output-buffer nil orig-file)))
141 (set-buffer output-buffer)
142 (goto-char (point-min))
144 (if (re-search-forward tm-pgp::good-signature-regexp nil t)
145 (buffer-substring (match-beginning 0) (match-end 0))
150 (defun mime-article/check-pgp-signature (beg end cal)
151 (let* ((encoding (cdr (assq 'encoding cal)))
152 (cnum (mime-article/point-content-number beg))
153 (rcnum (reverse cnum))
159 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
160 mime::article/content-info))
162 (basename (expand-file-name "tm" mime/tmp-dir))
163 (orig-file (make-temp-name basename))
164 (sig-file (concat orig-file ".sig"))
167 (setq str (buffer-substring
168 (mime::content-info/point-min oinfo)
169 (mime::content-info/point-max oinfo)
171 (set-buffer (get-buffer-create mime/temp-buffer-name))
173 (goto-char (point-min))
174 (while (re-search-forward "\n" nil t)
175 (replace-match "\r\n")
177 (let ((mc-flag nil) ; for Mule
179 (if (featurep 'mule) *noconv*))
180 kanji-flag ; for NEmacs
181 (emx-binary-mode t) ; for OS/2
182 jka-compr-compression-info-list ; for jka-compr
183 jam-zcat-filename-list ; for jam-zcat
184 require-final-newline)
185 (write-file orig-file)
187 (kill-buffer (current-buffer))
190 (setq str (buffer-substring
193 (and (search-forward "\n\n")
196 (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
198 (mime-decode-region (point-min)(point-max) encoding)
199 (let ((mc-flag nil) ; for Mule
201 (if (featurep 'mule) *noconv*))
202 kanji-flag ; for NEmacs
203 (emx-binary-mode t) ; for OS/2
204 jka-compr-compression-info-list ; for jka-compr
205 jam-zcat-filename-list ; for jam-zcat
206 require-final-newline)
207 (write-file sig-file)
209 ;;(define-program-coding-system (current-buffer) "pgp" *noconv*)
210 (mime-article/show-output-buffer)
211 ;;(get-buffer-create mime/output-buffer-name)
212 (or (mime::article/call-pgp-to-check-signature
213 mime/output-buffer-name orig-file)
216 (set-buffer mime/output-buffer-name)
217 (goto-char (point-min))
218 (if (re-search-forward tm-pgp::key-expected-regexp nil t)
220 (concat "0x" (buffer-substring-no-properties
225 (format "Key %s not found; attempt to fetch? " pgp-id))
226 (mc-pgp-fetch-key (cons nil pgp-id))
227 (mime::article/call-pgp-to-check-signature
228 mime/output-buffer-name orig-file)
230 (let ((other-window-scroll-buffer mime/output-buffer-name))
231 (scroll-other-window 8)
234 (delete-file orig-file)
235 (delete-file sig-file)
238 (set-atype 'mime/content-decoding-condition
239 '((type . "application/pgp-signature")
240 (method . mime-article/check-pgp-signature)
244 ;;; @ Internal method for application/pgp-encrypted
246 ;;; It is based on draft-elkins-pem-pgp-02.txt
248 (defun mime-article/decrypt-pgp (beg end cal)
249 (let* ((cnum (mime-article/point-content-number beg))
250 (rcnum (reverse cnum))
256 (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
257 mime::article/content-info))
258 (obeg (mime::content-info/point-min oinfo))
259 (oend (mime::content-info/point-max oinfo))
261 (mime-article/view-application/pgp obeg oend cal)
264 (set-atype 'mime/content-decoding-condition
265 '((type . "application/pgp-encrypted")
266 (method . mime-article/decrypt-pgp)
270 ;;; @ Internal method for application/pgp-keys
272 ;;; It is based on draft-elkins-pem-pgp-02.txt
274 (autoload 'mc-snarf-keys "mc-toplev")
276 (defun mime-article/add-pgp-keys (beg end cal)
277 (let* ((cnum (mime-article/point-content-number beg))
278 (cur-buf (current-buffer))
279 (new-name (format "%s-%s" (buffer-name) cnum))
280 (mother mime::article/preview-buffer)
281 (charset (cdr (assoc "charset" cal)))
282 (encoding (cdr (assq 'encoding cal)))
285 (setq str (buffer-substring beg end))
286 (switch-to-buffer new-name)
287 (setq buffer-read-only nil)
290 (goto-char (point-min))
291 (if (re-search-forward "^\n" nil t)
292 (delete-region (point-min) (match-end 0))
294 (mime-decode-region (point-min)(point-max) encoding)
296 (kill-buffer (current-buffer))
299 (set-atype 'mime/content-decoding-condition
300 '((type . "application/pgp-keys")
301 (method . mime-article/add-pgp-keys)
310 ;;; tm-pgp.el ends here