* pgg-gpg.el, pgg-pgp.el, pgg-pgp5.el (snarf-keys-region):
[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   "File name to load inferior shells from.  Bourne shell or its equivalent
41 \(not tcsh) is needed for \"2>\"."
42   :group 'pgg-gpg
43   :type 'string)
44
45 (defcustom pgg-gpg-shell-command-switch "-c"
46   "Switch used to have the shell execute its command line argument."
47   :group 'pgg-gpg
48   :type 'string)
49
50 (defcustom pgg-gpg-extra-args nil
51   "Extra arguments for every GnuPG invocation."
52   :group 'pgg-gpg
53   :type 'string)
54
55 (eval-and-compile
56   (luna-define-class pgg-scheme-gpg (pgg-scheme))
57   )
58   
59 (defvar pgg-gpg-user-id nil
60   "GnuPG ID of your default identity.")
61
62 (defvar pgg-scheme-gpg-instance nil)
63
64 ;;;###autoload
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))))
69
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")))
74          (status-file-name
75           (concat temporary-file-directory 
76                   (make-temp-name "pgg-status")))
77          (args 
78           (append
79            `("--status-fd" "3"
80              ,@(if passphrase '("--passphrase-fd" "0"))
81              ,@pgg-gpg-extra-args)
82            args
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)
93       (buffer-disable-undo)
94       (erase-buffer))
95     (setq process
96           (apply #'start-process-shell-command "*GnuPG*" output-buffer
97                  program args))
98     (set-process-sentinel process 'ignore)
99     (when passphrase
100       (process-send-string process (concat passphrase "\n")))
101     (process-send-region process start end)
102     (process-send-eof process)
103     (while (eq 'run (process-status process))
104       (accept-process-output process 5))
105     (setq status (process-status process)
106           exit-status (process-exit-status process))
107     (delete-process process)
108     (with-current-buffer output-buffer
109       (goto-char (point-min))
110       (while (search-forward "\r$" nil t)
111         (replace-match ""))
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))
116
117       (set-buffer (get-buffer-create errors-buffer))
118       (buffer-disable-undo)
119       (erase-buffer)
120       (insert-file-contents errors-file-name)
121       (delete-file errors-file-name)
122       
123       (set-buffer (get-buffer-create status-buffer))
124       (buffer-disable-undo)
125       (erase-buffer)
126       (insert-file-contents status-file-name)
127       (delete-file status-file-name)
128
129       (if (and process (eq 'run (process-status process)))
130           (interrupt-process process))
131       )
132     ))
133
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")
138                     string)))
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)
143         (substring 
144          (nth 3 (split-string 
145                  (buffer-substring (match-end 0)
146                                    (progn (end-of-line)(point)))
147                  ":"))
148          8)))
149     ))
150
151 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg) 
152                                     start end recipients)
153   (let* ((pgg-gpg-user-id pgg-default-user-id)
154          (passphrase
155           (pgg-read-passphrase 
156            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
157            (luna-send scheme 'lookup-key-string
158                       scheme pgg-gpg-user-id 'encrypt)))
159          (args 
160           `("--batch" "--armor" "--textmode" "--always-trust" "--encrypt"
161             ,@(if recipients
162                   (apply #'append 
163                          (mapcar (lambda (rcpt) 
164                                    (list "--remote-user" 
165                                          (concat "\"" rcpt "\""))) 
166                                  recipients))))))
167     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
168     (pgg-process-when-success
169       (let ((packet 
170              (cdr (assq 1 (pgg-parse-armor-region 
171                            (point-min)(point-max))))))
172         (pgg-add-passphrase-cache 
173          (cdr (assq 'key-identifier packet))
174          passphrase)))
175     ))
176
177 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg) 
178                                     start end)
179   (let* ((pgg-gpg-user-id pgg-default-user-id)
180          (passphrase
181           (pgg-read-passphrase 
182            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
183            (luna-send scheme 'lookup-key-string 
184                       scheme pgg-gpg-user-id 'encrypt)))
185          (args '("--batch" "--decrypt")))
186     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
187     (pgg-process-when-success nil)
188     ))
189
190 (luna-define-method sign-region ((scheme pgg-scheme-gpg) 
191                                  start end &optional cleartext)
192   (let* ((pgg-gpg-user-id pgg-default-user-id)
193          (passphrase
194           (pgg-read-passphrase 
195            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
196            (luna-send scheme 'lookup-key-string 
197                       scheme pgg-gpg-user-id 'sign)))
198          (args 
199           (list (if cleartext "--clearsign" "--detach-sign")
200                 "--armor" "--batch" "--verbose" 
201                 "--local-user" pgg-gpg-user-id)))
202     (goto-char start)
203     (setq end (set-marker (make-marker) (point-max)))
204     (while (progn (end-of-line) (> (marker-position end) (point)))
205       (insert "\r")
206       (forward-line 1))
207     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
208     (goto-char start)
209     (while (re-search-forward "\r$" end t)
210       (replace-match ""))
211     (pgg-process-when-success
212       (goto-char (point-min))
213       (while (re-search-forward "\r$" nil t)
214         (replace-match ""))
215       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
216         (let ((packet 
217                (cdr (assq 2 (pgg-parse-armor-region 
218                              (progn (beginning-of-line 2)
219                                     (point))
220                              (point-max))))))
221           (pgg-add-passphrase-cache 
222            (cdr (assq 'key-identifier packet))
223            passphrase))))
224     ))
225
226 (luna-define-method verify-region ((scheme pgg-scheme-gpg) 
227                                    start end &optional signature)
228   (let ((args '("--batch" "--verify")))
229     (when (stringp signature)
230       (setq args (append args (list signature))))
231     (pgg-gpg-process-region start end nil pgg-gpg-program args)
232     (save-excursion
233       (set-buffer pgg-status-buffer)
234       (goto-char (point-min))
235       (when (re-search-forward "^\\[GNUPG:] +GOODSIG +" nil t)
236         (set-buffer pgg-errors-buffer)
237         (goto-char (point-min))
238         (while (re-search-forward "^gpg: " nil t)
239           (replace-match ""))
240         (goto-char (point-min))
241         (let ((case-fold-search t))
242           (while (re-search-forward "^warning: " nil t)
243             (delete-region (match-beginning 0)
244                            (progn (beginning-of-line 2) (point)))))
245         (append-to-buffer pgg-output-buffer
246                           (point-min)(point-max))
247         t))
248     ))
249
250 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
251   (let* ((pgg-gpg-user-id pgg-default-user-id)
252          (args (list "--batch" "--export" "--armor" 
253                      (concat "\"" pgg-gpg-user-id "\""))))
254     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
255     (insert-buffer-substring pgg-output-buffer)
256     ))
257
258 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
259                                        start end)
260   (let ((args '("--import" "--batch" "-")) status)
261     (pgg-gpg-process-region start end nil pgg-gpg-program args)
262     (set-buffer pgg-status-buffer)
263     (goto-char (point-min))
264     (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
265       (setq status (buffer-substring (match-end 0) 
266                                      (progn (end-of-line) 
267                                             (point)))
268             status (vconcat (mapcar #'string-to-int 
269                                     (split-string status))))
270       (erase-buffer)
271       (insert (format "Imported %d key(s).
272 \tArmor contains %d key(s) [%d bad, %d old].\n"
273                       (+ (aref status 2)
274                          (aref status 10))
275                       (aref status 0)
276                       (aref status 1)
277                       (+ (aref status 4)
278                          (aref status 11)))
279               (if (zerop (aref status 9))
280                   ""
281                 "\tSecret keys are imported.\n")))
282     (append-to-buffer pgg-output-buffer
283                       (point-min)(point-max))
284     (pgg-process-when-success nil)
285     ))
286
287 (provide 'pgg-gpg)
288
289 ;;; pgg-gpg.el ends here
290