* mime-pgp.el (mime-add-application/pgp-keys): Don't display
[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 ;;         Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Created: 1995/12/7
8 ;;      Renamed: 1997/2/27 from tm-pgp.el
9 ;; Keywords: PGP, security, MIME, multimedia, mail, news
10
11 ;; This file is part of SEMI (Secure Emacs MIME Interface).
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;    This module is based on
31
32 ;;      [security-multipart] RFC 1847: "Security Multiparts for MIME:
33 ;;          Multipart/Signed and Multipart/Encrypted" by
34 ;;          Jim Galvin <galvin@tis.com>, Sandy Murphy <sandy@tis.com>,
35 ;;          Steve Crocker <crocker@cybercash.com> and
36 ;;          Ned Freed <ned@innosoft.com> (1995/10)
37
38 ;;      [PGP/MIME] RFC 2015: "MIME Security with Pretty Good Privacy
39 ;;          (PGP)" by Michael Elkins <elkins@aero.org> (1996/6)
40
41 ;;      [PGP-kazu] draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
42 ;;          by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp> (1995/10;
43 ;;          expired)
44
45 ;;; Code:
46
47 (require 'mime-play)
48 (require 'pgg-def)
49
50
51 ;;; @ Internal method for multipart/signed
52 ;;;
53 ;;; It is based on RFC 1847 (security-multipart).
54
55 (defun mime-verify-multipart/signed (entity situation)
56   "Internal method to verify multipart/signed."
57   (mime-play-entity
58    (nth 1 (mime-entity-children entity)) ; entity-info of signature
59    (list (assq 'mode situation)) ; play-mode
60    ))
61
62
63 ;;; @ internal method for application/pgp
64 ;;;
65 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
66
67 (defun mime-view-application/pgp (entity situation)
68   (let* ((p-win (or (get-buffer-window (current-buffer))
69                     (get-largest-window)))
70          (new-name
71           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
72          (mother (current-buffer))
73          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
74          representation-type)
75     (set-buffer (get-buffer-create new-name))
76     (erase-buffer)
77     (mime-insert-entity 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                     (point-min)(point-max))
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 (if (mime-entity-cooked-p entity)
98                                          'cooked))
99            )
100           ((progn
101              (goto-char (point-min))
102              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
103            (funcall (pgp-function 'decrypt)
104                     (point-min)(point-max))
105            (delete-region (point-min)(point-max))
106            (insert-buffer pgg-output-buffer)
107            (setq representation-type 'binary)
108            ))
109     (setq major-mode 'mime-show-message-mode)
110     (save-window-excursion (mime-view-buffer nil preview-buffer mother
111                                              nil representation-type))
112     (set-window-buffer p-win preview-buffer)
113     ))
114
115
116 ;;; @ Internal method for application/pgp-signature
117 ;;;
118 ;;; It is based on RFC 2015 (PGP/MIME).
119
120 (defvar mime-pgp-command "pgp"
121   "*Name of the PGP command.")
122
123 (defvar mime-pgp-default-language 'en
124   "*Symbol of language for pgp.
125 It should be ISO 639 2 letter language code such as en, ja, ...")
126
127 (defvar mime-pgp-good-signature-regexp-alist
128   '((en . "Good signature from user.*$"))
129   "Alist of language vs regexp to detect ``Good signature''.")
130
131 (defvar mime-pgp-key-expected-regexp-alist
132   '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
133   "Alist of language vs regexp to detect ``Key expected''.")
134
135 (defun mime-pgp-check-signature (output-buffer sig-file orig-file)
136   (save-excursion
137     (set-buffer output-buffer)
138     (erase-buffer))
139   (let* ((lang (or mime-pgp-default-language 'en))
140          (status (call-process-region (point-min)(point-max)
141                                       mime-pgp-command
142                                       nil output-buffer nil
143                                       sig-file orig-file (format "+language=%s" lang)))
144          (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
145     (if (= status 0)
146         (save-excursion
147           (set-buffer output-buffer)
148           (goto-char (point-min))
149           (message
150            (cond ((not (stringp regexp))
151                   "Please specify right regexp for specified language")
152                  ((re-search-forward regexp nil t)
153                   (buffer-substring (match-beginning 0) (match-end 0)))
154                  (t "Bad signature")))
155           ))))
156
157 (defun mime-verify-application/pgp-signature (entity situation)
158   "Internal method to check PGP/MIME signature."
159   (let* ((entity-node-id (mime-entity-node-id entity))
160          (mother (mime-entity-parent entity))
161          (knum (car entity-node-id))
162          (onum (if (> knum 0)
163                    (1- knum)
164                  (1+ knum)))
165          (orig-entity (nth onum (mime-entity-children mother)))
166          (basename (expand-file-name "tm" temporary-file-directory))
167          (sig-file (concat (make-temp-name basename) ".asc"))
168          )
169     (save-excursion (mime-show-echo-buffer))
170     (mime-write-entity-content entity sig-file)
171     (unwind-protect
172         (with-temp-buffer
173           (mime-insert-entity orig-entity)
174           (goto-char (point-min))
175           (while (progn (end-of-line) (not (eobp)))
176             (insert "\r")
177             (forward-line 1))
178           (let ((pgg-output-buffer mime-echo-buffer-name))
179             (funcall (pgp-function 'verify) 
180                      (point-min)(point-max) sig-file 'fetch)))
181       (delete-file sig-file))
182     ))
183
184
185 ;;; @ Internal method for application/pgp-encrypted
186 ;;;
187 ;;; It is based on RFC 2015 (PGP/MIME).
188
189 (defun mime-decrypt-application/pgp-encrypted (entity situation)
190   (let* ((entity-node-id (mime-entity-node-id entity))
191          (mother (mime-entity-parent entity))
192          (knum (car entity-node-id))
193          (onum (if (> knum 0)
194                    (1- knum)
195                  (1+ knum)))
196          (orig-entity (nth onum (mime-entity-children mother))))
197     (mime-view-application/pgp orig-entity situation)
198     ))
199
200
201 ;;; @ Internal method for application/pgp-keys
202 ;;;
203 ;;; It is based on RFC 2015 (PGP/MIME).
204
205 (defun mime-add-application/pgp-keys (entity situation)
206   (save-excursion (mime-show-echo-buffer))
207   (with-temp-buffer
208     (mime-insert-entity-content entity)
209     (mime-decode-region (point-min) (point-max)
210                         (cdr (assq 'encoding situation)))
211     (let ((pgg-output-buffer mime-echo-buffer-name))
212       (funcall (pgp-function 'snarf-keys)
213                (point-min)(point-max)))))
214
215
216 ;;; @ end
217 ;;;
218
219 (provide 'mime-pgp)
220
221 (run-hooks 'mime-pgp-load-hook)
222
223 ;;; mime-pgp.el ends here