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 encrypt-region ((scheme pgg-scheme-gpg)
128 start end recipients)
129 (let* ((pgg-gpg-user-id pgg-default-user-id)
132 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)))
134 `("--batch" "--armor" "--textmode" "--always-trust" "--encrypt"
137 (mapcar (lambda (rcpt)
138 (list "--remote-user"
139 (concat "\"" rcpt "\"")))
141 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
142 (with-current-buffer pgg-output-buffer
143 (when (zerop (buffer-size))
144 (insert-buffer-substring pgg-errors-buffer)))
147 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg)
149 (let* ((pgg-gpg-user-id pgg-default-user-id)
152 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)))
153 (args '("--batch" "--decrypt")))
154 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
155 (with-current-buffer pgg-output-buffer
156 (when (zerop (buffer-size))
157 (insert-buffer-substring pgg-errors-buffer)))
160 (luna-define-method sign-region ((scheme pgg-scheme-gpg)
162 (let* ((pgg-gpg-user-id pgg-default-user-id)
165 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)))
167 (list "--detach-sign" "--armor" "--batch" "--verbose"
168 "--local-user" pgg-gpg-user-id)))
170 (setq end (set-marker (make-marker) (point-max)))
171 (while (progn (end-of-line) (> (marker-position end) (point)))
174 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
176 (while (re-search-forward "\r$" end t)
178 (with-current-buffer pgg-output-buffer
179 (when (zerop (buffer-size))
180 (insert-buffer-substring pgg-errors-buffer)))
183 (luna-define-method verify-region ((scheme pgg-scheme-gpg)
184 start end &optional signature)
185 (let ((args '("--batch" "--verify")))
186 (when (stringp signature)
187 (setq args (append args (list signature))))
188 (pgg-gpg-process-region start end nil pgg-gpg-program args)
189 (set-buffer pgg-errors-buffer)
190 (goto-char (point-min))
191 (while (re-search-forward "^gpg: " nil t)
193 (goto-char (point-min))
194 (let ((case-fold-search t))
195 (while (re-search-forward "^warning: " nil t)
196 (delete-region (match-beginning 0)
197 (progn (beginning-of-line 2) (point)))))
198 (append-to-buffer pgg-output-buffer
199 (point-min)(point-max))
202 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
203 (let* ((pgg-gpg-user-id pgg-default-user-id)
204 (args (list "--batch" "--export" "--armor"
205 (concat "\"" pgg-gpg-user-id "\""))))
206 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
207 (insert-buffer-substring pgg-output-buffer)
210 (luna-define-method 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-status-buffer)
215 (goto-char (point-min))
216 (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
217 (setq status (buffer-substring (match-end 0)
220 status (vconcat (split-string status)))
222 (insert (aref status 0) "keys seen\n"
223 (format "\t%d bad, %d new, %d old\n"
224 (string-to-int (aref status 1))
225 (+ (string-to-int (aref status 2))
226 (string-to-int (aref status 10)))
227 (+ (string-to-int (aref status 4))
228 (string-to-int (aref status 11))))
229 (if (zerop (aref status 9))
231 "\tSecret keys are imported\n")))
232 (append-to-buffer pgg-output-buffer
233 (point-min)(point-max))
234 (with-current-buffer pgg-output-buffer
235 (when (zerop (buffer-size))
236 (insert-buffer-substring pgg-errors-buffer)))
241 ;;; pgg-gpg.el ends here