bzr branch
http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
399.1.3
by Daniel Schierbeck
Added GPG module. |
1 |
# Code for running GnuPG in batch mode and dealing with the results
|
2 |
||
3 |
__rcsid__ = '$Id: GPG.py,v 1.1.1.1 2000/04/17 02:17:24 amk Exp $' |
|
4 |
||
5 |
import os, string |
|
6 |
import cStringIO, popen2 |
|
7 |
||
8 |
class Signature: |
|
9 |
"Class used to hold information about a signature result"
|
|
10 |
||
11 |
def __init__(self): |
|
12 |
self.valid = 0 |
|
13 |
self.fingerprint = self.creation_date = self.timestamp = None |
|
14 |
self.signature_id = self.key_id = None |
|
15 |
self.username = None |
|
16 |
||
17 |
def is_valid(self): |
|
18 |
return self.valid |
|
19 |
||
20 |
class GPGSubprocess: |
|
21 |
||
22 |
# Default path used for searching for the GPG binary, when the
|
|
23 |
# PATH environment variable isn't set.
|
|
24 |
DEFAULT_PATH = ['/bin', '/usr/bin', '/usr/local/bin'] |
|
25 |
||
26 |
def __init__(self, gpg_binary = None): |
|
27 |
"""Initialize an object instance. Options are: |
|
28 |
||
29 |
gpg_binary -- full pathname for GPG binary. If not supplied,
|
|
30 |
the current value of PATH will be searched, falling back to the
|
|
31 |
DEFAULT_PATH class variable if PATH isn't available.
|
|
32 |
"""
|
|
33 |
||
34 |
# If needed, look for the gpg binary along the path
|
|
35 |
if gpg_binary is None: |
|
36 |
import os |
|
37 |
if os.environ.has_key('PATH'): |
|
38 |
path = os.environ['PATH'] |
|
39 |
path = string.split(path, os.pathsep) |
|
40 |
else: |
|
41 |
path = self.DEFAULT_PATH |
|
42 |
||
43 |
for dir in path: |
|
44 |
fullname = os.path.join(dir, 'gpg') |
|
45 |
if os.path.exists( fullname ): |
|
46 |
gpg_binary = fullname |
|
47 |
break
|
|
48 |
else: |
|
49 |
raise ValueError, ("Couldn't find 'gpg' binary on path" |
|
50 |
+ repr(path) ) |
|
51 |
||
52 |
self.gpg_binary = gpg_binary |
|
53 |
||
54 |
def verify(self, data): |
|
55 |
"Verify the signature on the contents of the string 'data'"
|
|
56 |
file = cStringIO.StringIO( data ) |
|
57 |
return self.verify_file( file ) |
|
58 |
||
59 |
def verify_file(self, file): |
|
60 |
"Verify the signature on the contents of the file-like object 'file'"
|
|
61 |
child_stdout, child_stdin, child_stderr = self._open_subprocess() |
|
62 |
||
63 |
# Copy the file to the GPG subprocess
|
|
64 |
while 1: |
|
65 |
data = file.read(1024) |
|
66 |
if data == "": break |
|
67 |
child_stdin.write(data) |
|
68 |
||
69 |
child_stdin.close() |
|
70 |
||
71 |
# Get the response information
|
|
72 |
resp = self._read_response(child_stderr) |
|
73 |
||
74 |
# Create an object to return, and fill it with data
|
|
75 |
sig = Signature() |
|
76 |
if resp.has_key('BADSIG'): |
|
77 |
sig.valid = 0 |
|
78 |
sig.key_id, sig.username = string.split(resp['BADSIG'], None, 1) |
|
79 |
elif resp.has_key('GOODSIG'): |
|
80 |
sig.valid = 1 |
|
81 |
sig.key_id, sig.username = string.split(resp['GOODSIG'], None, 1) |
|
82 |
||
83 |
if resp.has_key('VALIDSIG'): |
|
84 |
L = string.split(resp['VALIDSIG'], None) |
|
399.1.4
by Daniel Schierbeck
Fixed bug in GPG module. |
85 |
sig.fingerprint, sig.creation_date, sig.timestamp = L[0], L[1], L[2] |
399.1.3
by Daniel Schierbeck
Added GPG module. |
86 |
|
87 |
if resp.has_key('SIG_ID'): |
|
88 |
L = string.split(resp['SIG_ID'], None) |
|
89 |
sig.signature_id, sig.creation_date, sig.timestamp = L |
|
90 |
||
91 |
# Read the contents of the file from GPG's stdout
|
|
92 |
sig.data = "" |
|
93 |
while 1: |
|
94 |
data = child_stdout.read(1024) |
|
95 |
if data == "": break |
|
96 |
sig.data = sig.data + data |
|
97 |
||
98 |
return sig |
|
99 |
||
100 |
def _open_subprocess(self, *args): |
|
101 |
# Internal method: open a pipe to a GPG subprocess and return
|
|
102 |
# the file objects for communicating with it.
|
|
103 |
||
104 |
cmd = self.gpg_binary + ' --status-fd 2 ' + string.join(args) |
|
105 |
||
106 |
child_stdout, child_stdin, child_stderr = popen2.popen3(cmd) |
|
107 |
return child_stdout, child_stdin, child_stderr |
|
108 |
||
109 |
def _read_response(self, child_stdout): |
|
110 |
# Internal method: reads all the output from GPG, taking notice
|
|
111 |
# only of lines that begin with the magic [GNUPG:] prefix.
|
|
112 |
# (See doc/DETAILS in the GPG distribution for info on GPG's
|
|
113 |
# output when --status-fd is specified.)
|
|
114 |
#
|
|
115 |
# Returns a dictionary, mapping GPG's keywords to the arguments
|
|
116 |
# for that keyword.
|
|
117 |
||
118 |
resp = {} |
|
119 |
while 1: |
|
120 |
line = child_stdout.readline() |
|
121 |
if line == "": break |
|
122 |
line = string.rstrip( line ) |
|
123 |
if line[0:9] == '[GNUPG:] ': |
|
124 |
# Chop off the prefix
|
|
125 |
line = line[9:] |
|
126 |
L = string.split(line, None, 1) |
|
127 |
keyword = L[0] |
|
128 |
if len(L) > 1: |
|
129 |
resp[ keyword ] = L[1] |
|
130 |
else: |
|
131 |
resp[ keyword ] = "" |
|
132 |
return resp |
|
133 |
||
134 |
||
135 |
# Not yet implemented, because I don't need these methods
|
|
136 |
# The methods certainly don't have all the parameters they'd need.
|
|
137 |
||
138 |
def sign(self, data): |
|
139 |
"Sign the contents of the string 'data'"
|
|
140 |
pass
|
|
141 |
||
142 |
def sign_file(self, file): |
|
143 |
"Sign the contents of the file-like object 'file'"
|
|
144 |
pass
|
|
145 |
||
146 |
def encrypt_file(self, file): |
|
147 |
"Encrypt the message read from the file-like object 'file'"
|
|
148 |
pass
|
|
149 |
||
150 |
def encrypt(self, data): |
|
151 |
"Encrypt the message contained in the string 'data'"
|
|
152 |
pass
|
|
153 |
||
154 |
def decrypt_file(self, file): |
|
155 |
"Decrypt the message read from the file-like object 'file'"
|
|
156 |
pass
|
|
157 |
||
158 |
def decrypt(self, data): |
|
159 |
"Decrypt the message contained in the string 'data'"
|
|
160 |
pass
|
|
161 |
||
162 |
||
163 |
if __name__ == '__main__': |
|
164 |
import sys |
|
165 |
if len(sys.argv) == 1: |
|
166 |
print 'Usage: GPG.py <signed file>' |
|
167 |
sys.exit() |
|
168 |
||
169 |
obj = GPGSubprocess() |
|
170 |
file = open(sys.argv[1], 'rb') |
|
171 |
sig = obj.verify_file( file ) |
|
172 |
print sig.__dict__ |
|
173 |