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