~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/gpg.py

  • Committer: Samuel Bronson
  • Date: 2012-08-30 20:36:18 UTC
  • mto: (6015.57.3 2.4)
  • mto: This revision was merged to the branch mainline in revision 6558.
  • Revision ID: naesten@gmail.com-20120830203618-y2dzw91nqpvpgxvx
Update INSTALL for switch to Python 2.6 and up.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""GPG signing and checking logic."""
19
19
 
20
 
from __future__ import absolute_import
21
 
 
22
20
import os
23
21
import sys
24
22
from StringIO import StringIO
29
27
import subprocess
30
28
 
31
29
from bzrlib import (
32
 
    config,
33
30
    errors,
34
31
    trace,
35
32
    ui,
36
33
    )
37
 
from bzrlib.i18n import (
38
 
    gettext, 
39
 
    ngettext,
40
 
    )
41
34
""")
42
35
 
 
36
class i18n:
 
37
    """this class is ready to use bzrlib.i18n but bzrlib.i18n is not ready to
 
38
    use so here is a stub until it is"""
 
39
    @staticmethod
 
40
    def gettext(string):
 
41
        return string
 
42
        
 
43
    @staticmethod
 
44
    def ngettext(single, plural, number):
 
45
        if number == 1:
 
46
            return single
 
47
        else:
 
48
            return plural
 
49
 
43
50
#verification results
44
51
SIGNATURE_VALID = 0
45
52
SIGNATURE_KEY_MISSING = 1
46
53
SIGNATURE_NOT_VALID = 2
47
54
SIGNATURE_NOT_SIGNED = 3
48
 
SIGNATURE_EXPIRED = 4
49
55
 
50
56
 
51
57
class DisabledGPGStrategy(object):
102
108
        count = {SIGNATURE_VALID: 0,
103
109
                 SIGNATURE_KEY_MISSING: 0,
104
110
                 SIGNATURE_NOT_VALID: 0,
105
 
                 SIGNATURE_NOT_SIGNED: 0,
106
 
                 SIGNATURE_EXPIRED: 0}
 
111
                 SIGNATURE_NOT_SIGNED: 0}
107
112
        result = []
108
113
        all_verifiable = True
109
114
        for rev_id in revisions:
110
115
            verification_result, uid =\
111
 
                repository.verify_revision_signature(rev_id,self)
 
116
                                repository.verify_revision(rev_id,self)
112
117
            result.append([rev_id, verification_result, uid])
113
118
            count[verification_result] += 1
114
119
            if verification_result != SIGNATURE_VALID:
116
121
        return (count, result, all_verifiable)
117
122
 
118
123
    def valid_commits_message(self, count):
119
 
        return gettext(u"{0} commits with valid signatures").format(
120
 
                                        count[SIGNATURE_VALID])
 
124
        return i18n.gettext(u"{0} commits with valid signatures").format(
 
125
                                        count[SIGNATURE_VALID])            
121
126
 
122
127
    def unknown_key_message(self, count):
123
 
        return ngettext(u"{0} commit with unknown key",
 
128
        return i18n.ngettext(u"{0} commit with unknown key",
124
129
                             u"{0} commits with unknown keys",
125
130
                             count[SIGNATURE_KEY_MISSING]).format(
126
131
                                        count[SIGNATURE_KEY_MISSING])
127
132
 
128
133
    def commit_not_valid_message(self, count):
129
 
        return ngettext(u"{0} commit not valid",
 
134
        return i18n.ngettext(u"{0} commit not valid",
130
135
                             u"{0} commits not valid",
131
136
                             count[SIGNATURE_NOT_VALID]).format(
132
137
                                            count[SIGNATURE_NOT_VALID])
133
138
 
134
139
    def commit_not_signed_message(self, count):
135
 
        return ngettext(u"{0} commit not signed",
 
140
        return i18n.ngettext(u"{0} commit not signed",
136
141
                             u"{0} commits not signed",
137
142
                             count[SIGNATURE_NOT_SIGNED]).format(
138
143
                                        count[SIGNATURE_NOT_SIGNED])
139
144
 
140
 
    def expired_commit_message(self, count):
141
 
        return ngettext(u"{0} commit with key now expired",
142
 
                        u"{0} commits with key now expired",
143
 
                        count[SIGNATURE_EXPIRED]).format(
144
 
                                        count[SIGNATURE_EXPIRED])
145
 
 
146
145
 
147
146
def _set_gpg_tty():
148
147
    tty = os.environ.get('TTY')
162
161
 
163
162
    acceptable_keys = None
164
163
 
165
 
    def __init__(self, config_stack):
166
 
        self._config_stack = config_stack
167
 
        try:
168
 
            import gpgme
169
 
            self.context = gpgme.Context()
170
 
        except ImportError, error:
171
 
            pass # can't use verify()
172
 
 
173
164
    @staticmethod
174
165
    def verify_signatures_available():
175
166
        """
184
175
            return False
185
176
 
186
177
    def _command_line(self):
187
 
        key = self._config_stack.get('gpg_signing_key')
188
 
        if key is None or key == 'default':
189
 
            # 'default' or not setting gpg_signing_key at all means we should
190
 
            # use the user email address
191
 
            key = config.extract_email_address(self._config_stack.get('email'))
192
 
        return [self._config_stack.get('gpg_signing_command'), '--clearsign',
193
 
                '-u', key]
 
178
        return [self._config.gpg_signing_command(), '--clearsign']
 
179
 
 
180
    def __init__(self, config):
 
181
        self._config = config
 
182
        try:
 
183
            import gpgme
 
184
            self.context = gpgme.Context()
 
185
        except ImportError, error:
 
186
            pass # can't use verify()
194
187
 
195
188
    def sign(self, content):
196
189
        if isinstance(content, unicode):
243
236
 
244
237
        signature = StringIO(content)
245
238
        plain_output = StringIO()
 
239
        
246
240
        try:
247
241
            result = self.context.verify(signature, None, plain_output)
248
242
        except gpgme.GpgmeError,error:
249
243
            raise errors.SignatureVerificationFailed(error[2])
250
244
 
251
 
        # No result if input is invalid.
252
 
        # test_verify_invalid()
253
245
        if len(result) == 0:
254
246
            return SIGNATURE_NOT_VALID, None
255
 
        # User has specified a list of acceptable keys, check our result is in
256
 
        # it.  test_verify_unacceptable_key()
257
247
        fingerprint = result[0].fpr
258
248
        if self.acceptable_keys is not None:
259
249
            if not fingerprint in self.acceptable_keys:
260
250
                return SIGNATURE_KEY_MISSING, fingerprint[-8:]
261
 
        # Check the signature actually matches the testament.
262
 
        # test_verify_bad_testament()
263
251
        if testament != plain_output.getvalue():
264
252
            return SIGNATURE_NOT_VALID, None
265
 
        # Yay gpgme set the valid bit.
266
 
        # Can't write a test for this one as you can't set a key to be
267
 
        # trusted using gpgme.
268
253
        if result[0].summary & gpgme.SIGSUM_VALID:
269
254
            key = self.context.get_key(fingerprint)
270
255
            name = key.uids[0].name
271
256
            email = key.uids[0].email
272
257
            return SIGNATURE_VALID, name + " <" + email + ">"
273
 
        # Sigsum_red indicates a problem, unfortunatly I have not been able
274
 
        # to write any tests which actually set this.
275
258
        if result[0].summary & gpgme.SIGSUM_RED:
276
259
            return SIGNATURE_NOT_VALID, None
277
 
        # GPG does not know this key.
278
 
        # test_verify_unknown_key()
279
260
        if result[0].summary & gpgme.SIGSUM_KEY_MISSING:
280
261
            return SIGNATURE_KEY_MISSING, fingerprint[-8:]
281
 
        # Summary isn't set if sig is valid but key is untrusted but if user
282
 
        # has explicity set the key as acceptable we can validate it.
 
262
        #summary isn't set if sig is valid but key is untrusted
283
263
        if result[0].summary == 0 and self.acceptable_keys is not None:
284
264
            if fingerprint in self.acceptable_keys:
285
 
                # test_verify_untrusted_but_accepted()
286
265
                return SIGNATURE_VALID, None
287
 
        # test_verify_valid_but_untrusted()
288
 
        if result[0].summary == 0 and self.acceptable_keys is None:
289
 
            return SIGNATURE_NOT_VALID, None
290
 
        if result[0].summary & gpgme.SIGSUM_KEY_EXPIRED:
291
 
            expires = self.context.get_key(result[0].fpr).subkeys[0].expires
292
 
            if expires > result[0].timestamp:
293
 
                # The expired key was not expired at time of signing.
294
 
                # test_verify_expired_but_valid()
295
 
                return SIGNATURE_EXPIRED, fingerprint[-8:]
296
 
            else:
297
 
                # I can't work out how to create a test where the signature
298
 
                # was expired at the time of signing.
299
 
                return SIGNATURE_NOT_VALID, None
300
 
        # A signature from a revoked key gets this.
301
 
        # test_verify_revoked_signature()
302
 
        if result[0].summary & gpgme.SIGSUM_SYS_ERROR:
303
 
            return SIGNATURE_NOT_VALID, None
304
 
        # Other error types such as revoked keys should (I think) be caught by
305
 
        # SIGSUM_RED so anything else means something is buggy.
 
266
        else:
 
267
            return SIGNATURE_KEY_MISSING, None
306
268
        raise errors.SignatureVerificationFailed("Unknown GnuPG key "\
307
269
                                                 "verification result")
308
270
 
309
271
    def set_acceptable_keys(self, command_line_input):
310
 
        """Set the acceptable keys for verifying with this GPGStrategy.
 
272
        """sets the acceptable keys for verifying with this GPGStrategy
311
273
        
312
274
        :param command_line_input: comma separated list of patterns from
313
275
                                command line
314
276
        :return: nothing
315
277
        """
316
278
        key_patterns = None
317
 
        acceptable_keys_config = self._config_stack.get('acceptable_keys')
 
279
        acceptable_keys_config = self._config.acceptable_keys()
318
280
        try:
319
281
            if isinstance(acceptable_keys_config, unicode):
320
282
                acceptable_keys_config = str(acceptable_keys_config)
321
283
        except UnicodeEncodeError:
322
 
            # gpg Context.keylist(pattern) does not like unicode
323
 
            raise errors.BzrCommandError(
324
 
                gettext('Only ASCII permitted in option names'))
 
284
            #gpg Context.keylist(pattern) does not like unicode
 
285
            raise errors.BzrCommandError('Only ASCII permitted in option names')
325
286
 
326
287
        if acceptable_keys_config is not None:
327
288
            key_patterns = acceptable_keys_config
328
 
        if command_line_input is not None: # command line overrides config
 
289
        if command_line_input is not None: #command line overrides config
329
290
            key_patterns = command_line_input
330
291
        if key_patterns is not None:
331
292
            patterns = key_patterns.split(",")
339
300
                    self.acceptable_keys.append(key.subkeys[0].fpr)
340
301
                    trace.mutter("Added acceptable key: " + key.subkeys[0].fpr)
341
302
                if not found_key:
342
 
                    trace.note(gettext(
343
 
                            "No GnuPG key results for pattern: {0}"
 
303
                    trace.note(i18n.gettext(
 
304
                            "No GnuPG key results for pattern: {}"
344
305
                                ).format(pattern))
345
306
 
346
307
    def do_verifications(self, revisions, repository,
359
320
        count = {SIGNATURE_VALID: 0,
360
321
                 SIGNATURE_KEY_MISSING: 0,
361
322
                 SIGNATURE_NOT_VALID: 0,
362
 
                 SIGNATURE_NOT_SIGNED: 0,
363
 
                 SIGNATURE_EXPIRED: 0}
 
323
                 SIGNATURE_NOT_SIGNED: 0}
364
324
        result = []
365
325
        all_verifiable = True
366
326
        for rev_id in revisions:
367
327
            verification_result, uid =\
368
 
                repository.verify_revision_signature(rev_id, self)
 
328
                                repository.verify_revision(rev_id,self)
369
329
            result.append([rev_id, verification_result, uid])
370
330
            count[verification_result] += 1
371
331
            if verification_result != SIGNATURE_VALID:
383
343
                signers[uid] += 1
384
344
        result = []
385
345
        for uid, number in signers.items():
386
 
             result.append( ngettext(u"{0} signed {1} commit",
 
346
             result.append( i18n.ngettext(u"{0} signed {1} commit", 
387
347
                             u"{0} signed {1} commits",
388
348
                             number).format(uid, number) )
389
349
        return result
400
360
                signers[authors] += 1
401
361
        result = []
402
362
        for authors, number in signers.items():
403
 
            result.append( ngettext(u"{0} commit by author {1}",
 
363
            result.append( i18n.ngettext(u"{0} commit by author {1}", 
404
364
                                 u"{0} commits by author {1}",
405
365
                                 number).format(number, authors) )
406
366
        return result
416
376
                signers[authors] += 1
417
377
        result = []
418
378
        for authors, number in signers.items():
419
 
            result.append( ngettext(u"{0} commit by author {1}",
 
379
            result.append( i18n.ngettext(u"{0} commit by author {1}", 
420
380
                                 u"{0} commits by author {1}",
421
381
                                 number).format(number, authors) )
422
382
        return result
430
390
                signers[fingerprint] += 1
431
391
        result = []
432
392
        for fingerprint, number in signers.items():
433
 
            result.append( ngettext(u"Unknown key {0} signed {1} commit",
 
393
            result.append( i18n.ngettext(u"Unknown key {0} signed {1} commit", 
434
394
                                 u"Unknown key {0} signed {1} commits",
435
395
                                 number).format(fingerprint, number) )
436
396
        return result
437
397
 
438
 
    def verbose_expired_key_message(self, result, repo):
439
 
        """takes a verify result and returns list of expired key info"""
440
 
        signers = {}
441
 
        fingerprint_to_authors = {}
442
 
        for rev_id, validity, fingerprint in result:
443
 
            if validity == SIGNATURE_EXPIRED:
444
 
                revision = repo.get_revision(rev_id)
445
 
                authors = ', '.join(revision.get_apparent_authors())
446
 
                signers.setdefault(fingerprint, 0)
447
 
                signers[fingerprint] += 1
448
 
                fingerprint_to_authors[fingerprint] = authors
449
 
        result = []
450
 
        for fingerprint, number in signers.items():
451
 
            result.append(
452
 
                ngettext(u"{0} commit by author {1} with key {2} now expired",
453
 
                         u"{0} commits by author {1} with key {2} now expired",
454
 
                         number).format(
455
 
                    number, fingerprint_to_authors[fingerprint], fingerprint) )
456
 
        return result
457
 
 
458
398
    def valid_commits_message(self, count):
459
399
        """returns message for number of commits"""
460
 
        return gettext(u"{0} commits with valid signatures").format(
 
400
        return i18n.gettext(u"{0} commits with valid signatures").format(
461
401
                                        count[SIGNATURE_VALID])
462
402
 
463
403
    def unknown_key_message(self, count):
464
404
        """returns message for number of commits"""
465
 
        return ngettext(u"{0} commit with unknown key",
466
 
                        u"{0} commits with unknown keys",
467
 
                        count[SIGNATURE_KEY_MISSING]).format(
 
405
        return i18n.ngettext(u"{0} commit with unknown key",
 
406
                             u"{0} commits with unknown keys",
 
407
                             count[SIGNATURE_KEY_MISSING]).format(
468
408
                                        count[SIGNATURE_KEY_MISSING])
469
409
 
470
410
    def commit_not_valid_message(self, count):
471
411
        """returns message for number of commits"""
472
 
        return ngettext(u"{0} commit not valid",
473
 
                        u"{0} commits not valid",
474
 
                        count[SIGNATURE_NOT_VALID]).format(
 
412
        return i18n.ngettext(u"{0} commit not valid",
 
413
                             u"{0} commits not valid",
 
414
                             count[SIGNATURE_NOT_VALID]).format(
475
415
                                            count[SIGNATURE_NOT_VALID])
476
416
 
477
417
    def commit_not_signed_message(self, count):
478
418
        """returns message for number of commits"""
479
 
        return ngettext(u"{0} commit not signed",
480
 
                        u"{0} commits not signed",
481
 
                        count[SIGNATURE_NOT_SIGNED]).format(
 
419
        return i18n.ngettext(u"{0} commit not signed",
 
420
                             u"{0} commits not signed",
 
421
                             count[SIGNATURE_NOT_SIGNED]).format(
482
422
                                        count[SIGNATURE_NOT_SIGNED])
483
 
 
484
 
    def expired_commit_message(self, count):
485
 
        """returns message for number of commits"""
486
 
        return ngettext(u"{0} commit with key now expired",
487
 
                        u"{0} commits with key now expired",
488
 
                        count[SIGNATURE_EXPIRED]).format(
489
 
                                    count[SIGNATURE_EXPIRED])