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