2003-11-15 Simon Josefsson <jas@extundo.com>
[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
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          status)
141     (save-excursion 
142       (mime-show-echo-buffer)
143       (set-buffer mime-echo-buffer-name)
144       (set-window-start 
145        (get-buffer-window mime-echo-buffer-name)
146        (point-max)))
147     (mime-write-entity-content entity sig-file)
148     (unwind-protect
149         (with-temp-buffer
150           (mime-insert-entity orig-entity)
151           (goto-char (point-min))
152           (while (progn (end-of-line) (not (eobp)))
153             (insert "\r")
154             (forward-line 1))
155           (setq status (pgg-verify-region (point-min)(point-max) 
156                                           sig-file 'fetch))
157           (save-excursion 
158             (set-buffer mime-echo-buffer-name)
159             (insert-buffer-substring (if status pgg-output-buffer
160                                        pgg-errors-buffer))))
161       (delete-file sig-file))))
162
163
164 ;;; @ Internal method for application/pgp-encrypted
165 ;;;
166 ;;; It is based on RFC 2015 (PGP/MIME) and
167 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
168
169 (defun mime-decrypt-application/pgp-encrypted (entity situation)
170   (let* ((entity-node-id (mime-entity-node-id entity))
171          (mother (mime-entity-parent entity))
172          (knum (car entity-node-id))
173          (onum (if (> knum 0)
174                    (1- knum)
175                  (1+ knum)))
176          (orig-entity (nth onum (mime-entity-children mother))))
177     (mime-view-application/pgp orig-entity situation)))
178
179
180 ;;; @ Internal method for application/pgp-keys
181 ;;;
182 ;;; It is based on RFC 2015 (PGP/MIME) and
183 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
184
185 (defun mime-add-application/pgp-keys (entity situation)
186   (save-excursion 
187     (mime-show-echo-buffer)
188     (set-buffer mime-echo-buffer-name)
189     (set-window-start 
190      (get-buffer-window mime-echo-buffer-name)
191      (point-max)))
192   (with-temp-buffer
193     (mime-insert-entity-content entity)
194     (mime-decode-region (point-min) (point-max)
195                         (cdr (assq 'encoding situation)))
196     (let ((status (pgg-snarf-keys-region (point-min)(point-max))))
197       (save-excursion 
198         (set-buffer mime-echo-buffer-name)
199         (insert-buffer-substring (if status pgg-output-buffer
200                                    pgg-errors-buffer))))))
201
202
203 ;;; @ Internal method for application/pkcs7-signature
204 ;;;
205 ;;; It is based on the S/MIME user interface in Gnus.
206
207 (defun mime-verify-application/pkcs7-signature (entity situation)
208   "Internal method to check S/MIME signature."
209   (with-temp-buffer
210     (mime-insert-entity (mime-find-root-entity entity))
211     (let ((good-signature (smime-noverify-buffer))
212           (good-certificate
213            (and (or smime-CA-file smime-CA-directory)
214                 (smime-verify-buffer))))
215       (if (not good-signature)
216           ;; we couldn't verify message, fail with openssl output as message
217           (save-excursion
218             (mime-show-echo-buffer)
219             (set-buffer mime-echo-buffer-name)
220             (set-window-start 
221              (get-buffer-window mime-echo-buffer-name)
222              (point-max))
223             (insert-buffer-substring smime-details-buffer))
224         ;; verify mail addresses in mail against those in certificate
225         (when (and (smime-pkcs7-region (point-min)(point-max))
226                    (smime-pkcs7-certificates-region (point-min)(point-max)))
227           (if (not (member
228                     (downcase 
229                      (nth 1 (std11-extract-address-components
230                              (mime-entity-fetch-field
231                               (mime-find-root-entity entity) "From"))))
232                     (mime-smime-pkcs7-email-buffer (current-buffer))))
233               (message "Sender address forged")
234             (if good-certificate
235                 (message "Ok (sender authenticated)")
236               (message "Integrity OK (sender unknown)"))))))))
237
238 (defun mime-smime-pkcs7-email-buffer (buffer)
239   (with-temp-buffer
240     (insert-buffer-substring buffer)
241     (goto-char (point-min))
242     (let (addresses)
243       (while (re-search-forward "-----END CERTIFICATE-----" nil t)
244         (if (smime-pkcs7-email-region (point-min)(point))
245             (setq addresses (append (split-string
246                                      (buffer-substring (point-min)(point))
247                                      "[\n\r]+")
248                                     addresses)))
249         (delete-region (point-min)(point)))
250       (mapcar #'downcase addresses))))
251
252
253 ;;; @ Internal method for application/pkcs7-mime
254 ;;;
255 ;;; It is based on RFC 2633 (S/MIME version 3).
256
257 (defun mime-view-application/pkcs7-mime (entity situation)
258   (let* ((p-win (or (get-buffer-window (current-buffer))
259                     (get-largest-window)))
260          (new-name
261           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
262          (mother (current-buffer))
263          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
264          message-buf)
265     (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
266                 '(enveloped-data signed-data))
267       (set-buffer (setq message-buf (get-buffer-create new-name)))
268       (let ((inhibit-read-only t)
269             buffer-read-only)
270         (erase-buffer)
271         (mime-insert-entity entity)
272         (smime-decrypt-buffer))
273       (setq major-mode 'mime-show-message-mode)
274       (save-window-excursion
275         (mime-view-buffer nil preview-buffer mother
276                           nil 'binary)
277         (make-local-variable 'mime-view-temp-message-buffer)
278         (setq mime-view-temp-message-buffer message-buf))
279       (set-window-buffer p-win preview-buffer))))
280
281
282 ;;; @ end
283 ;;;
284
285 (provide 'mime-pgp)
286
287 (run-hooks 'mime-pgp-load-hook)
288
289 ;;; mime-pgp.el ends here