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