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 logging
import warn
17 from trust
import trust_db
18 from model
import SafeException
20 class Signature(object):
21 """Abstract base class for signature check results."""
24 def __init__(self
, status
):
27 def is_trusted(self
, domain
= None):
31 """Returns the ID of the key that must be downloaded to check this signature."""
34 class ValidSig(Signature
):
35 """A valid signature check result."""
40 return "Valid signature from " + self
.status
[self
.FINGERPRINT
]
42 def is_trusted(self
, domain
= None):
43 return trust_db
.is_trusted(self
.status
[self
.FINGERPRINT
], domain
)
45 def get_timestamp(self
):
46 return int(self
.status
[self
.TIMESTAMP
])
48 fingerprint
= property(lambda self
: self
.status
[self
.FINGERPRINT
])
50 def get_details(self
):
51 cin
, cout
= os
.popen2(('gpg', '--with-colons', '--no-secmem-warning', '--list-keys', self
.fingerprint
))
55 details
.append(line
.split(':'))
59 class BadSig(Signature
):
60 """A bad signature (doesn't match the message)."""
64 return "BAD signature by " + self
.status
[self
.KEYID
] + \
65 " (the message has been tampered with)"
67 class ErrSig(Signature
):
68 """Error while checking a signature."""
74 msg
= "ERROR signature by %s: " % self
.status
[self
.KEYID
]
75 rc
= int(self
.status
[self
.RC
])
77 msg
+= "Unknown or unsupported algorithm '%s'" % self
.status
[self
.ALG
]
79 msg
+= "Unknown key. Try 'gpg --recv-key %s'" % self
.status
[self
.KEYID
]
81 msg
+= "Unknown reason code %d" % rc
85 rc
= int(self
.status
[self
.RC
])
87 return self
.status
[self
.KEYID
]
94 def __init__(self
, fingerprint
):
95 self
.fingerprint
= fingerprint
96 self
.name
= '(unknown)'
98 def get_short_name(self
):
99 return self
.name
.split(' (', 1)[0].split(' <', 1)[0]
101 def load_keys(fingerprints
):
102 """Load a set of keys at once.
103 This is much more efficient than making individual calls to L{load_key}.
104 @return: a list of loaded keys, indexed by fingerprint
105 @rtype: {str: L{Key}}
110 # Otherwise GnuPG returns everything...
111 if not fingerprints
: return keys
113 for fp
in fingerprints
:
119 cin
, cout
= os
.popen2(['gpg', '--fixed-list-mode', '--with-colons', '--list-keys',
120 '--with-fingerprint', '--with-fingerprint'] + fingerprints
)
124 if line
.startswith('pub:'):
127 if line
.startswith('fpr:'):
128 current_fpr
= line
.split(':')[9]
129 if current_fpr
in keys
and current_uid
:
130 # This is probably a subordinate key, where the fingerprint
131 # comes after the uid, not before. Note: we assume the subkey is
132 # cross-certified, as recent always ones are.
133 keys
[current_fpr
].name
= current_uid
134 if line
.startswith('uid:'):
135 assert current_fpr
is not None
136 parts
= line
.split(':')
137 current_uid
= parts
[9]
138 if current_fpr
in keys
:
139 keys
[current_fpr
].name
= current_uid
145 def load_key(fingerprint
):
146 """Query gpg for information about this key.
150 return load_keys([fingerprint
])[fingerprint
]
152 def import_key(stream
):
153 """Run C{gpg --import} with this stream as stdin."""
154 errors
= tempfile
.TemporaryFile()
161 os
.dup2(stream
.fileno(), 0)
162 os
.dup2(errors
.fileno(), 2)
163 os
.execlp('gpg', 'gpg', '--no-secmem-warning', '--quiet', '--import')
165 traceback
.print_exc()
170 pid
, status
= os
.waitpid(child
, 0)
174 error_messages
= errors
.read().strip()
178 raise SafeException("Errors from 'gpg --import':\n%s" % error_messages
)
180 def _check_plain_stream(stream
):
181 data
= tempfile
.TemporaryFile() # Python2.2 does not support 'prefix'
182 errors
= tempfile
.TemporaryFile()
184 status_r
, status_w
= os
.pipe()
193 os
.dup2(stream
.fileno(), 0)
194 os
.dup2(data
.fileno(), 1)
195 os
.dup2(errors
.fileno(), 2)
196 os
.execlp('gpg', 'gpg', '--no-secmem-warning', '--decrypt',
197 # Not all versions support this:
198 #'--max-output', str(1024 * 1024),
200 '--status-fd', str(status_w
))
202 traceback
.print_exc()
211 sigs
= _get_sigs_from_gpg_status_stream(status_r
, child
, errors
)
216 def _check_xml_stream(stream
):
217 xml_comment_start
= '<!-- Base64 Signature'
219 data_to_check
= stream
.read()
221 last_comment
= data_to_check
.rfind('\n' + xml_comment_start
)
223 raise SafeException("No signature block in XML. Maybe this file isn't signed?")
224 last_comment
+= 1 # Include new-line in data
226 data
= tempfile
.TemporaryFile()
227 data
.write(data_to_check
[:last_comment
])
229 os
.lseek(data
.fileno(), 0, 0)
231 errors
= tempfile
.TemporaryFile()
233 sig_lines
= data_to_check
[last_comment
:].split('\n')
234 if sig_lines
[0].strip() != xml_comment_start
:
235 raise SafeException('Bad signature block: extra data on comment line')
236 while sig_lines
and not sig_lines
[-1].strip():
238 if sig_lines
[-1].strip() != '-->':
239 raise SafeException('Bad signature block: last line is not end-of-comment')
240 sig_data
= '\n'.join(sig_lines
[1:-1])
242 if re
.match('^[ A-Za-z0-9+/=\n]+$', sig_data
) is None:
243 raise SafeException("Invalid characters found in base 64 encoded signature")
245 sig_data
= base64
.decodestring(sig_data
) # (b64decode is Python 2.4)
246 except Exception, ex
:
247 raise SafeException("Invalid base 64 encoded signature: " + str(ex
))
249 sig_fd
, sig_name
= tempfile
.mkstemp(prefix
= 'injector-sig-')
251 sig_file
= os
.fdopen(sig_fd
, 'w')
252 sig_file
.write(sig_data
)
255 status_r
, status_w
= os
.pipe()
264 os
.dup2(data
.fileno(), 0)
265 os
.dup2(errors
.fileno(), 2)
266 os
.execlp('gpg', 'gpg', '--no-secmem-warning',
267 # Not all versions support this:
268 #'--max-output', str(1024 * 1024),
270 '--status-fd', str(status_w
),
271 '--verify', sig_name
, '-')
273 traceback
.print_exc()
282 sigs
= _get_sigs_from_gpg_status_stream(status_r
, child
, errors
)
284 os
.lseek(stream
.fileno(), 0, 0)
288 return (stream
, sigs
)
290 def _find_in_path(prog
):
291 for d
in os
.environ
['PATH'].split(':'):
292 path
= os
.path
.join(d
, prog
)
293 if os
.path
.isfile(path
):
297 def check_stream(stream
):
298 """Pass stream through gpg --decrypt to get the data, the error text,
299 and a list of signatures (good or bad). If stream starts with "<?xml "
300 then get the signature from a comment at the end instead (and the returned
301 data is the original stream). stream must be seekable.
302 @note: Stream returned may or may not be the one passed in. Be careful!
303 @return: (data_stream, [Signatures])"""
304 if not _find_in_path('gpg'):
305 raise SafeException("GnuPG is not installed ('gpg' not in $PATH). See http://gnupg.org")
311 start
= stream
.read(6)
313 if start
== "<?xml ":
314 return _check_xml_stream(stream
)
315 elif start
== '-----B':
316 warn("Plain GPG-signed feeds are deprecated!")
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.")