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."
45 (const :tag "None" nil)
46 (string :tag "Arguments")))
48 (defvar pgg-gpg-user-id nil
49 "GnuPG ID of your default identity.")
51 (defun pgg-gpg-process-region (start end passphrase program args)
52 (let* ((output-file-name (pgg-make-temp-file "pgg-output"))
55 ,@(if passphrase '("--passphrase-fd" "0"))
57 "--output" ,output-file-name
58 ,@pgg-gpg-extra-args ,@args))
59 (output-buffer pgg-output-buffer)
60 (errors-buffer pgg-errors-buffer)
61 (orig-mode (default-file-modes))
62 (process-connection-type nil)
64 (with-current-buffer (get-buffer-create errors-buffer)
69 (set-default-file-modes 448)
70 (let* ((coding-system-for-write 'binary)
71 (input (buffer-substring-no-properties start end)))
74 (insert passphrase "\n"))
77 (apply #'call-process-region (point-min) (point-max) program
78 nil errors-buffer nil args))))
79 (with-current-buffer (get-buffer-create output-buffer)
82 (if (file-exists-p output-file-name)
83 (let ((coding-system-for-read 'raw-text-dos))
84 (insert-file-contents output-file-name)))
85 (set-buffer errors-buffer)
86 (if (not (equal exit-status 0))
87 (insert (format "\n%s exited abnormally: '%s'\n"
88 program exit-status)))))
89 (if (file-exists-p output-file-name)
90 (delete-file output-file-name))
91 (set-default-file-modes orig-mode))))
93 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
94 (if (and pgg-cache-passphrase
96 (goto-char (point-min))
97 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
98 (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 (defun pgg-gpg-lookup-key (string &optional type)
107 "Search keys associated with STRING."
108 (let ((args (list "--with-colons" "--no-greeting" "--batch"
109 (if type "--list-secret-keys" "--list-keys")
112 (apply #'call-process pgg-gpg-program nil t nil args)
113 (goto-char (point-min))
114 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
117 (buffer-substring (match-end 0)
118 (progn (end-of-line)(point)))
121 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
122 "Encrypt the current region between START and END.
123 If optional argument SIGN is non-nil, do a combined sign and encrypt."
124 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
128 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
129 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
132 (list "--batch" "--armor" "--always-trust" "--encrypt")
133 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
136 (mapcar (lambda (rcpt)
137 (list "--remote-user" rcpt))
139 (if pgg-encrypt-for-me
140 (list pgg-gpg-user-id)))))))))
141 (pgg-as-lbt start end 'CRLF
142 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
144 (with-current-buffer pgg-errors-buffer
145 (pgg-gpg-possibly-cache-passphrase passphrase)))
146 (pgg-process-when-success)))
148 (defun pgg-gpg-decrypt-region (start end)
149 "Decrypt the current region between START and END."
150 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
153 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
154 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
155 (args '("--batch" "--decrypt")))
156 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
157 (with-current-buffer pgg-errors-buffer
158 (pgg-gpg-possibly-cache-passphrase passphrase)
159 (goto-char (point-min))
160 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
162 (defun pgg-gpg-sign-region (start end &optional cleartext)
163 "Make detached signature from text between START and END."
164 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
167 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
168 (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
170 (list (if cleartext "--clearsign" "--detach-sign")
171 "--armor" "--batch" "--verbose"
172 "--local-user" pgg-gpg-user-id))
173 (inhibit-read-only t)
175 (pgg-as-lbt start end 'CRLF
176 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
177 (with-current-buffer pgg-errors-buffer
178 (pgg-gpg-possibly-cache-passphrase passphrase))
179 (pgg-process-when-success)))
181 (defun pgg-gpg-verify-region (start end &optional signature)
182 "Verify region between START and END as the detached signature SIGNATURE."
183 (let ((args '("--batch" "--verify")))
184 (when (stringp signature)
185 (setq args (append args (list signature))))
186 (setq args (append args '("-")))
187 (pgg-gpg-process-region start end nil pgg-gpg-program args)
188 (with-current-buffer pgg-errors-buffer
189 (goto-char (point-min))
190 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
191 (with-current-buffer pgg-output-buffer
192 (insert-buffer-substring pgg-errors-buffer
193 (match-beginning 1) (match-end 0)))
194 (delete-region (match-beginning 0) (match-end 0)))
195 (goto-char (point-min))
196 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
198 (defun pgg-gpg-insert-key ()
199 "Insert public key at point."
200 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
201 (args (list "--batch" "--export" "--armor"
203 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
204 (insert-buffer-substring pgg-output-buffer)))
206 (defun pgg-gpg-snarf-keys-region (start end)
207 "Add all public keys in region between START and END to the keyring."
208 (let ((args '("--import" "--batch" "-")) status)
209 (pgg-gpg-process-region start end nil pgg-gpg-program args)
210 (set-buffer pgg-errors-buffer)
211 (goto-char (point-min))
212 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
213 (setq status (buffer-substring (match-end 0)
214 (progn (end-of-line)(point)))
215 status (vconcat (mapcar #'string-to-int (split-string status))))
217 (insert (format "Imported %d key(s).
218 \tArmor contains %d key(s) [%d bad, %d old].\n"
225 (if (zerop (aref status 9))
227 "\tSecret keys are imported.\n")))
228 (append-to-buffer pgg-output-buffer (point-min)(point-max))
229 (pgg-process-when-success)))
233 ;;; pgg-gpg.el ends here