225
201
:return: boolean if this strategy can verify signatures
230
except ImportError as error:
206
except ImportError, error:
233
def sign(self, content, mode):
209
def _command_line(self):
210
key = self._config_stack.get('gpg_signing_key')
211
if key is None or key == 'default':
212
# 'default' or not setting gpg_signing_key at all means we should
213
# use the user email address
214
key = config.extract_email_address(self._config_stack.get('email'))
215
return [self._config_stack.get('gpg_signing_command'), '--clearsign',
218
def sign(self, content):
235
219
if isinstance(content, unicode):
236
220
raise errors.BzrBadParameterUnicode('content')
221
ui.ui_factory.clear_term()
238
plain_text = gpg.Data(content)
223
preexec_fn = _set_gpg_tty
224
if sys.platform == 'win32':
225
# Win32 doesn't support preexec_fn, but wouldn't support TTY anyway.
240
output, result = self.context.sign(
242
MODE_DETACH: gpg.constants.sig.mode.DETACH,
243
MODE_CLEAR: gpg.constants.sig.mode.CLEAR,
244
MODE_NORMAL: gpg.constants.sig.mode.NORMAL,
246
except gpg.errors.GPGMEError as error:
247
raise SigningFailed(str(error))
251
def verify(self, signed_data, signature=None):
228
process = subprocess.Popen(self._command_line(),
229
stdin=subprocess.PIPE,
230
stdout=subprocess.PIPE,
231
preexec_fn=preexec_fn)
233
result = process.communicate(content)[0]
234
if process.returncode is None:
236
if process.returncode != 0:
237
raise errors.SigningFailed(self._command_line())
240
if e.errno == errno.EPIPE:
241
raise errors.SigningFailed(self._command_line())
245
# bad subprocess parameters, should never happen.
248
if e.errno == errno.ENOENT:
249
# gpg is not installed
250
raise errors.SigningFailed(self._command_line())
254
def verify(self, content, testament):
252
255
"""Check content has a valid signature.
254
:param signed_data; Signed data
255
:param signature: optional signature (if detached)
257
:param content: the commit signature
258
:param testament: the valid testament string for the commit
257
:return: SIGNATURE_VALID or a failed SIGNATURE_ value, key uid if valid, plain text
260
:return: SIGNATURE_VALID or a failed SIGNATURE_ value, key uid if valid
261
except ImportError as error:
262
raise errors.GpgNotInstalled(error)
264
except ImportError, error:
265
raise errors.GpgmeNotInstalled(error)
264
signed_data = gpg.Data(signed_data)
266
signature = gpg.Data(signature)
267
signature = StringIO(content)
268
plain_output = StringIO()
268
plain_output, result = self.context.verify(signed_data, signature)
269
except gpg.errors.BadSignatures as error:
270
fingerprint = error.result.signatures[0].fpr
271
if error.result.signatures[0].summary & gpg.constants.SIGSUM_KEY_EXPIRED:
272
expires = self.context.get_key(error.result.signatures[0].fpr).subkeys[0].expires
273
if expires > error.result.signatures[0].timestamp:
274
# The expired key was not expired at time of signing.
275
# test_verify_expired_but_valid()
276
return SIGNATURE_EXPIRED, fingerprint[-8:], None
278
# I can't work out how to create a test where the signature
279
# was expired at the time of signing.
280
return SIGNATURE_NOT_VALID, None, None
282
# GPG does not know this key.
283
# test_verify_unknown_key()
284
if error.result.signatures[0].summary & gpg.constants.SIGSUM_KEY_MISSING:
285
return SIGNATURE_KEY_MISSING, fingerprint[-8:], None
287
return SIGNATURE_NOT_VALID, None, None
288
except gpg.errors.GPGMEError as error:
289
raise SignatureVerificationFailed(error)
270
result = self.context.verify(signature, None, plain_output)
271
except gpgme.GpgmeError,error:
272
raise errors.SignatureVerificationFailed(error[2])
291
274
# No result if input is invalid.
292
275
# test_verify_invalid()
293
if len(result.signatures) == 0:
294
return SIGNATURE_NOT_VALID, None, plain_output
277
return SIGNATURE_NOT_VALID, None
296
278
# User has specified a list of acceptable keys, check our result is in
297
279
# it. test_verify_unacceptable_key()
298
fingerprint = result.signatures[0].fpr
280
fingerprint = result[0].fpr
299
281
if self.acceptable_keys is not None:
300
282
if not fingerprint in self.acceptable_keys:
301
return SIGNATURE_KEY_MISSING, fingerprint[-8:], plain_output
302
# Yay gpg set the valid bit.
283
return SIGNATURE_KEY_MISSING, fingerprint[-8:]
284
# Check the signature actually matches the testament.
285
# test_verify_bad_testament()
286
if testament != plain_output.getvalue():
287
return SIGNATURE_NOT_VALID, None
288
# Yay gpgme set the valid bit.
303
289
# Can't write a test for this one as you can't set a key to be
305
if result.signatures[0].summary & gpg.constants.SIGSUM_VALID:
290
# trusted using gpgme.
291
if result[0].summary & gpgme.SIGSUM_VALID:
306
292
key = self.context.get_key(fingerprint)
307
293
name = key.uids[0].name
308
294
email = key.uids[0].email
309
return SIGNATURE_VALID, name.decode('utf-8') + u" <" + email.decode('utf-8') + u">", plain_output
295
return SIGNATURE_VALID, name + " <" + email + ">"
310
296
# Sigsum_red indicates a problem, unfortunatly I have not been able
311
297
# to write any tests which actually set this.
312
if result.signatures[0].summary & gpg.constants.SIGSUM_RED:
313
return SIGNATURE_NOT_VALID, None, plain_output
298
if result[0].summary & gpgme.SIGSUM_RED:
299
return SIGNATURE_NOT_VALID, None
300
# GPG does not know this key.
301
# test_verify_unknown_key()
302
if result[0].summary & gpgme.SIGSUM_KEY_MISSING:
303
return SIGNATURE_KEY_MISSING, fingerprint[-8:]
314
304
# Summary isn't set if sig is valid but key is untrusted but if user
315
305
# has explicity set the key as acceptable we can validate it.
316
if result.signatures[0].summary == 0 and self.acceptable_keys is not None:
306
if result[0].summary == 0 and self.acceptable_keys is not None:
317
307
if fingerprint in self.acceptable_keys:
318
308
# test_verify_untrusted_but_accepted()
319
return SIGNATURE_VALID, None, plain_output
309
return SIGNATURE_VALID, None
320
310
# test_verify_valid_but_untrusted()
321
if result.signatures[0].summary == 0 and self.acceptable_keys is None:
322
return SIGNATURE_NOT_VALID, None, plain_output
311
if result[0].summary == 0 and self.acceptable_keys is None:
312
return SIGNATURE_NOT_VALID, None
313
if result[0].summary & gpgme.SIGSUM_KEY_EXPIRED:
314
expires = self.context.get_key(result[0].fpr).subkeys[0].expires
315
if expires > result[0].timestamp:
316
# The expired key was not expired at time of signing.
317
# test_verify_expired_but_valid()
318
return SIGNATURE_EXPIRED, fingerprint[-8:]
320
# I can't work out how to create a test where the signature
321
# was expired at the time of signing.
322
return SIGNATURE_NOT_VALID, None
323
# A signature from a revoked key gets this.
324
# test_verify_revoked_signature()
325
if ((result[0].summary & gpgme.SIGSUM_SYS_ERROR
326
or result[0].status.strerror == 'Certificate revoked')):
327
return SIGNATURE_NOT_VALID, None
323
328
# Other error types such as revoked keys should (I think) be caught by
324
329
# SIGSUM_RED so anything else means something is buggy.
325
raise SignatureVerificationFailed(
330
raise errors.SignatureVerificationFailed(
326
331
"Unknown GnuPG key verification result")
328
333
def set_acceptable_keys(self, command_line_input):
353
358
"No GnuPG key results for pattern: {0}"
354
359
).format(pattern))
361
@deprecated_method(deprecated_in((2, 6, 0)))
362
def do_verifications(self, revisions, repository,
363
process_events_callback=None):
364
"""do verifications on a set of revisions
366
:param revisions: list of revision ids to verify
367
:param repository: repository object
368
:param process_events_callback: method to call for GUI frontends that
369
want to keep their UI refreshed
371
:return: count dictionary of results of each type,
372
result list for each revision,
373
boolean True if all results are verified successfully
375
return bulk_verify_signatures(repository, revisions, self,
376
process_events_callback)
378
@deprecated_method(deprecated_in((2, 6, 0)))
379
def verbose_valid_message(self, result):
380
"""takes a verify result and returns list of signed commits strings"""
381
return verbose_valid_message(result)
383
@deprecated_method(deprecated_in((2, 6, 0)))
384
def verbose_not_valid_message(self, result, repo):
385
"""takes a verify result and returns list of not valid commit info"""
386
return verbose_not_valid_message(result, repo)
388
@deprecated_method(deprecated_in((2, 6, 0)))
389
def verbose_not_signed_message(self, result, repo):
390
"""takes a verify result and returns list of not signed commit info"""
391
return verbose_not_valid_message(result, repo)
393
@deprecated_method(deprecated_in((2, 6, 0)))
394
def verbose_missing_key_message(self, result):
395
"""takes a verify result and returns list of missing key info"""
396
return verbose_missing_key_message(result)
398
@deprecated_method(deprecated_in((2, 6, 0)))
399
def verbose_expired_key_message(self, result, repo):
400
"""takes a verify result and returns list of expired key info"""
401
return verbose_expired_key_message(result, repo)
403
@deprecated_method(deprecated_in((2, 6, 0)))
404
def valid_commits_message(self, count):
405
"""returns message for number of commits"""
406
return valid_commits_message(count)
408
@deprecated_method(deprecated_in((2, 6, 0)))
409
def unknown_key_message(self, count):
410
"""returns message for number of commits"""
411
return unknown_key_message(count)
413
@deprecated_method(deprecated_in((2, 6, 0)))
414
def commit_not_valid_message(self, count):
415
"""returns message for number of commits"""
416
return commit_not_valid_message(count)
418
@deprecated_method(deprecated_in((2, 6, 0)))
419
def commit_not_signed_message(self, count):
420
"""returns message for number of commits"""
421
return commit_not_signed_message(count)
423
@deprecated_method(deprecated_in((2, 6, 0)))
424
def expired_commit_message(self, count):
425
"""returns message for number of commits"""
426
return expired_commit_message(count)
357
429
def valid_commits_message(count):
358
430
"""returns message for number of commits"""