Add comment that this module is based on
[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 (require 'pgg-parse)
50
51
52 ;;; @ Internal method for multipart/signed
53 ;;;
54 ;;; It is based on RFC 1847 (security-multipart).
55
56 (defun mime-verify-multipart/signed (entity situation)
57   "Internal method to verify multipart/signed."
58   (mime-play-entity
59    (nth 1 (mime-entity-children entity)) ; entity-info of signature
60    (list (assq 'mode situation)) ; play-mode
61    ))
62
63
64 ;;; @ internal method for application/pgp
65 ;;;
66 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
67
68 (defun mime-view-application/pgp (entity situation)
69   (let* ((p-win (or (get-buffer-window (current-buffer))
70                     (get-largest-window)))
71          (new-name
72           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
73          (mother (current-buffer))
74          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
75          representation-type)
76     (set-buffer (get-buffer-create new-name))
77     (erase-buffer)
78     (mime-insert-entity entity)
79     (cond ((progn
80              (goto-char (point-min))
81              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
82            (funcall (pgp-function 'verify)
83                     (point-min)(point-max))
84            (goto-char (point-min))
85            (delete-region
86             (point-min)
87             (and
88              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
89              (match-end 0)))
90            (delete-region
91             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
92                  (match-beginning 0))
93             (point-max))
94            (goto-char (point-min))
95            (while (re-search-forward "^- -" nil t)
96              (replace-match "-")
97              )
98            (setq representation-type (if (mime-entity-cooked-p entity)
99                                          'cooked))
100            )
101           ((progn
102              (goto-char (point-min))
103              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
104            (funcall (pgp-function 'decrypt)
105                     (point-min)(point-max))
106            (delete-region (point-min)(point-max))
107            (insert-buffer pgg-output-buffer)
108            (setq representation-type 'binary)
109            ))
110     (setq major-mode 'mime-show-message-mode)
111     (save-window-excursion (mime-view-buffer nil preview-buffer mother
112                                              nil representation-type))
113     (set-window-buffer p-win 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 sig-file 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                                       sig-file 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          (sig-file (concat (make-temp-name basename) ".asc"))
169          )
170     (save-excursion (mime-show-echo-buffer))
171     (mime-write-entity-content entity sig-file)
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)))
181     (delete-file sig-file)
182     ))
183
184 (defun mime-display-application/pgp-signature (entity situation)
185   (let ((packet
186          (cdr (assq 2 (pgg-parse-armor (mime-entity-content entity)))))
187         field)
188     (insert 
189      "version: " 
190      (int-to-string (cdr (assq 'version packet)))
191      "\n"
192      "signature type: "
193      (cdr (assq 'signature-type packet))
194      "\n"
195      (if (setq field (cdr (assq 'hash-algorithm packet)))
196          (concat "hash algorithm: " (symbol-name field) "\n")
197        "")
198      (if (setq field (cdr (assq 'public-key-algorithm packet)))
199          (concat "public key algorithm: " (symbol-name field) "\n")
200        "")
201      (if (setq field (cdr (assq 'key-identifier packet)))
202          (concat "key identifier: " field "\n")
203        "")
204      (if (setq field (cdr (assq 'creation-time packet)))
205          (concat "creation time: " (current-time-string field) "\n")
206        "")
207      (if (setq field (cdr (assq 'signature-expiry packet)))
208          (concat "signature exipiration time: "
209                  (current-time-string field) "\n")
210        "")
211      (if (setq field (cdr (assq 'key-expiry packet)))
212          (concat "key exipiration time: " (current-time-string field) "\n")
213        "")
214      (if (setq field (cdr (assq 'trust-level packet)))
215          (concat "trust level: " (int-to-string field) "\n")
216        "")
217      (if (setq field (cdr (assq 'preferred-symmetric-key-algorithm packet)))
218          (concat "preferred symmetric algorithm: " 
219                  (symbol-name field) "\n")
220        "")
221      (if (setq field (cdr (assq 'preferred-hash-algorithm packet)))
222          (concat "preferred hash algorithm: " 
223                  (symbol-name field) "\n")
224        "")
225      (if (setq field (cdr (assq 'exportability packet)))
226          (concat "signature exportable: " 
227                  (if (< 0 field) "yes" "no") "\n")
228        "")
229      (if (setq field (cdr (assq 'revocability packet)))
230          (concat "signature revocable: "
231                  (if (< 0 field) "yes" "no") "\n")
232        "")
233      (if (setq field (cdr (assq 'policy-url packet)))
234          (concat "policy URL: " field "\n")
235        "")
236      (if (setq field 
237                (delq nil (mapcar 
238                           (function (lambda (nn) 
239                                       (and (eq (car nn) 'notation) nn)))
240                           packet)))
241          (concat "notations:\n" 
242                  (mapconcat (lambda (nn)
243                               (concat " " (cadr nn) ": " (cddr nn)))
244                             field "\n")
245                  "\n")
246        ""))
247     (mime-add-url-buttons)
248     (run-hooks 'mime-display-application/pgp-signature-hook)
249     ))
250
251
252 ;;; @ Internal method for application/pgp-encrypted
253 ;;;
254 ;;; It is based on RFC 2015 (PGP/MIME).
255
256 (defun mime-decrypt-application/pgp-encrypted (entity situation)
257   (let* ((entity-node-id (mime-entity-node-id entity))
258          (mother (mime-entity-parent entity))
259          (knum (car entity-node-id))
260          (onum (if (> knum 0)
261                    (1- knum)
262                  (1+ knum)))
263          (orig-entity (nth onum (mime-entity-children mother))))
264     (mime-view-application/pgp orig-entity situation)
265     ))
266
267 (defun mime-display-application/pgp-encrypted (entity situation)
268   (let* ((entity-node-id (mime-entity-node-id entity))
269          (mother (mime-entity-parent entity))
270          (knum (car entity-node-id))
271          (onum (if (> knum 0)
272                    (1- knum)
273                  (1+ knum)))
274          (orig-entity (nth onum (mime-entity-children mother)))
275          (packet (cdr (assq 1 (pgg-parse-armor 
276                                (mime-entity-content orig-entity))))))
277     (insert 
278      "version: " 
279      (int-to-string (cdr (assq 'version packet)))
280      "\n"
281      "public key identifier: " 
282      (cdr (assq 'public-key-identifier packet))
283      "\n"
284      "public key algorithm: " 
285      (symbol-name (cdr (assq 'public-key-algorithm packet)))
286      "\n\n")
287     (run-hooks 'mime-display-application/pgp-encrypted-hook)
288     ))
289
290 ;;; @ Internal method for application/pgp-keys
291 ;;;
292 ;;; It is based on RFC 2015 (PGP/MIME).
293
294 (defun mime-add-application/pgp-keys (entity situation)
295   (let* ((start (mime-entity-header-start-point entity))
296          (end (mime-entity-body-end-point entity))
297          (entity-number (mime-entity-number entity))
298          (new-name (format "%s-%s" (buffer-name) entity-number))
299          (encoding (cdr (assq 'encoding situation)))
300          str)
301     (setq str (buffer-substring start end))
302     (switch-to-buffer new-name)
303     (setq buffer-read-only nil)
304     (erase-buffer)
305     (insert str)
306     (goto-char (point-min))
307     (if (re-search-forward "^\n" nil t)
308         (delete-region (point-min) (match-end 0))
309       )
310     (mime-decode-region (point-min)(point-max) encoding)
311     (funcall (pgp-function 'snarf-keys)
312              (point-min)(point-max))
313     (kill-buffer (current-buffer))
314     ))
315
316 (defun mime-display-application/pgp-keys (entity situation)
317   (let ((packet
318          (cdr (assq 6 (pgg-parse-armor (mime-entity-content entity)))))
319         field)
320     (insert 
321      "version: " 
322      (int-to-string (cdr (assq 'version packet)))
323      "\n"
324      "creation time: " 
325      (current-time-string (cdr (assq 'creation-time packet)))
326      "\n"
327      "public key algorithm: " 
328      (symbol-name (cdr (assq 'public-key-algorithm packet)))
329      "\n"
330      (if (setq field (cdr (assq 'key-expiry packet)))
331          (concat "key exipiration time: " (current-time-string field) "\n")
332        ""))
333     (run-hooks 'mime-display-application/pgp-keys-hook)
334     ))
335
336
337 ;;; @ end
338 ;;;
339
340 (provide 'mime-pgp)
341
342 (run-hooks 'mime-pgp-load-hook)
343
344 ;;; mime-pgp.el ends here