1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
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 (eval-when-compile (require 'pgg))
34 (defcustom pgg-pgp5-pgpe-program "pgpe"
35 "PGP 5.* 'pgpe' executable."
39 (defcustom pgg-pgp5-pgps-program "pgps"
40 "PGP 5.* 'pgps' executable."
44 (defcustom pgg-pgp5-pgpk-program "pgpk"
45 "PGP 5.* 'pgpk' executable."
49 (defcustom pgg-pgp5-pgpv-program "pgpv"
50 "PGP 5.* 'pgpv' executable."
54 (defcustom pgg-pgp5-shell-file-name "/bin/sh"
55 "File name to load inferior shells from.
56 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
60 (defcustom pgg-pgp5-shell-command-switch "-c"
61 "Switch used to have the shell execute its command line argument."
65 (defcustom pgg-pgp5-extra-args nil
66 "Extra arguments for every PGP 5.* invocation."
69 (const :tag "None" nil)
70 (string :tag "Arguments")))
72 (defvar pgg-pgp5-user-id nil
73 "PGP 5.* ID of your default identity.")
75 (defun pgg-pgp5-process-region (start end passphrase program args)
76 (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
80 (list (concat "2>" errors-file-name))))
81 (shell-file-name pgg-pgp5-shell-file-name)
82 (shell-command-switch pgg-pgp5-shell-command-switch)
83 (process-environment process-environment)
84 (output-buffer pgg-output-buffer)
85 (errors-buffer pgg-errors-buffer)
86 (process-connection-type nil)
87 process status exit-status)
88 (with-current-buffer (get-buffer-create output-buffer)
92 (setenv "PGPPASSFD" "0"))
95 (let ((coding-system-for-read 'binary)
96 (coding-system-for-write 'binary))
99 #'start-process-shell-command "*PGP*" output-buffer
101 (set-process-sentinel process #'ignore)
103 (process-send-string process (concat passphrase "\n")))
104 (process-send-region process start end)
105 (process-send-eof process)
106 (while (eq 'run (process-status process))
107 (accept-process-output process 5))
108 (setq status (process-status process)
109 exit-status (process-exit-status process))
110 (delete-process process)
111 (with-current-buffer output-buffer
112 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
114 (if (memq status '(stop signal))
115 (error "%s exited abnormally: '%s'" program exit-status))
116 (if (= 127 exit-status)
117 (error "%s could not be found" program))
119 (set-buffer (get-buffer-create errors-buffer))
120 (buffer-disable-undo)
122 (insert-file-contents errors-file-name)))
123 (if (and process (eq 'run (process-status process)))
124 (interrupt-process process))
126 (delete-file errors-file-name)
129 (defun pgg-pgp5-lookup-key (string &optional type)
130 "Search keys associated with STRING."
131 (let ((args (list "+language=en" "-l" string)))
132 (with-current-buffer (get-buffer-create pgg-output-buffer)
133 (buffer-disable-undo)
135 (apply #'call-process pgg-pgp5-pgpk-program nil t nil args)
136 (goto-char (point-min))
137 (when (re-search-forward "^sec" nil t)
140 (buffer-substring (match-end 0)(progn (end-of-line)(point)))))
143 (defun pgg-pgp5-encrypt-region (start end recipients)
144 "Encrypt the current region between START and END."
145 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
147 `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
150 (mapcar (lambda (rcpt)
152 (concat "\"" rcpt "\"")))
154 (if pgg-encrypt-for-me
155 (list pgg-pgp5-user-id)))))))))
156 (pgg-pgp5-process-region start end nil pgg-pgp5-pgpe-program args)
157 (pgg-process-when-success nil)))
159 (defun pgg-pgp5-decrypt-region (start end)
160 "Decrypt the current region between START and END."
161 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
164 (format "PGP passphrase for %s: " pgg-pgp5-user-id)
165 (pgg-pgp5-lookup-key pgg-pgp5-user-id 'encrypt)))
167 '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
168 (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgpv-program args)
169 (pgg-process-when-success nil)))
171 (defun pgg-pgp5-sign-region (start end &optional clearsign)
172 "Make detached signature from text between START and END."
173 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
176 (format "PGP passphrase for %s: " pgg-pgp5-user-id)
177 (pgg-pgp5-lookup-key pgg-pgp5-user-id 'sign)))
179 (list (if clearsign "-fat" "-fbat")
180 "+verbose=1" "+language=us" "+batchmode=1"
181 "-u" pgg-pgp5-user-id)))
182 (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgps-program args)
183 (pgg-process-when-success
184 (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
186 (cdr (assq 2 (pgg-parse-armor-region
187 (progn (beginning-of-line 2)
190 (if pgg-cache-passphrase
191 (pgg-add-passphrase-cache
192 (cdr (assq 'key-identifier packet))
195 (defun pgg-pgp5-verify-region (start end &optional signature)
196 "Verify region between START and END as the detached signature SIGNATURE."
197 (let ((orig-file (pgg-make-temp-file "pgg"))
198 (args '("+verbose=1" "+batchmode=1" "+language=us"))
199 (orig-mode (default-file-modes)))
202 (set-default-file-modes 448)
203 (let ((coding-system-for-write 'binary)
204 jka-compr-compression-info-list jam-zcat-filename-list)
205 (write-region start end orig-file)))
206 (set-default-file-modes orig-mode))
207 (when (stringp signature)
208 (copy-file signature (setq signature (concat orig-file ".asc")))
209 (setq args (append args (list signature))))
210 (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpv-program args)
211 (delete-file orig-file)
212 (if signature (delete-file signature))
213 (with-current-buffer pgg-errors-buffer
214 (goto-char (point-min))
215 (if (re-search-forward "^Good signature" nil t)
217 (set-buffer pgg-output-buffer)
218 (insert-buffer-substring pgg-errors-buffer)
222 (defun pgg-pgp5-insert-key ()
223 "Insert public key at point."
224 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
226 (list "+verbose=1" "+batchmode=1" "+language=us" "-x"
227 (concat "\"" pgg-pgp5-user-id "\""))))
228 (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpk-program args)
229 (insert-buffer-substring pgg-output-buffer)))
231 (defun pgg-pgp5-snarf-keys-region (start end)
232 "Add all public keys in region between START and END to the keyring."
233 (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
234 (key-file (pgg-make-temp-file "pgg"))
236 (list "+verbose=1" "+batchmode=1" "+language=us" "-a"
238 (let ((coding-system-for-write 'raw-text-dos))
239 (write-region start end key-file))
240 (pgg-pgp5-process-region start end nil pgg-pgp5-pgpk-program args)
241 (delete-file key-file)
242 (pgg-process-when-success nil)))
246 ;;; pgg-pgp5.el ends here