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;
44 ;; PGP/MIME and PGP-kazu may be contrary to each other. You should
45 ;; decide which you support (Maybe you should not use PGP-kazu).
52 ;;; @ internal method for application/pgp
54 ;;; It is based on draft-kazu-pgp-mime-00.txt (PGP-kazu).
56 (defun mime-method-for-application/pgp (entity cal)
57 (let* ((start (mime-entity-point-min entity))
58 (end (mime-entity-point-max entity))
59 (entity-number (mime-raw-point-to-entity-number start))
60 (p-win (or (get-buffer-window mime-preview-buffer)
61 (get-largest-window)))
62 (new-name (format "%s-%s" (buffer-name) entity-number))
63 (the-buf (current-buffer))
64 (mother mime-preview-buffer)
67 (set-buffer (get-buffer-create new-name))
69 (insert-buffer-substring the-buf start end)
71 (goto-char (point-min))
72 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
73 (funcall (pgp-function 'verify))
74 (goto-char (point-min))
78 (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
81 (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
84 (goto-char (point-min))
85 (while (re-search-forward "^- -" nil t)
88 (setq representation-type
89 (cdr (or (assq mode mime-raw-representation-type-alist)
90 (assq t mime-raw-representation-type-alist))))
93 (goto-char (point-min))
94 (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
95 (as-binary-process (funcall (pgp-function 'decrypt)))
96 (goto-char (point-min))
97 (delete-region (point-min)
99 (search-forward "\n\n")
101 (setq representation-type (function mime-text-decode-buffer))
103 (setq major-mode 'mime-show-message-mode)
104 (setq mime-raw-representation-type representation-type)
105 (save-window-excursion (mime-view-mode mother))
106 (set-window-buffer p-win mime-preview-buffer)
110 ;;; @ Internal method for multipart/signed
112 ;;; It is based on RFC 1847 (security-multipart).
114 (defun mime-method-to-verify-multipart/signed (entity cal)
115 "Internal method to verify multipart/signed."
116 (mime-raw-play-entity
117 (nth 1 (mime-entity-children entity)) ; entity-info of signature
118 (cdr (assq 'mode cal)) ; play-mode
122 ;;; @ Internal method for application/pgp-signature
124 ;;; It is based on RFC 2015 (PGP/MIME).
126 (defvar mime-pgp-command "pgp"
127 "*Name of the PGP command.")
129 (defvar mime-pgp-default-language 'en
130 "*Symbol of language for pgp.
131 It should be ISO 639 2 letter language code such as en, ja, ...")
133 (defvar mime-pgp-good-signature-regexp-alist
134 '((en . "Good signature from user.*$"))
135 "Alist of language vs regexp to detect ``Good signature''.")
137 (defvar mime-pgp-key-expected-regexp-alist
138 '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
139 "Alist of language vs regexp to detect ``Key expected''.")
141 (defun mime-pgp-check-signature (output-buffer orig-file)
143 (set-buffer output-buffer)
145 (let* ((lang (or mime-pgp-default-language 'en))
146 (status (call-process-region (point-min)(point-max)
148 nil output-buffer nil
149 orig-file (format "+language=%s" lang)))
150 (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist))))
153 (set-buffer output-buffer)
154 (goto-char (point-min))
156 (cond ((not (stringp regexp))
157 "Please specify right regexp for specified language")
158 ((re-search-forward regexp nil t)
159 (buffer-substring (match-beginning 0) (match-end 0)))
160 (t "Bad signature")))
163 (defun mime-method-to-verify-application/pgp-signature (entity cal)
164 "Internal method to check PGP/MIME signature."
165 (let* ((start (mime-entity-point-min entity))
166 (end (mime-entity-point-max entity))
167 (encoding (cdr (assq 'encoding cal)))
168 (entity-node-id (mime-raw-point-to-entity-node-id start))
169 (mother-node-id (cdr entity-node-id))
170 (knum (car entity-node-id))
174 (oinfo (mime-raw-find-entity-from-node-id
175 (cons onum mother-node-id) mime-raw-message-info))
176 (basename (expand-file-name "tm" mime-temp-directory))
177 (orig-file (make-temp-name basename))
178 (sig-file (concat orig-file ".sig"))
180 (mime-raw-write-region (mime-entity-point-min oinfo)
181 (mime-entity-point-max oinfo)
183 (save-excursion (mime-show-echo-buffer))
184 (mime-write-decoded-region (save-excursion
186 (and (search-forward "\n\n")
188 ) end sig-file encoding)
189 (or (mime-pgp-check-signature mime-echo-buffer-name orig-file)
192 (set-buffer mime-echo-buffer-name)
193 (goto-char (point-min))
194 (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
195 mime-pgp-key-expected-regexp-alist))))
196 (cond ((not (stringp regexp))
198 "Please specify right regexp for specified language")
200 ((re-search-forward regexp nil t)
202 (concat "0x" (buffer-substring-no-properties
208 (format "Key %s not found; attempt to fetch? " pgp-id))
211 (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
212 (mime-pgp-check-signature mime-echo-buffer-name orig-file)
215 (let ((other-window-scroll-buffer mime-echo-buffer-name))
216 (scroll-other-window 8)
218 (delete-file orig-file)
219 (delete-file sig-file)
223 ;;; @ Internal method for application/pgp-encrypted
225 ;;; It is based on RFC 2015 (PGP/MIME).
227 (defun mime-method-to-decrypt-application/pgp-encrypted (entity cal)
228 (let* ((entity-node-id (mime-entity-node-id entity))
229 (mother-node-id (cdr entity-node-id))
230 (knum (car entity-node-id))
234 (oinfo (mime-raw-find-entity-from-node-id
235 (cons onum mother-node-id) mime-raw-message-info)))
236 (mime-method-for-application/pgp oinfo cal)
240 ;;; @ Internal method for application/pgp-keys
242 ;;; It is based on RFC 2015 (PGP/MIME).
244 (defun mime-method-to-add-application/pgp-keys (entity cal)
245 (let* ((start (mime-entity-point-min entity))
246 (end (mime-entity-point-max entity))
247 (entity-number (mime-raw-point-to-entity-number start))
248 (new-name (format "%s-%s" (buffer-name) entity-number))
249 (encoding (cdr (assq 'encoding cal)))
251 (setq str (buffer-substring start end))
252 (switch-to-buffer new-name)
253 (setq buffer-read-only nil)
256 (goto-char (point-min))
257 (if (re-search-forward "^\n" nil t)
258 (delete-region (point-min) (match-end 0))
260 (mime-decode-region (point-min)(point-max) encoding)
261 (funcall (pgp-function 'snarf-keys))
262 (kill-buffer (current-buffer))
271 (run-hooks 'mime-pgp-load-hook)
273 ;;; mime-pgp.el ends here