* mime-pgp.el (mime-verify-application/*-signature): New function.
[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 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            (message "%s"
91                     (epg-verify-result-to-string
92                      (epg-context-result-for context 'verify)))
93            (goto-char (point-min))
94            (delete-region
95             (point-min)
96             (and
97              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
98              (match-end 0)))
99            (delete-region
100             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
101                  (match-beginning 0))
102             (point-max))
103            (goto-char (point-min))
104            (while (re-search-forward "^- -" nil t)
105              (replace-match "-"))
106            (setq representation-type (if (mime-entity-cooked-p entity)
107                                          'cooked)))
108           ((progn
109              (goto-char (point-min))
110              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
111            (setq context (epg-make-context))
112            (setq plain
113                  (epg-decrypt-string
114                   context
115                   (buffer-substring (point-min)(point-max))))
116            (delete-region (point-min)(point-max))
117            (insert plain)
118            (setq representation-type 'binary)))
119     (setq major-mode 'mime-show-message-mode)
120     (save-window-excursion
121       (mime-view-buffer nil preview-buffer mother
122                         nil representation-type)
123       (make-local-variable 'mime-view-temp-message-buffer)
124       (setq mime-view-temp-message-buffer message-buf))
125     (set-window-buffer p-win preview-buffer)))
126
127
128 (defun mime-verify-application/*-signature (entity situation)
129   (let* ((entity-node-id (mime-entity-node-id entity))
130          (mother (mime-entity-parent entity))
131          (knum (car entity-node-id))
132          (onum (if (> knum 0)
133                    (1- knum)
134                  (1+ knum)))
135          (orig-entity (nth onum (mime-entity-children mother)))
136          (protocol (cdr (assoc "protocol" (mime-entity-parameters mother))))
137          (context (epg-make-context
138                    (if (equal protocol "application/pgp-signature")
139                        'OpenPGP
140                      (if (string-match
141                           "\\`application/\\(x-\\)?pkcs7-signature\\'"
142                           protocol)
143                        'CMS
144                        (error "Unknown protocol: %s" protocol)))))
145          verify-result)
146     (epg-verify-string context
147                        (mime-entity-content entity)
148                        (with-temp-buffer
149                          (if (fboundp 'set-buffer-multibyte)
150                              (set-buffer-multibyte nil))
151                          (mime-insert-entity orig-entity)
152                          (goto-char (point-min))
153                          (while (search-forward "\n" nil t)
154                            (replace-match "\r\n"))
155                          (buffer-substring)))
156     (setq verify-result
157           (mapcar (lambda (signature)
158                     (unless (stringp (epg-signature-user-id signature))
159                       (setq signature (copy-sequence signature))
160                       (epg-signature-set-user-id
161                        signature
162                        (epg-decode-dn (epg-signature-user-id signature))))
163                     signature)
164                   (epg-context-result-for context 'verify)))
165     (message "%s"
166              (epg-verify-result-to-string verify-result))))
167
168
169 ;;; @ Internal method for application/pgp-encrypted
170 ;;;
171 ;;; It is based on RFC 2015 (PGP/MIME) and
172 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
173
174 (defun mime-decrypt-application/pgp-encrypted (entity situation)
175   (let* ((entity-node-id (mime-entity-node-id entity))
176          (mother (mime-entity-parent entity))
177          (knum (car entity-node-id))
178          (onum (if (> knum 0)
179                    (1- knum)
180                  (1+ knum)))
181          (orig-entity (nth onum (mime-entity-children mother))))
182     (mime-view-application/pgp orig-entity situation)))
183
184
185 ;;; @ Internal method for application/pgp-keys
186 ;;;
187 ;;; It is based on RFC 2015 (PGP/MIME) and
188 ;;; draft-ietf-openpgp-mime-02.txt (OpenPGP/MIME).
189
190 (defun mime-add-application/pgp-keys (entity situation)
191   (with-temp-buffer
192     (mime-insert-entity-content entity)
193     (mime-decode-region (point-min) (point-max)
194                         (cdr (assq 'encoding situation)))
195     (epg-import-keys-from-string (epg-make-context)
196                                  (buffer-substring (point-min)(point-max)))
197     (epa-list-keys)))
198
199
200 ;;; @ Internal method for application/pkcs7-mime
201 ;;;
202 ;;; It is based on RFC 2633 (S/MIME version 3).
203
204 (defun mime-view-application/pkcs7-mime (entity situation)
205   (let* ((p-win (or (get-buffer-window (current-buffer))
206                     (get-largest-window)))
207          (new-name
208           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
209          (mother (current-buffer))
210          (preview-buffer (concat "*Preview-" (buffer-name) "*"))
211          (context (epg-make-context 'CMS))
212          message-buf)
213     (when (memq (or (cdr (assq 'smime-type situation)) 'enveloped-data)
214                 '(enveloped-data signed-data))
215       (set-buffer (setq message-buf (get-buffer-create new-name)))
216       (let ((inhibit-read-only t)
217             buffer-read-only)
218         (erase-buffer)
219         (insert (epg-decrypt-string context (mime-entity-content entity))))
220       (setq major-mode 'mime-show-message-mode)
221       (save-window-excursion
222         (mime-view-buffer nil preview-buffer mother
223                           nil 'binary)
224         (make-local-variable 'mime-view-temp-message-buffer)
225         (setq mime-view-temp-message-buffer message-buf))
226       (set-window-buffer p-win preview-buffer))))
227
228
229 ;;; @ end
230 ;;;
231
232 (provide 'mime-pgp)
233
234 (run-hooks 'mime-pgp-load-hook)
235
236 ;;; mime-pgp.el ends here