Rename `mime-playback-entity' -> `mime-raw-play-entity'.
[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 2 drafts about PGP MIME integration:
30
31 ;;      - RFC 2015: "MIME Security with Pretty Good Privacy (PGP)"
32 ;;              by Michael Elkins <elkins@aero.org> (1996/6)
33 ;;
34 ;;      - draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
35 ;;              by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp>
36 ;;                      (1995/10; expired)
37 ;;
38 ;;    These drafts may be contrary to each other.  You should decide
39 ;;  which you support.  (Maybe you should use PGP/MIME)
40
41 ;;; Code:
42
43 (require 'mime-play)
44
45
46 ;;; @ internal method for application/pgp
47 ;;;
48 ;;; It is based on draft-kazu-pgp-mime-00.txt
49
50 (defun mime-method-for-application/pgp (beg end cal)
51   (let* ((cnum (mime-raw-point-to-entity-number beg))
52          (p-win (or (get-buffer-window mime-preview-buffer)
53                     (get-largest-window)))
54          (new-name (format "%s-%s" (buffer-name) cnum))
55          (the-buf (current-buffer))
56          (mother mime-preview-buffer)
57          (mode major-mode)
58          text-decoder)
59     (set-buffer (get-buffer-create new-name))
60     (erase-buffer)
61     (insert-buffer-substring the-buf beg end)
62     (cond ((progn
63              (goto-char (point-min))
64              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
65              )
66            (funcall (pgp-function '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 text-decoder
84                  (cdr (or (assq mode mime-text-decoder-alist)
85                           (assq t    mime-text-decoder-alist))))
86            )
87           ((progn
88              (goto-char (point-min))
89              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
90              )
91            (as-binary-process (funcall (pgp-function 'decrypt)))
92            (goto-char (point-min))
93            (delete-region (point-min)
94                           (and
95                            (search-forward "\n\n")
96                            (match-end 0)))
97            (setq text-decoder (function mime-text-decode-buffer))
98            ))
99     (setq major-mode 'mime-show-message-mode)
100     (setq mime-text-decoder text-decoder)
101     (save-window-excursion (mime-view-mode mother))
102     (set-window-buffer p-win mime-preview-buffer)
103     ))
104
105 (set-atype 'mime-acting-condition
106            '((type . application)(subtype . pgp)
107              (method . mime-method-for-application/pgp)
108              ))
109
110 (set-atype 'mime-acting-condition
111            '((type . text)(subtype . x-pgp)
112              (method . mime-method-for-application/pgp)
113              ))
114
115
116 ;;; @ Internal method for multipart/signed
117 ;;;
118
119 (defun mime-method-to-verify-multipart/signed (start end cal)
120   "Internal method to verify multipart/signed."
121   (mime-raw-play-entity
122    ;; entity-info of signature
123    (mime-raw-reversed-entity-number-to-entity-info
124     ;; reversed-entity-number of signature
125     (cons 1 (reverse (mime-raw-point-to-entity-number start)))
126     mime-raw-message-info)
127    (cdr (assq 'mode cal)) ; play-mode
128    ))
129
130 (set-atype 'mime-acting-condition
131            '((type . multipart)(subtype . signed)
132              (method . mime-method-to-verify-multipart/signed)
133              ))
134
135
136 ;;; @ Internal method for application/pgp-signature
137 ;;;
138 ;;; It is based on RFC 2015.
139
140 (defvar mime-pgp-command "pgp"
141   "*Name of the PGP command.")
142
143 (defvar mime-pgp-default-language 'en
144   "*Symbol of language for pgp.
145 It should be ISO 639 2 letter language code such as en, ja, ...")
146
147 (defvar mime-pgp-good-signature-regexp-alist
148   '((en . "Good signature from user.*$"))
149   "Alist of language vs regexp to detect ``Good signature''.")
150
151 (defvar mime-pgp-key-expected-regexp-alist
152   '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
153   "Alist of language vs regexp to detect ``Key expected''.")
154
155 (defun mime-pgp-check-signature (output-buffer orig-file)
156   (save-excursion
157     (set-buffer output-buffer)
158     (erase-buffer))
159   (let* ((lang (or mime-pgp-default-language 'en))
160          (status (call-process-region (point-min)(point-max)
161                                       mime-pgp-command
162                                       nil output-buffer nil
163                                       orig-file (format "+language=%s" lang)))
164          (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
165     (if (= status 0)
166         (save-excursion
167           (set-buffer output-buffer)
168           (goto-char (point-min))
169           (message
170            (cond ((not (stringp regexp))
171                   "Please specify right regexp for specified language")
172                  ((re-search-forward regexp nil t)
173                   (buffer-substring (match-beginning 0) (match-end 0)))
174                  (t "Bad signature")))
175           ))))
176
177 (defun mime-method-to-verify-application/pgp-signature (beg end cal)
178   "Internal method to check PGP/MIME signature."
179   (let* ((encoding (cdr (assq 'encoding cal)))
180          (cnum (mime-raw-point-to-entity-number beg))
181          (rcnum (reverse cnum))
182          (rmcnum (cdr rcnum))
183          (knum (car rcnum))
184          (onum (if (> knum 0)
185                    (1- knum)
186                  (1+ knum)))
187          (raw-buf (current-buffer))
188          (oinfo (mime-raw-reversed-entity-number-to-entity-info (cons onum rmcnum)
189                                              mime-raw-message-info))
190          kbuf
191          (basename (expand-file-name "tm" mime-temp-directory))
192          (orig-file (make-temp-name basename))
193          (sig-file (concat orig-file ".sig"))
194          )
195     (save-excursion
196       (let ((p-min (mime-entity-info-point-min oinfo))
197             (p-max (mime-entity-info-point-max oinfo))
198             )
199         (set-buffer (get-buffer-create mime-temp-buffer-name))
200         (insert-buffer-substring raw-buf p-min p-max)
201         )
202       (goto-char (point-min))
203       (while (re-search-forward "\n" nil t)
204         (replace-match "\r\n")
205         )
206       (as-binary-output-file (write-region (point-min)(point-max) orig-file))
207       (kill-buffer (current-buffer))
208       )
209     (save-excursion (mime-show-echo-buffer))
210     (save-excursion
211       (let ((p-min (save-excursion
212                      (goto-char beg)
213                      (and (search-forward "\n\n")
214                           (match-end 0))
215                      )))
216         (set-buffer (setq kbuf (get-buffer-create mime-temp-buffer-name)))
217         (insert-buffer-substring raw-buf p-min end)
218         )
219       (mime-decode-region (point-min)(point-max) encoding)
220       (as-binary-output-file (write-region (point-min)(point-max) sig-file))
221       (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
222           (let (pgp-id)
223             (save-excursion
224               (set-buffer mime-echo-buffer-name)
225               (goto-char (point-min))
226               (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
227                                        mime-pgp-key-expected-regexp-alist))))
228                 (cond ((not (stringp regexp))
229                        (message
230                         "Please specify right regexp for specified language")
231                        )
232                       ((re-search-forward regexp nil t)
233                        (setq pgp-id
234                              (concat "0x" (buffer-substring-no-properties
235                                            (match-beginning 1)
236                                            (match-end 1))))
237                        ))))
238             (if (and pgp-id
239                      (y-or-n-p
240                       (format "Key %s not found; attempt to fetch? " pgp-id))
241                      )
242                 (progn
243                   (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
244                   (mime-pgp-check-signature mime-echo-buffer-name orig-file)
245                   ))
246             ))
247       (let ((other-window-scroll-buffer mime-echo-buffer-name))
248         (scroll-other-window 8)
249         )
250       (kill-buffer kbuf)
251       (delete-file orig-file)
252       (delete-file sig-file)
253       )))
254
255 (set-atype 'mime-acting-condition
256            '((type . application)(subtype . pgp-signature)
257              (method . mime-method-to-verify-application/pgp-signature)
258              ))
259
260
261 ;;; @ Internal method for application/pgp-encrypted
262 ;;;
263 ;;; It is based on RFC 2015.
264
265 (defun mime-method-to-decrypt-application/pgp-encrypted (beg end cal)
266   (let* ((cnum (mime-raw-point-to-entity-number beg))
267          (rcnum (reverse cnum))
268          (rmcnum (cdr rcnum))
269          (knum (car rcnum))
270          (onum (if (> knum 0)
271                    (1- knum)
272                  (1+ knum)))
273          (oinfo (mime-raw-reversed-entity-number-to-entity-info
274                  (cons onum rmcnum) mime-raw-message-info))
275          (obeg (mime-entity-info-point-min oinfo))
276          (oend (mime-entity-info-point-max oinfo))
277          )
278     (mime-method-for-application/pgp obeg oend cal)
279     ))
280
281 (set-atype 'mime-acting-condition
282            '((type . application)(subtype . pgp-encrypted)
283              (method . mime-method-to-decrypt-application/pgp-encrypted)
284              ))
285
286
287 ;;; @ Internal method for application/pgp-keys
288 ;;;
289 ;;; It is based on RFC 2015.
290
291 (defun mime-method-to-add-application/pgp-keys (beg end cal)
292   (let* ((cnum (mime-raw-point-to-entity-number beg))
293          (new-name (format "%s-%s" (buffer-name) cnum))
294          (encoding (cdr (assq 'encoding cal)))
295          str)
296     (setq str (buffer-substring beg end))
297     (switch-to-buffer new-name)
298     (setq buffer-read-only nil)
299     (erase-buffer)
300     (insert str)
301     (goto-char (point-min))
302     (if (re-search-forward "^\n" nil t)
303         (delete-region (point-min) (match-end 0))
304       )
305     (mime-decode-region (point-min)(point-max) encoding)
306     (funcall (pgp-function 'snarf-keys))
307     (kill-buffer (current-buffer))
308     ))
309
310 (set-atype 'mime-acting-condition
311            '((type . application)(subtype . pgp-keys)
312              (method . mime-method-to-add-application/pgp-keys)
313              ))
314
315          
316 ;;; @ end
317 ;;;
318
319 (provide 'mime-pgp)
320
321 (run-hooks 'mime-pgp-load-hook)
322
323 ;;; mime-pgp.el ends here