1 ;;; pgg-gpg.el --- GnuPG support for PGG.
3 ;; Copyright (C) 1999,2000 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 (require 'mel) ; binary-to-text-funcall
29 (eval-when-compile (require 'pgg))
35 (defcustom pgg-gpg-program "gpg"
36 "The GnuPG executable."
40 (defcustom pgg-gpg-extra-args nil
41 "Extra arguments for every GnuPG invocation."
46 (luna-define-class pgg-scheme-gpg (pgg-scheme)))
48 (defvar pgg-gpg-user-id nil
49 "GnuPG ID of your default identity.")
51 (defvar pgg-scheme-gpg-instance nil)
54 (defun pgg-make-scheme-gpg ()
55 (or pgg-scheme-gpg-instance
56 (setq pgg-scheme-gpg-instance
57 (luna-make-entity 'pgg-scheme-gpg))))
59 (defun pgg-gpg-process-region (start end passphrase program args)
60 (let* ((output-file-name (make-temp-file
61 (expand-file-name "pgg-output"
62 temporary-file-directory)))
65 ,@(if passphrase '("--passphrase-fd" "0"))
67 "--output" ,output-file-name
68 ,@pgg-gpg-extra-args ,@args))
69 (output-buffer pgg-output-buffer)
70 (errors-buffer pgg-errors-buffer)
71 (process-connection-type nil)
72 process status exit-status)
73 (with-current-buffer (get-buffer-create errors-buffer)
78 (let ((coding-system-for-write 'binary))
80 (apply #'start-process "*GnuPG*" errors-buffer
82 (set-process-sentinel process #'ignore)
84 (process-send-string process (concat passphrase "\n")))
85 (process-send-region process start end)
86 (process-send-eof process)
87 (while (eq 'run (process-status process))
88 (accept-process-output process 5))
89 (setq status (process-status process)
90 exit-status (process-exit-status process))
91 (delete-process process)
92 (with-current-buffer (get-buffer-create output-buffer)
95 (if (file-exists-p output-file-name)
96 (let ((coding-system-for-read 'raw-text-dos))
97 (insert-file-contents output-file-name)))
98 (set-buffer errors-buffer)
99 (if (memq status '(stop signal))
100 (error "%s exited abnormally: '%s'" program exit-status))
101 (if (= 127 exit-status)
102 (error "%s could not be found" program))))
103 (if (and process (eq 'run (process-status process)))
104 (interrupt-process process))
105 (if (file-exists-p output-file-name)
106 (delete-file output-file-name)))))
108 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
109 (if (and pgg-cache-passphrase
111 (goto-char (point-min))
112 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
113 (pgg-add-passphrase-cache
115 (goto-char (point-min))
116 (if (re-search-forward
117 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
118 (substring (match-string 0) -8)))
121 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-gpg)
122 string &optional type)
123 (let ((args (list "--with-colons" "--no-greeting" "--batch"
124 (if type "--list-secret-keys" "--list-keys")
127 (apply #'call-process pgg-gpg-program nil t nil args)
128 (goto-char (point-min))
129 (if (re-search-forward "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)"
131 (substring (match-string 2) 8)))))
133 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-gpg)
134 start end recipients)
135 (let* ((user-id (or pgg-overriding-user-id pgg-gpg-user-id
136 pgg-default-user-id))
138 `("--batch" "--armor" "--always-trust" "--encrypt"
141 (mapcar (lambda (rcpt)
142 (list "--remote-user" rcpt))
144 (if pgg-encrypt-for-me
145 (list user-id)))))))))
146 (pgg-as-lbt start end 'CRLF
147 (pgg-gpg-process-region start end nil pgg-gpg-program args))
148 (pgg-process-when-success)))
150 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-gpg)
152 (let* ((user-id (or pgg-overriding-user-id pgg-gpg-user-id
153 pgg-default-user-id))
156 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
157 (pgg-scheme-lookup-key scheme user-id 'encrypt)))
158 (args '("--batch" "--decrypt")))
159 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
160 (with-current-buffer pgg-errors-buffer
161 (pgg-gpg-possibly-cache-passphrase passphrase)
162 (goto-char (point-min))
163 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
165 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-gpg)
166 start end &optional cleartext)
167 (let* ((user-id (or pgg-overriding-user-id pgg-gpg-user-id
168 pgg-default-user-id))
171 (format "GnuPG passphrase for %s: " user-id)
172 (pgg-scheme-lookup-key scheme user-id 'sign)))
174 (list (if cleartext "--clearsign" "--detach-sign")
175 "--armor" "--batch" "--verbose"
176 "--local-user" user-id))
177 (inhibit-read-only t)
179 (pgg-as-lbt start end 'CRLF
180 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
181 (with-current-buffer pgg-errors-buffer
182 (pgg-gpg-possibly-cache-passphrase passphrase))
183 (pgg-process-when-success)))
185 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-gpg)
186 start end &optional signature)
187 (let ((args '("--batch" "--verify")))
188 (when (stringp signature)
189 (setq args (append args (list signature))))
190 (setq args (append args '("-")))
191 (pgg-gpg-process-region start end nil pgg-gpg-program args)
192 (with-current-buffer pgg-errors-buffer
193 (goto-char (point-min))
194 (while (re-search-forward "^gpg: " nil t)
196 (goto-char (point-min))
197 (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
198 (goto-char (point-min))
199 (delete-matching-lines "^warning\\|\\[GNUPG:]")
200 (set-buffer pgg-output-buffer)
201 (insert-buffer-substring pgg-errors-buffer)))))
203 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-gpg))
204 (let* ((user-id (or pgg-overriding-user-id pgg-gpg-user-id
205 pgg-default-user-id))
206 (args (list "--batch" "--export" "--armor" user-id)))
207 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
208 (insert-buffer-substring pgg-output-buffer)))
210 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-gpg)
212 (let ((args '("--import" "--batch" "-")) status)
213 (pgg-gpg-process-region start end nil pgg-gpg-program args)
214 (set-buffer pgg-errors-buffer)
215 (goto-char (point-min))
216 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
217 (setq status (buffer-substring (match-end 0)
218 (progn (end-of-line)(point)))
219 status (vconcat (mapcar #'string-to-int (split-string status))))
221 (insert (format "Imported %d key(s).
222 \tArmor contains %d key(s) [%d bad, %d old].\n"
229 (if (zerop (aref status 9))
231 "\tSecret keys are imported.\n")))
232 (append-to-buffer pgg-output-buffer (point-min)(point-max))
233 (pgg-process-when-success)))
237 ;;; pgg-gpg.el ends here