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