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 SEMI (Secure Emacs MIME Interface).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; 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 (error "%s exited abnormally: '%s'" program exit-status))))
87 (if (file-exists-p output-file-name)
88 (delete-file output-file-name))
89 (set-default-file-modes orig-mode))))
91 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
92 (if (and pgg-cache-passphrase
94 (goto-char (point-min))
95 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
96 (pgg-add-passphrase-cache
98 (goto-char (point-min))
99 (if (re-search-forward
100 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
101 (substring (match-string 0) -8)))
104 (defun pgg-gpg-lookup-key (string &optional type)
105 "Search keys associated with STRING."
106 (let ((args (list "--with-colons" "--no-greeting" "--batch"
107 (if type "--list-secret-keys" "--list-keys")
110 (apply #'call-process pgg-gpg-program nil t nil args)
111 (goto-char (point-min))
112 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
115 (buffer-substring (match-end 0)
116 (progn (end-of-line)(point)))
119 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
120 "Encrypt the current region between START and END.
121 If optional argument SIGN is non-nil, do a combined sign and encrypt."
122 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
126 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
127 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
130 (list "--batch" "--armor" "--always-trust" "--encrypt")
131 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
134 (mapcar (lambda (rcpt)
135 (list "--remote-user" rcpt))
137 (if pgg-encrypt-for-me
138 (list pgg-gpg-user-id)))))))))
139 (pgg-as-lbt start end 'CRLF
140 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
142 (with-current-buffer pgg-errors-buffer
143 (pgg-gpg-possibly-cache-passphrase passphrase)))
144 (pgg-process-when-success)))
146 (defun pgg-gpg-decrypt-region (start end)
147 "Decrypt the current region between START and END."
148 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
151 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
152 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
153 (args '("--batch" "--decrypt")))
154 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
155 (with-current-buffer pgg-errors-buffer
156 (pgg-gpg-possibly-cache-passphrase passphrase)
157 (goto-char (point-min))
158 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
160 (defun pgg-gpg-sign-region (start end &optional cleartext)
161 "Make detached signature from text between START and END."
162 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
165 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
166 (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
168 (list (if cleartext "--clearsign" "--detach-sign")
169 "--armor" "--batch" "--verbose"
170 "--local-user" pgg-gpg-user-id))
171 (inhibit-read-only t)
173 (pgg-as-lbt start end 'CRLF
174 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
175 (with-current-buffer pgg-errors-buffer
176 (pgg-gpg-possibly-cache-passphrase passphrase))
177 (pgg-process-when-success)))
179 (defun pgg-gpg-verify-region (start end &optional signature)
180 "Verify region between START and END as the detached signature SIGNATURE."
181 (let ((args '("--batch" "--verify")))
182 (when (stringp signature)
183 (setq args (append args (list signature))))
184 (setq args (append args '("-")))
185 (pgg-gpg-process-region start end nil pgg-gpg-program args)
186 (with-current-buffer pgg-errors-buffer
187 (goto-char (point-min))
188 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
189 (with-current-buffer pgg-output-buffer
190 (insert-buffer-substring pgg-errors-buffer
191 (match-beginning 1) (match-end 0)))
192 (delete-region (match-beginning 0) (match-end 0)))
193 (goto-char (point-min))
194 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
196 (defun pgg-gpg-insert-key ()
197 "Insert public key at point."
198 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
199 (args (list "--batch" "--export" "--armor"
201 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
202 (insert-buffer-substring pgg-output-buffer)))
204 (defun pgg-gpg-snarf-keys-region (start end)
205 "Add all public keys in region between START and END to the keyring."
206 (let ((args '("--import" "--batch" "-")) status)
207 (pgg-gpg-process-region start end nil pgg-gpg-program args)
208 (set-buffer pgg-errors-buffer)
209 (goto-char (point-min))
210 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
211 (setq status (buffer-substring (match-end 0)
212 (progn (end-of-line)(point)))
213 status (vconcat (mapcar #'string-to-int (split-string status))))
215 (insert (format "Imported %d key(s).
216 \tArmor contains %d key(s) [%d bad, %d old].\n"
223 (if (zerop (aref status 9))
225 "\tSecret keys are imported.\n")))
226 (append-to-buffer pgg-output-buffer (point-min)(point-max))
227 (pgg-process-when-success)))
231 ;;; pgg-gpg.el ends here