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