(lookup-key): New generic method.
[elisp/semi.git] / pgg-gpg.el
1 ;;; pgg-gpg.el --- GnuPG support for PGG.
2
3 ;; Copyright (C) 1999 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6 ;; Created: 1999/10/28
7 ;; Keywords: PGP, OpenPGP, GnuPG
8
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
10
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.
15
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.
20
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.
25
26 ;;; Code:
27
28 (eval-when-compile (require 'pgg))
29
30 (defgroup pgg-gpg ()
31   "GnuPG interface"
32   :group 'pgg)
33
34 (defcustom pgg-gpg-program "gpg" 
35   "The GnuPG executable."
36   :group 'pgg-gpg
37   :type 'string)
38
39 (defcustom pgg-gpg-shell-file-name "/bin/sh"
40   "The GnuPG executable."
41   :group 'pgg-gpg
42   :type 'string)
43
44 (defcustom pgg-gpg-extra-args nil
45   "Extra arguments for every GnuPG invocation."
46   :group 'pgg-gpg
47   :type 'string)
48
49 (eval-and-compile
50   (luna-define-class pgg-scheme-gpg (pgg-scheme))
51   )
52   
53 (defvar pgg-gpg-user-id nil
54   "GnuPG ID of your default identity.")
55
56 (defvar pgg-scheme-gpg-instance nil)
57
58 ;;;###autoload
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))))
63
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")))
68          (status-file-name
69           (concat temporary-file-directory 
70                   (make-temp-name "pgg-status")))
71          (args 
72           (append
73            `("--status-fd" "3"
74              ,@(if passphrase '("--passphrase-fd" "0"))
75              ,@pgg-gpg-extra-args)
76            args
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)
86       (buffer-disable-undo)
87       (erase-buffer))
88     (setq process
89           (apply #'start-process-shell-command "*GnuPG*" output-buffer
90                  program args))
91     (set-process-sentinel process 'ignore)
92     (when passphrase
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)
104         (replace-match ""))
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))
109
110       (set-buffer (get-buffer-create errors-buffer))
111       (buffer-disable-undo)
112       (erase-buffer)
113       (insert-file-contents errors-file-name)
114       (delete-file errors-file-name)
115       
116       (set-buffer (get-buffer-create status-buffer))
117       (buffer-disable-undo)
118       (erase-buffer)
119       (insert-file-contents status-file-name)
120       (delete-file status-file-name)
121
122       (if (and process (eq 'run (process-status process)))
123           (interrupt-process process))
124       )
125     ))
126
127 (luna-define-method lookup-key ((scheme pgg-scheme-gpg) string)
128   (let ((args (list "--with-colons" "--no-greeting" "--batch" 
129                     "--list-secret-keys" string)))
130     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
131     (with-current-buffer pgg-output-buffer
132       (goto-char (point-min))
133       (when (re-search-forward "^\\(sec\\|pub\\):"  nil t)
134         (nth 3 (split-string 
135                 (buffer-substring (match-end 0)
136                                   (progn (end-of-line)(point)))
137                 ":"))))
138     ))
139
140 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg) 
141                                     start end recipients)
142   (let* ((pgg-gpg-user-id pgg-default-user-id)
143          (passphrase
144           (pgg-read-passphrase 
145            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
146            (luna-send scheme 'lookup-key scheme pgg-gpg-user-id)))
147          (args 
148           `("--batch" "--armor" "--textmode" "--always-trust" "--encrypt"
149             ,@(if recipients
150                   (apply #'append 
151                          (mapcar (lambda (rcpt) 
152                                    (list "--remote-user" 
153                                          (concat "\"" rcpt "\""))) 
154                                  recipients))))))
155     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
156     (with-current-buffer pgg-output-buffer
157       (if (zerop (buffer-size))
158           (insert-buffer-substring pgg-errors-buffer)
159         (let ((packet 
160                (cdr (assq 1 (pgg-parse-armor-region 
161                              (point-min)(point-max))))))
162           (pgg-add-passphrase-cache 
163            (cdr (assq 'key-identifier packet))
164            passphrase))))
165     ))
166
167 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg) 
168                                     start end)
169   (let* ((pgg-gpg-user-id pgg-default-user-id)
170          (passphrase
171           (pgg-read-passphrase 
172            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
173            (luna-send scheme 'lookup-key scheme pgg-gpg-user-id)))
174          (args '("--batch" "--decrypt")))
175     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
176     (with-current-buffer pgg-output-buffer
177       (when (zerop (buffer-size))
178         (insert-buffer-substring pgg-errors-buffer)))
179     ))
180
181 (luna-define-method sign-region ((scheme pgg-scheme-gpg) 
182                                  start end)
183   (let* ((pgg-gpg-user-id pgg-default-user-id)
184          (passphrase
185           (pgg-read-passphrase 
186            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
187            (luna-send scheme 'lookup-key scheme pgg-gpg-user-id)))
188          (args 
189           (list "--detach-sign" "--armor" "--batch" "--verbose" 
190                 "--local-user" pgg-gpg-user-id)))
191     (goto-char start)
192     (setq end (set-marker (make-marker) (point-max)))
193     (while (progn (end-of-line) (> (marker-position end) (point)))
194       (insert "\r")
195       (forward-line 1))
196     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
197     (goto-char start)
198     (while (re-search-forward "\r$" end t)
199       (replace-match ""))
200     (with-current-buffer pgg-output-buffer
201       (if (zerop (buffer-size))
202           (insert-buffer-substring pgg-errors-buffer)
203         (let ((packet 
204                (cdr (assq 2 (pgg-parse-armor-region 
205                              (point-min)(point-max))))))
206           (pgg-add-passphrase-cache 
207            (cdr (assq 'key-identifier packet))
208            passphrase))))
209     ))
210
211 (luna-define-method verify-region ((scheme pgg-scheme-gpg) 
212                                    start end &optional signature)
213   (let ((args '("--batch" "--verify")))
214     (when (stringp signature)
215       (setq args (append args (list signature))))
216     (pgg-gpg-process-region start end nil pgg-gpg-program args)
217     (set-buffer pgg-errors-buffer)
218     (goto-char (point-min))
219     (while (re-search-forward "^gpg: " nil t)
220       (replace-match ""))
221     (goto-char (point-min))
222     (let ((case-fold-search t))
223       (while (re-search-forward "^warning: " nil t)
224         (delete-region (match-beginning 0)
225                        (progn (beginning-of-line 2) (point)))))
226     (append-to-buffer pgg-output-buffer
227                       (point-min)(point-max))
228     ))
229
230 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
231   (let* ((pgg-gpg-user-id pgg-default-user-id)
232          (args (list "--batch" "--export" "--armor" 
233                      (concat "\"" pgg-gpg-user-id "\""))))
234     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
235     (insert-buffer-substring pgg-output-buffer)
236     ))
237
238 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
239                                        start end)
240   (let ((args '("--import" "--batch")) status)
241     (pgg-gpg-process-region start end nil pgg-gpg-program args)
242     (set-buffer pgg-status-buffer)
243     (goto-char (point-min))
244     (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
245       (setq status (buffer-substring (match-end 0) 
246                                      (progn (end-of-line) 
247                                             (point)))
248             status (vconcat (split-string status)))
249       (erase-buffer)
250       (insert (aref status 0) "keys seen\n"
251               (format "\t%d bad, %d new, %d old\n"
252                       (string-to-int (aref status 1))
253                       (+ (string-to-int (aref status 2))
254                          (string-to-int (aref status 10)))
255                       (+ (string-to-int (aref status 4))
256                          (string-to-int (aref status 11))))
257               (if (zerop (aref status 9))
258                   ""
259                 "\tSecret keys are imported\n")))
260     (append-to-buffer pgg-output-buffer
261                       (point-min)(point-max))
262     (with-current-buffer pgg-output-buffer
263       (when (zerop (buffer-size))
264         (insert-buffer-substring pgg-errors-buffer)))
265     ))
266
267 (provide 'pgg-gpg)
268
269 ;;; pgg-gpg.el ends here
270