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