tm 7.69.
[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.16 1996/06/27 14:26:56 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            (as-binary-process (mc-decrypt))
93            (goto-char (point-min))
94            (delete-region (point-min)
95                           (and
96                            (search-forward "\n\n")
97                            (match-end 0)))
98            (setq code-converter (function mime-charset-decode-region))
99            ))
100     (setq major-mode 'mime/show-message-mode)
101     (setq mime::article/code-converter code-converter)
102     (mime/viewer-mode mother)
103     ))
104
105 (set-atype 'mime/content-decoding-condition
106            '((type . "application/pgp")
107              (method . mime-article/view-application/pgp)
108              ))
109
110 (set-atype 'mime/content-decoding-condition
111            '((type . "text/x-pgp")
112              (method . mime-article/view-application/pgp)
113              ))
114
115
116 ;;; @ Internal method for application/pgp-signature
117 ;;;
118 ;;; It is based on draft-elkins-pem-pgp-02.txt
119
120 (defvar tm-pgp::good-signature-regexp "Good signature from user.*$")
121
122 (defvar tm-pgp::key-expected-regexp
123   "Key matching expected Key ID \\(\\S +\\) not found")
124
125 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
126   (save-excursion
127     (set-buffer output-buffer)
128     (erase-buffer)
129     )
130   (let ((status
131          (call-process-region (point-min)(point-max)
132                               "pgp" nil output-buffer nil orig-file)))
133     (if (= status 0)
134         (save-excursion
135           (set-buffer output-buffer)
136           (goto-char (point-min))
137           (message
138            (if (re-search-forward tm-pgp::good-signature-regexp nil t)
139                (buffer-substring (match-beginning 0) (match-end 0))
140              "Bad signature"
141              ))
142           ))))
143
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))
148          (rmcnum (cdr rcnum))
149          (knum (car rcnum))
150          (onum (if (> knum 0)
151                    (1- knum)
152                  (1+ knum)))
153          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
154                                              mime::article/content-info))
155          status str kbuf
156          (basename (expand-file-name "tm" mime/tmp-dir))
157          (orig-file (make-temp-name basename))
158          (sig-file (concat orig-file ".sig"))
159          )
160     (save-excursion
161       (setq str (buffer-substring
162                  (mime::content-info/point-min oinfo)
163                  (mime::content-info/point-max oinfo)
164                  ))
165       (set-buffer (get-buffer-create mime/temp-buffer-name))
166       (insert str)
167       (goto-char (point-min))
168       (while (re-search-forward "\n" nil t)
169         (replace-match "\r\n")
170         )
171       (let ((mc-flag nil)                   ; for Mule
172             (file-coding-system
173              (if (featurep 'mule) *noconv*))
174             kanji-flag                      ; for NEmacs
175             (emx-binary-mode t)             ; for OS/2
176             jka-compr-compression-info-list ; for jka-compr
177             jam-zcat-filename-list          ; for jam-zcat
178             require-final-newline)
179         (write-file orig-file)
180         )
181       (kill-buffer (current-buffer))
182       )
183     (save-excursion
184       (setq str (buffer-substring
185                  (save-excursion
186                    (goto-char beg)
187                    (and (search-forward "\n\n")
188                         (match-end 0)))
189                  end))
190       (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
191       (insert str)
192       (mime-decode-region (point-min)(point-max) encoding)
193       (let ((mc-flag nil)                   ; for Mule
194             (file-coding-system
195              (if (featurep 'mule) *noconv*))
196             kanji-flag                      ; for NEmacs
197             (emx-binary-mode t)             ; for OS/2
198             jka-compr-compression-info-list ; for jka-compr
199             jam-zcat-filename-list          ; for jam-zcat
200             require-final-newline)
201         (write-file sig-file)
202         )
203       ;;(define-program-coding-system (current-buffer) "pgp" *noconv*)
204       (mime-article/show-output-buffer)
205       ;;(get-buffer-create mime/output-buffer-name)
206       (or (mime::article/call-pgp-to-check-signature
207            mime/output-buffer-name orig-file)
208           (let (pgp-id)
209             (save-excursion
210               (set-buffer mime/output-buffer-name)
211               (goto-char (point-min))
212               (if (re-search-forward tm-pgp::key-expected-regexp nil t)
213                   (setq pgp-id
214                         (concat "0x" (buffer-substring-no-properties
215                                       (match-beginning 1)
216                                       (match-end 1))))
217                 ))
218             (y-or-n-p
219              (format "Key %s not found; attempt to fetch? " pgp-id))
220             (mc-pgp-fetch-key (cons nil pgp-id))
221             (mime::article/call-pgp-to-check-signature
222              mime/output-buffer-name orig-file)
223             ))
224       (let ((other-window-scroll-buffer mime/output-buffer-name))
225         (scroll-other-window 8)
226         )
227       (kill-buffer kbuf)
228       (delete-file orig-file)
229       (delete-file sig-file)
230       )))
231
232 (set-atype 'mime/content-decoding-condition
233            '((type . "application/pgp-signature")
234              (method . mime-article/check-pgp-signature)
235              ))
236
237
238 ;;; @ Internal method for application/pgp-encrypted
239 ;;;
240 ;;; It is based on draft-elkins-pem-pgp-02.txt
241
242 (defun mime-article/decrypt-pgp (beg end cal)
243   (let* ((cnum (mime-article/point-content-number beg))
244          (rcnum (reverse cnum))
245          (rmcnum (cdr rcnum))
246          (knum (car rcnum))
247          (onum (if (> knum 0)
248                    (1- knum)
249                  (1+ knum)))
250          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
251                                              mime::article/content-info))
252          (obeg (mime::content-info/point-min oinfo))
253          (oend (mime::content-info/point-max oinfo))
254          )
255     (mime-article/view-application/pgp obeg oend cal)
256     ))
257
258 (set-atype 'mime/content-decoding-condition
259            '((type . "application/pgp-encrypted")
260              (method . mime-article/decrypt-pgp)
261              ))
262
263
264 ;;; @ Internal method for application/pgp-keys
265 ;;;
266 ;;; It is based on draft-elkins-pem-pgp-02.txt
267
268 (autoload 'mc-snarf-keys "mc-toplev")
269
270 (defun mime-article/add-pgp-keys (beg end cal)
271   (let* ((cnum (mime-article/point-content-number beg))
272          (cur-buf (current-buffer))
273          (new-name (format "%s-%s" (buffer-name) cnum))
274          (mother mime::article/preview-buffer)
275          (charset (cdr (assoc "charset" cal)))
276          (encoding (cdr (assq 'encoding cal)))
277          (mode major-mode)
278          str)
279     (setq str (buffer-substring beg end))
280     (switch-to-buffer new-name)
281     (setq buffer-read-only nil)
282     (erase-buffer)
283     (insert str)
284     (goto-char (point-min))
285     (if (re-search-forward "^\n" nil t)
286         (delete-region (point-min) (match-end 0))
287       )
288     (mime-decode-region (point-min)(point-max) encoding)
289     (mc-snarf-keys)
290     (kill-buffer (current-buffer))
291     ))
292
293 (set-atype 'mime/content-decoding-condition
294            '((type . "application/pgp-keys")
295              (method . mime-article/add-pgp-keys)
296              ))
297
298          
299 ;;; @ end
300 ;;;
301
302 (provide 'tm-pgp)
303
304 ;;; tm-pgp.el ends here