Merge semi-1_10_2.
[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* ((p-win (or (get-buffer-window mime-preview-buffer)
67                     (get-largest-window)))
68          (new-name
69           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
70          (the-buf (current-buffer))
71          (mother mime-preview-buffer)
72          representation-type)
73     (set-buffer (get-buffer-create new-name))
74     (erase-buffer)
75     (insert-buffer-substring
76      the-buf (mime-entity-point-min entity) (mime-entity-point-max entity))
77     (cond ((progn
78              (goto-char (point-min))
79              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
80            (funcall (pgp-function 'verify))
81            (goto-char (point-min))
82            (delete-region
83             (point-min)
84             (and
85              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
86              (match-end 0)))
87            (delete-region
88             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
89                  (match-beginning 0))
90             (point-max))
91            (goto-char (point-min))
92            (while (re-search-forward "^- -" nil t)
93              (replace-match "-")
94              )
95            (setq representation-type (if (mime-entity-cooked-p entity)
96                                          'cooked))
97            )
98           ((progn
99              (goto-char (point-min))
100              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
101            (as-binary-process (funcall (pgp-function 'decrypt)))
102            (goto-char (point-min))
103            (delete-region (point-min)
104                           (and
105                            (search-forward "\n\n")
106                            (match-end 0)))
107            (setq representation-type 'binary)
108            ))
109     (setq major-mode 'mime-show-message-mode)
110     (save-window-excursion (mime-view-buffer nil nil mother
111                                              nil representation-type))
112     (set-window-buffer p-win mime-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 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                                       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* ((start (mime-entity-point-min entity))
160          (end (mime-entity-point-max entity))
161          (encoding (cdr (assq 'encoding situation)))
162          (entity-node-id (mime-raw-point-to-entity-node-id start))
163          (mother (mime-entity-parent entity))
164          (knum (car entity-node-id))
165          (onum (if (> knum 0)
166                    (1- knum)
167                  (1+ knum)))
168          (orig-entity (nth onum (mime-entity-children mother)))
169          (basename (expand-file-name "tm" temporary-file-directory))
170          (orig-file (make-temp-name basename))
171          (sig-file (concat orig-file ".sig"))
172          )
173     (mime-write-entity orig-entity orig-file)
174     (save-excursion (mime-show-echo-buffer))
175     (mime-write-decoded-region (save-excursion
176                                  (goto-char start)
177                                  (and (search-forward "\n\n")
178                                       (match-end 0))
179                                  ) end sig-file encoding)
180     (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
181         (let (pgp-id)
182           (save-excursion
183             (set-buffer mime-echo-buffer-name)
184             (goto-char (point-min))
185             (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
186                                      mime-pgp-key-expected-regexp-alist))))
187               (cond ((not (stringp regexp))
188                      (message
189                       "Please specify right regexp for specified language")
190                      )
191                     ((re-search-forward regexp nil t)
192                      (setq pgp-id
193                            (concat "0x" (buffer-substring-no-properties
194                                          (match-beginning 1)
195                                          (match-end 1))))
196                      ))))
197           (if (and pgp-id
198                    (y-or-n-p
199                     (format "Key %s not found; attempt to fetch? " pgp-id))
200                    )
201               (progn
202                 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
203                 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
204                 ))
205           ))
206     (let ((other-window-scroll-buffer mime-echo-buffer-name))
207       (scroll-other-window 8)
208       )
209     (delete-file orig-file)
210     (delete-file sig-file)
211     ))
212
213
214 ;;; @ Internal method for application/pgp-encrypted
215 ;;;
216 ;;; It is based on RFC 2015 (PGP/MIME).
217
218 (defun mime-decrypt-application/pgp-encrypted (entity situation)
219   (let* ((entity-node-id (mime-entity-node-id entity))
220          (mother (mime-entity-parent entity))
221          (knum (car entity-node-id))
222          (onum (if (> knum 0)
223                    (1- knum)
224                  (1+ knum)))
225          (orig-entity (nth onum (mime-entity-children mother))))
226     (mime-view-application/pgp orig-entity situation)
227     ))
228
229
230 ;;; @ Internal method for application/pgp-keys
231 ;;;
232 ;;; It is based on RFC 2015 (PGP/MIME).
233
234 (defun mime-add-application/pgp-keys (entity situation)
235   (let* ((start (mime-entity-point-min entity))
236          (end (mime-entity-point-max entity))
237          (entity-number (mime-raw-point-to-entity-number start))
238          (new-name (format "%s-%s" (buffer-name) entity-number))
239          (encoding (cdr (assq 'encoding situation)))
240          str)
241     (setq str (buffer-substring start end))
242     (switch-to-buffer new-name)
243     (setq buffer-read-only nil)
244     (erase-buffer)
245     (insert str)
246     (goto-char (point-min))
247     (if (re-search-forward "^\n" nil t)
248         (delete-region (point-min) (match-end 0))
249       )
250     (mime-decode-region (point-min)(point-max) encoding)
251     (funcall (pgp-function 'snarf-keys))
252     (kill-buffer (current-buffer))
253     ))
254
255          
256 ;;; @ end
257 ;;;
258
259 (provide 'mime-pgp)
260
261 (run-hooks 'mime-pgp-load-hook)
262
263 ;;; mime-pgp.el ends here