(mime-article/view-application/pgp):
[elisp/semi.git] / mime-pgp.el
1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
2
3 ;; Copyright (C) 1995,1996,1997 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/12/7
7 ;;      Renamed: 1997/2/27 from tm-pgp.el
8 ;; Version: $Id: mime-pgp.el,v 0.5 1997-03-15 19:47:27 morioka Exp $
9 ;; Keywords: mail, news, MIME, multimedia, PGP, security
10
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;    This module is based on 2 drafts about PGP MIME integration:
31
32 ;;      - RFC 2015: "MIME Security with Pretty Good Privacy (PGP)"
33 ;;              by Michael Elkins <elkins@aero.org> (1996/6)
34 ;;
35 ;;      - draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
36 ;;              by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp>
37 ;;                      (1995/10; expired)
38 ;;
39 ;;    These drafts may be contrary to each other.  You should decide
40 ;;  which you support.  (Maybe you should use PGP/MIME)
41
42 ;;; Code:
43
44 (require 'mime-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          (p-win (or (get-buffer-window mime::article/preview-buffer)
55                     (get-largest-window)))
56          (new-name (format "%s-%s" (buffer-name) cnum))
57          (mother mime::article/preview-buffer)
58          (mode major-mode)
59          code-converter
60          (str (buffer-substring beg end))
61          )
62     (set-buffer (get-buffer-create new-name))
63     (erase-buffer)
64     (insert str)
65     (cond ((progn
66              (goto-char (point-min))
67              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
68              )
69            (funcall (pgp-function 'verify))
70            (goto-char (point-min))
71            (delete-region
72             (point-min)
73             (and
74              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
75              (match-end 0))
76             )
77            (delete-region
78             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
79                  (match-beginning 0))
80             (point-max)
81             )
82            (goto-char (point-min))
83            (while (re-search-forward "^- -" nil t)
84              (replace-match "-")
85              )
86            (setq code-converter
87                  (cdr (or (assq mode mime-text-decoder-alist)
88                           (assq t    mime-text-decoder-alist))))
89            )
90           ((progn
91              (goto-char (point-min))
92              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
93              )
94            (as-binary-process (funcall (pgp-function 'decrypt)))
95            (goto-char (point-min))
96            (delete-region (point-min)
97                           (and
98                            (search-forward "\n\n")
99                            (match-end 0)))
100            (setq code-converter (function mime-charset/decode-buffer))
101            ))
102     (setq major-mode 'mime/show-message-mode)
103     (setq mime::article/code-converter code-converter)
104     (save-window-excursion (mime-view-mode mother))
105     (set-window-buffer p-win mime::article/preview-buffer)
106     ))
107
108 (set-atype 'mime/content-decoding-condition
109            '((type . "application/pgp")
110              (method . mime-article/view-application/pgp)
111              ))
112
113 (set-atype 'mime/content-decoding-condition
114            '((type . "text/x-pgp")
115              (method . mime-article/view-application/pgp)
116              ))
117
118
119 ;;; @ Internal method for application/pgp-signature
120 ;;;
121 ;;; It is based on RFC 2015.
122
123 (defvar mime-pgp-command "pgp"
124   "*Name of the PGP command.")
125
126 (defvar mime-pgp-default-language 'en
127   "*Symbol of language for pgp.
128 It should be ISO 639 2 letter language code such as en, ja, ...")
129
130 (defvar mime-pgp-good-signature-regexp-alist
131   '((en . "Good signature from user.*$"))
132   "Alist of language vs regexp to detect ``Good signature''.")
133
134 (defvar mime-pgp-key-expected-regexp-alist
135   '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
136   "Alist of language vs regexp to detect ``Key expected''.")
137
138 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
139   (save-excursion
140     (set-buffer output-buffer)
141     (erase-buffer)
142     )
143   (let* ((lang (or mime-pgp-default-language 'en))
144          (status (call-process-region (point-min)(point-max)
145                                       mime-pgp-command
146                                       nil output-buffer nil
147                                       orig-file (format "+language=%s" lang)))
148          (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist)))
149          )
150     (if (= status 0)
151         (save-excursion
152           (set-buffer output-buffer)
153           (goto-char (point-min))
154           (message
155            (cond ((not (stringp regexp))
156                   "Please specify right regexp for specified language")
157                  ((re-search-forward regexp nil t)
158                   (buffer-substring (match-beginning 0) (match-end 0))
159                   )
160                  (t
161                   "Bad signature")))
162           ))))
163
164 (defun mime-article/check-pgp-signature (beg end cal)
165   (let* ((encoding (cdr (assq 'encoding cal)))
166          (cnum (mime-article/point-content-number beg))
167          (rcnum (reverse cnum))
168          (rmcnum (cdr rcnum))
169          (knum (car rcnum))
170          (onum (if (> knum 0)
171                    (1- knum)
172                  (1+ knum)))
173          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
174                                              mime::article/content-info))
175          status str kbuf
176          (basename (expand-file-name "tm" mime-temp-directory))
177          (orig-file (make-temp-name basename))
178          (sig-file (concat orig-file ".sig"))
179          )
180     (save-excursion
181       (setq str (buffer-substring
182                  (mime::content-info/point-min oinfo)
183                  (mime::content-info/point-max oinfo)
184                  ))
185       (set-buffer (get-buffer-create mime/temp-buffer-name))
186       (insert str)
187       (goto-char (point-min))
188       (while (re-search-forward "\n" nil t)
189         (replace-match "\r\n")
190         )
191       (as-binary-output-file (write-file orig-file))
192       (kill-buffer (current-buffer))
193       )
194     (save-excursion
195       (mime-article/show-output-buffer)
196       )
197     (save-excursion
198       (setq str (buffer-substring
199                  (save-excursion
200                    (goto-char beg)
201                    (and (search-forward "\n\n")
202                         (match-end 0)))
203                  end))
204       (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
205       (insert str)
206       (mime-decode-region (point-min)(point-max) encoding)
207       (as-binary-output-file (write-file sig-file))
208       (or (mime::article/call-pgp-to-check-signature
209            mime/output-buffer-name orig-file)
210           (let (pgp-id)
211             (save-excursion
212               (set-buffer mime/output-buffer-name)
213               (goto-char (point-min))
214               (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
215                                        mime-pgp-key-expected-regexp-alist))))
216                 (cond ((not (stringp regexp))
217                        (message
218                         "Please specify right regexp for specified language")
219                        )
220                       ((re-search-forward regexp nil t)
221                        (setq pgp-id
222                              (concat "0x" (buffer-substring-no-properties
223                                            (match-beginning 1)
224                                            (match-end 1))))
225                        ))))
226             (if (and pgp-id
227                      (y-or-n-p
228                       (format "Key %s not found; attempt to fetch? " pgp-id))
229                      )
230                 (progn
231                   (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
232                   (mime::article/call-pgp-to-check-signature
233                    mime/output-buffer-name orig-file)
234                   ))
235             ))
236       (let ((other-window-scroll-buffer mime/output-buffer-name))
237         (scroll-other-window 8)
238         )
239       (kill-buffer kbuf)
240       (delete-file orig-file)
241       (delete-file sig-file)
242       )))
243
244 (set-atype 'mime/content-decoding-condition
245            '((type . "application/pgp-signature")
246              (method . mime-article/check-pgp-signature)
247              ))
248
249
250 ;;; @ Internal method for application/pgp-encrypted
251 ;;;
252 ;;; It is based on RFC 2015.
253
254 (defun mime-article/decrypt-pgp (beg end cal)
255   (let* ((cnum (mime-article/point-content-number beg))
256          (rcnum (reverse cnum))
257          (rmcnum (cdr rcnum))
258          (knum (car rcnum))
259          (onum (if (> knum 0)
260                    (1- knum)
261                  (1+ knum)))
262          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
263                                              mime::article/content-info))
264          (obeg (mime::content-info/point-min oinfo))
265          (oend (mime::content-info/point-max oinfo))
266          )
267     (mime-article/view-application/pgp obeg oend cal)
268     ))
269
270 (set-atype 'mime/content-decoding-condition
271            '((type . "application/pgp-encrypted")
272              (method . mime-article/decrypt-pgp)
273              ))
274
275
276 ;;; @ Internal method for application/pgp-keys
277 ;;;
278 ;;; It is based on RFC 2015.
279
280 (defun mime-article/add-pgp-keys (beg end cal)
281   (let* ((cnum (mime-article/point-content-number beg))
282          (cur-buf (current-buffer))
283          (new-name (format "%s-%s" (buffer-name) cnum))
284          (mother mime::article/preview-buffer)
285          (charset (cdr (assoc "charset" cal)))
286          (encoding (cdr (assq 'encoding cal)))
287          (mode major-mode)
288          str)
289     (setq str (buffer-substring beg end))
290     (switch-to-buffer new-name)
291     (setq buffer-read-only nil)
292     (erase-buffer)
293     (insert str)
294     (goto-char (point-min))
295     (if (re-search-forward "^\n" nil t)
296         (delete-region (point-min) (match-end 0))
297       )
298     (mime-decode-region (point-min)(point-max) encoding)
299     (funcall (pgp-function 'snarf-keys))
300     (kill-buffer (current-buffer))
301     ))
302
303 (set-atype 'mime/content-decoding-condition
304            '((type . "application/pgp-keys")
305              (method . mime-article/add-pgp-keys)
306              ))
307
308          
309 ;;; @ end
310 ;;;
311
312 (provide 'mime-pgp)
313
314 (run-hooks 'mime-pgp-load-hook)
315
316 ;;; mime-pgp.el ends here