1 ;;; pgg-gpg.el --- GnuPG support for PGG.
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Symmetric encryption support added by: Sascha Wilde <wilde@sha-bang.de>
9 ;; Keywords: PGP, OpenPGP, GnuPG
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
37 (defcustom pgg-gpg-program "gpg"
38 "The GnuPG executable."
42 (defcustom pgg-gpg-extra-args nil
43 "Extra arguments for every GnuPG invocation."
45 :type '(repeat (string :tag "Argument")))
47 (defcustom pgg-gpg-recipient-argument "--recipient"
48 "GnuPG option to specify recipient."
50 :type '(choice (const :tag "New `--recipient' option" "--recipient")
51 (const :tag "Old `--remote-user' option" "--remote-user")))
53 (defcustom pgg-gpg-use-agent nil
54 "Whether to use gnupg agent for key caching."
58 (defvar pgg-gpg-user-id nil
59 "GnuPG ID of your default identity.")
61 (defvar pgg-gpg-user-id-alist nil
62 "An alist mapping from key ID to user ID.")
64 (defvar pgg-gpg-read-point nil)
65 (defvar pgg-gpg-output-file-name nil)
66 (defvar pgg-gpg-pending-status-list nil)
67 (defvar pgg-gpg-key-id nil)
68 (defvar pgg-gpg-passphrase nil)
69 (defvar pgg-gpg-debug nil)
71 (defun pgg-gpg-start-process (args)
72 (let* ((output-file-name (pgg-make-temp-file "pgg-output"))
74 (append (list "--no-tty"
78 "--output" output-file-name)
79 (if pgg-gpg-use-agent '("--use-agent"))
82 (coding-system-for-write 'binary)
83 (process-connection-type nil)
84 (orig-mode (default-file-modes))
85 (buffer (generate-new-buffer " *pgg-gpg*"))
87 (with-current-buffer buffer
88 (make-local-variable 'pgg-gpg-read-point)
89 (setq pgg-gpg-read-point (point-min))
90 (make-local-variable 'pgg-gpg-output-file-name)
91 (setq pgg-gpg-output-file-name output-file-name)
92 (make-local-variable 'pgg-gpg-pending-status-list)
93 (setq pgg-gpg-pending-status-list nil)
94 (make-local-variable 'pgg-gpg-key-id)
95 (setq pgg-gpg-key-id nil)
96 (make-local-variable 'pgg-gpg-passphrase)
97 (setq pgg-gpg-passphrase nil))
100 (set-default-file-modes 448)
102 (apply #'start-process "pgg-gpg" buffer pgg-gpg-program args)))
103 (set-default-file-modes orig-mode))
104 (set-process-filter process #'pgg-gpg-process-filter)
105 (set-process-sentinel process #'pgg-gpg-process-sentinel)
108 (defun pgg-gpg-process-filter (process input)
112 (set-buffer (get-buffer-create " *pgg-gpg-debug*"))
113 (goto-char (point-max))
115 (set-buffer (process-buffer process))
116 (goto-char (point-max))
118 (goto-char pgg-gpg-read-point)
120 (while (looking-at ".*\n") ;the input line is finished
122 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\)\\>.*")
123 (let* ((status (match-string 1))
124 (symbol (intern-soft (concat "pgg-gpg-status-" status)))
125 (entry (member status pgg-gpg-pending-status-list)))
127 (setq pgg-gpg-pending-status-list
129 pgg-gpg-pending-status-list)))
132 (funcall symbol process (buffer-substring (match-beginning 1)
135 (setq pgg-gpg-read-point (point))))
137 (defun pgg-gpg-process-sentinel (process status)
138 (set-process-filter process nil)
140 ;; Copy the contents of process-buffer to pgg-errors-buffer.
141 (set-buffer (get-buffer-create pgg-errors-buffer))
142 (buffer-disable-undo)
144 (when (buffer-live-p (process-buffer process))
145 (insert-buffer-substring (process-buffer process))
146 (goto-char (point-min))
147 (delete-matching-lines "^\\[GNUPG:] ")
148 (goto-char (point-min))
149 (while (re-search-forward "^gpg: " nil t)
151 ;; Read the contents of the output file to pgg-output-buffer.
152 (set-buffer (get-buffer-create pgg-output-buffer))
153 (buffer-disable-undo)
155 (if (and (equal status "finished\n")
156 (buffer-live-p (process-buffer process)))
157 (let ((output-file-name (with-current-buffer (process-buffer process)
158 pgg-gpg-output-file-name)))
159 (when (file-exists-p output-file-name)
160 (let ((coding-system-for-read (if pgg-text-mode
163 (insert-file-contents output-file-name))
164 (delete-file output-file-name))))))
166 (defun pgg-gpg-wait-for-status (process status-list)
167 (with-current-buffer (process-buffer process)
168 (setq pgg-gpg-pending-status-list status-list)
169 (while (and (eq (process-status process) 'run)
170 pgg-gpg-pending-status-list)
171 (accept-process-output process 1))))
173 (defun pgg-gpg-wait-for-completion (process &optional status-list)
174 (process-send-eof process)
175 (while (eq (process-status process) 'run)
178 (set-buffer (process-buffer process))
179 (setq status-list (copy-sequence status-list))
180 (let ((pointer status-list))
182 (goto-char (point-min))
183 (unless (re-search-forward
184 (concat "^\\[GNUPG:] " (car pointer) "\\>")
186 (setq status-list (delq (car pointer) status-list)))
187 (setq pointer (cdr pointer))))
188 (kill-buffer (process-buffer process))
191 (defun pgg-gpg-status-USERID_HINT (process line)
192 (if (string-match "\\`USERID_HINT \\([^ ]+\\) \\(.*\\)" line)
193 (let* ((key-id (match-string 1 line))
194 (user-id (match-string 2 line))
195 (entry (assoc key-id pgg-gpg-user-id-alist)))
197 (setcdr entry user-id)
198 (setq pgg-gpg-user-id-alist (cons (cons key-id user-id)
199 pgg-gpg-user-id-alist))))))
201 (defun pgg-gpg-status-NEED_PASSPHRASE (process line)
202 (if (string-match "\\`NEED_PASSPHRASE \\([^ ]+\\)" line)
203 (setq pgg-gpg-key-id (match-string 1 line))))
205 (defun pgg-gpg-status-NEED_PASSPHRASE_SYM (process line)
206 (setq pgg-gpg-key-id 'SYM))
208 (defun pgg-gpg-status-NEED_PASSPHRASE_PIN (process line)
209 (setq pgg-gpg-key-id 'PIN))
211 (defun pgg-gpg-status-GET_HIDDEN (process line)
212 (let ((entry (assoc pgg-gpg-key-id pgg-gpg-user-id-alist)))
213 (if (setq pgg-gpg-passphrase
214 (if (eq pgg-gpg-key-id 'SYM)
216 "GnuPG passphrase for symmetric encryption: ")
218 (format "GnuPG passphrase for %s: "
222 (if (eq pgg-gpg-key-id 'PIN)
225 (process-send-string process (concat pgg-gpg-passphrase "\n")))))
227 (defun pgg-gpg-status-GOOD_PASSPHRASE (process line)
228 (when (and pgg-gpg-passphrase
229 (stringp pgg-gpg-key-id))
230 (pgg-add-passphrase-to-cache pgg-gpg-key-id pgg-gpg-passphrase)
231 (setq pgg-gpg-passphrase nil)))
233 (defun pgg-gpg-status-BAD_PASSPHRASE (process line)
234 (when pgg-gpg-passphrase
235 (fillarray pgg-gpg-passphrase 0)
236 (setq pgg-gpg-passphrase nil)))
238 (defun pgg-gpg-lookup-key (string &optional type)
239 "Search keys associated with STRING."
240 (let ((args (list "--with-colons" "--no-greeting" "--batch"
241 (if type "--list-secret-keys" "--list-keys")
244 (apply #'call-process pgg-gpg-program nil t nil args)
245 (goto-char (point-min))
246 (if (re-search-forward "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)"
248 (substring (match-string 2) 8)))))
250 (defun pgg-gpg-encrypt-region (start end recipients &optional sign passphrase)
251 "Encrypt the current region between START and END.
253 If optional argument SIGN is non-nil, do a combined sign and encrypt."
254 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
257 '("--armor" "--always-trust" "--encrypt")
258 (if pgg-text-mode '("--textmode"))
259 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
262 (mapcar (lambda (rcpt)
263 (list pgg-gpg-recipient-argument rcpt))
265 (if pgg-encrypt-for-me
266 (list pgg-gpg-user-id))))))))
267 (process (pgg-gpg-start-process args)))
268 (if (and sign (not pgg-gpg-use-agent))
269 (pgg-gpg-wait-for-status process '("GOOD_PASSPHRASE")))
270 (process-send-region process start end)
271 (pgg-gpg-wait-for-completion process '("SIG_CREATED" "END_ENCRYPTION"))))
273 (defun pgg-gpg-encrypt-symmetric-region (start end &optional passphrase)
274 "Encrypt the current region between START and END with symmetric cipher."
276 (append '("--armor" "--symmetric")
277 (if pgg-text-mode '("--textmode"))))
278 (process (pgg-gpg-start-process args)))
279 (pgg-gpg-wait-for-status process '("BEGIN_ENCRYPTION"))
280 (process-send-region process start end)
281 (pgg-gpg-wait-for-completion process '("END_ENCRYPTION"))))
283 (defun pgg-gpg-decrypt-region (start end &optional passphrase)
284 "Decrypt the current region between START and END."
285 (let* ((args '("--decrypt"))
286 (process (pgg-gpg-start-process args)))
287 (process-send-region process start end)
288 (pgg-gpg-wait-for-status process '("BEGIN_DECRYPTION"))
289 (pgg-gpg-wait-for-completion process '("GOODSIG" "DECRYPTION_OKAY"))))
291 (defun pgg-gpg-sign-region (start end &optional cleartext passphrase)
292 "Make detached signature from text between START and END."
293 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
295 (append (list (if cleartext "--clearsign" "--detach-sign")
296 "--armor" "--verbose"
297 "--local-user" pgg-gpg-user-id)
298 (if pgg-text-mode '("--textmode"))))
299 (process (pgg-gpg-start-process args)))
300 (unless pgg-gpg-use-agent
301 (pgg-gpg-wait-for-status process '("GOOD_PASSPHRASE")))
302 (process-send-region process start end)
303 (pgg-gpg-wait-for-completion process '("SIG_CREATED"))))
305 (defun pgg-gpg-verify-region (start end &optional signature)
306 "Verify region between START and END as the detached signature SIGNATURE."
307 (let ((args '("--verify"))
309 (when (stringp signature)
310 (setq args (append args (list signature))))
311 (setq process (pgg-gpg-start-process (append args '("-"))))
312 (process-send-region process start end)
313 (pgg-gpg-wait-for-completion process '("GOODSIG"))))
315 (defun pgg-gpg-insert-key ()
316 "Insert public key at point."
317 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
318 (args (list "--export" "--armor"
320 (process (pgg-gpg-start-process args)))
321 (pgg-gpg-wait-for-completion process)
322 (insert-buffer-substring pgg-output-buffer)))
324 (defun pgg-gpg-snarf-keys-region (start end)
325 "Add all public keys in region between START and END to the keyring."
326 (let* ((args '("--import" "-"))
327 (process (pgg-gpg-start-process args))
329 (process-send-region process start end)
330 (pgg-gpg-wait-for-completion process '("IMPORT_RES"))))
334 ;;; arch-tag: 2aa5d5d8-93a0-4865-9312-33e29830e000
335 ;;; pgg-gpg.el ends here