1 ;;; pgg-gpg.el --- GnuPG support for PGG.
3 ;; Copyright (C) 1999 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
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-shell-file-name "/bin/sh"
40 "The GnuPG executable."
44 (defcustom pgg-gpg-extra-args nil
45 "Extra arguments for every GnuPG invocation."
50 (luna-define-class pgg-scheme-gpg (pgg-scheme))
53 (defvar pgg-gpg-user-id nil
54 "GnuPG ID of your default identity.")
56 (defvar pgg-scheme-gpg-instance nil)
59 (defun pgg-make-scheme-gpg ()
60 (or pgg-scheme-gpg-instance
61 (setq pgg-scheme-gpg-instance
62 (luna-make-entity 'pgg-scheme-gpg))))
64 (defun pgg-gpg-process-region (start end passphrase program args)
65 (let* ((errors-file-name
66 (concat temporary-file-directory
67 (make-temp-name "pgg-errors")))
69 (concat temporary-file-directory
70 (make-temp-name "pgg-status")))
74 ,@(if passphrase '("--passphrase-fd" "0"))
77 (list (concat "2>" errors-file-name)
78 (concat "3>" status-file-name))))
79 (shell-file-name pgg-gpg-shell-file-name)
80 (output-buffer pgg-output-buffer)
81 (errors-buffer pgg-errors-buffer)
82 (status-buffer pgg-status-buffer)
83 (process-connection-type nil)
84 process status exit-status)
85 (with-current-buffer (get-buffer-create output-buffer)
89 (apply #'start-process-shell-command "*GnuPG*" output-buffer
91 (set-process-sentinel process 'ignore)
93 (process-send-string process (concat passphrase "\n")))
94 (process-send-region process start end)
95 (process-send-eof process)
96 (while (eq 'run (process-status process))
97 (accept-process-output process 5))
98 (setq status (process-status process)
99 exit-status (process-exit-status process))
100 (delete-process process)
101 (with-current-buffer output-buffer
102 (goto-char (point-min))
103 (while (search-forward "\r$" nil t)
105 (if (memq status '(stop signal))
106 (error "%s exited abnormally: '%s'" program exit-status))
107 (if (= 127 exit-status)
108 (error "%s could not be found" program))
110 (set-buffer (get-buffer-create errors-buffer))
111 (buffer-disable-undo)
113 (insert-file-contents errors-file-name)
114 (delete-file errors-file-name)
116 (set-buffer (get-buffer-create status-buffer))
117 (buffer-disable-undo)
119 (insert-file-contents status-file-name)
120 (delete-file status-file-name)
122 (if (and process (eq 'run (process-status process)))
123 (interrupt-process process))
127 (luna-define-method lookup-key-string ((scheme pgg-scheme-gpg)
128 string &optional type)
129 (let ((args (list "--with-colons" "--no-greeting" "--batch"
130 (if type "--list-secret-keys" "--list-keys")
132 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
133 (with-current-buffer pgg-output-buffer
134 (goto-char (point-min))
135 (when (re-search-forward "^\\(sec\\|pub\\):" nil t)
138 (buffer-substring (match-end 0)
139 (progn (end-of-line)(point)))
144 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg)
145 start end recipients)
146 (let* ((pgg-gpg-user-id pgg-default-user-id)
149 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
150 (luna-send scheme 'lookup-key-string
151 scheme pgg-gpg-user-id 'encrypt)))
153 `("--batch" "--armor" "--textmode" "--always-trust" "--encrypt"
156 (mapcar (lambda (rcpt)
157 (list "--remote-user"
158 (concat "\"" rcpt "\"")))
160 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
161 (with-current-buffer pgg-output-buffer
162 (if (zerop (buffer-size))
163 (insert-buffer-substring pgg-errors-buffer)
165 (cdr (assq 1 (pgg-parse-armor-region
166 (point-min)(point-max))))))
167 (pgg-add-passphrase-cache
168 (cdr (assq 'key-identifier packet))
172 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg)
174 (let* ((pgg-gpg-user-id pgg-default-user-id)
177 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
178 (luna-send scheme 'lookup-key-string
179 scheme pgg-gpg-user-id 'encrypt)))
180 (args '("--batch" "--decrypt")))
181 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
182 (with-current-buffer pgg-output-buffer
183 (when (zerop (buffer-size))
184 (insert-buffer-substring pgg-errors-buffer)))
187 (luna-define-method sign-region ((scheme pgg-scheme-gpg)
189 (let* ((pgg-gpg-user-id pgg-default-user-id)
192 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
193 (luna-send scheme 'lookup-key-string
194 scheme pgg-gpg-user-id 'sign)))
196 (list "--detach-sign" "--armor" "--batch" "--verbose"
197 "--local-user" pgg-gpg-user-id)))
199 (setq end (set-marker (make-marker) (point-max)))
200 (while (progn (end-of-line) (> (marker-position end) (point)))
203 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
205 (while (re-search-forward "\r$" end t)
207 (with-current-buffer pgg-output-buffer
208 (if (zerop (buffer-size))
209 (insert-buffer-substring pgg-errors-buffer)
211 (cdr (assq 2 (pgg-parse-armor-region
212 (point-min)(point-max))))))
213 (pgg-add-passphrase-cache
214 (cdr (assq 'key-identifier packet))
218 (luna-define-method verify-region ((scheme pgg-scheme-gpg)
219 start end &optional signature)
220 (let ((args '("--batch" "--verify")))
221 (when (stringp signature)
222 (setq args (append args (list signature))))
223 (pgg-gpg-process-region start end nil pgg-gpg-program args)
224 (set-buffer pgg-errors-buffer)
225 (goto-char (point-min))
226 (while (re-search-forward "^gpg: " nil t)
228 (goto-char (point-min))
229 (let ((case-fold-search t))
230 (while (re-search-forward "^warning: " nil t)
231 (delete-region (match-beginning 0)
232 (progn (beginning-of-line 2) (point)))))
233 (append-to-buffer pgg-output-buffer
234 (point-min)(point-max))
237 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
238 (let* ((pgg-gpg-user-id pgg-default-user-id)
239 (args (list "--batch" "--export" "--armor"
240 (concat "\"" pgg-gpg-user-id "\""))))
241 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
242 (insert-buffer-substring pgg-output-buffer)
245 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
247 (let ((args '("--import" "--batch")) status)
248 (pgg-gpg-process-region start end nil pgg-gpg-program args)
249 (set-buffer pgg-status-buffer)
250 (goto-char (point-min))
251 (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
252 (setq status (buffer-substring (match-end 0)
255 status (vconcat (split-string status)))
257 (insert (aref status 0) "keys seen\n"
258 (format "\t%d bad, %d new, %d old\n"
259 (string-to-int (aref status 1))
260 (+ (string-to-int (aref status 2))
261 (string-to-int (aref status 10)))
262 (+ (string-to-int (aref status 4))
263 (string-to-int (aref status 11))))
264 (if (zerop (aref status 9))
266 "\tSecret keys are imported\n")))
267 (append-to-buffer pgg-output-buffer
268 (point-min)(point-max))
269 (with-current-buffer pgg-output-buffer
270 (when (zerop (buffer-size))
271 (insert-buffer-substring pgg-errors-buffer)))
276 ;;; pgg-gpg.el ends here