1 ;;; pgg-gpg.el --- GnuPG support for PGG.
3 ;; Copyright (C) 1999, 2000, 2003, 2004 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 (defcustom pgg-gpg-recipient-argument
"--recipient"
47 "GnuPG option to specify recipient."
49 :type
'(choice (const :tag
"New `--recipient' option" "--recipient")
50 (const :tag
"Old `--remote-user' option" "--remote-user")))
52 (defvar pgg-gpg-user-id nil
53 "GnuPG ID of your default identity.")
55 (defun pgg-gpg-process-region (start end passphrase program args
)
56 (let* ((output-file-name (pgg-make-temp-file "pgg-output"))
59 ,@(if passphrase
'("--passphrase-fd" "0"))
61 "--output" ,output-file-name
62 ,@pgg-gpg-extra-args
,@args
))
63 (output-buffer pgg-output-buffer
)
64 (errors-buffer pgg-errors-buffer
)
65 (orig-mode (default-file-modes))
66 (process-connection-type nil
)
68 (with-current-buffer (get-buffer-create errors-buffer
)
73 (set-default-file-modes 448)
74 (let ((coding-system-for-write 'binary
)
75 (input (buffer-substring-no-properties start end
))
76 (default-enable-multibyte-characters nil
))
79 (insert passphrase
"\n"))
82 (apply #'call-process-region
(point-min) (point-max) program
83 nil errors-buffer nil args
))))
84 (with-current-buffer (get-buffer-create output-buffer
)
87 (if (file-exists-p output-file-name
)
88 (let ((coding-system-for-read 'raw-text-dos
))
89 (insert-file-contents output-file-name
)))
90 (set-buffer errors-buffer
)
91 (if (not (equal exit-status
0))
92 (insert (format "\n%s exited abnormally: '%s'\n"
93 program exit-status
)))))
94 (if (file-exists-p output-file-name
)
95 (delete-file output-file-name
))
96 (set-default-file-modes orig-mode
))))
98 (defun pgg-gpg-possibly-cache-passphrase (passphrase &optional key
)
99 (if (and pgg-cache-passphrase
101 (goto-char (point-min))
102 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t
)))
103 (pgg-add-passphrase-cache
106 (goto-char (point-min))
107 (if (re-search-forward
108 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t
)
109 (substring (match-string 0) -
8))))
112 (defvar pgg-gpg-all-secret-keys
'unknown
)
114 (defun pgg-gpg-lookup-all-secret-keys ()
115 "Return all secret keys present in secret key ring."
116 (when (eq pgg-gpg-all-secret-keys
'unknown
)
117 (setq pgg-gpg-all-secret-keys
'())
118 (let ((args (list "--with-colons" "--no-greeting" "--batch"
119 "--list-secret-keys")))
121 (apply #'call-process pgg-gpg-program nil t nil args
)
122 (goto-char (point-min))
123 (while (re-search-forward
124 "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)" nil t
)
125 (push (substring (match-string 2) 8)
126 pgg-gpg-all-secret-keys
)))))
127 pgg-gpg-all-secret-keys
)
129 (defun pgg-gpg-lookup-key (string &optional type
)
130 "Search keys associated with STRING."
131 (let ((args (list "--with-colons" "--no-greeting" "--batch"
132 (if type
"--list-secret-keys" "--list-keys")
135 (apply #'call-process pgg-gpg-program nil t nil args
)
136 (goto-char (point-min))
137 (if (re-search-forward "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)"
139 (substring (match-string 2) 8)))))
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 pgg-gpg-recipient-argument 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 ;; Possibly cache passphrase under, e.g. "jas", for future sign.
166 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id
)
167 ;; Possibly cache passphrase under, e.g. B565716F, for future decrypt.
168 (pgg-gpg-possibly-cache-passphrase passphrase
)))
169 (pgg-process-when-success)))
171 (defun pgg-gpg-decrypt-region (start end
)
172 "Decrypt the current region between START and END."
173 (let* ((current-buffer (current-buffer))
174 (message-keys (with-temp-buffer
175 (insert-buffer-substring current-buffer
)
176 (pgg-decode-armor-region (point-min) (point-max))))
177 (secret-keys (pgg-gpg-lookup-all-secret-keys))
178 (key (pgg-gpg-select-matching-key message-keys secret-keys
))
179 (pgg-gpg-user-id (or key pgg-gpg-user-id pgg-default-user-id
))
182 (format "GnuPG passphrase for %s: " pgg-gpg-user-id
)
184 (args '("--batch" "--decrypt")))
185 (pgg-gpg-process-region start end passphrase pgg-gpg-program args
)
186 (with-current-buffer pgg-errors-buffer
187 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id
)
188 (goto-char (point-min))
189 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t
))))
191 (defun pgg-gpg-select-matching-key (message-keys secret-keys
)
192 "Choose a key from MESSAGE-KEYS that matches one of the keys in SECRET-KEYS."
193 (loop for message-key in message-keys
194 for message-key-id
= (and (equal (car message-key
) 1)
195 (cdr (assq 'key-identifier message-key
)))
196 for key
= (and message-key-id
(pgg-lookup-key message-key-id
'encrypt
))
197 when
(and key
(member key secret-keys
)) return key
))
199 (defun pgg-gpg-sign-region (start end
&optional cleartext
)
200 "Make detached signature from text between START and END."
201 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id
))
204 (format "GnuPG passphrase for %s: " pgg-gpg-user-id
)
207 (list (if cleartext
"--clearsign" "--detach-sign")
208 "--armor" "--batch" "--verbose"
209 "--local-user" pgg-gpg-user-id
))
210 (inhibit-read-only t
)
212 (pgg-as-lbt start end
'CRLF
213 (pgg-gpg-process-region start end passphrase pgg-gpg-program args
))
214 (with-current-buffer pgg-errors-buffer
215 ;; Possibly cache passphrase under, e.g. "jas", for future sign.
216 (pgg-gpg-possibly-cache-passphrase passphrase pgg-gpg-user-id
)
217 ;; Possibly cache passphrase under, e.g. B565716F, for future decrypt.
218 (pgg-gpg-possibly-cache-passphrase passphrase
))
219 (pgg-process-when-success)))
221 (defun pgg-gpg-verify-region (start end
&optional signature
)
222 "Verify region between START and END as the detached signature SIGNATURE."
223 (let ((args '("--batch" "--verify")))
224 (when (stringp signature
)
225 (setq args
(append args
(list signature
))))
226 (setq args
(append args
'("-")))
227 (pgg-gpg-process-region start end nil pgg-gpg-program args
)
228 (with-current-buffer pgg-errors-buffer
229 (goto-char (point-min))
230 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t
)
231 (with-current-buffer pgg-output-buffer
232 (insert-buffer-substring pgg-errors-buffer
233 (match-beginning 1) (match-end 0)))
234 (delete-region (match-beginning 0) (match-end 0)))
235 (goto-char (point-min))
236 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t
))))
238 (defun pgg-gpg-insert-key ()
239 "Insert public key at point."
240 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id
))
241 (args (list "--batch" "--export" "--armor"
243 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args
)
244 (insert-buffer-substring pgg-output-buffer
)))
246 (defun pgg-gpg-snarf-keys-region (start end
)
247 "Add all public keys in region between START and END to the keyring."
248 (let ((args '("--import" "--batch" "-")) status
)
249 (pgg-gpg-process-region start end nil pgg-gpg-program args
)
250 (set-buffer pgg-errors-buffer
)
251 (goto-char (point-min))
252 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t
)
253 (setq status
(buffer-substring (match-end 0)
254 (progn (end-of-line)(point)))
255 status
(vconcat (mapcar #'string-to-int
(split-string status
))))
257 (insert (format "Imported %d key(s).
258 \tArmor contains %d key(s) [%d bad, %d old].\n"
265 (if (zerop (aref status
9))
267 "\tSecret keys are imported.\n")))
268 (append-to-buffer pgg-output-buffer
(point-min)(point-max))
269 (pgg-process-when-success)))
273 ;;; arch-tag: 2aa5d5d8-93a0-4865-9312-33e29830e000
274 ;;; pgg-gpg.el ends here