105
143
self.acceptable_keys.append(pattern)
145
@deprecated_method(deprecated_in((2, 6, 0)))
107
146
def do_verifications(self, revisions, repository):
108
count = {SIGNATURE_VALID: 0,
109
SIGNATURE_KEY_MISSING: 0,
110
SIGNATURE_NOT_VALID: 0,
111
SIGNATURE_NOT_SIGNED: 0}
113
all_verifiable = True
114
for rev_id in revisions:
115
verification_result, uid =\
116
repository.verify_revision(rev_id,self)
117
result.append([rev_id, verification_result, uid])
118
count[verification_result] += 1
119
if verification_result != SIGNATURE_VALID:
120
all_verifiable = False
121
return (count, result, all_verifiable)
147
return bulk_verify_signatures(repository, revisions, self)
149
@deprecated_method(deprecated_in((2, 6, 0)))
123
150
def valid_commits_message(self, count):
124
return i18n.gettext(u"{0} commits with valid signatures").format(
125
count[SIGNATURE_VALID])
151
return valid_commits_message(count)
153
@deprecated_method(deprecated_in((2, 6, 0)))
127
154
def unknown_key_message(self, count):
128
return i18n.ngettext(u"{0} commit with unknown key",
129
u"{0} commits with unknown keys",
130
count[SIGNATURE_KEY_MISSING]).format(
131
count[SIGNATURE_KEY_MISSING])
155
return unknown_key_message(count)
157
@deprecated_method(deprecated_in((2, 6, 0)))
133
158
def commit_not_valid_message(self, count):
134
return i18n.ngettext(u"{0} commit not valid",
135
u"{0} commits not valid",
136
count[SIGNATURE_NOT_VALID]).format(
137
count[SIGNATURE_NOT_VALID])
159
return commit_not_valid_message(count)
161
@deprecated_method(deprecated_in((2, 6, 0)))
139
162
def commit_not_signed_message(self, count):
140
return i18n.ngettext(u"{0} commit not signed",
141
u"{0} commits not signed",
142
count[SIGNATURE_NOT_SIGNED]).format(
143
count[SIGNATURE_NOT_SIGNED])
163
return commit_not_signed_message(count)
165
@deprecated_method(deprecated_in((2, 6, 0)))
166
def expired_commit_message(self, count):
167
return expired_commit_message(count)
146
170
def _set_gpg_tty():
237
267
signature = StringIO(content)
238
268
plain_output = StringIO()
241
270
result = self.context.verify(signature, None, plain_output)
242
271
except gpgme.GpgmeError,error:
243
272
raise errors.SignatureVerificationFailed(error[2])
274
# No result if input is invalid.
275
# test_verify_invalid()
245
276
if len(result) == 0:
246
277
return SIGNATURE_NOT_VALID, None
278
# User has specified a list of acceptable keys, check our result is in
279
# it. test_verify_unacceptable_key()
247
280
fingerprint = result[0].fpr
248
281
if self.acceptable_keys is not None:
249
282
if not fingerprint in self.acceptable_keys:
250
283
return SIGNATURE_KEY_MISSING, fingerprint[-8:]
284
# Check the signature actually matches the testament.
285
# test_verify_bad_testament()
251
286
if testament != plain_output.getvalue():
252
287
return SIGNATURE_NOT_VALID, None
288
# Yay gpgme set the valid bit.
289
# Can't write a test for this one as you can't set a key to be
290
# trusted using gpgme.
253
291
if result[0].summary & gpgme.SIGSUM_VALID:
254
292
key = self.context.get_key(fingerprint)
255
293
name = key.uids[0].name
256
294
email = key.uids[0].email
257
295
return SIGNATURE_VALID, name + " <" + email + ">"
296
# Sigsum_red indicates a problem, unfortunatly I have not been able
297
# to write any tests which actually set this.
258
298
if result[0].summary & gpgme.SIGSUM_RED:
259
299
return SIGNATURE_NOT_VALID, None
300
# GPG does not know this key.
301
# test_verify_unknown_key()
260
302
if result[0].summary & gpgme.SIGSUM_KEY_MISSING:
261
303
return SIGNATURE_KEY_MISSING, fingerprint[-8:]
262
#summary isn't set if sig is valid but key is untrusted
304
# Summary isn't set if sig is valid but key is untrusted but if user
305
# has explicity set the key as acceptable we can validate it.
263
306
if result[0].summary == 0 and self.acceptable_keys is not None:
264
307
if fingerprint in self.acceptable_keys:
308
# test_verify_untrusted_but_accepted()
265
309
return SIGNATURE_VALID, None
267
return SIGNATURE_KEY_MISSING, None
310
# test_verify_valid_but_untrusted()
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
return SIGNATURE_NOT_VALID, None
327
# Other error types such as revoked keys should (I think) be caught by
328
# SIGSUM_RED so anything else means something is buggy.
268
329
raise errors.SignatureVerificationFailed("Unknown GnuPG key "\
269
330
"verification result")
271
332
def set_acceptable_keys(self, command_line_input):
272
"""sets the acceptable keys for verifying with this GPGStrategy
333
"""Set the acceptable keys for verifying with this GPGStrategy.
274
335
:param command_line_input: comma separated list of patterns from
279
acceptable_keys_config = self._config.acceptable_keys()
281
if isinstance(acceptable_keys_config, unicode):
282
acceptable_keys_config = str(acceptable_keys_config)
283
except UnicodeEncodeError:
284
#gpg Context.keylist(pattern) does not like unicode
285
raise errors.BzrCommandError('Only ASCII permitted in option names')
340
acceptable_keys_config = self._config_stack.get('acceptable_keys')
287
341
if acceptable_keys_config is not None:
288
key_patterns = acceptable_keys_config
289
if command_line_input is not None: #command line overrides config
290
key_patterns = command_line_input
291
if key_patterns is not None:
292
patterns = key_patterns.split(",")
342
patterns = acceptable_keys_config
343
if command_line_input is not None: # command line overrides config
344
patterns = command_line_input.split(',')
294
347
self.acceptable_keys = []
295
348
for pattern in patterns:
296
349
result = self.context.keylist(pattern)
300
353
self.acceptable_keys.append(key.subkeys[0].fpr)
301
354
trace.mutter("Added acceptable key: " + key.subkeys[0].fpr)
302
355
if not found_key:
303
trace.note(i18n.gettext(
304
"No GnuPG key results for pattern: {}"
357
"No GnuPG key results for pattern: {0}"
305
358
).format(pattern))
360
@deprecated_method(deprecated_in((2, 6, 0)))
307
361
def do_verifications(self, revisions, repository,
308
362
process_events_callback=None):
309
363
"""do verifications on a set of revisions
311
365
:param revisions: list of revision ids to verify
312
366
:param repository: repository object
313
367
:param process_events_callback: method to call for GUI frontends that
314
want to keep their UI refreshed
368
want to keep their UI refreshed
316
370
:return: count dictionary of results of each type,
317
371
result list for each revision,
318
372
boolean True if all results are verified successfully
320
count = {SIGNATURE_VALID: 0,
321
SIGNATURE_KEY_MISSING: 0,
322
SIGNATURE_NOT_VALID: 0,
323
SIGNATURE_NOT_SIGNED: 0}
325
all_verifiable = True
326
for rev_id in revisions:
327
verification_result, uid =\
328
repository.verify_revision(rev_id,self)
329
result.append([rev_id, verification_result, uid])
330
count[verification_result] += 1
331
if verification_result != SIGNATURE_VALID:
332
all_verifiable = False
333
if process_events_callback is not None:
334
process_events_callback()
335
return (count, result, all_verifiable)
374
return bulk_verify_signatures(repository, revisions, self,
375
process_events_callback)
377
@deprecated_method(deprecated_in((2, 6, 0)))
337
378
def verbose_valid_message(self, result):
338
379
"""takes a verify result and returns list of signed commits strings"""
340
for rev_id, validity, uid in result:
341
if validity == SIGNATURE_VALID:
342
signers.setdefault(uid, 0)
345
for uid, number in signers.items():
346
result.append( i18n.ngettext(u"{0} signed {1} commit",
347
u"{0} signed {1} commits",
348
number).format(uid, number) )
380
return verbose_valid_message(result)
382
@deprecated_method(deprecated_in((2, 6, 0)))
352
383
def verbose_not_valid_message(self, result, repo):
353
384
"""takes a verify result and returns list of not valid commit info"""
355
for rev_id, validity, empty in result:
356
if validity == SIGNATURE_NOT_VALID:
357
revision = repo.get_revision(rev_id)
358
authors = ', '.join(revision.get_apparent_authors())
359
signers.setdefault(authors, 0)
360
signers[authors] += 1
362
for authors, number in signers.items():
363
result.append( i18n.ngettext(u"{0} commit by author {1}",
364
u"{0} commits by author {1}",
365
number).format(number, authors) )
385
return verbose_not_valid_message(result, repo)
387
@deprecated_method(deprecated_in((2, 6, 0)))
368
388
def verbose_not_signed_message(self, result, repo):
369
389
"""takes a verify result and returns list of not signed commit info"""
371
for rev_id, validity, empty in result:
372
if validity == SIGNATURE_NOT_SIGNED:
373
revision = repo.get_revision(rev_id)
374
authors = ', '.join(revision.get_apparent_authors())
375
signers.setdefault(authors, 0)
376
signers[authors] += 1
378
for authors, number in signers.items():
379
result.append( i18n.ngettext(u"{0} commit by author {1}",
380
u"{0} commits by author {1}",
381
number).format(number, authors) )
390
return verbose_not_valid_message(result, repo)
392
@deprecated_method(deprecated_in((2, 6, 0)))
384
393
def verbose_missing_key_message(self, result):
385
394
"""takes a verify result and returns list of missing key info"""
387
for rev_id, validity, fingerprint in result:
388
if validity == SIGNATURE_KEY_MISSING:
389
signers.setdefault(fingerprint, 0)
390
signers[fingerprint] += 1
392
for fingerprint, number in signers.items():
393
result.append( i18n.ngettext(u"Unknown key {0} signed {1} commit",
394
u"Unknown key {0} signed {1} commits",
395
number).format(fingerprint, number) )
395
return verbose_missing_key_message(result)
397
@deprecated_method(deprecated_in((2, 6, 0)))
398
def verbose_expired_key_message(self, result, repo):
399
"""takes a verify result and returns list of expired key info"""
400
return verbose_expired_key_message(result, repo)
402
@deprecated_method(deprecated_in((2, 6, 0)))
398
403
def valid_commits_message(self, count):
399
404
"""returns message for number of commits"""
400
return i18n.gettext(u"{0} commits with valid signatures").format(
401
count[SIGNATURE_VALID])
405
return valid_commits_message(count)
407
@deprecated_method(deprecated_in((2, 6, 0)))
403
408
def unknown_key_message(self, count):
404
409
"""returns message for number of commits"""
405
return i18n.ngettext(u"{0} commit with unknown key",
406
u"{0} commits with unknown keys",
407
count[SIGNATURE_KEY_MISSING]).format(
408
count[SIGNATURE_KEY_MISSING])
410
return unknown_key_message(count)
412
@deprecated_method(deprecated_in((2, 6, 0)))
410
413
def commit_not_valid_message(self, count):
411
414
"""returns message for number of commits"""
412
return i18n.ngettext(u"{0} commit not valid",
413
u"{0} commits not valid",
414
count[SIGNATURE_NOT_VALID]).format(
415
count[SIGNATURE_NOT_VALID])
415
return commit_not_valid_message(count)
417
@deprecated_method(deprecated_in((2, 6, 0)))
417
418
def commit_not_signed_message(self, count):
418
419
"""returns message for number of commits"""
419
return i18n.ngettext(u"{0} commit not signed",
420
u"{0} commits not signed",
421
count[SIGNATURE_NOT_SIGNED]).format(
422
count[SIGNATURE_NOT_SIGNED])
420
return commit_not_signed_message(count)
422
@deprecated_method(deprecated_in((2, 6, 0)))
423
def expired_commit_message(self, count):
424
"""returns message for number of commits"""
425
return expired_commit_message(count)
428
def valid_commits_message(count):
429
"""returns message for number of commits"""
430
return gettext(u"{0} commits with valid signatures").format(
431
count[SIGNATURE_VALID])
434
def unknown_key_message(count):
435
"""returns message for number of commits"""
436
return ngettext(u"{0} commit with unknown key",
437
u"{0} commits with unknown keys",
438
count[SIGNATURE_KEY_MISSING]).format(
439
count[SIGNATURE_KEY_MISSING])
442
def commit_not_valid_message(count):
443
"""returns message for number of commits"""
444
return ngettext(u"{0} commit not valid",
445
u"{0} commits not valid",
446
count[SIGNATURE_NOT_VALID]).format(
447
count[SIGNATURE_NOT_VALID])
450
def commit_not_signed_message(count):
451
"""returns message for number of commits"""
452
return ngettext(u"{0} commit not signed",
453
u"{0} commits not signed",
454
count[SIGNATURE_NOT_SIGNED]).format(
455
count[SIGNATURE_NOT_SIGNED])
458
def expired_commit_message(count):
459
"""returns message for number of commits"""
460
return ngettext(u"{0} commit with key now expired",
461
u"{0} commits with key now expired",
462
count[SIGNATURE_EXPIRED]).format(
463
count[SIGNATURE_EXPIRED])
466
def verbose_expired_key_message(result, repo):
467
"""takes a verify result and returns list of expired key info"""
469
fingerprint_to_authors = {}
470
for rev_id, validity, fingerprint in result:
471
if validity == SIGNATURE_EXPIRED:
472
revision = repo.get_revision(rev_id)
473
authors = ', '.join(revision.get_apparent_authors())
474
signers.setdefault(fingerprint, 0)
475
signers[fingerprint] += 1
476
fingerprint_to_authors[fingerprint] = authors
478
for fingerprint, number in signers.items():
480
ngettext(u"{0} commit by author {1} with key {2} now expired",
481
u"{0} commits by author {1} with key {2} now expired",
483
number, fingerprint_to_authors[fingerprint], fingerprint))
487
def verbose_valid_message(result):
488
"""takes a verify result and returns list of signed commits strings"""
490
for rev_id, validity, uid in result:
491
if validity == SIGNATURE_VALID:
492
signers.setdefault(uid, 0)
495
for uid, number in signers.items():
496
result.append(ngettext(u"{0} signed {1} commit",
497
u"{0} signed {1} commits",
498
number).format(uid, number))
502
def verbose_not_valid_message(result, repo):
503
"""takes a verify result and returns list of not valid commit info"""
505
for rev_id, validity, empty in result:
506
if validity == SIGNATURE_NOT_VALID:
507
revision = repo.get_revision(rev_id)
508
authors = ', '.join(revision.get_apparent_authors())
509
signers.setdefault(authors, 0)
510
signers[authors] += 1
512
for authors, number in signers.items():
513
result.append(ngettext(u"{0} commit by author {1}",
514
u"{0} commits by author {1}",
515
number).format(number, authors))
519
def verbose_not_signed_message(result, repo):
520
"""takes a verify result and returns list of not signed commit info"""
522
for rev_id, validity, empty in result:
523
if validity == SIGNATURE_NOT_SIGNED:
524
revision = repo.get_revision(rev_id)
525
authors = ', '.join(revision.get_apparent_authors())
526
signers.setdefault(authors, 0)
527
signers[authors] += 1
529
for authors, number in signers.items():
530
result.append(ngettext(u"{0} commit by author {1}",
531
u"{0} commits by author {1}",
532
number).format(number, authors))
536
def verbose_missing_key_message(result):
537
"""takes a verify result and returns list of missing key info"""
539
for rev_id, validity, fingerprint in result:
540
if validity == SIGNATURE_KEY_MISSING:
541
signers.setdefault(fingerprint, 0)
542
signers[fingerprint] += 1
544
for fingerprint, number in signers.items():
545
result.append(ngettext(u"Unknown key {0} signed {1} commit",
546
u"Unknown key {0} signed {1} commits",
547
number).format(fingerprint, number))