update.
[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-string ((scheme pgg-scheme-gpg)
128                                        string &optional type)
129   (let ((args (list "--with-colons" "--no-greeting" "--batch" 
130                     (if type "--list-secret-keys" "--list-keys")
131                     string)))
132     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
133     (with-current-buffer pgg-output-buffer
134       (goto-char (point-min))
135       (when (re-search-forward "^\\(sec\\|pub\\):"  nil t)
136         (substring 
137          (nth 3 (split-string 
138                  (buffer-substring (match-end 0)
139                                    (progn (end-of-line)(point)))
140                  ":"))
141          8)))
142     ))
143
144 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg) 
145                                     start end recipients)
146   (let* ((pgg-gpg-user-id pgg-default-user-id)
147          (passphrase
148           (pgg-read-passphrase 
149            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
150            (luna-send scheme 'lookup-key-string
151                       scheme pgg-gpg-user-id 'encrypt)))
152          (args 
153           `("--batch" "--armor" "--textmode" "--always-trust" "--encrypt"
154             ,@(if recipients
155                   (apply #'append 
156                          (mapcar (lambda (rcpt) 
157                                    (list "--remote-user" 
158                                          (concat "\"" rcpt "\""))) 
159                                  recipients))))))
160     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
161     (with-current-buffer pgg-output-buffer
162       (if (zerop (buffer-size))
163           (insert-buffer-substring pgg-errors-buffer)
164         (let ((packet 
165                (cdr (assq 1 (pgg-parse-armor-region 
166                              (point-min)(point-max))))))
167           (pgg-add-passphrase-cache 
168            (cdr (assq 'key-identifier packet))
169            passphrase))))
170     ))
171
172 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg) 
173                                     start end)
174   (let* ((pgg-gpg-user-id pgg-default-user-id)
175          (passphrase
176           (pgg-read-passphrase 
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     (with-current-buffer pgg-output-buffer
183       (when (zerop (buffer-size))
184         (insert-buffer-substring pgg-errors-buffer)))
185     ))
186
187 (luna-define-method sign-region ((scheme pgg-scheme-gpg) 
188                                  start end)
189   (let* ((pgg-gpg-user-id pgg-default-user-id)
190          (passphrase
191           (pgg-read-passphrase 
192            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
193            (luna-send scheme 'lookup-key-string 
194                       scheme pgg-gpg-user-id 'sign)))
195          (args 
196           (list "--detach-sign" "--armor" "--batch" "--verbose" 
197                 "--local-user" pgg-gpg-user-id)))
198     (goto-char start)
199     (setq end (set-marker (make-marker) (point-max)))
200     (while (progn (end-of-line) (> (marker-position end) (point)))
201       (insert "\r")
202       (forward-line 1))
203     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
204     (goto-char start)
205     (while (re-search-forward "\r$" end t)
206       (replace-match ""))
207     (with-current-buffer pgg-output-buffer
208       (if (zerop (buffer-size))
209           (insert-buffer-substring pgg-errors-buffer)
210         (let ((packet 
211                (cdr (assq 2 (pgg-parse-armor-region 
212                              (point-min)(point-max))))))
213           (pgg-add-passphrase-cache 
214            (cdr (assq 'key-identifier packet))
215            passphrase))))
216     ))
217
218 (luna-define-method verify-region ((scheme pgg-scheme-gpg) 
219                                    start end &optional signature)
220   (let ((args '("--batch" "--verify")))
221     (when (stringp signature)
222       (setq args (append args (list signature))))
223     (pgg-gpg-process-region start end nil pgg-gpg-program args)
224     (set-buffer pgg-errors-buffer)
225     (goto-char (point-min))
226     (while (re-search-forward "^gpg: " nil t)
227       (replace-match ""))
228     (goto-char (point-min))
229     (let ((case-fold-search t))
230       (while (re-search-forward "^warning: " nil t)
231         (delete-region (match-beginning 0)
232                        (progn (beginning-of-line 2) (point)))))
233     (append-to-buffer pgg-output-buffer
234                       (point-min)(point-max))
235     ))
236
237 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
238   (let* ((pgg-gpg-user-id pgg-default-user-id)
239          (args (list "--batch" "--export" "--armor" 
240                      (concat "\"" pgg-gpg-user-id "\""))))
241     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
242     (insert-buffer-substring pgg-output-buffer)
243     ))
244
245 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
246                                        start end)
247   (let ((args '("--import" "--batch")) status)
248     (pgg-gpg-process-region start end nil pgg-gpg-program args)
249     (set-buffer pgg-status-buffer)
250     (goto-char (point-min))
251     (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
252       (setq status (buffer-substring (match-end 0) 
253                                      (progn (end-of-line) 
254                                             (point)))
255             status (vconcat (split-string status)))
256       (erase-buffer)
257       (insert (aref status 0) "keys seen\n"
258               (format "\t%d bad, %d new, %d old\n"
259                       (string-to-int (aref status 1))
260                       (+ (string-to-int (aref status 2))
261                          (string-to-int (aref status 10)))
262                       (+ (string-to-int (aref status 4))
263                          (string-to-int (aref status 11))))
264               (if (zerop (aref status 9))
265                   ""
266                 "\tSecret keys are imported\n")))
267     (append-to-buffer pgg-output-buffer
268                       (point-min)(point-max))
269     (with-current-buffer pgg-output-buffer
270       (when (zerop (buffer-size))
271         (insert-buffer-substring pgg-errors-buffer)))
272     ))
273
274 (provide 'pgg-gpg)
275
276 ;;; pgg-gpg.el ends here
277