1 ;;; pgg-gpg.el --- GnuPG support for PGG.
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, OpenPGP, GnuPG
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-gpg-program "gpg"
35 "The GnuPG executable."
39 (defcustom pgg-gpg-extra-args nil
40 "Extra arguments for every GnuPG invocation."
44 (defvar pgg-gpg-user-id nil
45 "GnuPG ID of your default identity.")
47 (defun pgg-gpg-process-region (start end passphrase program args)
48 (let* ((output-file-name
49 (expand-file-name (make-temp-name "pgg-output")
50 pgg-temporary-file-directory))
53 ,@(if passphrase '("--passphrase-fd" "0"))
54 "--output" ,output-file-name
55 ,@pgg-gpg-extra-args ,@args))
56 (output-buffer pgg-output-buffer)
57 (errors-buffer pgg-errors-buffer)
58 (orig-mode (default-file-modes))
59 (process-connection-type nil)
60 process status exit-status)
61 (with-current-buffer (get-buffer-create errors-buffer)
66 (set-default-file-modes 448)
67 (let ((coding-system-for-write 'binary))
69 (apply #'start-process "*GnuPG*" errors-buffer
71 (set-process-sentinel process #'ignore)
73 (process-send-string process (concat passphrase "\n")))
74 (process-send-region process start end)
75 (process-send-eof process)
76 (while (eq 'run (process-status process))
77 (accept-process-output process 5))
78 (setq status (process-status process)
79 exit-status (process-exit-status process))
80 (delete-process process)
81 (with-current-buffer (get-buffer-create output-buffer)
84 (if (file-exists-p output-file-name)
85 (let ((coding-system-for-read 'raw-text-dos))
86 (insert-file-contents output-file-name)))
87 (set-buffer errors-buffer)
88 (if (memq status '(stop signal))
89 (error "%s exited abnormally: '%s'" program exit-status))
90 (if (= 127 exit-status)
91 (error "%s could not be found" program))))
92 (if (and process (eq 'run (process-status process)))
93 (interrupt-process process))
94 (if (file-exists-p output-file-name)
95 (delete-file output-file-name))
96 (set-default-file-modes orig-mode))))
98 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
99 (if (and pgg-cache-passphrase
101 (goto-char (point-min))
102 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
103 (pgg-add-passphrase-cache
105 (goto-char (point-min))
106 (if (re-search-forward
107 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
108 (substring (match-string 0) -8)))
111 (defun pgg-gpg-lookup-key (string &optional type)
112 "Search keys associated with STRING."
113 (let ((args (list "--with-colons" "--no-greeting" "--batch"
114 (if type "--list-secret-keys" "--list-keys")
117 (apply #'call-process pgg-gpg-program nil t nil args)
118 (goto-char (point-min))
119 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
122 (buffer-substring (match-end 0)
123 (progn (end-of-line)(point)))
126 (defun pgg-gpg-encrypt-region (start end recipients)
127 "Encrypt the current region between START and END."
128 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
130 `("--batch" "--armor" "--always-trust" "--encrypt"
133 (mapcar (lambda (rcpt)
134 (list "--remote-user" rcpt))
136 (if pgg-encrypt-for-me
137 (list pgg-gpg-user-id)))))))))
138 (pgg-as-lbt start end 'CRLF
139 (pgg-gpg-process-region start end nil pgg-gpg-program args))
140 (pgg-process-when-success)))
142 (defun pgg-gpg-decrypt-region (start end)
143 "Decrypt the current region between START and END."
144 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
147 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
148 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
149 (args '("--batch" "--decrypt")))
150 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
151 (with-current-buffer pgg-errors-buffer
152 (pgg-gpg-possibly-cache-passphrase passphrase)
153 (goto-char (point-min))
154 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
156 (defun pgg-gpg-sign-region (start end &optional cleartext)
157 "Make detached signature from text between START and END."
158 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
161 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
162 (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
164 (list (if cleartext "--clearsign" "--detach-sign")
165 "--armor" "--batch" "--verbose"
166 "--local-user" pgg-gpg-user-id))
167 (inhibit-read-only t)
169 (pgg-as-lbt start end 'CRLF
170 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
171 (with-current-buffer pgg-errors-buffer
172 (pgg-gpg-possibly-cache-passphrase passphrase))
173 (pgg-process-when-success)))
175 (defun pgg-gpg-verify-region (start end &optional signature)
176 "Verify region between START and END as the detached signature SIGNATURE."
177 (let ((args '("--batch" "--verify")))
178 (when (stringp signature)
179 (setq args (append args (list signature))))
180 (setq args (append args '("-")))
181 (pgg-gpg-process-region start end nil pgg-gpg-program args)
182 (with-current-buffer pgg-errors-buffer
183 (goto-char (point-min))
184 (while (re-search-forward "^gpg: " nil t)
186 (goto-char (point-min))
187 (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
188 (goto-char (point-min))
189 (delete-matching-lines "^warning\\|\\[GNUPG:]")
190 (set-buffer pgg-output-buffer)
191 (insert-buffer-substring pgg-errors-buffer)))))
193 (defun pgg-gpg-insert-key ()
194 "Insert public key at point."
195 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
196 (args (list "--batch" "--export" "--armor"
198 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
199 (insert-buffer-substring pgg-output-buffer)))
201 (defun pgg-gpg-snarf-keys-region (start end)
202 "Add all public keys in region between START and END to the keyring."
203 (let ((args '("--import" "--batch" "-")) status)
204 (pgg-gpg-process-region start end nil pgg-gpg-program args)
205 (set-buffer pgg-errors-buffer)
206 (goto-char (point-min))
207 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
208 (setq status (buffer-substring (match-end 0)
209 (progn (end-of-line)(point)))
210 status (vconcat (mapcar #'string-to-int (split-string status))))
212 (insert (format "Imported %d key(s).
213 \tArmor contains %d key(s) [%d bad, %d old].\n"
220 (if (zerop (aref status 9))
222 "\tSecret keys are imported.\n")))
223 (append-to-buffer pgg-output-buffer (point-min)(point-max))
224 (pgg-process-when-success)))
228 ;;; pgg-gpg.el ends here