2 Python interface to GnuPG.
4 This module is used to invoke GnuPG to check the digital signatures on interfaces.
6 @see: L{iface_cache.PendingFeed}
9 # Copyright (C) 2006, Thomas Leonard
10 # See the README file for details, or visit http://0install.net.
16 from trust
import trust_db
17 from model
import SafeException
19 class Signature(object):
20 """Abstract base class for signature check results."""
23 def __init__(self
, status
):
26 def is_trusted(self
, domain
= None):
30 """Returns the ID of the key that must be downloaded to check this signature."""
33 class ValidSig(Signature
):
34 """A valid signature check result."""
39 return "Valid signature from " + self
.status
[self
.FINGERPRINT
]
41 def is_trusted(self
, domain
= None):
42 return trust_db
.is_trusted(self
.status
[self
.FINGERPRINT
], domain
)
44 def get_timestamp(self
):
45 return int(self
.status
[self
.TIMESTAMP
])
47 fingerprint
= property(lambda self
: self
.status
[self
.FINGERPRINT
])
49 def get_details(self
):
50 cin
, cout
= os
.popen2(('gpg', '--with-colons', '--no-secmem-warning', '--list-keys', self
.fingerprint
))
54 details
.append(line
.split(':'))
58 class BadSig(Signature
):
59 """A bad signature (doesn't match the message)."""
63 return "BAD signature by " + self
.status
[self
.KEYID
] + \
64 " (the message has been tampered with)"
66 class ErrSig(Signature
):
67 """Error while checking a signature."""
73 msg
= "ERROR signature by %s: " % self
.status
[self
.KEYID
]
74 rc
= int(self
.status
[self
.RC
])
76 msg
+= "Unknown or unsupported algorithm '%s'" % self
.status
[self
.ALG
]
78 msg
+= "Unknown key. Try 'gpg --recv-key %s'" % self
.status
[self
.KEYID
]
80 msg
+= "Unknown reason code %d" % rc
84 rc
= int(self
.status
[self
.RC
])
86 return self
.status
[self
.KEYID
]
93 def __init__(self
, fingerprint
):
94 self
.fingerprint
= fingerprint
95 self
.name
= '(unknown)'
97 def get_short_name(self
):
98 return self
.name
.split(' (', 1)[0].split(' <', 1)[0]
100 def load_keys(fingerprints
):
101 """Load a set of keys at once.
102 This is much more efficient than making individual calls to L{load_key}.
103 @return: a list of loaded keys, indexed by fingerprint
104 @rtype: {str: L{Key}}
109 # Otherwise GnuPG returns everything...
110 if not fingerprints
: return keys
112 for fp
in fingerprints
:
118 cin
, cout
= os
.popen2(['gpg', '--fixed-list-mode', '--with-colons', '--list-keys',
119 '--with-fingerprint', '--with-fingerprint'] + fingerprints
)
123 if line
.startswith('pub:'):
126 if line
.startswith('fpr:'):
127 current_fpr
= line
.split(':')[9]
128 if current_fpr
in keys
and current_uid
:
129 # This is probably a subordinate key, where the fingerprint
130 # comes after the uid, not before. Note: we assume the subkey is
131 # cross-certified, as recent always ones are.
132 keys
[current_fpr
].name
= current_uid
133 if line
.startswith('uid:'):
134 assert current_fpr
is not None
135 parts
= line
.split(':')
136 current_uid
= parts
[9]
137 if current_fpr
in keys
:
138 keys
[current_fpr
].name
= current_uid
144 def load_key(fingerprint
):
145 """Query gpg for information about this key.
149 return load_keys([fingerprint
])[fingerprint
]
151 def import_key(stream
):
152 """Run C{gpg --import} with this stream as stdin."""
153 errors
= tempfile
.TemporaryFile()
160 os
.dup2(stream
.fileno(), 0)
161 os
.dup2(errors
.fileno(), 2)
162 os
.execlp('gpg', 'gpg', '--no-secmem-warning', '--quiet', '--import')
164 traceback
.print_exc()
169 pid
, status
= os
.waitpid(child
, 0)
173 error_messages
= errors
.read().strip()
177 raise SafeException("Errors from 'gpg --import':\n%s" % error_messages
)
179 def _check_plain_stream(stream
):
180 data
= tempfile
.TemporaryFile() # Python2.2 does not support 'prefix'
181 errors
= tempfile
.TemporaryFile()
183 status_r
, status_w
= os
.pipe()
192 os
.dup2(stream
.fileno(), 0)
193 os
.dup2(data
.fileno(), 1)
194 os
.dup2(errors
.fileno(), 2)
195 os
.execlp('gpg', 'gpg', '--no-secmem-warning', '--decrypt',
196 # Not all versions support this:
197 #'--max-output', str(1024 * 1024),
199 '--status-fd', str(status_w
))
201 traceback
.print_exc()
210 sigs
= _get_sigs_from_gpg_status_stream(status_r
, child
, errors
)
215 def _check_xml_stream(stream
):
216 xml_comment_start
= '<!-- Base64 Signature'
218 data_to_check
= stream
.read()
220 last_comment
= data_to_check
.rfind('\n' + xml_comment_start
)
222 raise SafeException("No signature block in XML. Maybe this file isn't signed?")
223 last_comment
+= 1 # Include new-line in data
225 data
= tempfile
.TemporaryFile()
226 data
.write(data_to_check
[:last_comment
])
228 os
.lseek(data
.fileno(), 0, 0)
230 errors
= tempfile
.TemporaryFile()
232 sig_lines
= data_to_check
[last_comment
:].split('\n')
233 if sig_lines
[0].strip() != xml_comment_start
:
234 raise SafeException('Bad signature block: extra data on comment line')
235 while sig_lines
and not sig_lines
[-1].strip():
237 if sig_lines
[-1].strip() != '-->':
238 raise SafeException('Bad signature block: last line is not end-of-comment')
239 sig_data
= '\n'.join(sig_lines
[1:-1])
241 if re
.match('^[ A-Za-z0-9+/=\n]+$', sig_data
) is None:
242 raise SafeException("Invalid characters found in base 64 encoded signature")
244 sig_data
= base64
.decodestring(sig_data
) # (b64decode is Python 2.4)
245 except Exception, ex
:
246 raise SafeException("Invalid base 64 encoded signature: " + str(ex
))
248 sig_fd
, sig_name
= tempfile
.mkstemp(prefix
= 'injector-sig-')
250 sig_file
= os
.fdopen(sig_fd
, 'w')
251 sig_file
.write(sig_data
)
254 status_r
, status_w
= os
.pipe()
263 os
.dup2(data
.fileno(), 0)
264 os
.dup2(errors
.fileno(), 2)
265 os
.execlp('gpg', 'gpg', '--no-secmem-warning',
266 # Not all versions support this:
267 #'--max-output', str(1024 * 1024),
269 '--status-fd', str(status_w
),
270 '--verify', sig_name
, '-')
272 traceback
.print_exc()
281 sigs
= _get_sigs_from_gpg_status_stream(status_r
, child
, errors
)
283 os
.lseek(stream
.fileno(), 0, 0)
287 return (stream
, sigs
)
289 def _find_in_path(prog
):
290 for d
in os
.environ
['PATH'].split(':'):
291 path
= os
.path
.join(d
, prog
)
292 if os
.path
.isfile(path
):
296 def check_stream(stream
):
297 """Pass stream through gpg --decrypt to get the data, the error text,
298 and a list of signatures (good or bad). If stream starts with "<?xml "
299 then get the signature from a comment at the end instead (and the returned
300 data is the original stream). stream must be seekable.
301 @note: Stream returned may or may not be the one passed in. Be careful!
302 @return: (data_stream, [Signatures])"""
303 if not _find_in_path('gpg'):
304 raise SafeException("GnuPG is not installed ('gpg' not in $PATH). See http://gnupg.org")
310 start
= stream
.read(6)
312 if start
== "<?xml ":
313 return _check_xml_stream(stream
)
314 elif start
== '-----B':
316 warnings
.warn("Plain GPG-signed feeds are deprecated!", DeprecationWarning, stacklevel
= 2)
317 os
.lseek(stream
.fileno(), 0, 0)
318 return _check_plain_stream(stream
)
320 raise SafeException("This is not a Zero Install feed! It should be an XML document, but it starts:\n%s" % repr(stream
.read(120)))
322 def _get_sigs_from_gpg_status_stream(status_r
, child
, errors
):
323 """Read messages from status_r and collect signatures from it.
324 When done, reap 'child'.
325 If there are no signatures, throw SafeException (using errors
326 for the error message if non-empty)."""
329 # Should we error out on bad signatures, even if there's a good
332 for line
in os
.fdopen(status_r
):
333 assert line
.endswith('\n')
334 assert line
.startswith('[GNUPG:] ')
336 split_line
= line
.split(' ')
338 args
= split_line
[1:]
339 if code
== 'VALIDSIG':
340 sigs
.append(ValidSig(args
))
341 elif code
== 'BADSIG':
342 sigs
.append(BadSig(args
))
343 elif code
== 'ERRSIG':
344 sigs
.append(ErrSig(args
))
346 pid
, status
= os
.waitpid(child
, 0)
351 error_messages
= errors
.read().strip()
356 raise SafeException("No signatures found. Errors from GPG:\n%s" % error_messages
)
358 raise SafeException("No signatures found. No error messages from GPG.")