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-gpg-messages-coding-system pgg-messages-coding-system
52 "Coding system used when reading from a GnuPG external process.")
54 (defvar pgg-scheme-gpg-instance nil)
57 (defun pgg-make-scheme-gpg ()
58 (or pgg-scheme-gpg-instance
59 (setq pgg-scheme-gpg-instance
60 (luna-make-entity 'pgg-scheme-gpg))))
62 (defun pgg-gpg-process-region (start end passphrase program args)
63 (let* ((output-file-name (make-temp-file
64 (expand-file-name "pgg-output"
65 temporary-file-directory)))
68 ,@(if passphrase '("--passphrase-fd" "0"))
70 "--output" ,output-file-name
71 ,@pgg-gpg-extra-args ,@args))
72 (output-buffer pgg-output-buffer)
73 (errors-buffer pgg-errors-buffer)
74 (process-connection-type nil)
75 process status exit-status)
76 (with-current-buffer (get-buffer-create errors-buffer)
82 (apply #'binary-to-text-funcall
83 pgg-gpg-messages-coding-system
84 #'start-process "*GnuPG*" errors-buffer
86 (set-process-sentinel process #'ignore)
88 (process-send-string process (concat passphrase "\n")))
89 (process-send-region process start end)
90 (process-send-eof process)
91 (while (eq 'run (process-status process))
92 (accept-process-output process 5))
93 (setq status (process-status process)
94 exit-status (process-exit-status process))
95 (delete-process process)
96 (with-current-buffer (get-buffer-create output-buffer)
99 (if (file-exists-p output-file-name)
100 (let ((coding-system-for-read 'raw-text-dos))
101 (insert-file-contents output-file-name)))
102 (set-buffer errors-buffer)
103 (if (memq status '(stop signal))
104 (error "%s exited abnormally: '%s'" program exit-status))
105 (if (= 127 exit-status)
106 (error "%s could not be found" program))))
107 (if (and process (eq 'run (process-status process)))
108 (interrupt-process process))
109 (if (file-exists-p output-file-name)
110 (delete-file output-file-name)))))
112 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
113 (if (and pgg-cache-passphrase
115 (goto-char (point-min))
116 (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
117 (pgg-add-passphrase-cache
119 (goto-char (point-min))
120 (if (re-search-forward
121 "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
122 (substring (match-string 0) -8)))
125 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-gpg)
126 string &optional type)
127 (let ((args (list "--with-colons" "--no-greeting" "--batch"
128 (if type "--list-secret-keys" "--list-keys")
131 (apply #'call-process pgg-gpg-program nil t nil args)
132 (goto-char (point-min))
133 (if (re-search-forward "^\\(sec\\|pub\\):" nil t)
136 (buffer-substring (match-end 0)
137 (progn (end-of-line)(point)))
140 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-gpg)
141 start end recipients)
142 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
144 `("--batch" "--armor" "--always-trust" "--encrypt"
147 (mapcar (lambda (rcpt)
148 (list "--remote-user" rcpt))
150 (if pgg-encrypt-for-me
151 (list pgg-gpg-user-id)))))))))
152 (pgg-as-lbt start end 'CRLF
153 (pgg-gpg-process-region start end nil pgg-gpg-program args))
154 (pgg-process-when-success)))
156 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-gpg)
158 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
161 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
162 (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'encrypt)))
163 (args '("--batch" "--decrypt")))
164 (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
165 (with-current-buffer pgg-errors-buffer
166 (pgg-gpg-possibly-cache-passphrase passphrase)
167 (goto-char (point-min))
168 (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
170 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-gpg)
171 start end &optional cleartext)
172 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
175 (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
176 (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'sign)))
178 (list (if cleartext "--clearsign" "--detach-sign")
179 "--armor" "--batch" "--verbose"
180 "--local-user" pgg-gpg-user-id))
181 (inhibit-read-only t)
183 (pgg-as-lbt start end 'CRLF
184 (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
185 (with-current-buffer pgg-errors-buffer
186 (pgg-gpg-possibly-cache-passphrase passphrase))
187 (pgg-process-when-success)))
189 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-gpg)
190 start end &optional signature)
191 (let ((args '("--batch" "--verify")))
192 (when (stringp signature)
193 (setq args (append args (list signature))))
194 (setq args (append args '("-")))
195 (pgg-gpg-process-region start end nil pgg-gpg-program args)
196 (with-current-buffer pgg-errors-buffer
197 (goto-char (point-min))
198 (while (re-search-forward "^gpg: " nil t)
200 (goto-char (point-min))
201 (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
202 (goto-char (point-min))
203 (delete-matching-lines "^warning\\|\\[GNUPG:]")
204 (set-buffer pgg-output-buffer)
205 (insert-buffer-substring pgg-errors-buffer)))))
207 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-gpg))
208 (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
209 (args (list "--batch" "--export" "--armor"
211 (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
212 (insert-buffer-substring pgg-output-buffer)))
214 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-gpg)
216 (let ((args '("--import" "--batch" "-")) status)
217 (pgg-gpg-process-region start end nil pgg-gpg-program args)
218 (set-buffer pgg-errors-buffer)
219 (goto-char (point-min))
220 (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
221 (setq status (buffer-substring (match-end 0)
222 (progn (end-of-line)(point)))
223 status (vconcat (mapcar #'string-to-int (split-string status))))
225 (insert (format "Imported %d key(s).
226 \tArmor contains %d key(s) [%d bad, %d old].\n"
233 (if (zerop (aref status 9))
235 "\tSecret keys are imported.\n")))
236 (append-to-buffer pgg-output-buffer (point-min)(point-max))
237 (pgg-process-when-success)))
241 ;;; pgg-gpg.el ends here