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