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 "File name to load inferior shells from. Bourne shell or its equivalent
41 \(not tcsh) is needed for \"2>\"."
45 (defcustom pgg-gpg-shell-command-switch "-c"
46 "Switch used to have the shell execute its command line argument."
50 (defcustom pgg-gpg-extra-args nil
51 "Extra arguments for every GnuPG invocation."
56 (luna-define-class pgg-scheme-gpg (pgg-scheme))
59 (defvar pgg-gpg-user-id nil
60 "GnuPG ID of your default identity.")
62 (defvar pgg-scheme-gpg-instance nil)
65 (defun pgg-make-scheme-gpg ()
66 (or pgg-scheme-gpg-instance
67 (setq pgg-scheme-gpg-instance
68 (luna-make-entity 'pgg-scheme-gpg))))
70 (defun pgg-gpg-process-region (start end passphrase program args)
71 (let* ((errors-file-name
72 (concat temporary-file-directory
73 (make-temp-name "pgg-errors")))
75 (concat temporary-file-directory
76 (make-temp-name "pgg-status")))
80 ,@(if passphrase '("--passphrase-fd" "0"))
83 (list (concat "2>" errors-file-name)
84 (concat "3>" status-file-name))))
85 (shell-file-name pgg-gpg-shell-file-name)
86 (shell-command-switch pgg-gpg-shell-command-switch)
87 (output-buffer pgg-output-buffer)
88 (errors-buffer pgg-errors-buffer)
89 (status-buffer pgg-status-buffer)
90 (process-connection-type nil)
91 process status exit-status)
92 (with-current-buffer (get-buffer-create output-buffer)
97 (apply #'start-process-shell-command "*GnuPG*" output-buffer
99 (set-process-sentinel process 'ignore)
101 (process-send-string process (concat passphrase "\n")))
102 (process-send-region process start end)
103 (process-send-eof process)
104 (while (eq 'run (process-status process))
105 (accept-process-output process 5))
106 (setq status (process-status process)
107 exit-status (process-exit-status process))
108 (delete-process process)
109 (with-current-buffer output-buffer
110 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
112 (if (memq status '(stop signal))
113 (error "%s exited abnormally: '%s'" program exit-status))
114 (if (= 127 exit-status)
115 (error "%s could not be found" program))
117 (set-buffer (get-buffer-create errors-buffer))
118 (buffer-disable-undo)
120 (insert-file-contents errors-file-name)
121 (delete-file errors-file-name)
123 (set-buffer (get-buffer-create status-buffer))
124 (buffer-disable-undo)
126 (insert-file-contents status-file-name)
127 (delete-file status-file-name)
129 (if (and process (eq 'run (process-status process)))
130 (interrupt-process process))
134 (luna-define-method lookup-key-string ((scheme pgg-scheme-gpg)
135 string &optional type)
136 (let ((args (list "--with-colons" "--no-greeting" "--batch"
137 (if type "--list-secret-keys" "--list-keys")
139 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
140 (with-current-buffer pgg-output-buffer
141 (goto-char (point-min))
142 (when (re-search-forward "^\\(sec\\|pub\\):" nil t)
145 (buffer-substring (match-end 0)
146 (progn (end-of-line)(point)))
151 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg)
152 start end recipients)
153 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
155 `("--batch" "--armor" "--always-trust" "--encrypt"
158 (mapcar (lambda (rcpt)
159 (list "--remote-user"
160 (concat "\"" rcpt "\"")))
162 (if pgg-encrypt-for-me
163 (list pgg-gpg-user-id)))))))
165 (pgg-as-lbt start end 'CRLF
166 (pgg-gpg-process-region start end nil pgg-gpg-program args)
168 (pgg-process-when-success
169 (pgg-convert-lbt-region (point-min)(point-max) 'LF))
172 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg)
174 (let* ((pgg-gpg-user-id (or 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 (pgg-process-when-success nil)
185 (luna-define-method sign-region ((scheme pgg-scheme-gpg)
186 start end &optional cleartext)
187 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
190 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
191 (luna-send scheme 'lookup-key-string
192 scheme pgg-gpg-user-id 'sign)))
194 (list (if cleartext "--clearsign" "--detach-sign")
195 "--armor" "--batch" "--verbose"
196 "--local-user" pgg-gpg-user-id))
197 (inhibit-read-only t)
199 (pgg-as-lbt start end 'CRLF
200 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
202 (pgg-process-when-success
203 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
204 (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
206 (cdr (assq 2 (pgg-parse-armor-region
207 (progn (beginning-of-line 2)
210 (if pgg-cache-passphrase
211 (pgg-add-passphrase-cache
212 (cdr (assq 'key-identifier packet))
216 (luna-define-method verify-region ((scheme pgg-scheme-gpg)
217 start end &optional signature)
218 (let ((args '("--batch" "--verify")))
219 (when (stringp signature)
220 (setq args (append args (list signature))))
221 (pgg-gpg-process-region start end nil pgg-gpg-program args)
223 (set-buffer pgg-errors-buffer)
224 (goto-char (point-min))
225 (while (re-search-forward "^gpg: " nil t)
227 (goto-char (point-min))
228 (let ((case-fold-search t))
229 (while (re-search-forward "^warning: " nil t)
230 (delete-region (match-beginning 0)
231 (progn (beginning-of-line 2) (point)))))
232 (set-buffer pgg-status-buffer)
233 (goto-char (point-min))
234 (if (re-search-forward "^\\[GNUPG:] +GOODSIG +" nil t)
236 (set-buffer pgg-output-buffer)
237 (insert-buffer-substring pgg-errors-buffer)
242 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
243 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
244 (args (list "--batch" "--export" "--armor"
245 (concat "\"" pgg-gpg-user-id "\""))))
246 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
247 (insert-buffer-substring pgg-output-buffer)
250 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
252 (let ((args '("--import" "--batch" "-")) status)
253 (pgg-gpg-process-region start end nil pgg-gpg-program args)
254 (set-buffer pgg-status-buffer)
255 (goto-char (point-min))
256 (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
257 (setq status (buffer-substring (match-end 0)
260 status (vconcat (mapcar #'string-to-int
261 (split-string status))))
263 (insert (format "Imported %d key(s).
264 \tArmor contains %d key(s) [%d bad, %d old].\n"
271 (if (zerop (aref status 9))
273 "\tSecret keys are imported.\n")))
274 (append-to-buffer pgg-output-buffer
275 (point-min)(point-max))
276 (pgg-process-when-success nil)
281 ;;; pgg-gpg.el ends here