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 default-enable-multibyte-characters
86 (buffer (generate-new-buffer " *pgg-gpg*"))
88 (with-current-buffer buffer
89 (make-local-variable 'pgg-gpg-read-point)
90 (setq pgg-gpg-read-point (point-min))
91 (make-local-variable 'pgg-gpg-output-file-name)
92 (setq pgg-gpg-output-file-name output-file-name)
93 (make-local-variable 'pgg-gpg-pending-status-list)
94 (setq pgg-gpg-pending-status-list nil)
95 (make-local-variable 'pgg-gpg-key-id)
96 (setq pgg-gpg-key-id nil)
97 (make-local-variable 'pgg-gpg-passphrase)
98 (setq pgg-gpg-passphrase nil))
101 (set-default-file-modes 448)
103 (apply #'start-process "pgg-gpg" buffer pgg-gpg-program args)))
104 (set-default-file-modes orig-mode))
105 (set-process-filter process #'pgg-gpg-process-filter)
106 (set-process-sentinel process #'pgg-gpg-process-sentinel)
109 (defun pgg-gpg-process-filter (process input)
113 (set-buffer (get-buffer-create " *pgg-gpg-debug*"))
114 (goto-char (point-max))
116 (set-buffer (process-buffer process))
117 (goto-char (point-max))
119 (goto-char pgg-gpg-read-point)
121 (while (looking-at ".*\n") ;the input line is finished
123 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\)\\>.*")
124 (let* ((status (match-string 1))
125 (symbol (intern-soft (concat "pgg-gpg-status-" status)))
126 (entry (member status pgg-gpg-pending-status-list)))
128 (setq pgg-gpg-pending-status-list
130 pgg-gpg-pending-status-list)))
133 (funcall symbol process (buffer-substring (match-beginning 1)
136 (setq pgg-gpg-read-point (point))))
138 (defun pgg-gpg-process-sentinel (process status)
139 (set-process-filter process nil)
141 ;; Copy the contents of process-buffer to pgg-errors-buffer.
142 (set-buffer (get-buffer-create pgg-errors-buffer))
143 (buffer-disable-undo)
145 (when (buffer-live-p (process-buffer process))
146 (insert-buffer-substring (process-buffer process))
147 (goto-char (point-min))
148 (delete-matching-lines "^\\[GNUPG:] ")
149 (goto-char (point-min))
150 (while (re-search-forward "^gpg: " nil t)
152 ;; Read the contents of the output file to pgg-output-buffer.
153 (set-buffer (get-buffer-create pgg-output-buffer))
154 (buffer-disable-undo)
156 (if (and (equal status "finished\n")
157 (buffer-live-p (process-buffer process)))
158 (let ((output-file-name (with-current-buffer (process-buffer process)
159 pgg-gpg-output-file-name)))
160 (when (file-exists-p output-file-name)
161 (let ((coding-system-for-read (if pgg-text-mode
164 (insert-file-contents output-file-name))
165 (delete-file output-file-name))))))
167 (defun pgg-gpg-wait-for-status (process status-list)
168 (with-current-buffer (process-buffer process)
169 (setq pgg-gpg-pending-status-list status-list)
170 (while (and (eq (process-status process) 'run)
171 pgg-gpg-pending-status-list)
172 (accept-process-output process 1))))
174 (defun pgg-gpg-wait-for-completion (process &optional status-list)
175 (process-send-eof process)
176 (while (eq (process-status process) 'run)
179 (set-buffer (process-buffer process))
180 (setq status-list (copy-sequence status-list))
181 (let ((pointer status-list))
183 (goto-char (point-min))
184 (unless (re-search-forward
185 (concat "^\\[GNUPG:] " (car pointer) "\\>")
187 (setq status-list (delq (car pointer) status-list)))
188 (setq pointer (cdr pointer))))
189 (kill-buffer (process-buffer process))
192 (defun pgg-gpg-status-USERID_HINT (process line)
193 (if (string-match "\\`USERID_HINT \\([^ ]+\\) \\(.*\\)" line)
194 (let* ((key-id (match-string 1 line))
195 (user-id (match-string 2 line))
196 (entry (assoc key-id pgg-gpg-user-id-alist)))
198 (setcdr entry user-id)
199 (setq pgg-gpg-user-id-alist (cons (cons key-id user-id)
200 pgg-gpg-user-id-alist))))))
202 (defun pgg-gpg-status-NEED_PASSPHRASE (process line)
203 (if (string-match "\\`NEED_PASSPHRASE \\([^ ]+\\)" line)
204 (setq pgg-gpg-key-id (match-string 1 line))))
206 (defun pgg-gpg-status-NEED_PASSPHRASE_SYM (process line)
207 (setq pgg-gpg-key-id 'SYM))
209 (defun pgg-gpg-status-NEED_PASSPHRASE_PIN (process line)
210 (setq pgg-gpg-key-id 'PIN))
212 (defun pgg-gpg-status-GET_HIDDEN (process line)
213 (let ((entry (assoc pgg-gpg-key-id pgg-gpg-user-id-alist)))
214 (if (setq pgg-gpg-passphrase
215 (if (eq pgg-gpg-key-id 'SYM)
217 "GnuPG passphrase for symmetric encryption: ")
219 (format "GnuPG passphrase for %s: "
223 (if (eq pgg-gpg-key-id 'PIN)
226 (process-send-string process (concat pgg-gpg-passphrase "\n")))))
228 (defun pgg-gpg-status-GOOD_PASSPHRASE (process line)
229 (when (and pgg-gpg-passphrase
230 (stringp pgg-gpg-key-id))
231 (pgg-add-passphrase-to-cache pgg-gpg-key-id pgg-gpg-passphrase)
232 (setq pgg-gpg-passphrase nil)))
234 (defun pgg-gpg-status-BAD_PASSPHRASE (process line)
235 (when pgg-gpg-passphrase
236 (fillarray pgg-gpg-passphrase 0)
237 (setq pgg-gpg-passphrase nil)))
239 (defun pgg-gpg-lookup-key (string &optional type)
240 "Search keys associated with STRING."
241 (let ((args (list "--with-colons" "--no-greeting" "--batch"
242 (if type "--list-secret-keys" "--list-keys")
245 (apply #'call-process pgg-gpg-program nil t nil args)
246 (goto-char (point-min))
247 (if (re-search-forward "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)"
249 (substring (match-string 2) 8)))))
251 (defun pgg-gpg-encrypt-region (start end recipients &optional sign passphrase)
252 "Encrypt the current region between START and END.
254 If optional argument SIGN is non-nil, do a combined sign and encrypt."
255 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
258 '("--armor" "--always-trust" "--encrypt")
259 (if pgg-text-mode '("--textmode"))
260 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
263 (mapcar (lambda (rcpt)
264 (list pgg-gpg-recipient-argument rcpt))
266 (if pgg-encrypt-for-me
267 (list pgg-gpg-user-id))))))))
268 (process (pgg-gpg-start-process args)))
269 (if (and sign (not pgg-gpg-use-agent))
270 (pgg-gpg-wait-for-status process '("GOOD_PASSPHRASE")))
271 (process-send-region process start end)
272 (pgg-gpg-wait-for-completion process '("SIG_CREATED" "END_ENCRYPTION"))))
274 (defun pgg-gpg-encrypt-symmetric-region (start end &optional passphrase)
275 "Encrypt the current region between START and END with symmetric cipher."
277 (append '("--armor" "--symmetric")
278 (if pgg-text-mode '("--textmode"))))
279 (process (pgg-gpg-start-process args)))
280 (pgg-gpg-wait-for-status process '("BEGIN_ENCRYPTION"))
281 (process-send-region process start end)
282 (pgg-gpg-wait-for-completion process '("END_ENCRYPTION"))))
284 (defun pgg-gpg-decrypt-region (start end &optional passphrase)
285 "Decrypt the current region between START and END."
286 (let* ((args '("--decrypt"))
287 (process (pgg-gpg-start-process args)))
288 (process-send-region process start end)
289 (pgg-gpg-wait-for-status process '("BEGIN_DECRYPTION"))
290 (pgg-gpg-wait-for-completion process '("GOODSIG" "DECRYPTION_OKAY"))))
292 (defun pgg-gpg-sign-region (start end &optional cleartext passphrase)
293 "Make detached signature from text between START and END."
294 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
296 (append (list (if cleartext "--clearsign" "--detach-sign")
297 "--armor" "--verbose"
298 "--local-user" pgg-gpg-user-id)
299 (if pgg-text-mode '("--textmode"))))
300 (process (pgg-gpg-start-process args)))
301 (unless pgg-gpg-use-agent
302 (pgg-gpg-wait-for-status process '("GOOD_PASSPHRASE")))
303 (process-send-region process start end)
304 (pgg-gpg-wait-for-completion process '("SIG_CREATED"))))
306 (defun pgg-gpg-verify-region (start end &optional signature)
307 "Verify region between START and END as the detached signature SIGNATURE."
308 (let ((args '("--verify"))
310 (when (stringp signature)
311 (setq args (append args (list signature))))
312 (setq process (pgg-gpg-start-process (append args '("-"))))
313 (process-send-region process start end)
314 (pgg-gpg-wait-for-completion process '("GOODSIG"))))
316 (defun pgg-gpg-insert-key ()
317 "Insert public key at point."
318 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
319 (args (list "--export" "--armor"
321 (process (pgg-gpg-start-process args)))
322 (pgg-gpg-wait-for-completion process)
323 (insert-buffer-substring pgg-output-buffer)))
325 (defun pgg-gpg-snarf-keys-region (start end)
326 "Add all public keys in region between START and END to the keyring."
327 (let* ((args '("--import" "-"))
328 (process (pgg-gpg-start-process args))
330 (process-send-region process start end)
331 (pgg-gpg-wait-for-completion process '("IMPORT_RES"))))
335 ;;; arch-tag: 2aa5d5d8-93a0-4865-9312-33e29830e000
336 ;;; pgg-gpg.el ends here