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