Sync with semi21-1_14_0-pre4-2.
[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@unixuser.org>
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-extra-args nil
40   "Extra arguments for every GnuPG invocation."
41   :group 'pgg-gpg
42   :type 'string)
43
44 (eval-and-compile
45   (luna-define-class pgg-scheme-gpg (pgg-scheme)))
46
47 (defvar pgg-gpg-user-id nil
48   "GnuPG ID of your default identity.")
49
50 (defvar pgg-scheme-gpg-instance nil)
51
52 ;;;###autoload
53 (defun pgg-make-scheme-gpg ()
54   (or pgg-scheme-gpg-instance
55       (setq pgg-scheme-gpg-instance
56             (luna-make-entity 'pgg-scheme-gpg))))
57
58 (defun pgg-gpg-process-region (start end passphrase program args)
59   (let* ((output-file-name
60           (concat temporary-file-directory (make-temp-name "pgg-output")))
61          (args
62           `("--status-fd" "2"
63             ,@(if passphrase '("--passphrase-fd" "0"))
64             "--output" ,output-file-name
65             ,@pgg-gpg-extra-args ,@args))
66          (output-buffer pgg-output-buffer)
67          (errors-buffer pgg-errors-buffer)
68          (orig-mode (default-file-modes))
69          (process-connection-type nil)
70          process status exit-status)
71     (with-current-buffer (get-buffer-create errors-buffer)
72       (buffer-disable-undo)
73       (erase-buffer))
74     (unwind-protect
75         (progn
76           (set-default-file-modes 448)
77           (setq process
78                 (apply #'binary-start-process "*GnuPG*" errors-buffer
79                        program args))
80           (set-process-sentinel process #'ignore)
81           (when passphrase
82             (process-send-string process (concat passphrase "\n")))
83           (process-send-region process start end)
84           (process-send-eof process)
85           (while (eq 'run (process-status process))
86             (accept-process-output process 5))
87           (setq status (process-status process)
88                 exit-status (process-exit-status process))
89           (delete-process process)
90           (with-current-buffer (get-buffer-create output-buffer)
91             (buffer-disable-undo)
92             (erase-buffer)
93             (if (file-exists-p output-file-name)
94                 (let ((coding-system-for-read 'raw-text-dos))
95                   (insert-file-contents output-file-name)))
96             (set-buffer errors-buffer)
97             (if (memq status '(stop signal))
98                 (error "%s exited abnormally: '%s'" program exit-status))
99             (if (= 127 exit-status)
100                 (error "%s could not be found" program))))
101       (if (and process (eq 'run (process-status process)))
102           (interrupt-process process))
103       (if (file-exists-p output-file-name)
104           (delete-file output-file-name))
105       (set-default-file-modes orig-mode))))
106
107 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
108   (if (and pgg-cache-passphrase
109            (progn
110              (goto-char (point-min))
111              (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
112       (pgg-add-passphrase-cache
113        (progn
114          (goto-char (point-min))
115          (if (re-search-forward
116               "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
117              (substring (match-string 0) -8)))
118        passphrase)))
119
120 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-gpg)
121                                            string &optional type)
122   (let ((args (list "--with-colons" "--no-greeting" "--batch"
123                     (if type "--list-secret-keys" "--list-keys")
124                     string)))
125     (with-temp-buffer
126       (apply #'call-process pgg-gpg-program nil t nil args)
127       (goto-char (point-min))
128       (if (re-search-forward "^\\(sec\\|pub\\):"  nil t)
129           (substring
130            (nth 3 (split-string
131                    (buffer-substring (match-end 0)
132                                      (progn (end-of-line)(point)))
133                    ":")) 8)))))
134
135 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-gpg)
136                                                start end recipients)
137   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
138          (args
139           `("--batch" "--armor" "--always-trust" "--encrypt"
140             ,@(if recipients
141                   (apply #'nconc
142                          (mapcar (lambda (rcpt)
143                                    (list "--remote-user" rcpt))
144                                  (append recipients
145                                          (if pgg-encrypt-for-me
146                                              (list pgg-gpg-user-id)))))))))
147     (pgg-as-lbt start end 'CRLF
148       (pgg-gpg-process-region start end nil pgg-gpg-program args))
149     (pgg-process-when-success)))
150
151 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-gpg)
152                                                start end)
153   (let* ((pgg-gpg-user-id (or 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            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'encrypt)))
158          (args '("--batch" "--decrypt")))
159     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
160     (with-current-buffer pgg-errors-buffer
161       (pgg-gpg-possibly-cache-passphrase passphrase)
162       (goto-char (point-min))
163       (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
164
165 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-gpg)
166                                             start end &optional cleartext)
167   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
168          (passphrase
169           (pgg-read-passphrase
170            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
171            (pgg-scheme-lookup-key scheme pgg-gpg-user-id 'sign)))
172          (args
173           (list (if cleartext "--clearsign" "--detach-sign")
174                 "--armor" "--batch" "--verbose"
175                 "--local-user" pgg-gpg-user-id))
176          (inhibit-read-only t)
177          buffer-read-only)
178     (pgg-as-lbt start end 'CRLF
179       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
180     (with-current-buffer pgg-errors-buffer
181       (pgg-gpg-possibly-cache-passphrase passphrase))
182     (pgg-process-when-success)))
183
184 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-gpg)
185                                               start end &optional signature)
186   (let ((args '("--batch" "--verify")))
187     (when (stringp signature)
188       (setq args (append args (list signature))))
189     (pgg-gpg-process-region start end nil pgg-gpg-program args)
190     (with-current-buffer pgg-errors-buffer
191       (goto-char (point-min))
192       (while (re-search-forward "^gpg: " nil t)
193         (replace-match ""))
194       (goto-char (point-min))
195       (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
196         (goto-char (point-min))
197         (delete-matching-lines "^warning\\|\\[GNUPG:]")
198         (set-buffer pgg-output-buffer)
199         (insert-buffer-substring pgg-errors-buffer)))))
200
201 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-gpg))
202   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
203          (args (list "--batch" "--export" "--armor"
204                      pgg-gpg-user-id)))
205     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
206     (insert-buffer-substring pgg-output-buffer)))
207
208 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-gpg)
209                                                   start end)
210   (let ((args '("--import" "--batch" "-")) status)
211     (pgg-gpg-process-region start end nil pgg-gpg-program args)
212     (set-buffer pgg-errors-buffer)
213     (goto-char (point-min))
214     (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
215       (setq status (buffer-substring (match-end 0)
216                                      (progn (end-of-line)(point)))
217             status (vconcat (mapcar #'string-to-int (split-string status))))
218       (erase-buffer)
219       (insert (format "Imported %d key(s).
220 \tArmor contains %d key(s) [%d bad, %d old].\n"
221                       (+ (aref status 2)
222                          (aref status 10))
223                       (aref status 0)
224                       (aref status 1)
225                       (+ (aref status 4)
226                          (aref status 11)))
227               (if (zerop (aref status 9))
228                   ""
229                 "\tSecret keys are imported.\n")))
230     (append-to-buffer pgg-output-buffer (point-min)(point-max))
231     (pgg-process-when-success)))
232
233 (provide 'pgg-gpg)
234
235 ;;; pgg-gpg.el ends here