1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Renamed: 1997/2/27 from tm-pgp.el
8 ;; Keywords: PGP, security, MIME, multimedia, mail, news
10 ;; This file is part of SEMI (Secure Emacs MIME Interface).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This module is based on
31 ;; [security-multipart] RFC 1847: "Security Multiparts for MIME:
32 ;; Multipart/Signed and Multipart/Encrypted" by
33 ;; Jim Galvin <galvin@tis.com>, Sandy Murphy <sandy@tis.com>,
34 ;; Steve Crocker <crocker@cybercash.com> and
35 ;; Ned Freed <ned@innosoft.com> (1995/10)
37 ;; [PGP/MIME] RFC 2015: "MIME Security with Pretty Good Privacy
38 ;; (PGP)" by Michael Elkins <elkins@aero.org> (1996/6)
40 ;; [PGP-kazu] draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
41 ;; by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp> (1995/10;
49 ;;; @ Internal method for multipart/signed
51 ;;; It is based on RFC 1847 (security-multipart).
53 (defun mime-verify-multipart/signed (entity situation)
54 "Internal method to verify multipart/signed."
56 (nth 1 (mime-entity-children entity)) ; entity-info of signature
57 (cdr (assq 'mode situation)) ; play-mode
61 ;;; @ internal method for application/pgp
63 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
65 (defun mime-view-application/pgp (entity situation)
66 (let* ((start (mime-entity-point-min entity))
67 (end (mime-entity-point-max entity))
68 (entity-number (mime-raw-point-to-entity-number start))
69 (p-win (or (get-buffer-window mime-preview-buffer)
70 (get-largest-window)))
71 (new-name (format "%s-%s" (buffer-name) entity-number))
72 (the-buf (current-buffer))
73 (mother mime-preview-buffer)
75 (set-buffer (get-buffer-create new-name))
77 (insert-buffer-substring the-buf start end)
79 (goto-char (point-min))
80 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
81 (funcall (pgp-function 'verify))
82 (goto-char (point-min))
86 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
89 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
92 (goto-char (point-min))
93 (while (re-search-forward "^- -" nil t)
96 (setq representation-type (mime-entity-representation-type entity))
99 (goto-char (point-min))
100 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
101 (as-binary-process (funcall (pgp-function 'decrypt)))
102 (goto-char (point-min))
103 (delete-region (point-min)
105 (search-forward "\n\n")
107 (setq representation-type 'binary)
109 (setq major-mode 'mime-show-message-mode)
110 (setq mime-raw-representation-type representation-type)
111 (save-window-excursion (mime-view-mode mother))
112 (set-window-buffer p-win mime-preview-buffer)
116 ;;; @ Internal method for application/pgp-signature
118 ;;; It is based on RFC 2015 (PGP/MIME).
120 (defvar mime-pgp-command "pgp"
121 "*Name of the PGP command.")
123 (defvar mime-pgp-default-language 'en
124 "*Symbol of language for pgp.
125 It should be ISO 639 2 letter language code such as en, ja, ...")
127 (defvar mime-pgp-good-signature-regexp-alist
128 '((en . "Good signature from user.*$"))
129 "Alist of language vs regexp to detect ``Good signature''.")
131 (defvar mime-pgp-key-expected-regexp-alist
132 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
133 "Alist of language vs regexp to detect ``Key expected''.")
135 (defun mime-pgp-check-signature (output-buffer orig-file)
137 (set-buffer output-buffer)
139 (let* ((lang (or mime-pgp-default-language 'en))
140 (status (call-process-region (point-min)(point-max)
142 nil output-buffer nil
143 orig-file (format "+language=%s" lang)))
144 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
147 (set-buffer output-buffer)
148 (goto-char (point-min))
150 (cond ((not (stringp regexp))
151 "Please specify right regexp for specified language")
152 ((re-search-forward regexp nil t)
153 (buffer-substring (match-beginning 0) (match-end 0)))
154 (t "Bad signature")))
157 (defun mime-verify-application/pgp-signature (entity situation)
158 "Internal method to check PGP/MIME signature."
159 (let* ((start (mime-entity-point-min entity))
160 (end (mime-entity-point-max entity))
161 (encoding (cdr (assq 'encoding situation)))
162 (entity-node-id (mime-raw-point-to-entity-node-id start))
163 (mother-node-id (cdr entity-node-id))
164 (knum (car entity-node-id))
168 (orig-entity (mime-raw-find-entity-from-node-id
169 (cons onum mother-node-id) mime-message-structure))
170 (basename (expand-file-name "tm" mime-temp-directory))
171 (orig-file (make-temp-name basename))
172 (sig-file (concat orig-file ".sig"))
174 (mime-raw-write-region (mime-entity-point-min orig-entity)
175 (mime-entity-point-max orig-entity)
177 (save-excursion (mime-show-echo-buffer))
178 (mime-write-decoded-region (save-excursion
180 (and (search-forward "\n\n")
182 ) end sig-file encoding)
183 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
186 (set-buffer mime-echo-buffer-name)
187 (goto-char (point-min))
188 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
189 mime-pgp-key-expected-regexp-alist))))
190 (cond ((not (stringp regexp))
192 "Please specify right regexp for specified language")
194 ((re-search-forward regexp nil t)
196 (concat "0x" (buffer-substring-no-properties
202 (format "Key %s not found; attempt to fetch? " pgp-id))
205 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
206 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
209 (let ((other-window-scroll-buffer mime-echo-buffer-name))
210 (scroll-other-window 8)
212 (delete-file orig-file)
213 (delete-file sig-file)
217 ;;; @ Internal method for application/pgp-encrypted
219 ;;; It is based on RFC 2015 (PGP/MIME).
221 (defun mime-decrypt-application/pgp-encrypted (entity situation)
222 (let* ((entity-node-id (mime-entity-node-id entity))
223 (mother-node-id (cdr entity-node-id))
224 (knum (car entity-node-id))
228 (orig-entity (mime-raw-find-entity-from-node-id
229 (cons onum mother-node-id) mime-message-structure)))
230 (mime-view-application/pgp orig-entity situation)
234 ;;; @ Internal method for application/pgp-keys
236 ;;; It is based on RFC 2015 (PGP/MIME).
238 (defun mime-add-application/pgp-keys (entity situation)
239 (let* ((start (mime-entity-point-min entity))
240 (end (mime-entity-point-max entity))
241 (entity-number (mime-raw-point-to-entity-number start))
242 (new-name (format "%s-%s" (buffer-name) entity-number))
243 (encoding (cdr (assq 'encoding situation)))
245 (setq str (buffer-substring start end))
246 (switch-to-buffer new-name)
247 (setq buffer-read-only nil)
250 (goto-char (point-min))
251 (if (re-search-forward "^\n" nil t)
252 (delete-region (point-min) (match-end 0))
254 (mime-decode-region (point-min)(point-max) encoding)
255 (funcall (pgp-function 'snarf-keys))
256 (kill-buffer (current-buffer))
265 (run-hooks 'mime-pgp-load-hook)
267 ;;; mime-pgp.el ends here