1 ;;; pgg-gpg.el --- GnuPG support for PGG.
3 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, OpenPGP, GnuPG
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU 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.
29 (require 'cl) ; for gpg macros
36 (defcustom pgg-gpg-program "gpg"
37 "The GnuPG executable."
41 (defcustom pgg-gpg-extra-args nil
42 "Extra arguments for every GnuPG invocation."
44 :type '(repeat (string :tag "Argument")))
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 (pgg-make-temp-file "pgg-output"))
53 ,@(if passphrase '("--passphrase-fd" "0"))
55 "--output" ,output-file-name
56 ,@pgg-gpg-extra-args ,@args))
57 (output-buffer pgg-output-buffer)
58 (errors-buffer pgg-errors-buffer)
59 (orig-mode (default-file-modes))
60 (process-connection-type nil)
62 (with-current-buffer (get-buffer-create errors-buffer)
67 (set-default-file-modes 448)
68 (let ((coding-system-for-write 'binary)
69 (input (buffer-substring-no-properties start end))
70 (default-enable-multibyte-characters nil))
73 (insert passphrase "\n"))
76 (apply #'call-process-region (point-min) (point-max) program
77 nil errors-buffer nil args))))
78 (with-current-buffer (get-buffer-create output-buffer)
81 (if (file-exists-p output-file-name)
82 (let ((coding-system-for-read 'raw-text-dos))
83 (insert-file-contents output-file-name)))
84 (set-buffer errors-buffer)
85 (if (not (equal exit-status 0))
86 (insert (format "\n%s exited abnormally: '%s'\n"
87 program exit-status)))))
88 (if (file-exists-p output-file-name)
89 (delete-file output-file-name))
90 (set-default-file-modes orig-mode))))
92 (defun pgg-gpg-possibly-cache-passphrase (passphrase &optional key)
93 (if (and pgg-cache-passphrase
95 (goto-char (point-min))
96 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
97 (pgg-add-passphrase-cache
100 (goto-char (point-min))
101 (if (re-search-forward
102 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
103 (substring (match-string 0) -8))))
106 (defvar pgg-gpg-all-secret-keys 'unknown)
108 (defun pgg-gpg-lookup-all-secret-keys ()
109 "Return all secret keys present in secret key ring."
110 (when (eq pgg-gpg-all-secret-keys 'unknown)
111 (setq pgg-gpg-all-secret-keys '())
112 (let ((args (list "--with-colons" "--no-greeting" "--batch"
113 "--list-secret-keys")))
115 (apply #'call-process pgg-gpg-program nil t nil args)
116 (goto-char (point-min))
117 (while (re-search-forward "^\\(sec\\|pub\\):" nil t)
120 (buffer-substring (match-end 0)
121 (progn (end-of-line) (point)))
123 pgg-gpg-all-secret-keys)))))
124 pgg-gpg-all-secret-keys)
126 (defun pgg-gpg-lookup-key (string &optional type)
127 "Search keys associated with STRING."
128 (let ((args (list "--with-colons" "--no-greeting" "--batch"
129 (if type "--list-secret-keys" "--list-keys")
132 (apply #'call-process pgg-gpg-program nil t nil args)
133 (goto-char (point-min))
134 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
137 (buffer-substring (match-end 0)
138 (progn (end-of-line)(point)))
141 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
142 "Encrypt the current region between START and END.
143 If optional argument SIGN is non-nil, do a combined sign and encrypt."
144 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
148 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
152 (list "--batch" "--armor" "--always-trust" "--encrypt")
153 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
156 (mapcar (lambda (rcpt)
157 (list "--remote-user" rcpt))
159 (if pgg-encrypt-for-me
160 (list pgg-gpg-user-id)))))))))
161 (pgg-as-lbt start end 'CRLF
162 (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 pgg-gpg-user-id)))
166 (pgg-process-when-success)))
168 (defun pgg-gpg-decrypt-region (start end)
169 "Decrypt the current region between START and END."
170 (let* ((current-buffer (current-buffer))
171 (message-keys (with-temp-buffer
172 (insert-buffer-substring current-buffer)
173 (pgg-decode-armor-region (point-min) (point-max))))
174 (secret-keys (pgg-gpg-lookup-all-secret-keys))
175 (key (pgg-gpg-select-matching-key message-keys secret-keys))
176 (pgg-gpg-user-id (or key pgg-gpg-user-id pgg-default-user-id))
179 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
181 (args '("--batch" "--decrypt")))
182 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
183 (with-current-buffer pgg-errors-buffer
184 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id)
185 (goto-char (point-min))
186 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
188 (defun pgg-gpg-select-matching-key (message-keys secret-keys)
189 "Choose a key from MESSAGE-KEYS that matches one of the keys in SECRET-KEYS."
190 (loop for message-key in message-keys
191 for message-key-id = (and (equal (car message-key) 1)
192 (cdr (assq 'key-identifier message-key)))
193 for key = (and message-key-id (pgg-lookup-key message-key-id 'encrypt))
194 when (and key (member key secret-keys)) return key))
196 (defun pgg-gpg-sign-region (start end &optional cleartext)
197 "Make detached signature from text between START and END."
198 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
201 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
204 (list (if cleartext "--clearsign" "--detach-sign")
205 "--armor" "--batch" "--verbose"
206 "--local-user" pgg-gpg-user-id))
207 (inhibit-read-only t)
209 (pgg-as-lbt start end 'CRLF
210 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
211 (with-current-buffer pgg-errors-buffer
212 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id))
213 (pgg-process-when-success)))
215 (defun pgg-gpg-verify-region (start end &optional signature)
216 "Verify region between START and END as the detached signature SIGNATURE."
217 (let ((args '("--batch" "--verify")))
218 (when (stringp signature)
219 (setq args (append args (list signature))))
220 (setq args (append args '("-")))
221 (pgg-gpg-process-region start end nil pgg-gpg-program args)
222 (with-current-buffer pgg-errors-buffer
223 (goto-char (point-min))
224 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
225 (with-current-buffer pgg-output-buffer
226 (insert-buffer-substring pgg-errors-buffer
227 (match-beginning 1) (match-end 0)))
228 (delete-region (match-beginning 0) (match-end 0)))
229 (goto-char (point-min))
230 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
232 (defun pgg-gpg-insert-key ()
233 "Insert public key at point."
234 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
235 (args (list "--batch" "--export" "--armor"
237 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
238 (insert-buffer-substring pgg-output-buffer)))
240 (defun pgg-gpg-snarf-keys-region (start end)
241 "Add all public keys in region between START and END to the keyring."
242 (let ((args '("--import" "--batch" "-")) status)
243 (pgg-gpg-process-region start end nil pgg-gpg-program args)
244 (set-buffer pgg-errors-buffer)
245 (goto-char (point-min))
246 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
247 (setq status (buffer-substring (match-end 0)
248 (progn (end-of-line)(point)))
249 status (vconcat (mapcar #'string-to-int (split-string status))))
251 (insert (format "Imported %d key(s).
252 \tArmor contains %d key(s) [%d bad, %d old].\n"
259 (if (zerop (aref status 9))
261 "\tSecret keys are imported.\n")))
262 (append-to-buffer pgg-output-buffer (point-min)(point-max))
263 (pgg-process-when-success)))
267 ;;; pgg-gpg.el ends here