/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: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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
 
import sys
22
23
 
23
 
from bzrlib.lazy_import import lazy_import
 
24
from breezy.lazy_import import lazy_import
24
25
lazy_import(globals(), """
25
 
import errno
26
 
import subprocess
27
 
 
28
 
from bzrlib import (
29
 
    errors,
 
26
from breezy import (
 
27
    config,
30
28
    trace,
31
29
    ui,
32
30
    )
 
31
from breezy.i18n import (
 
32
    gettext,
 
33
    ngettext,
 
34
    )
33
35
""")
34
36
 
 
37
from . import (
 
38
    errors,
 
39
    )
 
40
from .sixish import (
 
41
    text_type,
 
42
    )
 
43
 
 
44
# verification results
 
45
SIGNATURE_VALID = 0
 
46
SIGNATURE_KEY_MISSING = 1
 
47
SIGNATURE_NOT_VALID = 2
 
48
SIGNATURE_NOT_SIGNED = 3
 
49
SIGNATURE_EXPIRED = 4
 
50
 
 
51
MODE_NORMAL = 0
 
52
MODE_DETACH = 1
 
53
MODE_CLEAR = 2
 
54
 
 
55
 
 
56
class GpgNotInstalled(errors.DependencyNotPresent):
 
57
 
 
58
    _fmt = ('python-gpg is not installed, it is needed to create or '
 
59
            'verify signatures. %(error)s')
 
60
 
 
61
    def __init__(self, error):
 
62
        errors.DependencyNotPresent.__init__(self, 'gpg', error)
 
63
 
 
64
 
 
65
class SigningFailed(errors.BzrError):
 
66
 
 
67
    _fmt = 'Failed to GPG sign data: "%(error)s"'
 
68
 
 
69
    def __init__(self, error):
 
70
        errors.BzrError.__init__(self, error=error)
 
71
 
 
72
 
 
73
class SignatureVerificationFailed(errors.BzrError):
 
74
 
 
75
    _fmt = 'Failed to verify GPG signature data with error "%(error)s"'
 
76
 
 
77
    def __init__(self, error):
 
78
        errors.BzrError.__init__(self, error=error)
 
79
 
 
80
 
 
81
def bulk_verify_signatures(repository, revids, strategy,
 
82
                           process_events_callback=None):
 
83
    """Do verifications on a set of revisions
 
84
 
 
85
    :param repository: repository object
 
86
    :param revids: list of revision ids to verify
 
87
    :param strategy: GPG strategy to use
 
88
    :param process_events_callback: method to call for GUI frontends that
 
89
        want to keep their UI refreshed
 
90
 
 
91
    :return: count dictionary of results of each type,
 
92
             result list for each revision,
 
93
             boolean True if all results are verified successfully
 
94
    """
 
95
    count = {SIGNATURE_VALID: 0,
 
96
             SIGNATURE_KEY_MISSING: 0,
 
97
             SIGNATURE_NOT_VALID: 0,
 
98
             SIGNATURE_NOT_SIGNED: 0,
 
99
             SIGNATURE_EXPIRED: 0}
 
100
    result = []
 
101
    all_verifiable = True
 
102
    total = len(revids)
 
103
    with ui.ui_factory.nested_progress_bar() as pb:
 
104
        for i, (rev_id, verification_result, uid) in enumerate(
 
105
                repository.verify_revision_signatures(
 
106
                    revids, strategy)):
 
107
            pb.update("verifying signatures", i, total)
 
108
            result.append([rev_id, verification_result, uid])
 
109
            count[verification_result] += 1
 
110
            if verification_result != SIGNATURE_VALID:
 
111
                all_verifiable = False
 
112
            if process_events_callback is not None:
 
113
                process_events_callback()
 
114
    return (count, result, all_verifiable)
 
115
 
35
116
 
36
117
class DisabledGPGStrategy(object):
37
118
    """A GPG Strategy that makes everything fail."""
38
119
 
 
120
    @staticmethod
 
121
    def verify_signatures_available():
 
122
        return True
 
123
 
39
124
    def __init__(self, ignored):
40
125
        """Real strategies take a configuration."""
41
126
 
42
 
    def sign(self, content):
43
 
        raise errors.SigningFailed('Signing is disabled.')
 
127
    def sign(self, content, mode):
 
128
        raise SigningFailed('Signing is disabled.')
 
129
 
 
130
    def verify(self, signed_data, signature=None):
 
131
        raise SignatureVerificationFailed('Signature verification is \
 
132
disabled.')
 
133
 
 
134
    def set_acceptable_keys(self, command_line_input):
 
135
        pass
44
136
 
45
137
 
46
138
class LoopbackGPGStrategy(object):
47
 
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
139
    """A GPG Strategy that acts like 'cat' - data is just passed through.
 
140
    Used in tests.
 
141
    """
 
142
 
 
143
    @staticmethod
 
144
    def verify_signatures_available():
 
145
        return True
48
146
 
49
147
    def __init__(self, ignored):
50
148
        """Real strategies take a configuration."""
51
149
 
52
 
    def sign(self, content):
53
 
        return ("-----BEGIN PSEUDO-SIGNED CONTENT-----\n" + content +
54
 
                "-----END PSEUDO-SIGNED CONTENT-----\n")
 
150
    def sign(self, content, mode):
 
151
        return (b"-----BEGIN PSEUDO-SIGNED CONTENT-----\n" + content
 
152
                + b"-----END PSEUDO-SIGNED CONTENT-----\n")
 
153
 
 
154
    def verify(self, signed_data, signature=None):
 
155
        plain_text = signed_data.replace(
 
156
            b"-----BEGIN PSEUDO-SIGNED CONTENT-----\n", b"")
 
157
        plain_text = plain_text.replace(
 
158
            b"-----END PSEUDO-SIGNED CONTENT-----\n", b"")
 
159
        return SIGNATURE_VALID, None, plain_text
 
160
 
 
161
    def set_acceptable_keys(self, command_line_input):
 
162
        if command_line_input is not None:
 
163
            patterns = command_line_input.split(",")
 
164
            self.acceptable_keys = []
 
165
            for pattern in patterns:
 
166
                if pattern == "unknown":
 
167
                    pass
 
168
                else:
 
169
                    self.acceptable_keys.append(pattern)
55
170
 
56
171
 
57
172
def _set_gpg_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
 
73
 
    def _command_line(self):
74
 
        return [self._config.gpg_signing_command(), '--clearsign']
75
 
 
76
 
    def __init__(self, config):
77
 
        self._config = config
78
 
 
79
 
    def sign(self, content):
80
 
        if isinstance(content, unicode):
 
188
    acceptable_keys = None
 
189
 
 
190
    def __init__(self, config_stack):
 
191
        self._config_stack = config_stack
 
192
        try:
 
193
            import gpg
 
194
            self.context = gpg.Context()
 
195
            self.context.armor = True
 
196
            self.context.signers = self._get_signing_keys()
 
197
        except ImportError:
 
198
            pass  # can't use verify()
 
199
 
 
200
    def _get_signing_keys(self):
 
201
        import gpg
 
202
        keyname = self._config_stack.get('gpg_signing_key')
 
203
        if keyname == 'default':
 
204
            # Leave things to gpg
 
205
            return []
 
206
 
 
207
        if keyname:
 
208
            try:
 
209
                return [self.context.get_key(keyname)]
 
210
            except gpg.errors.KeyNotFound:
 
211
                pass
 
212
 
 
213
        if keyname is None:
 
214
            # not setting gpg_signing_key at all means we should
 
215
            # use the user email address
 
216
            keyname = config.extract_email_address(
 
217
                self._config_stack.get('email'))
 
218
        if keyname == 'default':
 
219
            return []
 
220
        possible_keys = self.context.keylist(keyname, secret=True)
 
221
        try:
 
222
            return [next(possible_keys)]
 
223
        except StopIteration:
 
224
            return []
 
225
 
 
226
    @staticmethod
 
227
    def verify_signatures_available():
 
228
        """
 
229
        check if this strategy can verify signatures
 
230
 
 
231
        :return: boolean if this strategy can verify signatures
 
232
        """
 
233
        try:
 
234
            import gpg  # noqa: F401
 
235
            return True
 
236
        except ImportError:
 
237
            return False
 
238
 
 
239
    def sign(self, content, mode):
 
240
        try:
 
241
            import gpg
 
242
        except ImportError as error:
 
243
            raise GpgNotInstalled(
 
244
                'Set create_signatures=no to disable creating signatures.')
 
245
 
 
246
        if isinstance(content, text_type):
81
247
            raise errors.BzrBadParameterUnicode('content')
82
 
        ui.ui_factory.clear_term()
83
 
 
84
 
        preexec_fn = _set_gpg_tty
85
 
        if sys.platform == 'win32':
86
 
            # Win32 doesn't support preexec_fn, but wouldn't support TTY anyway.
87
 
            preexec_fn = None
88
 
        try:
89
 
            process = subprocess.Popen(self._command_line(),
90
 
                                       stdin=subprocess.PIPE,
91
 
                                       stdout=subprocess.PIPE,
92
 
                                       preexec_fn=preexec_fn)
93
 
            try:
94
 
                result = process.communicate(content)[0]
95
 
                if process.returncode is None:
96
 
                    process.wait()
97
 
                if process.returncode != 0:
98
 
                    raise errors.SigningFailed(self._command_line())
99
 
                return result
100
 
            except OSError, e:
101
 
                if e.errno == errno.EPIPE:
102
 
                    raise errors.SigningFailed(self._command_line())
 
248
 
 
249
        plain_text = gpg.Data(content)
 
250
        try:
 
251
            output, result = self.context.sign(
 
252
                plain_text, mode={
 
253
                    MODE_DETACH: gpg.constants.sig.mode.DETACH,
 
254
                    MODE_CLEAR: gpg.constants.sig.mode.CLEAR,
 
255
                    MODE_NORMAL: gpg.constants.sig.mode.NORMAL,
 
256
                    }[mode])
 
257
        except gpg.errors.GPGMEError as error:
 
258
            raise SigningFailed(str(error))
 
259
 
 
260
        return output
 
261
 
 
262
    def verify(self, signed_data, signature=None):
 
263
        """Check content has a valid signature.
 
264
 
 
265
        :param signed_data; Signed data
 
266
        :param signature: optional signature (if detached)
 
267
 
 
268
        :return: SIGNATURE_VALID or a failed SIGNATURE_ value, key uid if valid, plain text
 
269
        """
 
270
        try:
 
271
            import gpg
 
272
        except ImportError as error:
 
273
            raise GpgNotInstalled(
 
274
                'Set check_signatures=ignore to disable verifying signatures.')
 
275
 
 
276
        signed_data = gpg.Data(signed_data)
 
277
        if signature:
 
278
            signature = gpg.Data(signature)
 
279
        try:
 
280
            plain_output, result = self.context.verify(signed_data, signature)
 
281
        except gpg.errors.BadSignatures as error:
 
282
            fingerprint = error.result.signatures[0].fpr
 
283
            if error.result.signatures[0].summary & gpg.constants.SIGSUM_KEY_EXPIRED:
 
284
                expires = self.context.get_key(
 
285
                    error.result.signatures[0].fpr).subkeys[0].expires
 
286
                if expires > error.result.signatures[0].timestamp:
 
287
                    # The expired key was not expired at time of signing.
 
288
                    # test_verify_expired_but_valid()
 
289
                    return SIGNATURE_EXPIRED, fingerprint[-8:], None
103
290
                else:
104
 
                    raise
105
 
        except ValueError:
106
 
            # bad subprocess parameters, should never happen.
107
 
            raise
108
 
        except OSError, e:
109
 
            if e.errno == errno.ENOENT:
110
 
                # gpg is not installed
111
 
                raise errors.SigningFailed(self._command_line())
112
 
            else:
113
 
                raise
 
291
                    # I can't work out how to create a test where the signature
 
292
                    # was expired at the time of signing.
 
293
                    return SIGNATURE_NOT_VALID, None, None
 
294
 
 
295
            # GPG does not know this key.
 
296
            # test_verify_unknown_key()
 
297
            if (error.result.signatures[0].summary &
 
298
                    gpg.constants.SIGSUM_KEY_MISSING):
 
299
                return SIGNATURE_KEY_MISSING, fingerprint[-8:], None
 
300
 
 
301
            return SIGNATURE_NOT_VALID, None, None
 
302
        except gpg.errors.GPGMEError as error:
 
303
            raise SignatureVerificationFailed(error)
 
304
 
 
305
        # No result if input is invalid.
 
306
        # test_verify_invalid()
 
307
        if len(result.signatures) == 0:
 
308
            return SIGNATURE_NOT_VALID, None, plain_output
 
309
 
 
310
        # User has specified a list of acceptable keys, check our result is in
 
311
        # it.  test_verify_unacceptable_key()
 
312
        fingerprint = result.signatures[0].fpr
 
313
        if self.acceptable_keys is not None:
 
314
            if fingerprint not in self.acceptable_keys:
 
315
                return SIGNATURE_KEY_MISSING, fingerprint[-8:], plain_output
 
316
        # Yay gpg set the valid bit.
 
317
        # Can't write a test for this one as you can't set a key to be
 
318
        # trusted using gpg.
 
319
        if result.signatures[0].summary & gpg.constants.SIGSUM_VALID:
 
320
            key = self.context.get_key(fingerprint)
 
321
            name = key.uids[0].name
 
322
            if isinstance(name, bytes):
 
323
                name = name.decode('utf-8')
 
324
            email = key.uids[0].email
 
325
            if isinstance(email, bytes):
 
326
                email = email.decode('utf-8')
 
327
            return (SIGNATURE_VALID, name + u" <" + email + u">", plain_output)
 
328
        # Sigsum_red indicates a problem, unfortunatly I have not been able
 
329
        # to write any tests which actually set this.
 
330
        if result.signatures[0].summary & gpg.constants.SIGSUM_RED:
 
331
            return SIGNATURE_NOT_VALID, None, plain_output
 
332
        # Summary isn't set if sig is valid but key is untrusted but if user
 
333
        # has explicity set the key as acceptable we can validate it.
 
334
        if (result.signatures[0].summary == 0 and
 
335
                self.acceptable_keys is not None):
 
336
            if fingerprint in self.acceptable_keys:
 
337
                # test_verify_untrusted_but_accepted()
 
338
                return SIGNATURE_VALID, None, plain_output
 
339
        # test_verify_valid_but_untrusted()
 
340
        if result.signatures[0].summary == 0 and self.acceptable_keys is None:
 
341
            return SIGNATURE_NOT_VALID, None, plain_output
 
342
        # Other error types such as revoked keys should (I think) be caught by
 
343
        # SIGSUM_RED so anything else means something is buggy.
 
344
        raise SignatureVerificationFailed(
 
345
            "Unknown GnuPG key verification result")
 
346
 
 
347
    def set_acceptable_keys(self, command_line_input):
 
348
        """Set the acceptable keys for verifying with this GPGStrategy.
 
349
 
 
350
        :param command_line_input: comma separated list of patterns from
 
351
                                command line
 
352
        :return: nothing
 
353
        """
 
354
        patterns = None
 
355
        acceptable_keys_config = self._config_stack.get('acceptable_keys')
 
356
        if acceptable_keys_config is not None:
 
357
            patterns = acceptable_keys_config
 
358
        if command_line_input is not None:  # command line overrides config
 
359
            patterns = command_line_input.split(',')
 
360
 
 
361
        if patterns:
 
362
            self.acceptable_keys = []
 
363
            for pattern in patterns:
 
364
                result = self.context.keylist(pattern)
 
365
                found_key = False
 
366
                for key in result:
 
367
                    found_key = True
 
368
                    self.acceptable_keys.append(key.subkeys[0].fpr)
 
369
                    trace.mutter("Added acceptable key: " + key.subkeys[0].fpr)
 
370
                if not found_key:
 
371
                    trace.note(gettext(
 
372
                        "No GnuPG key results for pattern: {0}"
 
373
                        ).format(pattern))
 
374
 
 
375
 
 
376
def valid_commits_message(count):
 
377
    """returns message for number of commits"""
 
378
    return gettext(u"{0} commits with valid signatures").format(
 
379
        count[SIGNATURE_VALID])
 
380
 
 
381
 
 
382
def unknown_key_message(count):
 
383
    """returns message for number of commits"""
 
384
    return ngettext(u"{0} commit with unknown key",
 
385
                    u"{0} commits with unknown keys",
 
386
                    count[SIGNATURE_KEY_MISSING]).format(
 
387
        count[SIGNATURE_KEY_MISSING])
 
388
 
 
389
 
 
390
def commit_not_valid_message(count):
 
391
    """returns message for number of commits"""
 
392
    return ngettext(u"{0} commit not valid",
 
393
                    u"{0} commits not valid",
 
394
                    count[SIGNATURE_NOT_VALID]).format(
 
395
        count[SIGNATURE_NOT_VALID])
 
396
 
 
397
 
 
398
def commit_not_signed_message(count):
 
399
    """returns message for number of commits"""
 
400
    return ngettext(u"{0} commit not signed",
 
401
                    u"{0} commits not signed",
 
402
                    count[SIGNATURE_NOT_SIGNED]).format(
 
403
        count[SIGNATURE_NOT_SIGNED])
 
404
 
 
405
 
 
406
def expired_commit_message(count):
 
407
    """returns message for number of commits"""
 
408
    return ngettext(u"{0} commit with key now expired",
 
409
                    u"{0} commits with key now expired",
 
410
                    count[SIGNATURE_EXPIRED]).format(
 
411
        count[SIGNATURE_EXPIRED])
 
412
 
 
413
 
 
414
def verbose_expired_key_message(result, repo):
 
415
    """takes a verify result and returns list of expired key info"""
 
416
    signers = {}
 
417
    fingerprint_to_authors = {}
 
418
    for rev_id, validity, fingerprint in result:
 
419
        if validity == SIGNATURE_EXPIRED:
 
420
            revision = repo.get_revision(rev_id)
 
421
            authors = ', '.join(revision.get_apparent_authors())
 
422
            signers.setdefault(fingerprint, 0)
 
423
            signers[fingerprint] += 1
 
424
            fingerprint_to_authors[fingerprint] = authors
 
425
    result = []
 
426
    for fingerprint, number in signers.items():
 
427
        result.append(
 
428
            ngettext(u"{0} commit by author {1} with key {2} now expired",
 
429
                     u"{0} commits by author {1} with key {2} now expired",
 
430
                     number).format(
 
431
                number, fingerprint_to_authors[fingerprint], fingerprint))
 
432
    return result
 
433
 
 
434
 
 
435
def verbose_valid_message(result):
 
436
    """takes a verify result and returns list of signed commits strings"""
 
437
    signers = {}
 
438
    for rev_id, validity, uid in result:
 
439
        if validity == SIGNATURE_VALID:
 
440
            signers.setdefault(uid, 0)
 
441
            signers[uid] += 1
 
442
    result = []
 
443
    for uid, number in signers.items():
 
444
        result.append(ngettext(u"{0} signed {1} commit",
 
445
                               u"{0} signed {1} commits",
 
446
                               number).format(uid, number))
 
447
    return result
 
448
 
 
449
 
 
450
def verbose_not_valid_message(result, repo):
 
451
    """takes a verify result and returns list of not valid commit info"""
 
452
    signers = {}
 
453
    for rev_id, validity, empty in result:
 
454
        if validity == SIGNATURE_NOT_VALID:
 
455
            revision = repo.get_revision(rev_id)
 
456
            authors = ', '.join(revision.get_apparent_authors())
 
457
            signers.setdefault(authors, 0)
 
458
            signers[authors] += 1
 
459
    result = []
 
460
    for authors, number in signers.items():
 
461
        result.append(ngettext(u"{0} commit by author {1}",
 
462
                               u"{0} commits by author {1}",
 
463
                               number).format(number, authors))
 
464
    return result
 
465
 
 
466
 
 
467
def verbose_not_signed_message(result, repo):
 
468
    """takes a verify result and returns list of not signed commit info"""
 
469
    signers = {}
 
470
    for rev_id, validity, empty in result:
 
471
        if validity == SIGNATURE_NOT_SIGNED:
 
472
            revision = repo.get_revision(rev_id)
 
473
            authors = ', '.join(revision.get_apparent_authors())
 
474
            signers.setdefault(authors, 0)
 
475
            signers[authors] += 1
 
476
    result = []
 
477
    for authors, number in signers.items():
 
478
        result.append(ngettext(u"{0} commit by author {1}",
 
479
                               u"{0} commits by author {1}",
 
480
                               number).format(number, authors))
 
481
    return result
 
482
 
 
483
 
 
484
def verbose_missing_key_message(result):
 
485
    """takes a verify result and returns list of missing key info"""
 
486
    signers = {}
 
487
    for rev_id, validity, fingerprint in result:
 
488
        if validity == SIGNATURE_KEY_MISSING:
 
489
            signers.setdefault(fingerprint, 0)
 
490
            signers[fingerprint] += 1
 
491
    result = []
 
492
    for fingerprint, number in list(signers.items()):
 
493
        result.append(ngettext(u"Unknown key {0} signed {1} commit",
 
494
                               u"Unknown key {0} signed {1} commits",
 
495
                               number).format(fingerprint, number))
 
496
    return result