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 (pgg-make-temp-file "pgg-output"))
53 ,@(if passphrase '("--passphrase-fd" "0"))
54 "--output" ,output-file-name
55 ,@pgg-gpg-extra-args ,@args))
56 (output-buffer pgg-output-buffer)
57 (errors-buffer pgg-errors-buffer)
58 (orig-mode (default-file-modes))
59 (process-connection-type nil)
61 (with-current-buffer (get-buffer-create errors-buffer)
66 (set-default-file-modes 448)
67 (let* ((coding-system-for-write 'binary)
68 (input (buffer-substring-no-properties start end)))
71 (insert passphrase "\n"))
74 (apply #'call-process-region (point-min) (point-max) program
75 nil errors-buffer nil args))))
76 (with-current-buffer (get-buffer-create output-buffer)
79 (if (file-exists-p output-file-name)
80 (let ((coding-system-for-read 'raw-text-dos))
81 (insert-file-contents output-file-name)))
82 (set-buffer errors-buffer)
83 (if (not (equal exit-status 0))
84 (insert (format "\n%s exited abnormally: '%s'\n"
85 program exit-status)))))
86 (if (file-exists-p output-file-name)
87 (delete-file output-file-name))
88 (set-default-file-modes orig-mode))))
90 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
91 (if (and pgg-cache-passphrase
93 (goto-char (point-min))
94 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
95 (pgg-add-passphrase-cache
97 (goto-char (point-min))
98 (if (re-search-forward
99 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
100 (substring (match-string 0) -8)))
103 (defun pgg-gpg-lookup-key (string &optional type)
104 "Search keys associated with STRING."
105 (let ((args (list "--with-colons" "--no-greeting" "--batch"
106 (if type "--list-secret-keys" "--list-keys")
109 (apply #'call-process pgg-gpg-program nil t nil args)
110 (goto-char (point-min))
111 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
114 (buffer-substring (match-end 0)
115 (progn (end-of-line)(point)))
118 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
119 "Encrypt the current region between START and END.
120 If optional argument SIGN is non-nil, do a combined sign and encrypt."
121 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
125 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
126 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
129 (list "--batch" "--armor" "--always-trust" "--encrypt")
130 (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
133 (mapcar (lambda (rcpt)
134 (list "--remote-user" rcpt))
136 (if pgg-encrypt-for-me
137 (list pgg-gpg-user-id)))))))))
138 (pgg-as-lbt start end 'CRLF
139 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
141 (with-current-buffer pgg-errors-buffer
142 (pgg-gpg-possibly-cache-passphrase passphrase)))
143 (pgg-process-when-success)))
145 (defun pgg-gpg-decrypt-region (start end)
146 "Decrypt the current region between START and END."
147 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
150 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
151 (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
152 (args '("--batch" "--decrypt")))
153 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
154 (with-current-buffer pgg-errors-buffer
155 (pgg-gpg-possibly-cache-passphrase passphrase)
156 (goto-char (point-min))
157 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
159 (defun pgg-gpg-sign-region (start end &optional cleartext)
160 "Make detached signature from text between START and END."
161 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
164 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
165 (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
167 (list (if cleartext "--clearsign" "--detach-sign")
168 "--armor" "--batch" "--verbose"
169 "--local-user" pgg-gpg-user-id))
170 (inhibit-read-only t)
172 (pgg-as-lbt start end 'CRLF
173 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
174 (with-current-buffer pgg-errors-buffer
175 (pgg-gpg-possibly-cache-passphrase passphrase))
176 (pgg-process-when-success)))
178 (defun pgg-gpg-verify-region (start end &optional signature)
179 "Verify region between START and END as the detached signature SIGNATURE."
180 (let ((args '("--batch" "--verify")))
181 (when (stringp signature)
182 (setq args (append args (list signature))))
183 (setq args (append args '("-")))
184 (pgg-gpg-process-region start end nil pgg-gpg-program args)
185 (with-current-buffer pgg-errors-buffer
186 (goto-char (point-min))
187 (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
188 (with-current-buffer pgg-output-buffer
189 (insert-buffer-substring pgg-errors-buffer
190 (match-beginning 1) (match-end 0)))
191 (delete-region (match-beginning 0) (match-end 0)))
192 (goto-char (point-min))
193 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
195 (defun pgg-gpg-insert-key ()
196 "Insert public key at point."
197 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
198 (args (list "--batch" "--export" "--armor"
200 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
201 (insert-buffer-substring pgg-output-buffer)))
203 (defun pgg-gpg-snarf-keys-region (start end)
204 "Add all public keys in region between START and END to the keyring."
205 (let ((args '("--import" "--batch" "-")) status)
206 (pgg-gpg-process-region start end nil pgg-gpg-program args)
207 (set-buffer pgg-errors-buffer)
208 (goto-char (point-min))
209 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
210 (setq status (buffer-substring (match-end 0)
211 (progn (end-of-line)(point)))
212 status (vconcat (mapcar #'string-to-int (split-string status))))
214 (insert (format "Imported %d key(s).
215 \tArmor contains %d key(s) [%d bad, %d old].\n"
222 (if (zerop (aref status 9))
224 "\tSecret keys are imported.\n")))
225 (append-to-buffer pgg-output-buffer (point-min)(point-max))
226 (pgg-process-when-success)))
230 ;;; pgg-gpg.el ends here