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