Synch with the semi-1_14 branch.
[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,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;;      Daiki Ueno <ueno@unixuser.org>
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 ;;      [OpenPGP/MIME] draft-ietf-openpgp-mime-02.txt: "MIME
46 ;;          Security with OpenPGP" by
47 ;;          John W. Noerenberg II <jwn2@qualcomm.com>,
48 ;;          Dave Del Torto <ddt@cryptorights.org> and
49 ;;          Michael Elkins <michael_elkins@nai.com> (2000/8)
50
51 ;;; Code:
52
53 (require 'mime-play)
54 (require 'pgg-def)
55
56 (autoload 'pgg-decrypt-region "pgg"
57   "PGP decryption of current region." t)
58 (autoload 'pgg-verify-region "pgg"
59   "PGP verification of current region." t)
60 (autoload 'pgg-snarf-keys-region "pgg"
61   "Snarf PGP public keys in current region." t)
62 (autoload 'smime-decrypt-buffer "smime"
63   "S/MIME decryption of current region.")
64 (autoload 'smime-verify-buffer "smime"
65   "Verify integrity of S/MIME message in BUFFER.")
66 (autoload 'smime-noverify-buffer "smime"
67   "Verify integrity of S/MIME message in BUFFER.")
68 (autoload 'smime-pkcs7-region "smime"
69   "Convert S/MIME message into a PKCS7 message.")
70 (autoload 'smime-pkcs7-certificates-region "smime"
71   "Extract any certificates enclosed in PKCS7 message.")
72 (autoload 'smime-pkcs7-email-region "smime"
73   "Get email addresses contained in certificate.")
74 (defvar smime-details-buffer)
75 (defvar smime-CA-file)
76 (defvar smime-CA-directory)
77
78 ;;; @ Internal method for multipart/signed
79 ;;;
80 ;;; It is based on RFC 1847 (security-multipart).
81
82 (defun mime-verify-multipart/signed (entity situation)
83   "Internal method to verify multipart/signed."
84   (mime-play-entity
85    (nth 1 (mime-entity-children entity)) ; entity-info of signature
86    (list (assq 'mode situation)) ; play-mode
87    ))
88
89
90 ;;; @ internal method for application/pgp
91 ;;;
92 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
93
94 (defun mime-view-application/pgp (entity situation)
95   (let* ((p-win (or (get-buffer-window (current-buffer))
96                     (get-largest-window)))
97          (new-name
98           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
99          (mother (current-buffer))
100          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
101          representation-type message-buf)
102     (set-buffer (setq message-buf (get-buffer-create new-name)))
103     (erase-buffer)
104     (mime-insert-entity entity)
105     (cond ((progn
106              (goto-char (point-min))
107              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
108            (pgg-verify-region (match-beginning 0)(point-max) nil 'fetch)
109            (goto-char (point-min))
110            (delete-region
111             (point-min)
112             (and
113              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
114              (match-end 0)))
115            (delete-region
116             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
117                  (match-beginning 0))
118             (point-max))
119            (goto-char (point-min))
120            (while (re-search-forward "^- -" nil t)
121              (replace-match "-"))
122            (setq representation-type (if (mime-entity-cooked-p entity)
123                                          'cooked)))
124           ((progn
125              (goto-char (point-min))
126              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
127            (pgg-decrypt-region (point-min)(point-max))
128            (delete-region (point-min)(point-max))
129            (insert-buffer pgg-output-buffer)
130            (setq representation-type 'binary)))
131     (setq major-mode 'mime-show-message-mode)
132     (save-window-excursion
133       (mime-view-buffer nil preview-buffer mother
134                         nil representation-type)
135       (make-local-variable 'mime-view-temp-message-buffer)
136       (setq mime-view-temp-message-buffer message-buf))
137     (set-window-buffer p-win preview-buffer)))
138
139
140 ;;; @ Internal method for application/pgp-signature
141 ;;;
142 ;;; It is based on RFC 2015 (PGP/MIME) and
143 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
144
145 (defun mime-verify-application/pgp-signature (entity situation)
146   "Internal method to check PGP/MIME signature."
147   (let* ((entity-node-id (mime-entity-node-id entity))
148          (mother (mime-entity-parent entity))
149          (knum (car entity-node-id))
150          (onum (if (> knum 0)
151                    (1- knum)
152                  (1+ knum)))
153          (orig-entity (nth onum (mime-entity-children mother)))
154          (basename (expand-file-name "tm" temporary-file-directory))
155          (sig-file (concat (make-temp-name basename) ".asc"))
156          status)
157     (save-excursion 
158       (mime-show-echo-buffer)
159       (set-buffer mime-echo-buffer-name)
160       (set-window-start 
161        (get-buffer-window mime-echo-buffer-name)
162        (point-max)))
163     (mime-write-entity-content entity sig-file)
164     (unwind-protect
165         (with-temp-buffer
166           (mime-insert-entity orig-entity)
167           (goto-char (point-min))
168           (while (progn (end-of-line) (not (eobp)))
169             (insert "\r")
170             (forward-line 1))
171           (setq status (pgg-verify-region (point-min)(point-max) 
172                                           sig-file 'fetch))
173           (save-excursion 
174             (set-buffer mime-echo-buffer-name)
175             (insert-buffer-substring (if status pgg-output-buffer
176                                        pgg-errors-buffer))))
177       (delete-file sig-file))))
178
179
180 ;;; @ Internal method for application/pgp-encrypted
181 ;;;
182 ;;; It is based on RFC 2015 (PGP/MIME) and
183 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
184
185 (defun mime-decrypt-application/pgp-encrypted (entity situation)
186   (let* ((entity-node-id (mime-entity-node-id entity))
187          (mother (mime-entity-parent entity))
188          (knum (car entity-node-id))
189          (onum (if (> knum 0)
190                    (1- knum)
191                  (1+ knum)))
192          (orig-entity (nth onum (mime-entity-children mother))))
193     (mime-view-application/pgp orig-entity situation)))
194
195
196 ;;; @ Internal method for application/pgp-keys
197 ;;;
198 ;;; It is based on RFC 2015 (PGP/MIME) and
199 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
200
201 (defun mime-add-application/pgp-keys (entity situation)
202   (save-excursion 
203     (mime-show-echo-buffer)
204     (set-buffer mime-echo-buffer-name)
205     (set-window-start 
206      (get-buffer-window mime-echo-buffer-name)
207      (point-max)))
208   (with-temp-buffer
209     (mime-insert-entity-content entity)
210     (mime-decode-region (point-min) (point-max)
211                         (cdr (assq 'encoding situation)))
212     (let ((status (pgg-snarf-keys-region (point-min)(point-max))))
213       (save-excursion 
214         (set-buffer mime-echo-buffer-name)
215         (insert-buffer-substring (if status pgg-output-buffer
216                                    pgg-errors-buffer))))))
217
218
219 ;;; @ Internal method for application/pkcs7-signature
220 ;;;
221 ;;; It is based on the S/MIME user interface in Gnus.
222
223 (defun mime-verify-application/pkcs7-signature (entity situation)
224   "Internal method to check S/MIME signature."
225   (with-temp-buffer
226     (mime-insert-entity (mime-find-root-entity entity))
227     (let ((good-signature (smime-noverify-buffer))
228           (good-certificate
229            (and (or smime-CA-file smime-CA-directory)
230                 (smime-verify-buffer))))
231       (if (not good-signature)
232           ;; we couldn't verify message, fail with openssl output as message
233           (save-excursion
234             (mime-show-echo-buffer)
235             (set-buffer mime-echo-buffer-name)
236             (set-window-start 
237              (get-buffer-window mime-echo-buffer-name)
238              (point-max))
239             (insert-buffer-substring smime-details-buffer))
240         ;; verify mail addresses in mail against those in certificate
241         (when (and (smime-pkcs7-region (point-min)(point-max))
242                    (smime-pkcs7-certificates-region (point-min)(point-max)))
243           (if (not (member
244                     (downcase 
245                      (nth 1 (std11-extract-address-components
246                              (mime-entity-fetch-field
247                               (mime-find-root-entity entity) "From"))))
248                     (mime-smime-pkcs7-email-buffer (current-buffer))))
249               (message "Sender address forged")
250             (if good-certificate
251                 (message "Ok (sender authenticated)")
252               (message "Integrity OK (sender unknown)"))))))))
253
254 (defun mime-smime-pkcs7-email-buffer (buffer)
255   (with-temp-buffer
256     (insert-buffer-substring buffer)
257     (goto-char (point-min))
258     (let (addresses)
259       (while (re-search-forward "-----END CERTIFICATE-----" nil t)
260         (if (smime-pkcs7-email-region (point-min)(point))
261             (setq addresses (append (split-string
262                                      (buffer-substring (point-min)(point))
263                                      "[\n\r]+")
264                                     addresses)))
265         (delete-region (point-min)(point)))
266       (mapcar #'downcase addresses))))
267
268
269 ;;; @ Internal method for application/pkcs7-mime
270 ;;;
271 ;;; It is based on RFC 2633 (S/MIME version 3).
272
273 (defun mime-view-application/pkcs7-mime (entity situation)
274   (let* ((p-win (or (get-buffer-window (current-buffer))
275                     (get-largest-window)))
276          (new-name
277           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
278          (mother (current-buffer))
279          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
280          message-buf)
281     (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
282                 '(enveloped-data signed-data))
283       (set-buffer (setq message-buf (get-buffer-create new-name)))
284       (let ((inhibit-read-only t)
285             buffer-read-only)
286         (erase-buffer)
287         (mime-insert-entity entity)
288         (smime-decrypt-buffer))
289       (setq major-mode 'mime-show-message-mode)
290       (save-window-excursion
291         (mime-view-buffer nil preview-buffer mother
292                           nil 'binary)
293         (make-local-variable 'mime-view-temp-message-buffer)
294         (setq mime-view-temp-message-buffer message-buf))
295       (set-window-buffer p-win preview-buffer))))
296
297
298 ;;; @ end
299 ;;;
300
301 (provide 'mime-pgp)
302
303 (run-hooks 'mime-pgp-load-hook)
304
305 ;;; mime-pgp.el ends here