Import Oort Gnus v0.16.
[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                 (insert (format "\n%s exited abnormally: '%s'\n"
87                                 program exit-status)))))
88       (if (file-exists-p output-file-name)
89           (delete-file output-file-name))
90       (set-default-file-modes orig-mode))))
91
92 (defun pgg-gpg-possibly-cache-passphrase (passphrase)
93   (if (and pgg-cache-passphrase
94            (progn
95              (goto-char (point-min))
96              (re-search-forward "^\\[GNUPG:] GOOD_PASSPHRASE\\>" nil t)))
97       (pgg-add-passphrase-cache
98        (progn
99          (goto-char (point-min))
100          (if (re-search-forward
101               "^\\[GNUPG:] NEED_PASSPHRASE \\w+ ?\\w*" nil t)
102              (substring (match-string 0) -8)))
103        passphrase)))
104
105 (defun pgg-gpg-lookup-key (string &optional type)
106   "Search keys associated with STRING."
107   (let ((args (list "--with-colons" "--no-greeting" "--batch"
108                     (if type "--list-secret-keys" "--list-keys")
109                     string)))
110     (with-temp-buffer
111       (apply #'call-process pgg-gpg-program nil t nil args)
112       (goto-char (point-min))
113       (if (re-search-forward "^\\(sec\\|pub\\):"  nil t)
114           (substring
115            (nth 3 (split-string
116                    (buffer-substring (match-end 0)
117                                      (progn (end-of-line)(point)))
118                    ":")) 8)))))
119
120 (defun pgg-gpg-encrypt-region (start end recipients &optional sign)
121   "Encrypt the current region between START and END.
122 If optional argument SIGN is non-nil, do a combined sign and encrypt."
123   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
124          (passphrase
125           (when sign
126             (pgg-read-passphrase
127              (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
128              (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt))))
129          (args
130           (append
131            (list "--batch" "--armor" "--always-trust" "--encrypt")
132            (if sign (list "--sign" "--local-user" pgg-gpg-user-id))
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 passphrase pgg-gpg-program args))
142     (when sign
143       (with-current-buffer pgg-errors-buffer
144         (pgg-gpg-possibly-cache-passphrase passphrase)))
145     (pgg-process-when-success)))
146
147 (defun pgg-gpg-decrypt-region (start end)
148   "Decrypt the current region between START and END."
149   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
150          (passphrase
151           (pgg-read-passphrase
152            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
153            (pgg-gpg-lookup-key pgg-gpg-user-id 'encrypt)))
154          (args '("--batch" "--decrypt")))
155     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
156     (with-current-buffer pgg-errors-buffer
157       (pgg-gpg-possibly-cache-passphrase passphrase)
158       (goto-char (point-min))
159       (re-search-forward "^\\[GNUPG:] DECRYPTION_OKAY\\>" nil t))))
160
161 (defun pgg-gpg-sign-region (start end &optional cleartext)
162   "Make detached signature from text between START and END."
163   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
164          (passphrase
165           (pgg-read-passphrase
166            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)
167            (pgg-gpg-lookup-key pgg-gpg-user-id 'sign)))
168          (args
169           (list (if cleartext "--clearsign" "--detach-sign")
170                 "--armor" "--batch" "--verbose"
171                 "--local-user" pgg-gpg-user-id))
172          (inhibit-read-only t)
173          buffer-read-only)
174     (pgg-as-lbt start end 'CRLF
175       (pgg-gpg-process-region start end passphrase pgg-gpg-program args))
176     (with-current-buffer pgg-errors-buffer
177       (pgg-gpg-possibly-cache-passphrase passphrase))
178     (pgg-process-when-success)))
179
180 (defun pgg-gpg-verify-region (start end &optional signature)
181   "Verify region between START and END as the detached signature SIGNATURE."
182   (let ((args '("--batch" "--verify")))
183     (when (stringp signature)
184       (setq args (append args (list signature))))
185     (setq args (append args '("-")))
186     (pgg-gpg-process-region start end nil pgg-gpg-program args)
187     (with-current-buffer pgg-errors-buffer
188       (goto-char (point-min))
189       (while (re-search-forward "^gpg: \\(.*\\)\n" nil t)
190         (with-current-buffer pgg-output-buffer
191           (insert-buffer-substring pgg-errors-buffer
192                                    (match-beginning 1) (match-end 0)))
193         (delete-region (match-beginning 0) (match-end 0)))
194       (goto-char (point-min))
195       (re-search-forward "^\\[GNUPG:] GOODSIG\\>" nil t))))
196
197 (defun pgg-gpg-insert-key ()
198   "Insert public key at point."
199   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
200          (args (list "--batch" "--export" "--armor"
201                      pgg-gpg-user-id)))
202     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
203     (insert-buffer-substring pgg-output-buffer)))
204
205 (defun pgg-gpg-snarf-keys-region (start end)
206   "Add all public keys in region between START and END to the keyring."
207   (let ((args '("--import" "--batch" "-")) status)
208     (pgg-gpg-process-region start end nil pgg-gpg-program args)
209     (set-buffer pgg-errors-buffer)
210     (goto-char (point-min))
211     (when (re-search-forward "^\\[GNUPG:] IMPORT_RES\\>" nil t)
212       (setq status (buffer-substring (match-end 0)
213                                      (progn (end-of-line)(point)))
214             status (vconcat (mapcar #'string-to-int (split-string status))))
215       (erase-buffer)
216       (insert (format "Imported %d key(s).
217 \tArmor contains %d key(s) [%d bad, %d old].\n"
218                       (+ (aref status 2)
219                          (aref status 10))
220                       (aref status 0)
221                       (aref status 1)
222                       (+ (aref status 4)
223                          (aref status 11)))
224               (if (zerop (aref status 9))
225                   ""
226                 "\tSecret keys are imported.\n")))
227     (append-to-buffer pgg-output-buffer (point-min)(point-max))
228     (pgg-process-when-success)))
229
230 (provide 'pgg-gpg)
231
232 ;;; pgg-gpg.el ends here