1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
3 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, OpenPGP
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU 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."
54 (const :tag "None" nil)
55 (string :tag "Arguments")))
57 (defvar pgg-pgp-user-id nil
58 "PGP ID of your default identity.")
60 (defun pgg-pgp-process-region (start end passphrase program args)
61 (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
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* ((orig-file (pgg-make-temp-file "pgg"))
186 (args '("+verbose=1" "+batchmode" "+language=us"))
187 (orig-mode (default-file-modes)))
190 (set-default-file-modes 448)
191 (let ((coding-system-for-write 'binary)
192 jka-compr-compression-info-list jam-zcat-filename-list)
193 (write-region start end orig-file)))
194 (set-default-file-modes orig-mode))
195 (when (stringp signature)
196 (copy-file signature (setq signature (concat orig-file ".asc")))
197 (setq args (append args (list signature orig-file))))
198 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
199 (delete-file orig-file)
200 (if signature (delete-file signature))
201 (pgg-process-when-success
202 (goto-char (point-min))
203 (let ((case-fold-search t))
204 (while (re-search-forward "^warning: " nil t)
205 (delete-region (match-beginning 0)
206 (progn (beginning-of-line 2) (point)))))
207 (goto-char (point-min))
208 (when (re-search-forward "^\\.$" nil t)
209 (delete-region (point-min)
210 (progn (beginning-of-line 2)
213 (defun pgg-pgp-insert-key ()
214 "Insert public key at point."
215 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
217 (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
218 (concat "\"" pgg-pgp-user-id "\""))))
219 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
220 (insert-buffer-substring pgg-output-buffer)))
222 (defun pgg-pgp-snarf-keys-region (start end)
223 "Add all public keys in region between START and END to the keyring."
224 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
225 (key-file (pgg-make-temp-file "pgg"))
227 (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
229 (let ((coding-system-for-write 'raw-text-dos))
230 (write-region start end key-file))
231 (pgg-pgp-process-region start end nil pgg-pgp-program args)
232 (delete-file key-file)
233 (pgg-process-when-success nil)))
237 ;;; pgg-pgp.el ends here