* pgg-pgp.el (sign-region): Use `pgg-as-lbt'.
[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       (pgg-convert-lbt-region (point-min)(point-max) 'LF)
110
111       (if (memq status '(stop signal))
112           (error "%s exited abnormally: '%s'" program exit-status))
113       (if (= 127 exit-status)
114           (error "%s could not be found" program))
115
116       (set-buffer (get-buffer-create errors-buffer))
117       (buffer-disable-undo)
118       (erase-buffer)
119       (insert-file-contents errors-file-name)
120       (delete-file errors-file-name)
121       
122       (set-buffer (get-buffer-create status-buffer))
123       (buffer-disable-undo)
124       (erase-buffer)
125       (insert-file-contents status-file-name)
126       (delete-file status-file-name)
127
128       (if (and process (eq 'run (process-status process)))
129           (interrupt-process process))
130       )
131     ))
132
133 (luna-define-method lookup-key-string ((scheme pgg-scheme-gpg)
134                                        string &optional type)
135   (let ((args (list "--with-colons" "--no-greeting" "--batch" 
136                     (if type "--list-secret-keys" "--list-keys")
137                     string)))
138     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
139     (with-current-buffer pgg-output-buffer
140       (goto-char (point-min))
141       (when (re-search-forward "^\\(sec\\|pub\\):"  nil t)
142         (substring 
143          (nth 3 (split-string 
144                  (buffer-substring (match-end 0)
145                                    (progn (end-of-line)(point)))
146                  ":"))
147          8)))
148     ))
149
150 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg) 
151                                     start end recipients)
152   (let* ((pgg-gpg-user-id pgg-default-user-id)
153          (passphrase
154           (pgg-read-passphrase 
155            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
156            (luna-send scheme 'lookup-key-string
157                       scheme pgg-gpg-user-id 'encrypt)))
158          (args 
159           `("--batch" "--armor" "--always-trust" "--encrypt"
160             ,@(if recipients
161                   (apply #'append 
162                          (mapcar (lambda (rcpt) 
163                                    (list "--remote-user" 
164                                          (concat "\"" rcpt "\""))) 
165                                  recipients))))))
166     (pgg-as-lbt start end 'CRLF
167       (pgg-gpg-process-region start end passphrase 
168                               pgg-gpg-program args)
169       )
170     (pgg-process-when-success
171       (pgg-convert-lbt-region (point-min)(point-max) 'LF)
172       (let ((packet 
173              (cdr (assq 1 (pgg-parse-armor-region 
174                            (point-min)(point-max))))))
175         (pgg-add-passphrase-cache 
176          (cdr (assq 'key-identifier packet))
177          passphrase)))
178     ))
179
180 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg) 
181                                     start end)
182   (let* ((pgg-gpg-user-id pgg-default-user-id)
183          (passphrase
184           (pgg-read-passphrase 
185            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
186            (luna-send scheme 'lookup-key-string 
187                       scheme pgg-gpg-user-id 'encrypt)))
188          (args '("--batch" "--decrypt")))
189     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
190     (pgg-process-when-success nil)
191     ))
192
193 (luna-define-method sign-region ((scheme pgg-scheme-gpg) 
194                                  start end &optional cleartext)
195   (let* ((pgg-gpg-user-id pgg-default-user-id)
196          (passphrase
197           (pgg-read-passphrase 
198            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
199            (luna-send scheme 'lookup-key-string 
200                       scheme pgg-gpg-user-id 'sign)))
201          (args 
202           (list (if cleartext "--clearsign" "--detach-sign")
203                 "--armor" "--batch" "--verbose" 
204                 "--local-user" pgg-gpg-user-id))
205          (inhibit-read-only t)
206          buffer-read-only)
207     (pgg-as-lbt start end 'CRLF
208       (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
209       )
210     (pgg-process-when-success
211       (pgg-convert-lbt-region (point-min)(point-max) 'LF)
212       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
213         (let ((packet 
214                (cdr (assq 2 (pgg-parse-armor-region 
215                              (progn (beginning-of-line 2)
216                                     (point))
217                              (point-max))))))
218           (pgg-add-passphrase-cache 
219            (cdr (assq 'key-identifier packet))
220            passphrase))))
221     ))
222
223 (luna-define-method verify-region ((scheme pgg-scheme-gpg) 
224                                    start end &optional signature)
225   (let ((args '("--batch" "--verify")))
226     (when (stringp signature)
227       (setq args (append args (list signature))))
228     (pgg-gpg-process-region start end nil pgg-gpg-program args)
229     (save-excursion
230       (set-buffer pgg-errors-buffer)
231       (goto-char (point-min))
232       (while (re-search-forward "^gpg: " nil t)
233         (replace-match ""))
234       (goto-char (point-min))
235       (let ((case-fold-search t))
236         (while (re-search-forward "^warning: " nil t)
237           (delete-region (match-beginning 0)
238                          (progn (beginning-of-line 2) (point)))))
239       (set-buffer pgg-status-buffer)
240       (goto-char (point-min))
241       (if (re-search-forward "^\\[GNUPG:] +GOODSIG +" nil t)
242           (progn
243             (set-buffer pgg-output-buffer)
244             (insert-buffer-substring pgg-errors-buffer)
245             t)
246         nil))
247     ))
248
249 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
250   (let* ((pgg-gpg-user-id pgg-default-user-id)
251          (args (list "--batch" "--export" "--armor" 
252                      (concat "\"" pgg-gpg-user-id "\""))))
253     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
254     (insert-buffer-substring pgg-output-buffer)
255     ))
256
257 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
258                                        start end)
259   (let ((args '("--import" "--batch" "-")) status)
260     (pgg-gpg-process-region start end nil pgg-gpg-program args)
261     (set-buffer pgg-status-buffer)
262     (goto-char (point-min))
263     (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
264       (setq status (buffer-substring (match-end 0) 
265                                      (progn (end-of-line) 
266                                             (point)))
267             status (vconcat (mapcar #'string-to-int 
268                                     (split-string status))))
269       (erase-buffer)
270       (insert (format "Imported %d key(s).
271 \tArmor contains %d key(s) [%d bad, %d old].\n"
272                       (+ (aref status 2)
273                          (aref status 10))
274                       (aref status 0)
275                       (aref status 1)
276                       (+ (aref status 4)
277                          (aref status 11)))
278               (if (zerop (aref status 9))
279                   ""
280                 "\tSecret keys are imported.\n")))
281     (append-to-buffer pgg-output-buffer
282                       (point-min)(point-max))
283     (pgg-process-when-success nil)
284     ))
285
286 (provide 'pgg-gpg)
287
288 ;;; pgg-gpg.el ends here
289