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.
28 (eval-when-compile (require 'pgg))
34 (defcustom pgg-gpg-program "gpg"
35 "The GnuPG executable."
39 (defcustom pgg-gpg-extra-args nil
40 "Extra arguments for every GnuPG invocation."
43 (const :tag "None" nil)
44 (string :tag "Arguments")))
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
51 (expand-file-name (make-temp-name "pgg-output")
52 pgg-temporary-file-directory))
55 ,@(if passphrase '("--passphrase-fd" "0"))
56 "--output" ,output-file-name
57 ,@pgg-gpg-extra-args ,@args))
58 (output-buffer pgg-output-buffer)
59 (errors-buffer pgg-errors-buffer)
60 (orig-mode (default-file-modes))
61 (process-connection-type nil)
63 (with-current-buffer (get-buffer-create errors-buffer)
68 (set-default-file-modes 448)
69 (let* ((coding-system-for-write 'binary)
70 (input (buffer-substring-no-properties start end)))
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)
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
99 (goto-char (point-min))
100 (if (re-search-forward
101 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
102 (substring (match-string 0) -8)))
105 (defun pgg-gpg-lookup-key (string &optional type)
106 "Search keys associated with STRING."
107 (let ((args (list "--with-colons" "--no-greeting" "--batch"
108 (if type "--list-secret-keys" "--list-keys")
111 (apply #'call-process pgg-gpg-program nil t nil args)
112 (goto-char (point-min))
113 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
116 (buffer-substring (match-end 0)
117 (progn (end-of-line)(point)))
120 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
121 "Encrypt the current region between START and END.
122 If optional argument SIGN is non-nil, do a combined sign and encrypt."
123 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
127 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
128 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
131 (list "--batch" "--armor" "--always-trust" "--encrypt")
132 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
135 (mapcar (lambda (rcpt)
136 (list "--remote-user" rcpt))
138 (if pgg-encrypt-for-me
139 (list pgg-gpg-user-id)))))))))
140 (pgg-as-lbt start end 'CRLF
141 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
143 (with-current-buffer pgg-errors-buffer
144 (pgg-gpg-possibly-cache-passphrase passphrase)))
145 (pgg-process-when-success)))
147 (defun pgg-gpg-decrypt-region (start end)
148 "Decrypt the current region between START and END."
149 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
152 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
153 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
154 (args '("--batch" "--decrypt")))
155 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
156 (with-current-buffer pgg-errors-buffer
157 (pgg-gpg-possibly-cache-passphrase passphrase)
158 (goto-char (point-min))
159 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
161 (defun pgg-gpg-sign-region (start end &optional cleartext)
162 "Make detached signature from text between START and END."
163 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
166 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
167 (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
169 (list (if cleartext "--clearsign" "--detach-sign")
170 "--armor" "--batch" "--verbose"
171 "--local-user" pgg-gpg-user-id))
172 (inhibit-read-only t)
174 (pgg-as-lbt start end 'CRLF
175 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
176 (with-current-buffer pgg-errors-buffer
177 (pgg-gpg-possibly-cache-passphrase passphrase))
178 (pgg-process-when-success)))
180 (defun pgg-gpg-verify-region (start end &optional signature)
181 "Verify region between START and END as the detached signature SIGNATURE."
182 (let ((args '("--batch" "--verify")))
183 (when (stringp signature)
184 (setq args (append args (list signature))))
185 (setq args (append args '("-")))
186 (pgg-gpg-process-region start end nil pgg-gpg-program args)
187 (with-current-buffer pgg-errors-buffer
188 (goto-char (point-min))
189 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
190 (with-current-buffer pgg-output-buffer
191 (insert-buffer-substring pgg-errors-buffer
192 (match-beginning 1) (match-end 0)))
193 (delete-region (match-beginning 0) (match-end 0)))
194 (goto-char (point-min))
195 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
197 (defun pgg-gpg-insert-key ()
198 "Insert public key at point."
199 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
200 (args (list "--batch" "--export" "--armor"
202 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
203 (insert-buffer-substring pgg-output-buffer)))
205 (defun pgg-gpg-snarf-keys-region (start end)
206 "Add all public keys in region between START and END to the keyring."
207 (let ((args '("--import" "--batch" "-")) status)
208 (pgg-gpg-process-region start end nil pgg-gpg-program args)
209 (set-buffer pgg-errors-buffer)
210 (goto-char (point-min))
211 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
212 (setq status (buffer-substring (match-end 0)
213 (progn (end-of-line)(point)))
214 status (vconcat (mapcar #'string-to-int (split-string status))))
216 (insert (format "Imported %d key(s).
217 \tArmor contains %d key(s) [%d bad, %d old].\n"
224 (if (zerop (aref status 9))
226 "\tSecret keys are imported.\n")))
227 (append-to-buffer pgg-output-buffer (point-min)(point-max))
228 (pgg-process-when-success)))
232 ;;; pgg-gpg.el ends here