(mime-view-application/pgp): Use function
[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          representation-type)
75     (set-buffer (get-buffer-create new-name))
76     (erase-buffer)
77     (insert-buffer-substring the-buf start end)
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 (mime-entity-representation-type entity))
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     (setq mime-raw-representation-type representation-type)
111     (save-window-excursion (mime-view-mode mother))
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-node-id (cdr entity-node-id))
164          (knum (car entity-node-id))
165          (onum (if (> knum 0)
166                    (1- knum)
167                  (1+ knum)))
168          (orig-entity (mime-raw-find-entity-from-node-id
169                        (cons onum mother-node-id) mime-message-structure))
170          (basename (expand-file-name "tm" mime-temp-directory))
171          (orig-file (make-temp-name basename))
172          (sig-file (concat orig-file ".sig"))
173          )
174     (mime-raw-write-region (mime-entity-point-min orig-entity)
175                            (mime-entity-point-max orig-entity)
176                            orig-file)
177     (save-excursion (mime-show-echo-buffer))
178     (mime-write-decoded-region (save-excursion
179                                  (goto-char start)
180                                  (and (search-forward "\n\n")
181                                       (match-end 0))
182                                  ) end sig-file encoding)
183     (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
184         (let (pgp-id)
185           (save-excursion
186             (set-buffer mime-echo-buffer-name)
187             (goto-char (point-min))
188             (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
189                                      mime-pgp-key-expected-regexp-alist))))
190               (cond ((not (stringp regexp))
191                      (message
192                       "Please specify right regexp for specified language")
193                      )
194                     ((re-search-forward regexp nil t)
195                      (setq pgp-id
196                            (concat "0x" (buffer-substring-no-properties
197                                          (match-beginning 1)
198                                          (match-end 1))))
199                      ))))
200           (if (and pgp-id
201                    (y-or-n-p
202                     (format "Key %s not found; attempt to fetch? " pgp-id))
203                    )
204               (progn
205                 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
206                 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
207                 ))
208           ))
209     (let ((other-window-scroll-buffer mime-echo-buffer-name))
210       (scroll-other-window 8)
211       )
212     (delete-file orig-file)
213     (delete-file sig-file)
214     ))
215
216
217 ;;; @ Internal method for application/pgp-encrypted
218 ;;;
219 ;;; It is based on RFC 2015 (PGP/MIME).
220
221 (defun mime-decrypt-application/pgp-encrypted (entity situation)
222   (let* ((entity-node-id (mime-entity-node-id entity))
223          (mother-node-id (cdr entity-node-id))
224          (knum (car entity-node-id))
225          (onum (if (> knum 0)
226                    (1- knum)
227                  (1+ knum)))
228          (orig-entity (mime-raw-find-entity-from-node-id
229                        (cons onum mother-node-id) mime-message-structure)))
230     (mime-view-application/pgp orig-entity situation)
231     ))
232
233
234 ;;; @ Internal method for application/pgp-keys
235 ;;;
236 ;;; It is based on RFC 2015 (PGP/MIME).
237
238 (defun mime-add-application/pgp-keys (entity situation)
239   (let* ((start (mime-entity-point-min entity))
240          (end (mime-entity-point-max entity))
241          (entity-number (mime-raw-point-to-entity-number start))
242          (new-name (format "%s-%s" (buffer-name) entity-number))
243          (encoding (cdr (assq 'encoding situation)))
244          str)
245     (setq str (buffer-substring start end))
246     (switch-to-buffer new-name)
247     (setq buffer-read-only nil)
248     (erase-buffer)
249     (insert str)
250     (goto-char (point-min))
251     (if (re-search-forward "^\n" nil t)
252         (delete-region (point-min) (match-end 0))
253       )
254     (mime-decode-region (point-min)(point-max) encoding)
255     (funcall (pgp-function 'snarf-keys))
256     (kill-buffer (current-buffer))
257     ))
258
259          
260 ;;; @ end
261 ;;;
262
263 (provide 'mime-pgp)
264
265 (run-hooks 'mime-pgp-load-hook)
266
267 ;;; mime-pgp.el ends here