Sync up with remi-1_5_0_9.
[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* ((entity-number (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) entity-number))
61          (the-buf (current-buffer))
62          (mother mime-preview-buffer)
63          (mode major-mode)
64          representation-type)
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            (funcall (pgp-function 'verify))
72            (goto-char (point-min))
73            (delete-region
74             (point-min)
75             (and
76              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
77              (match-end 0)))
78            (delete-region
79             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
80                  (match-beginning 0))
81             (point-max))
82            (goto-char (point-min))
83            (while (re-search-forward "^- -" nil t)
84              (replace-match "-")
85              )
86            (setq representation-type
87                  (cdr (or (assq mode mime-raw-representation-type-alist)
88                           (assq t    mime-raw-representation-type-alist))))
89            )
90           ((progn
91              (goto-char (point-min))
92              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
93            (as-binary-process (funcall (pgp-function 'decrypt)))
94            (goto-char (point-min))
95            (delete-region (point-min)
96                           (and
97                            (search-forward "\n\n")
98                            (match-end 0)))
99            (setq representation-type (function mime-text-decode-buffer))
100            ))
101     (setq major-mode 'mime-show-message-mode)
102     (setq mime-raw-representation-type representation-type)
103     (save-window-excursion (mime-view-mode mother))
104     (set-window-buffer p-win mime-preview-buffer)
105     ))
106
107
108 ;;; @ Internal method for multipart/signed
109 ;;;
110 ;;; It is based on RFC 1847 (security-multipart).
111
112 (defun mime-method-to-verify-multipart/signed (start end cal)
113   "Internal method to verify multipart/signed."
114   (mime-raw-play-entity
115    ;; entity-info of signature
116    (mime-raw-find-entity-from-node-id
117     ;; entity-node-id of signature
118     (cons 1 (mime-raw-point-to-entity-node-id start)))
119    (cdr (assq 'mode cal)) ; play-mode
120    ))
121
122
123 ;;; @ Internal method for application/pgp-signature
124 ;;;
125 ;;; It is based on RFC 2015 (PGP/MIME).
126
127 (defvar mime-pgp-command "pgp"
128   "*Name of the PGP command.")
129
130 (defvar mime-pgp-default-language 'en
131   "*Symbol of language for pgp.
132 It should be ISO 639 2 letter language code such as en, ja, ...")
133
134 (defvar mime-pgp-good-signature-regexp-alist
135   '((en . "Good signature from user.*$"))
136   "Alist of language vs regexp to detect ``Good signature''.")
137
138 (defvar mime-pgp-key-expected-regexp-alist
139   '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
140   "Alist of language vs regexp to detect ``Key expected''.")
141
142 (defun mime-pgp-check-signature (output-buffer orig-file)
143   (save-excursion
144     (set-buffer output-buffer)
145     (erase-buffer))
146   (let* ((lang (or mime-pgp-default-language 'en))
147          (status (call-process-region (point-min)(point-max)
148                                       mime-pgp-command
149                                       nil output-buffer nil
150                                       orig-file (format "+language=%s" lang)))
151          (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
152     (if (= status 0)
153         (save-excursion
154           (set-buffer output-buffer)
155           (goto-char (point-min))
156           (message
157            (cond ((not (stringp regexp))
158                   "Please specify right regexp for specified language")
159                  ((re-search-forward regexp nil t)
160                   (buffer-substring (match-beginning 0) (match-end 0)))
161                  (t "Bad signature")))
162           ))))
163
164 (defun mime-method-to-verify-application/pgp-signature (start end cal)
165   "Internal method to check PGP/MIME signature."
166   (let* ((encoding (cdr (assq 'encoding cal)))
167          (entity-node-id (mime-raw-point-to-entity-node-id start))
168          (mother-node-id (cdr entity-node-id))
169          (knum (car entity-node-id))
170          (onum (if (> knum 0)
171                    (1- knum)
172                  (1+ knum)))
173          (oinfo (mime-raw-find-entity-from-node-id
174                  (cons onum mother-node-id) mime-raw-message-info))
175          (basename (expand-file-name "tm" mime-temp-directory))
176          (orig-file (make-temp-name basename))
177          (sig-file (concat orig-file ".sig"))
178          )
179     (mime-raw-write-region (mime-entity-point-min oinfo)
180                            (mime-entity-point-max oinfo)
181                            orig-file)
182     (save-excursion (mime-show-echo-buffer))
183     (mime-write-decoded-region (save-excursion
184                                  (goto-char start)
185                                  (and (search-forward "\n\n")
186                                       (match-end 0))
187                                  ) end sig-file encoding)
188     (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
189         (let (pgp-id)
190           (save-excursion
191             (set-buffer mime-echo-buffer-name)
192             (goto-char (point-min))
193             (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
194                                      mime-pgp-key-expected-regexp-alist))))
195               (cond ((not (stringp regexp))
196                      (message
197                       "Please specify right regexp for specified language")
198                      )
199                     ((re-search-forward regexp nil t)
200                      (setq pgp-id
201                            (concat "0x" (buffer-substring-no-properties
202                                          (match-beginning 1)
203                                          (match-end 1))))
204                      ))))
205           (if (and pgp-id
206                    (y-or-n-p
207                     (format "Key %s not found; attempt to fetch? " pgp-id))
208                    )
209               (progn
210                 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
211                 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
212                 ))
213           ))
214     (let ((other-window-scroll-buffer mime-echo-buffer-name))
215       (scroll-other-window 8)
216       )
217     (delete-file orig-file)
218     (delete-file sig-file)
219     ))
220
221
222 ;;; @ Internal method for application/pgp-encrypted
223 ;;;
224 ;;; It is based on RFC 2015 (PGP/MIME).
225
226 (defun mime-method-to-decrypt-application/pgp-encrypted (start end cal)
227   (let* ((entity-node-id (mime-raw-point-to-entity-node-id start))
228          (mother-node-id (cdr entity-node-id))
229          (knum (car entity-node-id))
230          (onum (if (> knum 0)
231                    (1- knum)
232                  (1+ knum)))
233          (oinfo (mime-raw-find-entity-from-node-id
234                  (cons onum mother-node-id) mime-raw-message-info))
235          (obeg (mime-entity-point-min oinfo))
236          (oend (mime-entity-point-max oinfo))
237          )
238     (mime-method-for-application/pgp obeg oend cal)
239     ))
240
241
242 ;;; @ Internal method for application/pgp-keys
243 ;;;
244 ;;; It is based on RFC 2015 (PGP/MIME).
245
246 (defun mime-method-to-add-application/pgp-keys (start end cal)
247   (let* ((entity-number (mime-raw-point-to-entity-number start))
248          (new-name (format "%s-%s" (buffer-name) entity-number))
249          (encoding (cdr (assq 'encoding cal)))
250          str)
251     (setq str (buffer-substring start end))
252     (switch-to-buffer new-name)
253     (setq buffer-read-only nil)
254     (erase-buffer)
255     (insert str)
256     (goto-char (point-min))
257     (if (re-search-forward "^\n" nil t)
258         (delete-region (point-min) (match-end 0))
259       )
260     (mime-decode-region (point-min)(point-max) encoding)
261     (funcall (pgp-function 'snarf-keys))
262     (kill-buffer (current-buffer))
263     ))
264
265          
266 ;;; @ end
267 ;;;
268
269 (provide 'mime-pgp)
270
271 (run-hooks 'mime-pgp-load-hook)
272
273 ;;; mime-pgp.el ends here