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