be9f7cdfa95fcad26dc0c68c78080ee2ece2e3f0
[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          (passphrase
131           (when sign
132             (pgg-read-passphrase
133              (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
134              (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
135          (args
136           (append
137            (list "--batch" "--armor" "--always-trust" "--encrypt")
138            (if sign '("--sign"))
139            (if recipients
140                (apply #'nconc
141                       (mapcar (lambda (rcpt)
142                                 (list "--remote-user" rcpt))
143                               (append recipients
144                                       (if pgg-encrypt-for-me
145                                           (list pgg-gpg-user-id)))))))))
146     (pgg-as-lbt start end 'CRLF
147       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
148     (when sign
149       (with-current-buffer pgg-errors-buffer
150         (pgg-gpg-possibly-cache-passphrase passphrase)))
151     (pgg-process-when-success)))
152
153 (defun pgg-gpg-decrypt-region (start end)
154   "Decrypt the current region between START and END."
155   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
156          (passphrase
157           (pgg-read-passphrase
158            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
159            (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
160          (args '("--batch" "--decrypt")))
161     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
162     (with-current-buffer pgg-errors-buffer
163       (pgg-gpg-possibly-cache-passphrase passphrase)
164       (goto-char (point-min))
165       (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
166
167 (defun pgg-gpg-sign-region (start end &optional cleartext)
168   "Make detached signature from text between START and END."
169   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
170          (passphrase
171           (pgg-read-passphrase
172            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
173            (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
174          (args
175           (list (if cleartext "--clearsign" "--detach-sign")
176                 "--armor" "--batch" "--verbose"
177                 "--local-user" pgg-gpg-user-id))
178          (inhibit-read-only t)
179          buffer-read-only)
180     (pgg-as-lbt start end 'CRLF
181       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
182     (with-current-buffer pgg-errors-buffer
183       (pgg-gpg-possibly-cache-passphrase passphrase))
184     (pgg-process-when-success)))
185
186 (defun pgg-gpg-verify-region (start end &optional signature)
187   "Verify region between START and END as the detached signature SIGNATURE."
188   (let ((args '("--batch" "--verify")))
189     (when (stringp signature)
190       (setq args (append args (list signature))))
191     (setq args (append args '("-")))
192     (pgg-gpg-process-region start end nil pgg-gpg-program args)
193     (with-current-buffer pgg-errors-buffer
194       (goto-char (point-min))
195       (while (re-search-forward "^gpg: " nil t)
196         (replace-match ""))
197       (goto-char (point-min))
198       (prog1 (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t)
199         (goto-char (point-min))
200         (delete-matching-lines "^warning\\|\\[GNUPG:]")
201         (set-buffer pgg-output-buffer)
202         (insert-buffer-substring pgg-errors-buffer)))))
203
204 (defun pgg-gpg-insert-key ()
205   "Insert public key at point."
206   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
207          (args (list "--batch" "--export" "--armor"
208                      pgg-gpg-user-id)))
209     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
210     (insert-buffer-substring pgg-output-buffer)))
211
212 (defun pgg-gpg-snarf-keys-region (start end)
213   "Add all public keys in region between START and END to the keyring."
214   (let ((args '("--import" "--batch" "-")) status)
215     (pgg-gpg-process-region start end nil pgg-gpg-program args)
216     (set-buffer pgg-errors-buffer)
217     (goto-char (point-min))
218     (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
219       (setq status (buffer-substring (match-end 0)
220                                      (progn (end-of-line)(point)))
221             status (vconcat (mapcar #'string-to-int (split-string status))))
222       (erase-buffer)
223       (insert (format "Imported %d key(s).
224 \tArmor contains %d key(s) [%d bad, %d old].\n"
225                       (+ (aref status 2)
226                          (aref status 10))
227                       (aref status 0)
228                       (aref status 1)
229                       (+ (aref status 4)
230                          (aref status 11)))
231               (if (zerop (aref status 9))
232                   ""
233                 "\tSecret keys are imported.\n")))
234     (append-to-buffer pgg-output-buffer (point-min)(point-max))
235     (pgg-process-when-success)))
236
237 (provide 'pgg-gpg)
238
239 ;;; pgg-gpg.el ends here