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