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