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."
43 (const :tag "None" nil)
44 (string :tag "Arguments")))
46 (defvar pgg-gpg-user-id nil
47 "GnuPG ID of your default identity.")
49 (defun pgg-gpg-process-region (start end passphrase program args)
50 (let* ((output-file-name
51 (expand-file-name (make-temp-name "pgg-output")
52 pgg-temporary-file-directory))
55 ,@(if passphrase '("--passphrase-fd" "0"))
56 "--output" ,output-file-name
57 ,@pgg-gpg-extra-args ,@args))
58 (output-buffer pgg-output-buffer)
59 (errors-buffer pgg-errors-buffer)
60 (orig-mode (default-file-modes))
61 (process-connection-type nil)
62 process status exit-status)
63 (with-current-buffer (get-buffer-create errors-buffer)
68 (set-default-file-modes 448)
69 (let ((coding-system-for-write 'binary))
71 (apply #'start-process "*GnuPG*" errors-buffer
73 (set-process-sentinel process #'ignore)
75 (process-send-string process (concat passphrase "\n")))
76 (process-send-region process start end)
77 (process-send-eof process)
78 (while (eq 'run (process-status process))
79 (accept-process-output process 5))
80 (setq status (process-status process)
81 exit-status (process-exit-status process))
82 (delete-process process)
83 (with-current-buffer (get-buffer-create output-buffer)
86 (if (file-exists-p output-file-name)
87 (let ((coding-system-for-read 'raw-text-dos))
88 (insert-file-contents output-file-name)))
89 (set-buffer errors-buffer)
90 (if (memq status '(stop signal))
91 (error "%s exited abnormally: '%s'" program exit-status))
92 (if (= 127 exit-status)
93 (error "%s could not be found" program))))
94 (if (and process (eq 'run (process-status process)))
95 (interrupt-process process))
96 (if (file-exists-p output-file-name)
97 (delete-file output-file-name))
98 (set-default-file-modes orig-mode))))
100 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
101 (if (and pgg-cache-passphrase
103 (goto-char (point-min))
104 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
105 (pgg-add-passphrase-cache
107 (goto-char (point-min))
108 (if (re-search-forward
109 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
110 (substring (match-string 0) -8)))
113 (defun pgg-gpg-lookup-key (string &optional type)
114 "Search keys associated with STRING."
115 (let ((args (list "--with-colons" "--no-greeting" "--batch"
116 (if type "--list-secret-keys" "--list-keys")
119 (apply #'call-process pgg-gpg-program nil t nil args)
120 (goto-char (point-min))
121 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
124 (buffer-substring (match-end 0)
125 (progn (end-of-line)(point)))
128 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
129 "Encrypt the current region between START and END.
130 If optional argument SIGN is non-nil, do a combined sign and encrypt."
131 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
135 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
136 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
139 (list "--batch" "--armor" "--always-trust" "--encrypt")
140 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
143 (mapcar (lambda (rcpt)
144 (list "--remote-user" rcpt))
146 (if pgg-encrypt-for-me
147 (list pgg-gpg-user-id)))))))))
148 (pgg-as-lbt start end 'CRLF
149 (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 (pgg-process-when-success)))
155 (defun pgg-gpg-decrypt-region (start end)
156 "Decrypt the current region between START and END."
157 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
160 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
161 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
162 (args '("--batch" "--decrypt")))
163 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
164 (with-current-buffer pgg-errors-buffer
165 (pgg-gpg-possibly-cache-passphrase passphrase)
166 (goto-char (point-min))
167 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
169 (defun pgg-gpg-sign-region (start end &optional cleartext)
170 "Make detached signature from text between START and END."
171 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
174 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
175 (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
177 (list (if cleartext "--clearsign" "--detach-sign")
178 "--armor" "--batch" "--verbose"
179 "--local-user" pgg-gpg-user-id))
180 (inhibit-read-only t)
182 (pgg-as-lbt start end 'CRLF
183 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
184 (with-current-buffer pgg-errors-buffer
185 (pgg-gpg-possibly-cache-passphrase passphrase))
186 (pgg-process-when-success)))
188 (defun pgg-gpg-verify-region (start end &optional signature)
189 "Verify region between START and END as the detached signature SIGNATURE."
190 (let ((args '("--batch" "--verify")))
191 (when (stringp signature)
192 (setq args (append args (list signature))))
193 (setq args (append args '("-")))
194 (pgg-gpg-process-region start end nil pgg-gpg-program args)
195 (with-current-buffer pgg-errors-buffer
196 (goto-char (point-min))
197 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
198 (with-current-buffer pgg-output-buffer
199 (insert-buffer-substring pgg-errors-buffer
200 (match-beginning 1) (match-end 0)))
201 (delete-region (match-beginning 0) (match-end 0)))
202 (goto-char (point-min))
203 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
205 (defun pgg-gpg-insert-key ()
206 "Insert public key at point."
207 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
208 (args (list "--batch" "--export" "--armor"
210 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
211 (insert-buffer-substring pgg-output-buffer)))
213 (defun pgg-gpg-snarf-keys-region (start end)
214 "Add all public keys in region between START and END to the keyring."
215 (let ((args '("--import" "--batch" "-")) status)
216 (pgg-gpg-process-region start end nil pgg-gpg-program args)
217 (set-buffer pgg-errors-buffer)
218 (goto-char (point-min))
219 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
220 (setq status (buffer-substring (match-end 0)
221 (progn (end-of-line)(point)))
222 status (vconcat (mapcar #'string-to-int (split-string status))))
224 (insert (format "Imported %d key(s).
225 \tArmor contains %d key(s) [%d bad, %d old].\n"
232 (if (zerop (aref status 9))
234 "\tSecret keys are imported.\n")))
235 (append-to-buffer pgg-output-buffer (point-min)(point-max))
236 (pgg-process-when-success)))
240 ;;; pgg-gpg.el ends here