1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
3 ;; Copyright (C) 1999,2000 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Keywords: PGP, OpenPGP
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 (require 'mel) ; binary-to-text-funcall, binary-write-decoded-region
29 (eval-when-compile (require 'pgg))
35 (defcustom pgg-pgp5-pgpe-program "pgpe"
36 "PGP 5.* 'pgpe' executable."
40 (defcustom pgg-pgp5-pgps-program "pgps"
41 "PGP 5.* 'pgps' executable."
45 (defcustom pgg-pgp5-pgpk-program "pgpk"
46 "PGP 5.* 'pgpk' executable."
50 (defcustom pgg-pgp5-pgpv-program "pgpv"
51 "PGP 5.* 'pgpv' executable."
55 (defcustom pgg-pgp5-shell-file-name "/bin/sh"
56 "File name to load inferior shells from.
57 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
61 (defcustom pgg-pgp5-shell-command-switch "-c"
62 "Switch used to have the shell execute its command line argument."
66 (defcustom pgg-pgp5-extra-args nil
67 "Extra arguments for every PGP 5.* invocation."
72 (luna-define-class pgg-scheme-pgp5 (pgg-scheme)))
74 (defvar pgg-pgp5-user-id nil
75 "PGP 5.* ID of your default identity.")
77 (defvar pgg-pgp5-messages-coding-system pgg-messages-coding-system
78 "Coding system used when reading from a PGP5 external process.")
80 (defvar pgg-scheme-pgp5-instance nil)
83 (defun pgg-make-scheme-pgp5 ()
84 (or pgg-scheme-pgp5-instance
85 (setq pgg-scheme-pgp5-instance
86 (luna-make-entity 'pgg-scheme-pgp5))))
88 (defun pgg-pgp5-process-region (start end passphrase program args)
89 (let* ((errors-file-name
90 (concat temporary-file-directory
91 (make-temp-name "pgg-errors")))
95 (list (concat "2>" errors-file-name))))
96 (shell-file-name pgg-pgp5-shell-file-name)
97 (shell-command-switch pgg-pgp5-shell-command-switch)
98 (process-environment process-environment)
99 (output-buffer pgg-output-buffer)
100 (errors-buffer pgg-errors-buffer)
101 (process-connection-type nil)
102 process status exit-status)
103 (with-current-buffer (get-buffer-create output-buffer)
104 (buffer-disable-undo)
107 (setenv "PGPPASSFD" "0"))
111 (apply #'binary-to-text-funcall
112 pgg-pgp5-messages-coding-system
113 #'start-process-shell-command "*PGP*" output-buffer
115 (set-process-sentinel process #'ignore)
117 (process-send-string process (concat passphrase "\n")))
118 (process-send-region process start end)
119 (process-send-eof process)
120 (while (eq 'run (process-status process))
121 (accept-process-output process 5))
122 (setq status (process-status process)
123 exit-status (process-exit-status process))
124 (delete-process process)
125 (with-current-buffer output-buffer
126 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
128 (if (memq status '(stop signal))
129 (error "%s exited abnormally: '%s'" program exit-status))
130 (if (= 127 exit-status)
131 (error "%s could not be found" program))
133 (set-buffer (get-buffer-create errors-buffer))
134 (buffer-disable-undo)
136 (insert-file-contents errors-file-name)))
137 (if (and process (eq 'run (process-status process)))
138 (interrupt-process process))
140 (delete-file errors-file-name)
143 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-pgp5)
144 string &optional type)
145 (let ((args (list "+language=en" "-l" string)))
146 (with-current-buffer (get-buffer-create pgg-output-buffer)
147 (buffer-disable-undo)
149 (apply #'call-process pgg-pgp5-pgpk-program nil t nil args)
150 (goto-char (point-min))
151 (when (re-search-forward "^sec" nil t)
154 (buffer-substring (match-end 0)(progn (end-of-line)(point)))))
157 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-pgp5)
158 start end recipients)
159 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
161 `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
164 (mapcar (lambda (rcpt)
166 (concat "\"" rcpt "\"")))
168 (if pgg-encrypt-for-me
169 (list pgg-pgp5-user-id)))))))))
170 (pgg-pgp5-process-region start end nil pgg-pgp5-pgpe-program args)
171 (pgg-process-when-success nil)))
173 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-pgp5)
175 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
178 (format "PGP passphrase for %s: " pgg-pgp5-user-id)
179 (pgg-scheme-lookup-key scheme pgg-pgp5-user-id 'encrypt)))
181 '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
182 (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgpv-program args)
183 (pgg-process-when-success nil)))
185 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-pgp5)
186 start end &optional clearsign)
187 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
190 (format "PGP passphrase for %s: " pgg-pgp5-user-id)
191 (pgg-scheme-lookup-key scheme pgg-pgp5-user-id 'sign)))
193 (list (if clearsign "-fat" "-fbat")
194 "+verbose=1" "+language=us" "+batchmode=1"
195 "-u" pgg-pgp5-user-id)))
196 (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgps-program args)
197 (pgg-process-when-success
198 (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
200 (cdr (assq 2 (pgg-parse-armor-region
201 (progn (beginning-of-line 2)
204 (if pgg-cache-passphrase
205 (pgg-add-passphrase-cache
206 (cdr (assq 'key-identifier packet))
209 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-pgp5)
210 start end &optional signature)
211 (let* ((basename (expand-file-name "pgg" temporary-file-directory))
212 (orig-file (make-temp-name basename))
213 (args '("+verbose=1" "+batchmode=1" "+language=us"))
214 (orig-mode (default-file-modes)))
217 (set-default-file-modes 448)
218 (binary-write-decoded-region start end orig-file))
219 (set-default-file-modes orig-mode))
220 (when (stringp signature)
221 (copy-file signature (setq signature (concat orig-file ".asc")))
222 (setq args (append args (list signature))))
223 (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpv-program args)
224 (delete-file orig-file)
225 (if signature (delete-file signature))
226 (with-current-buffer pgg-errors-buffer
227 (goto-char (point-min))
228 (if (re-search-forward "^Good signature" nil t)
230 (set-buffer pgg-output-buffer)
231 (insert-buffer-substring pgg-errors-buffer)
235 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-pgp5))
236 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
238 (list "+verbose=1" "+batchmode=1" "+language=us" "-x"
239 (concat "\"" pgg-pgp5-user-id "\""))))
240 (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpk-program args)
241 (insert-buffer-substring pgg-output-buffer)))
243 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-pgp5)
245 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
246 (basename (expand-file-name "pgg" temporary-file-directory))
247 (key-file (make-temp-name basename))
249 (list "+verbose=1" "+batchmode=1" "+language=us" "-a"
251 (let ((coding-system-for-write 'raw-text-dos))
252 (write-region start end key-file))
253 (pgg-pgp5-process-region start end nil pgg-pgp5-pgpk-program args)
254 (delete-file key-file)
255 (pgg-process-when-success nil)))
259 ;;; pgg-pgp5.el ends here