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