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