/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/gpg.py

  • Committer: Martin
  • Date: 2017-05-24 23:30:47 UTC
  • mto: This revision was merged to the branch mainline in revision 6633.
  • Revision ID: gzlist@googlemail.com-20170524233047-5etu6ukzpgaxbw3y
Fix per_versionedfile test failures and rethink future_builtins

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2009, 2011, 2012, 2013, 2016 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
17
17
 
18
18
"""GPG signing and checking logic."""
19
19
 
 
20
from __future__ import absolute_import
 
21
 
20
22
import os
21
23
import sys
22
24
 
23
 
from bzrlib.lazy_import import lazy_import
 
25
from breezy.lazy_import import lazy_import
24
26
lazy_import(globals(), """
25
27
import errno
26
28
import subprocess
27
29
 
28
 
from bzrlib import (
 
30
from breezy import (
 
31
    config,
29
32
    errors,
30
33
    trace,
31
34
    ui,
32
35
    )
 
36
from breezy.i18n import (
 
37
    gettext,
 
38
    ngettext,
 
39
    )
33
40
""")
34
41
 
 
42
from .sixish import (
 
43
    BytesIO,
 
44
    )
 
45
from .symbol_versioning import (
 
46
    deprecated_in,
 
47
    deprecated_method,
 
48
    )
 
49
 
 
50
#verification results
 
51
SIGNATURE_VALID = 0
 
52
SIGNATURE_KEY_MISSING = 1
 
53
SIGNATURE_NOT_VALID = 2
 
54
SIGNATURE_NOT_SIGNED = 3
 
55
SIGNATURE_EXPIRED = 4
 
56
 
 
57
 
 
58
def bulk_verify_signatures(repository, revids, strategy,
 
59
        process_events_callback=None):
 
60
    """Do verifications on a set of revisions
 
61
 
 
62
    :param repository: repository object
 
63
    :param revids: list of revision ids to verify
 
64
    :param strategy: GPG strategy to use
 
65
    :param process_events_callback: method to call for GUI frontends that
 
66
        want to keep their UI refreshed
 
67
 
 
68
    :return: count dictionary of results of each type,
 
69
             result list for each revision,
 
70
             boolean True if all results are verified successfully
 
71
    """
 
72
    count = {SIGNATURE_VALID: 0,
 
73
             SIGNATURE_KEY_MISSING: 0,
 
74
             SIGNATURE_NOT_VALID: 0,
 
75
             SIGNATURE_NOT_SIGNED: 0,
 
76
             SIGNATURE_EXPIRED: 0}
 
77
    result = []
 
78
    all_verifiable = True
 
79
    total = len(revids)
 
80
    pb = ui.ui_factory.nested_progress_bar()
 
81
    try:
 
82
        for i, (rev_id, verification_result, uid) in enumerate(
 
83
                repository.verify_revision_signatures(
 
84
                    revids, strategy)):
 
85
            pb.update("verifying signatures", i, total)
 
86
            result.append([rev_id, verification_result, uid])
 
87
            count[verification_result] += 1
 
88
            if verification_result != SIGNATURE_VALID:
 
89
                all_verifiable = False
 
90
            if process_events_callback is not None:
 
91
                process_events_callback()
 
92
    finally:
 
93
        pb.finished()
 
94
    return (count, result, all_verifiable)
 
95
 
35
96
 
36
97
class DisabledGPGStrategy(object):
37
98
    """A GPG Strategy that makes everything fail."""
38
99
 
 
100
    @staticmethod
 
101
    def verify_signatures_available():
 
102
        return True
 
103
 
39
104
    def __init__(self, ignored):
40
105
        """Real strategies take a configuration."""
41
106
 
42
107
    def sign(self, content):
43
108
        raise errors.SigningFailed('Signing is disabled.')
44
109
 
 
110
    def verify(self, content, testament):
 
111
        raise errors.SignatureVerificationFailed('Signature verification is \
 
112
disabled.')
 
113
 
 
114
    def set_acceptable_keys(self, command_line_input):
 
115
        pass
 
116
 
45
117
 
46
118
class LoopbackGPGStrategy(object):
47
 
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
119
    """A GPG Strategy that acts like 'cat' - data is just passed through.
 
120
    Used in tests.
 
121
    """
 
122
 
 
123
    @staticmethod
 
124
    def verify_signatures_available():
 
125
        return True
48
126
 
49
127
    def __init__(self, ignored):
50
128
        """Real strategies take a configuration."""
53
131
        return ("-----BEGIN PSEUDO-SIGNED CONTENT-----\n" + content +
54
132
                "-----END PSEUDO-SIGNED CONTENT-----\n")
55
133
 
 
134
    def verify(self, content, testament):
 
135
        return SIGNATURE_VALID, None
 
136
 
 
137
    def set_acceptable_keys(self, command_line_input):
 
138
        if command_line_input is not None:
 
139
            patterns = command_line_input.split(",")
 
140
            self.acceptable_keys = []
 
141
            for pattern in patterns:
 
142
                if pattern == "unknown":
 
143
                    pass
 
144
                else:
 
145
                    self.acceptable_keys.append(pattern)
 
146
 
 
147
    @deprecated_method(deprecated_in((2, 6, 0)))
 
148
    def do_verifications(self, revisions, repository):
 
149
        return bulk_verify_signatures(repository, revisions, self)
 
150
 
 
151
    @deprecated_method(deprecated_in((2, 6, 0)))
 
152
    def valid_commits_message(self, count):
 
153
        return valid_commits_message(count)
 
154
 
 
155
    @deprecated_method(deprecated_in((2, 6, 0)))
 
156
    def unknown_key_message(self, count):
 
157
        return unknown_key_message(count)
 
158
 
 
159
    @deprecated_method(deprecated_in((2, 6, 0)))
 
160
    def commit_not_valid_message(self, count):
 
161
        return commit_not_valid_message(count)
 
162
 
 
163
    @deprecated_method(deprecated_in((2, 6, 0)))
 
164
    def commit_not_signed_message(self, count):
 
165
        return commit_not_signed_message(count)
 
166
 
 
167
    @deprecated_method(deprecated_in((2, 6, 0)))
 
168
    def expired_commit_message(self, count):
 
169
        return expired_commit_message(count)
 
170
 
56
171
 
57
172
def _set_gpg_tty():
58
173
    tty = os.environ.get('TTY')
62
177
    else:
63
178
        # This is not quite worthy of a warning, because some people
64
179
        # don't need GPG_TTY to be set. But it is worthy of a big mark
65
 
        # in ~/.bzr.log, so that people can debug it if it happens to them
 
180
        # in ~/.brz.log, so that people can debug it if it happens to them
66
181
        trace.mutter('** Env var TTY empty, cannot set GPG_TTY.'
67
182
                     '  Is TTY exported?')
68
183
 
70
185
class GPGStrategy(object):
71
186
    """GPG Signing and checking facilities."""
72
187
 
 
188
    acceptable_keys = None
 
189
 
 
190
    def __init__(self, config_stack):
 
191
        self._config_stack = config_stack
 
192
        try:
 
193
            import gpgme
 
194
            self.context = gpgme.Context()
 
195
        except ImportError as error:
 
196
            pass # can't use verify()
 
197
 
 
198
    @staticmethod
 
199
    def verify_signatures_available():
 
200
        """
 
201
        check if this strategy can verify signatures
 
202
 
 
203
        :return: boolean if this strategy can verify signatures
 
204
        """
 
205
        try:
 
206
            import gpgme
 
207
            return True
 
208
        except ImportError as error:
 
209
            return False
 
210
 
73
211
    def _command_line(self):
74
 
        return [self._config.gpg_signing_command(), '--clearsign']
75
 
 
76
 
    def __init__(self, config):
77
 
        self._config = config
 
212
        key = self._config_stack.get('gpg_signing_key')
 
213
        if key is None or key == 'default':
 
214
            # 'default' or not setting gpg_signing_key at all means we should
 
215
            # use the user email address
 
216
            key = config.extract_email_address(self._config_stack.get('email'))
 
217
        return [self._config_stack.get('gpg_signing_command'), '--clearsign',
 
218
                '-u', key]
78
219
 
79
220
    def sign(self, content):
80
221
        if isinstance(content, unicode):
97
238
                if process.returncode != 0:
98
239
                    raise errors.SigningFailed(self._command_line())
99
240
                return result
100
 
            except OSError, e:
 
241
            except OSError as e:
101
242
                if e.errno == errno.EPIPE:
102
243
                    raise errors.SigningFailed(self._command_line())
103
244
                else:
105
246
        except ValueError:
106
247
            # bad subprocess parameters, should never happen.
107
248
            raise
108
 
        except OSError, e:
 
249
        except OSError as e:
109
250
            if e.errno == errno.ENOENT:
110
251
                # gpg is not installed
111
252
                raise errors.SigningFailed(self._command_line())
112
253
            else:
113
254
                raise
 
255
 
 
256
    def verify(self, content, testament):
 
257
        """Check content has a valid signature.
 
258
 
 
259
        :param content: the commit signature
 
260
        :param testament: the valid testament string for the commit
 
261
 
 
262
        :return: SIGNATURE_VALID or a failed SIGNATURE_ value, key uid if valid
 
263
        """
 
264
        try:
 
265
            import gpgme
 
266
        except ImportError as error:
 
267
            raise errors.GpgmeNotInstalled(error)
 
268
 
 
269
        signature = BytesIO(content)
 
270
        plain_output = BytesIO()
 
271
        try:
 
272
            result = self.context.verify(signature, None, plain_output)
 
273
        except gpgme.GpgmeError as error:
 
274
            raise errors.SignatureVerificationFailed(error[2])
 
275
 
 
276
        # No result if input is invalid.
 
277
        # test_verify_invalid()
 
278
        if len(result) == 0:
 
279
            return SIGNATURE_NOT_VALID, None
 
280
        # User has specified a list of acceptable keys, check our result is in
 
281
        # it.  test_verify_unacceptable_key()
 
282
        fingerprint = result[0].fpr
 
283
        if self.acceptable_keys is not None:
 
284
            if not fingerprint in self.acceptable_keys:
 
285
                return SIGNATURE_KEY_MISSING, fingerprint[-8:]
 
286
        # Check the signature actually matches the testament.
 
287
        # test_verify_bad_testament()
 
288
        if testament != plain_output.getvalue():
 
289
            return SIGNATURE_NOT_VALID, None
 
290
        # Yay gpgme set the valid bit.
 
291
        # Can't write a test for this one as you can't set a key to be
 
292
        # trusted using gpgme.
 
293
        if result[0].summary & gpgme.SIGSUM_VALID:
 
294
            key = self.context.get_key(fingerprint)
 
295
            name = key.uids[0].name
 
296
            email = key.uids[0].email
 
297
            return SIGNATURE_VALID, name + " <" + email + ">"
 
298
        # Sigsum_red indicates a problem, unfortunatly I have not been able
 
299
        # to write any tests which actually set this.
 
300
        if result[0].summary & gpgme.SIGSUM_RED:
 
301
            return SIGNATURE_NOT_VALID, None
 
302
        # GPG does not know this key.
 
303
        # test_verify_unknown_key()
 
304
        if result[0].summary & gpgme.SIGSUM_KEY_MISSING:
 
305
            return SIGNATURE_KEY_MISSING, fingerprint[-8:]
 
306
        # Summary isn't set if sig is valid but key is untrusted but if user
 
307
        # has explicity set the key as acceptable we can validate it.
 
308
        if result[0].summary == 0 and self.acceptable_keys is not None:
 
309
            if fingerprint in self.acceptable_keys:
 
310
                # test_verify_untrusted_but_accepted()
 
311
                return SIGNATURE_VALID, None
 
312
        # test_verify_valid_but_untrusted()
 
313
        if result[0].summary == 0 and self.acceptable_keys is None:
 
314
            return SIGNATURE_NOT_VALID, None
 
315
        if result[0].summary & gpgme.SIGSUM_KEY_EXPIRED:
 
316
            expires = self.context.get_key(result[0].fpr).subkeys[0].expires
 
317
            if expires > result[0].timestamp:
 
318
                # The expired key was not expired at time of signing.
 
319
                # test_verify_expired_but_valid()
 
320
                return SIGNATURE_EXPIRED, fingerprint[-8:]
 
321
            else:
 
322
                # I can't work out how to create a test where the signature
 
323
                # was expired at the time of signing.
 
324
                return SIGNATURE_NOT_VALID, None
 
325
        # A signature from a revoked key gets this.
 
326
        # test_verify_revoked_signature()
 
327
        if ((result[0].summary & gpgme.SIGSUM_SYS_ERROR
 
328
             or result[0].status.strerror == 'Certificate revoked')):
 
329
            return SIGNATURE_NOT_VALID, None
 
330
        # Other error types such as revoked keys should (I think) be caught by
 
331
        # SIGSUM_RED so anything else means something is buggy.
 
332
        raise errors.SignatureVerificationFailed(
 
333
            "Unknown GnuPG key verification result")
 
334
 
 
335
    def set_acceptable_keys(self, command_line_input):
 
336
        """Set the acceptable keys for verifying with this GPGStrategy.
 
337
 
 
338
        :param command_line_input: comma separated list of patterns from
 
339
                                command line
 
340
        :return: nothing
 
341
        """
 
342
        patterns = None
 
343
        acceptable_keys_config = self._config_stack.get('acceptable_keys')
 
344
        if acceptable_keys_config is not None:
 
345
            patterns = acceptable_keys_config
 
346
        if command_line_input is not None: # command line overrides config
 
347
            patterns = command_line_input.split(',')
 
348
 
 
349
        if patterns:
 
350
            self.acceptable_keys = []
 
351
            for pattern in patterns:
 
352
                result = self.context.keylist(pattern)
 
353
                found_key = False
 
354
                for key in result:
 
355
                    found_key = True
 
356
                    self.acceptable_keys.append(key.subkeys[0].fpr)
 
357
                    trace.mutter("Added acceptable key: " + key.subkeys[0].fpr)
 
358
                if not found_key:
 
359
                    trace.note(gettext(
 
360
                            "No GnuPG key results for pattern: {0}"
 
361
                                ).format(pattern))
 
362
 
 
363
    @deprecated_method(deprecated_in((2, 6, 0)))
 
364
    def do_verifications(self, revisions, repository,
 
365
                            process_events_callback=None):
 
366
        """do verifications on a set of revisions
 
367
 
 
368
        :param revisions: list of revision ids to verify
 
369
        :param repository: repository object
 
370
        :param process_events_callback: method to call for GUI frontends that
 
371
            want to keep their UI refreshed
 
372
 
 
373
        :return: count dictionary of results of each type,
 
374
                 result list for each revision,
 
375
                 boolean True if all results are verified successfully
 
376
        """
 
377
        return bulk_verify_signatures(repository, revisions, self,
 
378
            process_events_callback)
 
379
 
 
380
    @deprecated_method(deprecated_in((2, 6, 0)))
 
381
    def verbose_valid_message(self, result):
 
382
        """takes a verify result and returns list of signed commits strings"""
 
383
        return verbose_valid_message(result)
 
384
 
 
385
    @deprecated_method(deprecated_in((2, 6, 0)))
 
386
    def verbose_not_valid_message(self, result, repo):
 
387
        """takes a verify result and returns list of not valid commit info"""
 
388
        return verbose_not_valid_message(result, repo)
 
389
 
 
390
    @deprecated_method(deprecated_in((2, 6, 0)))
 
391
    def verbose_not_signed_message(self, result, repo):
 
392
        """takes a verify result and returns list of not signed commit info"""
 
393
        return verbose_not_valid_message(result, repo)
 
394
 
 
395
    @deprecated_method(deprecated_in((2, 6, 0)))
 
396
    def verbose_missing_key_message(self, result):
 
397
        """takes a verify result and returns list of missing key info"""
 
398
        return verbose_missing_key_message(result)
 
399
 
 
400
    @deprecated_method(deprecated_in((2, 6, 0)))
 
401
    def verbose_expired_key_message(self, result, repo):
 
402
        """takes a verify result and returns list of expired key info"""
 
403
        return verbose_expired_key_message(result, repo)
 
404
 
 
405
    @deprecated_method(deprecated_in((2, 6, 0)))
 
406
    def valid_commits_message(self, count):
 
407
        """returns message for number of commits"""
 
408
        return valid_commits_message(count)
 
409
 
 
410
    @deprecated_method(deprecated_in((2, 6, 0)))
 
411
    def unknown_key_message(self, count):
 
412
        """returns message for number of commits"""
 
413
        return unknown_key_message(count)
 
414
 
 
415
    @deprecated_method(deprecated_in((2, 6, 0)))
 
416
    def commit_not_valid_message(self, count):
 
417
        """returns message for number of commits"""
 
418
        return commit_not_valid_message(count)
 
419
 
 
420
    @deprecated_method(deprecated_in((2, 6, 0)))
 
421
    def commit_not_signed_message(self, count):
 
422
        """returns message for number of commits"""
 
423
        return commit_not_signed_message(count)
 
424
 
 
425
    @deprecated_method(deprecated_in((2, 6, 0)))
 
426
    def expired_commit_message(self, count):
 
427
        """returns message for number of commits"""
 
428
        return expired_commit_message(count)
 
429
 
 
430
 
 
431
def valid_commits_message(count):
 
432
    """returns message for number of commits"""
 
433
    return gettext(u"{0} commits with valid signatures").format(
 
434
                                    count[SIGNATURE_VALID])
 
435
 
 
436
 
 
437
def unknown_key_message(count):
 
438
    """returns message for number of commits"""
 
439
    return ngettext(u"{0} commit with unknown key",
 
440
                    u"{0} commits with unknown keys",
 
441
                    count[SIGNATURE_KEY_MISSING]).format(
 
442
                                    count[SIGNATURE_KEY_MISSING])
 
443
 
 
444
 
 
445
def commit_not_valid_message(count):
 
446
    """returns message for number of commits"""
 
447
    return ngettext(u"{0} commit not valid",
 
448
                    u"{0} commits not valid",
 
449
                    count[SIGNATURE_NOT_VALID]).format(
 
450
                                        count[SIGNATURE_NOT_VALID])
 
451
 
 
452
 
 
453
def commit_not_signed_message(count):
 
454
    """returns message for number of commits"""
 
455
    return ngettext(u"{0} commit not signed",
 
456
                    u"{0} commits not signed",
 
457
                    count[SIGNATURE_NOT_SIGNED]).format(
 
458
                                    count[SIGNATURE_NOT_SIGNED])
 
459
 
 
460
 
 
461
def expired_commit_message(count):
 
462
    """returns message for number of commits"""
 
463
    return ngettext(u"{0} commit with key now expired",
 
464
                    u"{0} commits with key now expired",
 
465
                    count[SIGNATURE_EXPIRED]).format(
 
466
                                count[SIGNATURE_EXPIRED])
 
467
 
 
468
 
 
469
def verbose_expired_key_message(result, repo):
 
470
    """takes a verify result and returns list of expired key info"""
 
471
    signers = {}
 
472
    fingerprint_to_authors = {}
 
473
    for rev_id, validity, fingerprint in result:
 
474
        if validity == SIGNATURE_EXPIRED:
 
475
            revision = repo.get_revision(rev_id)
 
476
            authors = ', '.join(revision.get_apparent_authors())
 
477
            signers.setdefault(fingerprint, 0)
 
478
            signers[fingerprint] += 1
 
479
            fingerprint_to_authors[fingerprint] = authors
 
480
    result = []
 
481
    for fingerprint, number in signers.items():
 
482
        result.append(
 
483
            ngettext(u"{0} commit by author {1} with key {2} now expired",
 
484
                     u"{0} commits by author {1} with key {2} now expired",
 
485
                     number).format(
 
486
                number, fingerprint_to_authors[fingerprint], fingerprint))
 
487
    return result
 
488
 
 
489
 
 
490
def verbose_valid_message(result):
 
491
    """takes a verify result and returns list of signed commits strings"""
 
492
    signers = {}
 
493
    for rev_id, validity, uid in result:
 
494
        if validity == SIGNATURE_VALID:
 
495
            signers.setdefault(uid, 0)
 
496
            signers[uid] += 1
 
497
    result = []
 
498
    for uid, number in signers.items():
 
499
         result.append(ngettext(u"{0} signed {1} commit",
 
500
                                u"{0} signed {1} commits",
 
501
                                number).format(uid, number))
 
502
    return result
 
503
 
 
504
 
 
505
def verbose_not_valid_message(result, repo):
 
506
    """takes a verify result and returns list of not valid commit info"""
 
507
    signers = {}
 
508
    for rev_id, validity, empty in result:
 
509
        if validity == SIGNATURE_NOT_VALID:
 
510
            revision = repo.get_revision(rev_id)
 
511
            authors = ', '.join(revision.get_apparent_authors())
 
512
            signers.setdefault(authors, 0)
 
513
            signers[authors] += 1
 
514
    result = []
 
515
    for authors, number in signers.items():
 
516
        result.append(ngettext(u"{0} commit by author {1}",
 
517
                               u"{0} commits by author {1}",
 
518
                               number).format(number, authors))
 
519
    return result
 
520
 
 
521
 
 
522
def verbose_not_signed_message(result, repo):
 
523
    """takes a verify result and returns list of not signed commit info"""
 
524
    signers = {}
 
525
    for rev_id, validity, empty in result:
 
526
        if validity == SIGNATURE_NOT_SIGNED:
 
527
            revision = repo.get_revision(rev_id)
 
528
            authors = ', '.join(revision.get_apparent_authors())
 
529
            signers.setdefault(authors, 0)
 
530
            signers[authors] += 1
 
531
    result = []
 
532
    for authors, number in signers.items():
 
533
        result.append(ngettext(u"{0} commit by author {1}",
 
534
                               u"{0} commits by author {1}",
 
535
                               number).format(number, authors))
 
536
    return result
 
537
 
 
538
 
 
539
def verbose_missing_key_message(result):
 
540
    """takes a verify result and returns list of missing key info"""
 
541
    signers = {}
 
542
    for rev_id, validity, fingerprint in result:
 
543
        if validity == SIGNATURE_KEY_MISSING:
 
544
            signers.setdefault(fingerprint, 0)
 
545
            signers[fingerprint] += 1
 
546
    result = []
 
547
    for fingerprint, number in signers.items():
 
548
        result.append(ngettext(u"Unknown key {0} signed {1} commit",
 
549
                               u"Unknown key {0} signed {1} commits",
 
550
                               number).format(fingerprint, number))
 
551
    return result