1 ;;; pgg-gpg.el --- GnuPG support for PGG.
3 ;; Copyright (C) 1999,2000 Daiki Ueno
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."
45 (luna-define-class pgg-scheme-gpg (pgg-scheme)))
47 (defvar pgg-gpg-user-id nil
48 "GnuPG ID of your default identity.")
50 (defvar pgg-scheme-gpg-instance nil)
53 (defun pgg-make-scheme-gpg ()
54 (or pgg-scheme-gpg-instance
55 (setq pgg-scheme-gpg-instance
56 (luna-make-entity 'pgg-scheme-gpg))))
58 (defun pgg-gpg-process-region (start end passphrase program args)
59 (let* ((output-file-name
60 (concat temporary-file-directory (make-temp-name "pgg-output")))
63 ,@(if passphrase '("--passphrase-fd" "0"))
64 "--output" ,output-file-name
65 ,@pgg-gpg-extra-args ,@args))
66 (output-buffer pgg-output-buffer)
67 (errors-buffer pgg-errors-buffer)
68 (orig-mode (default-file-modes))
69 (process-connection-type nil)
70 process status exit-status)
71 (with-current-buffer (get-buffer-create errors-buffer)
76 (set-default-file-modes 448)
77 (as-binary-output-file
79 (apply #'start-process "*GnuPG*" errors-buffer program args)))
80 (set-process-sentinel process #'ignore)
82 (process-send-string process (concat passphrase "\n")))
83 (process-send-region process start end)
84 (process-send-eof process)
85 (while (eq 'run (process-status process))
86 (accept-process-output process 5))
87 (setq status (process-status process)
88 exit-status (process-exit-status process))
89 (delete-process process)
90 (with-current-buffer (get-buffer-create output-buffer)
93 (if (file-exists-p output-file-name)
94 (insert-file-contents-as-raw-text-CRLF output-file-name))
95 (set-buffer errors-buffer)
96 (if (memq status '(stop signal))
97 (error "%s exited abnormally: '%s'" program exit-status))
98 (if (= 127 exit-status)
99 (error "%s could not be found" program))))
100 (if (and process (eq 'run (process-status process)))
101 (interrupt-process process))
102 (if (file-exists-p output-file-name)
103 (delete-file output-file-name))
104 (set-default-file-modes orig-mode))))
106 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
107 (if (and pgg-cache-passphrase
109 (goto-char (point-min))
110 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
111 (pgg-add-passphrase-cache
113 (goto-char (point-min))
114 (if (re-search-forward
115 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
116 (substring (match-string 0) -8)))
119 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-gpg)
120 string &optional type)
121 (let ((args (list "--with-colons" "--no-greeting" "--batch"
122 (if type "--list-secret-keys" "--list-keys")
125 (apply #'call-process pgg-gpg-program nil t nil args)
126 (goto-char (point-min))
127 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
130 (buffer-substring (match-end 0)
131 (progn (end-of-line)(point)))
134 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-gpg)
135 start end recipients)
136 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id 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 pgg-gpg-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* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
155 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
156 (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'encrypt)))
157 (args '("--batch" "--decrypt")))
158 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
159 (with-current-buffer pgg-errors-buffer
160 (pgg-gpg-possibly-cache-passphrase passphrase)
161 (goto-char (point-min))
162 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
164 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-gpg)
165 start end &optional cleartext)
166 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
169 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
170 (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'sign)))
172 (list (if cleartext "--clearsign" "--detach-sign")
173 "--armor" "--batch" "--verbose"
174 "--local-user" pgg-gpg-user-id))
175 (inhibit-read-only t)
177 (pgg-as-lbt start end 'CRLF
178 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
179 (with-current-buffer pgg-errors-buffer
180 (pgg-gpg-possibly-cache-passphrase passphrase))
181 (pgg-process-when-success)))
183 (luna-define-method pgg-scheme-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 (with-current-buffer pgg-errors-buffer
190 (goto-char (point-min))
191 (while (re-search-forward "^gpg: " nil t)
193 (goto-char (point-min))
194 (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
195 (goto-char (point-min))
196 (delete-matching-lines "^warning\\|\\[GNUPG:]")
197 (set-buffer pgg-output-buffer)
198 (insert-buffer-substring pgg-errors-buffer)))))
200 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-gpg))
201 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
202 (args (list "--batch" "--export" "--armor"
204 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
205 (insert-buffer-substring pgg-output-buffer)))
207 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-gpg)
209 (let ((args '("--import" "--batch" "-")) status)
210 (pgg-gpg-process-region start end nil pgg-gpg-program args)
211 (set-buffer pgg-errors-buffer)
212 (goto-char (point-min))
213 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
214 (setq status (buffer-substring (match-end 0)
215 (progn (end-of-line)(point)))
216 status (vconcat (mapcar #'string-to-int (split-string status))))
218 (insert (format "Imported %d key(s).
219 \tArmor contains %d key(s) [%d bad, %d old].\n"
226 (if (zerop (aref status 9))
228 "\tSecret keys are imported.\n")))
229 (append-to-buffer pgg-output-buffer (point-min)(point-max))
230 (pgg-process-when-success)))
234 ;;; pgg-gpg.el ends here