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