(store_next_glyph): Renamed from append_glyph.
[bpt/emacs.git] / lisp / pgg-gpg.el
CommitLineData
23f87bed
MB
1;;; pgg-gpg.el --- GnuPG support for PGG.
2
e84b4b86 3;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
aaef169d 4;; 2005, 2006 Free Software Foundation, Inc.
23f87bed
MB
5
6;; Author: Daiki Ueno <ueno@unixuser.org>
df570e6f 7;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de>
23f87bed
MB
8;; Created: 1999/10/28
9;; Keywords: PGP, OpenPGP, GnuPG
10
11;; This file is part of GNU Emacs.
12
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)
16;; any later version.
17
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.
22
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
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
23f87bed
MB
27
28;;; Code:
29
30(eval-when-compile
31 (require 'cl) ; for gpg macros
32 (require 'pgg))
33
34(defgroup pgg-gpg ()
4a836a63 35 "GnuPG interface."
23f87bed
MB
36 :group 'pgg)
37
38(defcustom pgg-gpg-program "gpg"
39 "The GnuPG executable."
40 :group 'pgg-gpg
41 :type 'string)
42
43(defcustom pgg-gpg-extra-args nil
44 "Extra arguments for every GnuPG invocation."
45 :group 'pgg-gpg
46 :type '(repeat (string :tag "Argument")))
47
48(defcustom pgg-gpg-recipient-argument "--recipient"
49 "GnuPG option to specify recipient."
50 :group 'pgg-gpg
51 :type '(choice (const :tag "New `--recipient' option" "--recipient")
52 (const :tag "Old `--remote-user' option" "--remote-user")))
53
e563e53b
SJ
54(defcustom pgg-gpg-use-agent (if (getenv "GPG_AGENT_INFO") t nil)
55 "Whether to use gnupg agent for key caching.
56By default, it will be enabled iff the environment variable
57\"GPG_AGENT_INFO\" is set."
58 :group 'pgg-gpg
59 :type 'boolean)
60
23f87bed
MB
61(defvar pgg-gpg-user-id nil
62 "GnuPG ID of your default identity.")
63
64(defun pgg-gpg-process-region (start end passphrase program args)
65 (let* ((output-file-name (pgg-make-temp-file "pgg-output"))
66 (args
67 `("--status-fd" "2"
e563e53b
SJ
68 ,@(if pgg-gpg-use-agent '("--use-agent")
69 (if passphrase '("--passphrase-fd" "0")))
23f87bed
MB
70 "--yes" ; overwrite
71 "--output" ,output-file-name
72 ,@pgg-gpg-extra-args ,@args))
73 (output-buffer pgg-output-buffer)
74 (errors-buffer pgg-errors-buffer)
75 (orig-mode (default-file-modes))
76 (process-connection-type nil)
77 exit-status)
78 (with-current-buffer (get-buffer-create errors-buffer)
79 (buffer-disable-undo)
80 (erase-buffer))
81 (unwind-protect
82 (progn
83 (set-default-file-modes 448)
84 (let ((coding-system-for-write 'binary)
85 (input (buffer-substring-no-properties start end))
86 (default-enable-multibyte-characters nil))
87 (with-temp-buffer
88 (when passphrase
89 (insert passphrase "\n"))
90 (insert input)
91 (setq exit-status
92 (apply #'call-process-region (point-min) (point-max) program
93 nil errors-buffer nil args))))
94 (with-current-buffer (get-buffer-create output-buffer)
95 (buffer-disable-undo)
96 (erase-buffer)
97 (if (file-exists-p output-file-name)
34128042
MB
98 (let ((coding-system-for-read (if pgg-text-mode
99 'raw-text
100 'binary)))
23f87bed
MB
101 (insert-file-contents output-file-name)))
102 (set-buffer errors-buffer)
103 (if (not (equal exit-status 0))
104 (insert (format "\n%s exited abnormally: '%s'\n"
105 program exit-status)))))
106 (if (file-exists-p output-file-name)
107 (delete-file output-file-name))
108 (set-default-file-modes orig-mode))))
109
df570e6f 110(defun pgg-gpg-possibly-cache-passphrase (passphrase &optional key notruncate)
e563e53b
SJ
111 (if (and passphrase
112 pgg-cache-passphrase
23f87bed
MB
113 (progn
114 (goto-char (point-min))
82259e50 115 (re-search-forward "^\\[GNUPG:] \\(GOOD_PASSPHRASE\\>\\)\\|\\(SIG_CREATED\\)" nil t)))
df570e6f 116 (pgg-add-passphrase-to-cache
23f87bed
MB
117 (or key
118 (progn
119 (goto-char (point-min))
120 (if (re-search-forward
82259e50 121 "^\\[GNUPG:] NEED_PASSPHRASE\\(_PIN\\)? \\w+ ?\\w*" nil t)
23f87bed 122 (substring (match-string 0) -8))))
df570e6f
EZ
123 passphrase
124 notruncate)))
23f87bed
MB
125
126(defvar pgg-gpg-all-secret-keys 'unknown)
127
128(defun pgg-gpg-lookup-all-secret-keys ()
129 "Return all secret keys present in secret key ring."
130 (when (eq pgg-gpg-all-secret-keys 'unknown)
131 (setq pgg-gpg-all-secret-keys '())
132 (let ((args (list "--with-colons" "--no-greeting" "--batch"
133 "--list-secret-keys")))
134 (with-temp-buffer
135 (apply #'call-process pgg-gpg-program nil t nil args)
136 (goto-char (point-min))
137 (while (re-search-forward
138 "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)" nil t)
139 (push (substring (match-string 2) 8)
140 pgg-gpg-all-secret-keys)))))
141 pgg-gpg-all-secret-keys)
142
143(defun pgg-gpg-lookup-key (string &optional type)
144 "Search keys associated with STRING."
145 (let ((args (list "--with-colons" "--no-greeting" "--batch"
146 (if type "--list-secret-keys" "--list-keys")
147 string)))
148 (with-temp-buffer
149 (apply #'call-process pgg-gpg-program nil t nil args)
150 (goto-char (point-min))
151 (if (re-search-forward "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)"
152 nil t)
153 (substring (match-string 2) 8)))))
154
df570e6f
EZ
155(defun pgg-gpg-lookup-key-owner (string &optional all)
156 "Search keys associated with STRING and return owner of identified key.
157
158The value may be just the bare key id, or it may be a combination of the
159user name associated with the key and the key id, with the key id enclosed
160in \"<...>\" angle brackets.
161
162Optional ALL non-nil means search all keys, including secret keys."
163 (let ((args (list "--with-colons" "--no-greeting" "--batch"
164 (if all "--list-secret-keys" "--list-keys")
165 string))
7b97a7a5
RS
166 (key-regexp (concat "^\\(sec\\|pub\\)"
167 ":[^:]*:[^:]*:[^:]*:\\([^:]*\\):[^:]*"
168 ":[^:]*:[^:]*:[^:]*:\\([^:]*\\):")))
df570e6f
EZ
169 (with-temp-buffer
170 (apply #'call-process pgg-gpg-program nil t nil args)
171 (goto-char (point-min))
172 (if (re-search-forward key-regexp
7b97a7a5
RS
173 nil t)
174 (match-string 3)))))
df570e6f
EZ
175
176(defun pgg-gpg-key-id-from-key-owner (key-owner)
177 (cond ((not key-owner) nil)
7b97a7a5
RS
178 ;; Extract bare key id from outermost paired angle brackets, if any:
179 ((string-match "[^<]*<\\(.+\\)>[^>]*" key-owner)
180 (substring key-owner (match-beginning 1)(match-end 1)))
181 (key-owner)))
df570e6f
EZ
182
183(defun pgg-gpg-encrypt-region (start end recipients &optional sign passphrase)
23f87bed 184 "Encrypt the current region between START and END.
df570e6f
EZ
185
186If optional argument SIGN is non-nil, do a combined sign and encrypt.
187
188If optional PASSPHRASE is not specified, it will be obtained from the
189passphrase cache or user."
23f87bed 190 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
df570e6f 191 (passphrase (or passphrase
e563e53b
SJ
192 (when (and sign (not pgg-gpg-use-agent))
193 (pgg-read-passphrase
194 (format "GnuPG passphrase for %s: "
195 pgg-gpg-user-id)
196 pgg-gpg-user-id))))
23f87bed
MB
197 (args
198 (append
34128042
MB
199 (list "--batch" "--armor" "--always-trust" "--encrypt")
200 (if pgg-text-mode (list "--textmode"))
23f87bed
MB
201 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
202 (if recipients
203 (apply #'nconc
204 (mapcar (lambda (rcpt)
205 (list pgg-gpg-recipient-argument rcpt))
206 (append recipients
207 (if pgg-encrypt-for-me
208 (list pgg-gpg-user-id)))))))))
34128042 209 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
23f87bed
MB
210 (when sign
211 (with-current-buffer pgg-errors-buffer
212 ;; Possibly cache passphrase under, e.g. "jas", for future sign.
213 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id)
214 ;; Possibly cache passphrase under, e.g. B565716F, for future decrypt.
215 (pgg-gpg-possibly-cache-passphrase passphrase)))
216 (pgg-process-when-success)))
217
df570e6f
EZ
218(defun pgg-gpg-encrypt-symmetric-region (start end &optional passphrase)
219 "Encrypt the current region between START and END with symmetric cipher.
220
221If optional PASSPHRASE is not specified, it will be obtained from the
222passphrase cache or user."
223 (let* ((passphrase (or passphrase
e563e53b
SJ
224 (when (not pgg-gpg-use-agent)
225 (pgg-read-passphrase
226 "GnuPG passphrase for symmetric encryption: "))))
df570e6f 227 (args
34128042
MB
228 (append (list "--batch" "--armor" "--symmetric" )
229 (if pgg-text-mode (list "--textmode")))))
230 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
df570e6f
EZ
231 (pgg-process-when-success)))
232
233(defun pgg-gpg-decrypt-region (start end &optional passphrase)
234 "Decrypt the current region between START and END.
235
236If optional PASSPHRASE is not specified, it will be obtained from the
237passphrase cache or user."
23f87bed
MB
238 (let* ((current-buffer (current-buffer))
239 (message-keys (with-temp-buffer
240 (insert-buffer-substring current-buffer)
241 (pgg-decode-armor-region (point-min) (point-max))))
242 (secret-keys (pgg-gpg-lookup-all-secret-keys))
7b97a7a5
RS
243 ;; XXX the user is stuck if they need to use the passphrase for
244 ;; any but the first secret key for which the message is
245 ;; encrypted. ideally, we would incrementally give them a
246 ;; chance with subsequent keys each time they fail with one.
23f87bed 247 (key (pgg-gpg-select-matching-key message-keys secret-keys))
7b97a7a5 248 (key-owner (and key (pgg-gpg-lookup-key-owner key t)))
df570e6f
EZ
249 (key-id (pgg-gpg-key-id-from-key-owner key-owner))
250 (pgg-gpg-user-id (or key-id key
7b97a7a5 251 pgg-gpg-user-id pgg-default-user-id))
df570e6f 252 (passphrase (or passphrase
e563e53b
SJ
253 (when (not pgg-gpg-use-agent)
254 (pgg-read-passphrase
255 (format (if (pgg-gpg-symmetric-key-p message-keys)
256 "Passphrase for symmetric decryption: "
257 "GnuPG passphrase for %s: ")
258 (or key-owner "??"))
259 pgg-gpg-user-id))))
23f87bed
MB
260 (args '("--batch" "--decrypt")))
261 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
262 (with-current-buffer pgg-errors-buffer
263 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id)
264 (goto-char (point-min))
265 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
266
df570e6f
EZ
267;;;###autoload
268(defun pgg-gpg-symmetric-key-p (message-keys)
269 "True if decoded armor MESSAGE-KEYS has symmetric encryption indicator."
270 (let (result)
271 (dolist (key message-keys result)
272 (when (and (eq (car key) 3)
273 (member '(symmetric-key-algorithm) key))
274 (setq result key)))))
275
23f87bed
MB
276(defun pgg-gpg-select-matching-key (message-keys secret-keys)
277 "Choose a key from MESSAGE-KEYS that matches one of the keys in SECRET-KEYS."
278 (loop for message-key in message-keys
279 for message-key-id = (and (equal (car message-key) 1)
df570e6f 280 (cdr (assq 'key-identifier
7b97a7a5 281 (cdr message-key))))
23f87bed
MB
282 for key = (and message-key-id (pgg-lookup-key message-key-id 'encrypt))
283 when (and key (member key secret-keys)) return key))
284
df570e6f 285(defun pgg-gpg-sign-region (start end &optional cleartext passphrase)
23f87bed
MB
286 "Make detached signature from text between START and END."
287 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
df570e6f 288 (passphrase (or passphrase
e563e53b
SJ
289 (when (not pgg-gpg-use-agent)
290 (pgg-read-passphrase
291 (format "GnuPG passphrase for %s: "
292 pgg-gpg-user-id)
293 pgg-gpg-user-id))))
23f87bed 294 (args
34128042
MB
295 (append (list (if cleartext "--clearsign" "--detach-sign")
296 "--armor" "--batch" "--verbose"
297 "--local-user" pgg-gpg-user-id)
298 (if pgg-text-mode (list "--textmode"))))
23f87bed
MB
299 (inhibit-read-only t)
300 buffer-read-only)
34128042 301 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
23f87bed
MB
302 (with-current-buffer pgg-errors-buffer
303 ;; Possibly cache passphrase under, e.g. "jas", for future sign.
304 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id)
305 ;; Possibly cache passphrase under, e.g. B565716F, for future decrypt.
306 (pgg-gpg-possibly-cache-passphrase passphrase))
307 (pgg-process-when-success)))
308
309(defun pgg-gpg-verify-region (start end &optional signature)
310 "Verify region between START and END as the detached signature SIGNATURE."
311 (let ((args '("--batch" "--verify")))
312 (when (stringp signature)
313 (setq args (append args (list signature))))
314 (setq args (append args '("-")))
315 (pgg-gpg-process-region start end nil pgg-gpg-program args)
316 (with-current-buffer pgg-errors-buffer
317 (goto-char (point-min))
318 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
319 (with-current-buffer pgg-output-buffer
320 (insert-buffer-substring pgg-errors-buffer
321 (match-beginning 1) (match-end 0)))
322 (delete-region (match-beginning 0) (match-end 0)))
323 (goto-char (point-min))
324 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
325
326(defun pgg-gpg-insert-key ()
327 "Insert public key at point."
328 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
329 (args (list "--batch" "--export" "--armor"
330 pgg-gpg-user-id)))
331 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
332 (insert-buffer-substring pgg-output-buffer)))
333
334(defun pgg-gpg-snarf-keys-region (start end)
335 "Add all public keys in region between START and END to the keyring."
336 (let ((args '("--import" "--batch" "-")) status)
337 (pgg-gpg-process-region start end nil pgg-gpg-program args)
338 (set-buffer pgg-errors-buffer)
339 (goto-char (point-min))
340 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
341 (setq status (buffer-substring (match-end 0)
342 (progn (end-of-line)(point)))
e9bd5782 343 status (vconcat (mapcar #'string-to-number (split-string status))))
23f87bed
MB
344 (erase-buffer)
345 (insert (format "Imported %d key(s).
346\tArmor contains %d key(s) [%d bad, %d old].\n"
347 (+ (aref status 2)
348 (aref status 10))
349 (aref status 0)
350 (aref status 1)
351 (+ (aref status 4)
352 (aref status 11)))
353 (if (zerop (aref status 9))
354 ""
355 "\tSecret keys are imported.\n")))
356 (append-to-buffer pgg-output-buffer (point-min)(point-max))
357 (pgg-process-when-success)))
358
359(provide 'pgg-gpg)
360
361;;; arch-tag: 2aa5d5d8-93a0-4865-9312-33e29830e000
362;;; pgg-gpg.el ends here