update.
[elisp/semi.git] / pgg-gpg.el
1 ;;; pgg-gpg.el --- GnuPG support for PGG.
2
3 ;; Copyright (C) 1999,2000 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.
41 Bourne shell or its equivalent \(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 (defvar pgg-gpg-user-id nil
59   "GnuPG ID of your default identity.")
60
61 (defvar pgg-scheme-gpg-instance nil)
62
63 ;;;###autoload
64 (defun pgg-make-scheme-gpg ()
65   (or pgg-scheme-gpg-instance
66       (setq pgg-scheme-gpg-instance
67             (luna-make-entity 'pgg-scheme-gpg))))
68
69 (defun pgg-gpg-process-region (start end passphrase program args)
70   (let* ((errors-file-name
71           (concat temporary-file-directory
72                   (make-temp-name "pgg-errors")))
73          (status-file-name
74           (concat temporary-file-directory
75                   (make-temp-name "pgg-status")))
76          (args
77           (append
78            `("--status-fd" "3"
79              ,@(if passphrase '("--passphrase-fd" "0"))
80              ,@pgg-gpg-extra-args)
81            args
82            (list (concat "2>" errors-file-name)
83                  (concat "3>" status-file-name))))
84          (shell-file-name pgg-gpg-shell-file-name)
85          (shell-command-switch pgg-gpg-shell-command-switch)
86          (output-buffer pgg-output-buffer)
87          (errors-buffer pgg-errors-buffer)
88          (status-buffer pgg-status-buffer)
89          (process-connection-type nil)
90          process status exit-status)
91     (with-current-buffer (get-buffer-create output-buffer)
92       (buffer-disable-undo)
93       (erase-buffer))
94     (as-binary-process
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 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-gpg)
132                                            string &optional type)
133   (let ((args (list "--with-colons" "--no-greeting" "--batch"
134                     (if type "--list-secret-keys" "--list-keys")
135                     string)))
136     (with-current-buffer (get-buffer-create pgg-output-buffer)
137       (buffer-disable-undo)
138       (erase-buffer)
139       (apply #'call-process pgg-gpg-program nil t nil args)
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 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-gpg)
150                                                start end recipients)
151   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
152          (args
153           `("--batch" "--armor" "--always-trust" "--encrypt"
154             ,@(if recipients
155                   (apply #'append
156                          (mapcar (lambda (rcpt)
157                                    (list "--remote-user"
158                                          (concat "\"" rcpt "\"")))
159                                  (append recipients
160                                          (if pgg-encrypt-for-me
161                                              (list pgg-gpg-user-id)))))))))
162     (pgg-as-lbt start end 'CRLF
163       (pgg-gpg-process-region start end nil pgg-gpg-program args))
164     (pgg-process-when-success
165       (pgg-convert-lbt-region (point-min)(point-max) 'LF))))
166
167 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-gpg)
168                                                start end)
169   (let* ((pgg-gpg-user-id (or 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            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'encrypt)))
174          (args '("--batch" "--decrypt")))
175     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
176     (pgg-process-when-success nil)))
177
178 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-gpg)
179                                             start end &optional cleartext)
180   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
181          (passphrase
182           (pgg-read-passphrase
183            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
184            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'sign)))
185          (args
186           (list (if cleartext "--clearsign" "--detach-sign")
187                 "--armor" "--batch" "--verbose"
188                 "--local-user" pgg-gpg-user-id))
189          (inhibit-read-only t)
190          buffer-read-only)
191     (pgg-as-lbt start end 'CRLF
192       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
193     (pgg-process-when-success
194       (pgg-convert-lbt-region (point-min)(point-max) 'LF)
195       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
196         (let ((packet
197                (cdr (assq 2 (pgg-parse-armor-region
198                              (progn (beginning-of-line 2)
199                                     (point))
200                              (point-max))))))
201           (if pgg-cache-passphrase
202               (pgg-add-passphrase-cache
203                (cdr (assq 'key-identifier packet))
204                passphrase)))))))
205
206 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-gpg)
207                                               start end &optional signature)
208   (let ((args '("--batch" "--verify")))
209     (when (stringp signature)
210       (setq args (append args (list signature))))
211     (pgg-gpg-process-region start end nil pgg-gpg-program args)
212     (save-excursion
213       (set-buffer pgg-errors-buffer)
214       (goto-char (point-min))
215       (while (re-search-forward "^gpg: " nil t)
216         (replace-match ""))
217       (goto-char (point-min))
218       (let ((case-fold-search t))
219         (while (re-search-forward "^warning: " nil t)
220           (delete-region (match-beginning 0)
221                          (progn (beginning-of-line 2) (point)))))
222       (set-buffer pgg-status-buffer)
223       (goto-char (point-min))
224       (if (re-search-forward "^\\[GNUPG:] +GOODSIG +" nil t)
225           (progn
226             (set-buffer pgg-output-buffer)
227             (insert-buffer-substring pgg-errors-buffer)
228             t)
229         nil))))
230
231 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-gpg))
232   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
233          (args (list "--batch" "--export" "--armor"
234                      (concat "\"" pgg-gpg-user-id "\""))))
235     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
236     (insert-buffer-substring pgg-output-buffer)))
237
238 (luna-define-method pgg-scheme-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 (mapcar #'string-to-int
249                                     (split-string status))))
250       (erase-buffer)
251       (insert (format "Imported %d key(s).
252 \tArmor contains %d key(s) [%d bad, %d old].\n"
253                       (+ (aref status 2)
254                          (aref status 10))
255                       (aref status 0)
256                       (aref status 1)
257                       (+ (aref status 4)
258                          (aref status 11)))
259               (if (zerop (aref status 9))
260                   ""
261                 "\tSecret keys are imported.\n")))
262     (append-to-buffer pgg-output-buffer
263                       (point-min)(point-max))
264     (pgg-process-when-success nil)))
265
266 (provide 'pgg-gpg)
267
268 ;;; pgg-gpg.el ends here