1 ;;; pgg-pgp.el --- PGP 2.* and 6.* 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))
31 "PGP 2.* and 6.* interface"
34 (defcustom pgg-pgp-program "pgp"
35 "PGP 2.* and 6.* executable."
39 (defcustom pgg-pgp-shell-file-name "/bin/sh"
40 "File name to load inferior shells from.
41 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
45 (defcustom pgg-pgp-shell-command-switch "-c"
46 "Switch used to have the shell execute its command line argument."
50 (defcustom pgg-pgp-extra-args nil
51 "Extra arguments for every PGP invocation."
55 (defvar pgg-pgp-user-id nil
56 "PGP ID of your default identity.")
58 (defun pgg-pgp-process-region (start end passphrase program args)
59 (let* ((errors-file-name
60 (expand-file-name (make-temp-name "pgg-errors")
61 pgg-temporary-file-directory))
65 (list (concat "2>" errors-file-name))))
66 (shell-file-name pgg-pgp-shell-file-name)
67 (shell-command-switch pgg-pgp-shell-command-switch)
68 (process-environment process-environment)
69 (output-buffer pgg-output-buffer)
70 (errors-buffer pgg-errors-buffer)
71 (process-connection-type nil)
72 process status exit-status)
73 (with-current-buffer (get-buffer-create output-buffer)
77 (setenv "PGPPASSFD" "0"))
80 (let ((coding-system-for-read 'binary)
81 (coding-system-for-write 'binary))
84 #'start-process-shell-command "*PGP*" output-buffer
86 (set-process-sentinel process #'ignore)
88 (process-send-string process (concat passphrase "\n")))
89 (process-send-region process start end)
90 (process-send-eof process)
91 (while (eq 'run (process-status process))
92 (accept-process-output process 5))
93 (setq status (process-status process)
94 exit-status (process-exit-status process))
95 (delete-process process)
96 (with-current-buffer output-buffer
97 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
99 (if (memq status '(stop signal))
100 (error "%s exited abnormally: '%s'" program exit-status))
101 (if (= 127 exit-status)
102 (error "%s could not be found" program))
104 (set-buffer (get-buffer-create errors-buffer))
105 (buffer-disable-undo)
107 (insert-file-contents errors-file-name)))
108 (if (and process (eq 'run (process-status process)))
109 (interrupt-process process))
111 (delete-file errors-file-name)
114 (defun pgg-pgp-lookup-key (string &optional type)
115 "Search keys associated with STRING."
116 (let ((args (list "+batchmode" "+language=en" "-kv" string)))
117 (with-current-buffer (get-buffer-create pgg-output-buffer)
118 (buffer-disable-undo)
120 (apply #'call-process pgg-pgp-program nil t nil args)
121 (goto-char (point-min))
123 ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
124 (buffer-substring (point)(+ 8 (point))))
125 ((re-search-forward "^Type" nil t);PGP 6.*
126 (beginning-of-line 2)
129 (buffer-substring (point)(progn (end-of-line) (point)))))
132 (defun pgg-pgp-encrypt-region (start end recipients)
133 "Encrypt the current region between START and END."
134 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
136 `("+encrypttoself=off +verbose=1" "+batchmode"
137 "+language=us" "-fate"
139 (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
141 (if pgg-encrypt-for-me
142 (list pgg-pgp-user-id))))))))
143 (pgg-pgp-process-region start end nil pgg-pgp-program args)
144 (pgg-process-when-success nil)))
146 (defun pgg-pgp-decrypt-region (start end)
147 "Decrypt the current region between START and END."
148 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
151 (format "PGP passphrase for %s: " pgg-pgp-user-id)
152 (pgg-pgp-lookup-key pgg-pgp-user-id 'encrypt)))
154 '("+verbose=1" "+batchmode" "+language=us" "-f")))
155 (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
156 (pgg-process-when-success nil)))
158 (defun pgg-pgp-sign-region (start end &optional clearsign)
159 "Make detached signature from text between START and END."
160 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
163 (format "PGP passphrase for %s: " pgg-pgp-user-id)
164 (pgg-pgp-lookup-key pgg-pgp-user-id 'sign)))
166 (list (if clearsign "-fast" "-fbast")
167 "+verbose=1" "+language=us" "+batchmode"
168 "-u" pgg-pgp-user-id)))
169 (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
170 (pgg-process-when-success
171 (goto-char (point-min))
172 (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
174 (cdr (assq 2 (pgg-parse-armor-region
175 (progn (beginning-of-line 2)
178 (if pgg-cache-passphrase
179 (pgg-add-passphrase-cache
180 (cdr (assq 'key-identifier packet))
183 (defun pgg-pgp-verify-region (start end &optional signature)
184 "Verify region between START and END as the detached signature SIGNATURE."
185 (let* ((basename (expand-file-name "pgg" temporary-file-directory))
186 (orig-file (make-temp-name basename))
187 (args '("+verbose=1" "+batchmode" "+language=us"))
188 (orig-mode (default-file-modes)))
191 (set-default-file-modes 448)
192 (let ((coding-system-for-write 'binary)
193 jka-compr-compression-info-list jam-zcat-filename-list)
194 (write-region start end orig-file)))
195 (set-default-file-modes orig-mode))
196 (when (stringp signature)
197 (copy-file signature (setq signature (concat orig-file ".asc")))
198 (setq args (append args (list signature orig-file))))
199 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
200 (delete-file orig-file)
201 (if signature (delete-file signature))
202 (pgg-process-when-success
203 (goto-char (point-min))
204 (let ((case-fold-search t))
205 (while (re-search-forward "^warning: " nil t)
206 (delete-region (match-beginning 0)
207 (progn (beginning-of-line 2) (point)))))
208 (goto-char (point-min))
209 (when (re-search-forward "^\\.$" nil t)
210 (delete-region (point-min)
211 (progn (beginning-of-line 2)
214 (defun pgg-pgp-insert-key ()
215 "Insert public key at point."
216 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
218 (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
219 (concat "\"" pgg-pgp-user-id "\""))))
220 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
221 (insert-buffer-substring pgg-output-buffer)))
223 (defun pgg-pgp-snarf-keys-region (start end)
224 "Add all public keys in region between START and END to the keyring."
225 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
226 (basename (expand-file-name "pgg" temporary-file-directory))
227 (key-file (make-temp-name basename))
229 (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
231 (let ((coding-system-for-write 'raw-text-dos))
232 (write-region start end key-file))
233 (pgg-pgp-process-region start end nil pgg-pgp-program args)
234 (delete-file key-file)
235 (pgg-process-when-success nil)))
239 ;;; pgg-pgp.el ends here