2f3c098daed64e2aa4a7380c7a3df04a7fe38e05
[gnus] / lisp / pgg-gpg.el
1 ;;; pgg-gpg.el --- GnuPG support for PGG.
2
3 ;; Copyright (C) 1999, 2000, 2003 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 '(choice
43           (const :tag "None" nil)
44           (string :tag "Arguments")))
45
46 (defvar pgg-gpg-user-id nil
47   "GnuPG ID of your default identity.")
48
49 (defun pgg-gpg-process-region (start end passphrase program args)
50   (let* ((output-file-name (pgg-make-temp-file "pgg-output"))
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          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                  (input (buffer-substring-no-properties start end)))
69             (with-temp-buffer
70               (when passphrase
71                 (insert passphrase "\n"))
72               (insert input)
73               (setq exit-status
74                     (apply #'call-process-region (point-min) (point-max) program
75                            nil errors-buffer nil args))))
76           (with-current-buffer (get-buffer-create output-buffer)
77             (buffer-disable-undo)
78             (erase-buffer)
79             (if (file-exists-p output-file-name)
80                 (let ((coding-system-for-read 'raw-text-dos))
81                   (insert-file-contents output-file-name)))
82             (set-buffer errors-buffer)
83             (if (not (equal exit-status 0))
84                 (insert (format "\n%s exited abnormally: '%s'\n"
85                                 program exit-status)))))
86       (if (file-exists-p output-file-name)
87           (delete-file output-file-name))
88       (set-default-file-modes orig-mode))))
89
90 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
91   (if (and pgg-cache-passphrase
92            (progn
93              (goto-char (point-min))
94              (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
95       (pgg-add-passphrase-cache
96        (progn
97          (goto-char (point-min))
98          (if (re-search-forward
99               "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
100              (substring (match-string 0) -8)))
101        passphrase)))
102
103 (defun pgg-gpg-lookup-key (string &optional type)
104   "Search keys associated with STRING."
105   (let ((args (list "--with-colons" "--no-greeting" "--batch"
106                     (if type "--list-secret-keys" "--list-keys")
107                     string)))
108     (with-temp-buffer
109       (apply #'call-process pgg-gpg-program nil t nil args)
110       (goto-char (point-min))
111       (if (re-search-forward "^\\(sec\\|pub\\):"  nil t)
112           (substring
113            (nth 3 (split-string
114                    (buffer-substring (match-end 0)
115                                      (progn (end-of-line)(point)))
116                    ":")) 8)))))
117
118 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
119   "Encrypt the current region between START and END.
120 If optional argument SIGN is non-nil, do a combined sign and encrypt."
121   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
122          (passphrase
123           (when sign
124             (pgg-read-passphrase
125              (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
126              (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
127          (args
128           (append
129            (list "--batch" "--armor" "--always-trust" "--encrypt")
130            (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
131            (if recipients
132                (apply #'nconc
133                       (mapcar (lambda (rcpt)
134                                 (list "--remote-user" rcpt))
135                               (append recipients
136                                       (if pgg-encrypt-for-me
137                                           (list pgg-gpg-user-id)))))))))
138     (pgg-as-lbt start end 'CRLF
139       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
140     (when sign
141       (with-current-buffer pgg-errors-buffer
142         (pgg-gpg-possibly-cache-passphrase passphrase)))
143     (pgg-process-when-success)))
144
145 (defun pgg-gpg-decrypt-region (start end)
146   "Decrypt the current region between START and END."
147   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
148          (passphrase
149           (pgg-read-passphrase
150            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
151            (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
152          (args '("--batch" "--decrypt")))
153     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
154     (with-current-buffer pgg-errors-buffer
155       (pgg-gpg-possibly-cache-passphrase passphrase)
156       (goto-char (point-min))
157       (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
158
159 (defun pgg-gpg-sign-region (start end &optional cleartext)
160   "Make detached signature from text between START and END."
161   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
162          (passphrase
163           (pgg-read-passphrase
164            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
165            (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
166          (args
167           (list (if cleartext "--clearsign" "--detach-sign")
168                 "--armor" "--batch" "--verbose"
169                 "--local-user" pgg-gpg-user-id))
170          (inhibit-read-only t)
171          buffer-read-only)
172     (pgg-as-lbt start end 'CRLF
173       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
174     (with-current-buffer pgg-errors-buffer
175       (pgg-gpg-possibly-cache-passphrase passphrase))
176     (pgg-process-when-success)))
177
178 (defun pgg-gpg-verify-region (start end &optional signature)
179   "Verify region between START and END as the detached signature SIGNATURE."
180   (let ((args '("--batch" "--verify")))
181     (when (stringp signature)
182       (setq args (append args (list signature))))
183     (setq args (append args '("-")))
184     (pgg-gpg-process-region start end nil pgg-gpg-program args)
185     (with-current-buffer pgg-errors-buffer
186       (goto-char (point-min))
187       (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
188         (with-current-buffer pgg-output-buffer
189           (insert-buffer-substring pgg-errors-buffer
190                                    (match-beginning 1) (match-end 0)))
191         (delete-region (match-beginning 0) (match-end 0)))
192       (goto-char (point-min))
193       (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
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