tm 7.67.
[elisp/tm.git] / tm-pgp.el
1 ;;;
2 ;;; tm-pgp.el --- tm-view internal methods for PGP.
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995,1996 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; Created: 1995/12/7
10 ;;; Version:
11 ;;;     $Id: tm-pgp.el,v 7.15 1996/05/27 08:16:38 morioka Exp $
12 ;;; Keywords: mail, news, MIME, multimedia, PGP, security
13 ;;;
14 ;;; This file is part of tm (Tools for MIME).
15 ;;;
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.
20 ;;;
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.
25 ;;;
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.
29 ;;;
30 ;;; Commentary:
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.
40 ;;;
41 ;;; Code:
42
43 (require 'mailcrypt)
44 (require 'tm-play)
45
46
47 ;;; @ internal method for application/pgp
48 ;;;
49 ;;; It is based on draft-kazu-pgp-mime-00.txt
50
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)
56          (mode major-mode)
57          code-converter str)
58     (setq str (buffer-substring beg end))
59     (switch-to-buffer new-name)
60     (erase-buffer)
61     (insert str)
62     (cond ((progn
63              (goto-char (point-min))
64              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
65              )
66            (mc-verify)
67            (goto-char (point-min))
68            (delete-region
69             (point-min)
70             (and
71              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
72              (match-end 0))
73             )
74            (delete-region
75             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
76                  (match-beginning 0))
77             (point-max)
78             )
79            (goto-char (point-min))
80            (while (re-search-forward "^- -" nil t)
81              (replace-match "-")
82              )
83            (setq code-converter
84                  (or
85                   (cdr (assq mode mime-viewer/code-converter-alist))
86                   (function mime-viewer/default-code-convert-region)))
87            )
88           ((progn
89              (goto-char (point-min))
90              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
91              )
92            (let ((program-coding-system-alist
93                   (if (boundp 'program-coding-system-alist)
94                       (cons '((nil . ".*pgp.*") *noconv* . *noconv*)
95                             program-coding-system-alist))
96                   ))
97              (mc-decrypt)
98              )
99            (goto-char (point-min))
100            (delete-region (point-min)
101                           (and
102                            (search-forward "\n\n")
103                            (match-end 0)))
104            (setq code-converter (function mime-charset-decode-region))
105            ))
106     (setq major-mode 'mime/show-message-mode)
107     (setq mime::article/code-converter code-converter)
108     (mime/viewer-mode mother)
109     ))
110
111 (set-atype 'mime/content-decoding-condition
112            '((type . "application/pgp")
113              (method . mime-article/view-application/pgp)
114              ))
115
116 (set-atype 'mime/content-decoding-condition
117            '((type . "text/x-pgp")
118              (method . mime-article/view-application/pgp)
119              ))
120
121
122 ;;; @ Internal method for application/pgp-signature
123 ;;;
124 ;;; It is based on draft-elkins-pem-pgp-02.txt
125
126 (defvar tm-pgp::good-signature-regexp "Good signature from user.*$")
127
128 (defvar tm-pgp::key-expected-regexp
129   "Key matching expected Key ID \\(\\S +\\) not found")
130
131 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
132   (save-excursion
133     (set-buffer output-buffer)
134     (erase-buffer)
135     )
136   (let ((status
137          (call-process-region (point-min)(point-max)
138                               "pgp" nil output-buffer nil orig-file)))
139     (if (= status 0)
140         (save-excursion
141           (set-buffer output-buffer)
142           (goto-char (point-min))
143           (message
144            (if (re-search-forward tm-pgp::good-signature-regexp nil t)
145                (buffer-substring (match-beginning 0) (match-end 0))
146              "Bad signature"
147              ))
148           ))))
149
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))
154          (rmcnum (cdr rcnum))
155          (knum (car rcnum))
156          (onum (if (> knum 0)
157                    (1- knum)
158                  (1+ knum)))
159          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
160                                              mime::article/content-info))
161          status str kbuf
162          (basename (expand-file-name "tm" mime/tmp-dir))
163          (orig-file (make-temp-name basename))
164          (sig-file (concat orig-file ".sig"))
165          )
166     (save-excursion
167       (setq str (buffer-substring
168                  (mime::content-info/point-min oinfo)
169                  (mime::content-info/point-max oinfo)
170                  ))
171       (set-buffer (get-buffer-create mime/temp-buffer-name))
172       (insert str)
173       (goto-char (point-min))
174       (while (re-search-forward "\n" nil t)
175         (replace-match "\r\n")
176         )
177       (let ((mc-flag nil)                   ; for Mule
178             (file-coding-system
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)
186         )
187       (kill-buffer (current-buffer))
188       )
189     (save-excursion
190       (setq str (buffer-substring
191                  (save-excursion
192                    (goto-char beg)
193                    (and (search-forward "\n\n")
194                         (match-end 0)))
195                  end))
196       (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
197       (insert str)
198       (mime-decode-region (point-min)(point-max) encoding)
199       (let ((mc-flag nil)                   ; for Mule
200             (file-coding-system
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)
208         )
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)
214           (let (pgp-id)
215             (save-excursion
216               (set-buffer mime/output-buffer-name)
217               (goto-char (point-min))
218               (if (re-search-forward tm-pgp::key-expected-regexp nil t)
219                   (setq pgp-id
220                         (concat "0x" (buffer-substring-no-properties
221                                       (match-beginning 1)
222                                       (match-end 1))))
223                 ))
224             (y-or-n-p
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)
229             ))
230       (let ((other-window-scroll-buffer mime/output-buffer-name))
231         (scroll-other-window 8)
232         )
233       (kill-buffer kbuf)
234       (delete-file orig-file)
235       (delete-file sig-file)
236       )))
237
238 (set-atype 'mime/content-decoding-condition
239            '((type . "application/pgp-signature")
240              (method . mime-article/check-pgp-signature)
241              ))
242
243
244 ;;; @ Internal method for application/pgp-encrypted
245 ;;;
246 ;;; It is based on draft-elkins-pem-pgp-02.txt
247
248 (defun mime-article/decrypt-pgp (beg end cal)
249   (let* ((cnum (mime-article/point-content-number beg))
250          (rcnum (reverse cnum))
251          (rmcnum (cdr rcnum))
252          (knum (car rcnum))
253          (onum (if (> knum 0)
254                    (1- knum)
255                  (1+ knum)))
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))
260          )
261     (mime-article/view-application/pgp obeg oend cal)
262     ))
263
264 (set-atype 'mime/content-decoding-condition
265            '((type . "application/pgp-encrypted")
266              (method . mime-article/decrypt-pgp)
267              ))
268
269
270 ;;; @ Internal method for application/pgp-keys
271 ;;;
272 ;;; It is based on draft-elkins-pem-pgp-02.txt
273
274 (autoload 'mc-snarf-keys "mc-toplev")
275
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)))
283          (mode major-mode)
284          str)
285     (setq str (buffer-substring beg end))
286     (switch-to-buffer new-name)
287     (setq buffer-read-only nil)
288     (erase-buffer)
289     (insert str)
290     (goto-char (point-min))
291     (if (re-search-forward "^\n" nil t)
292         (delete-region (point-min) (match-end 0))
293       )
294     (mime-decode-region (point-min)(point-max) encoding)
295     (mc-snarf-keys)
296     (kill-buffer (current-buffer))
297     ))
298
299 (set-atype 'mime/content-decoding-condition
300            '((type . "application/pgp-keys")
301              (method . mime-article/add-pgp-keys)
302              ))
303
304          
305 ;;; @ end
306 ;;;
307
308 (provide 'tm-pgp)
309
310 ;;; tm-pgp.el ends here