diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2016-07-01 08:44:41 -0400 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2016-07-01 08:44:41 -0400 |
| commit | 4cd441aa298bd4433cac84cb57aedacaf86ee021 (patch) | |
| tree | 42ee0d4776bda29e9244744c59c156064ea70a43 /passlib | |
| parent | 339dd6a12ca5242409cacf8e696f32508888fcda (diff) | |
| download | passlib-4cd441aa298bd4433cac84cb57aedacaf86ee021.tar.gz | |
bugfix: bcrypt: fixed some test failures
* _finalize_backend_mixin() now traps AssertionError,
to detect assert failures like when bcrypt 3.0.0 returns
2b hash from 2y string. makes _norm_digest_args() correctly
work around the broken behavior.
* fixed bcrypt fuzz verifier to also work around 3.0.0's broken 2y support
* django_bcrypt_sha256 also needed the truncate patches that bcrypt_sha256 has,
so factored things out into common class. (commented out / removed some unused
code along the way).
Diffstat (limited to 'passlib')
| -rw-r--r-- | passlib/handlers/bcrypt.py | 67 | ||||
| -rw-r--r-- | passlib/handlers/django.py | 9 | ||||
| -rw-r--r-- | passlib/tests/test_handlers_bcrypt.py | 2 |
3 files changed, 46 insertions, 32 deletions
diff --git a/passlib/handlers/bcrypt.py b/passlib/handlers/bcrypt.py index 509312d..4fd3c70 100644 --- a/passlib/handlers/bcrypt.py +++ b/passlib/handlers/bcrypt.py @@ -295,6 +295,13 @@ class _BcryptCommon(uh.SubclassBackendMixin, uh.TruncateMixin, uh.HasManyIdents, # pybcrypt, bcrypt -- raises ValueError # bcryptor -- raises bcryptor.engine.SaltError return NotImplemented + except AssertionError as err: + # _calc_checksum() code may also throw AssertionError + # if correct hash isn't returned (e.g. 2y hash converted to 2b, + # such as happens with bcrypt 3.0.0) + log.debug("trapped unexpected response from %r backend: verify(%r, %r):", + backend, secret, hash, exc_info=True) + return NotImplemented def assert_lacks_8bit_bug(ident): """ @@ -383,7 +390,7 @@ class _BcryptCommon(uh.SubclassBackendMixin, uh.TruncateMixin, uh.HasManyIdents, if detect_wrap_bug(IDENT_2A): warn("passlib.hash.bcrypt: Your installation of the %r backend is vulnerable to " "the bsd wraparound bug, " - "and should be upgraded or replaced with another backend" + "and should be upgraded or replaced with another backend " "(enabling workaround for now)." % backend, uh.exc.PasslibSecurityWarning) mixin_cls._has_2a_wraparound_bug = True @@ -835,7 +842,40 @@ class bcrypt(_NoBackend, _BcryptCommon): #============================================================================= _UDOLLAR = u("$") -class bcrypt_sha256(bcrypt): +# XXX: it might be better to have all the bcrypt variants share a common base class, +# and have the (django_)bcrypt_sha256 wrappers just proxy bcrypt instead of subclassing it. +class _wrapped_bcrypt(bcrypt): + """ + abstracts out some bits bcrypt_sha256 & django_bcrypt_sha256 share. + - bypass backend-loading wrappers for hash() etc + - disable truncation support, sha256 wrappers don't need it. + """ + setting_kwds = tuple(elem for elem in bcrypt.setting_kwds if elem not in ["truncate_error"]) + truncate_size = None + + # XXX: these will be needed if any bcrypt backends directly implement this... + # @classmethod + # def hash(cls, secret, **kwds): + # # bypass bcrypt backend overriding this method + # # XXX: would wrapping bcrypt make this easier than subclassing it? + # return super(_BcryptCommon, cls).hash(secret, **kwds) + # + # @classmethod + # def verify(cls, secret, hash): + # # bypass bcrypt backend overriding this method + # return super(_BcryptCommon, cls).verify(secret, hash) + # + # @classmethod + # def genhash(cls, secret, hash): + # # bypass bcrypt backend overriding this method + # return super(_BcryptCommon, cls).genhash(secret, hash) + + @classmethod + def _check_truncate_policy(cls, secret): + # disable check performed by bcrypt(), since this doesn't truncate passwords. + pass + +class bcrypt_sha256(_wrapped_bcrypt): """This class implements a composition of BCrypt+SHA256, and follows the :ref:`password-hash-api`. It supports a fixed-length salt, and a variable number of rounds. @@ -850,8 +890,6 @@ class bcrypt_sha256(bcrypt): Now defaults to '2b' bcrypt algorithm. """ name = "bcrypt_sha256" - setting_kwds = tuple(elem for elem in bcrypt.setting_kwds if elem not in ["truncate_error"]) - truncate_size = None # this is locked at 2a/2b for now. ident_values = (IDENT_2A, IDENT_2B) @@ -915,22 +953,6 @@ class bcrypt_sha256(bcrypt): self.rounds, self.salt, self.checksum) return uascii_to_str(hash) - @classmethod - def hash(cls, secret): - # bypass bcrypt backend overriding this method - # XXX: would wrapping bcrypt make this easier than subclassing it? - return super(_BcryptCommon, cls).hash(secret) - - @classmethod - def verify(cls, secret, hash): - # bypass bcrypt backend overriding this method - return super(_BcryptCommon, cls).verify(secret, hash) - - @classmethod - def genhash(cls, secret, hash): - # bypass bcrypt backend overriding this method - return super(_BcryptCommon, cls).genhash(secret, hash) - def _calc_checksum(self, secret): # NOTE: can't use digest directly, since bcrypt stops at first NULL. # NOTE: bcrypt doesn't fully mix entropy for bytes 55-72 of password @@ -948,11 +970,6 @@ class bcrypt_sha256(bcrypt): # hand result off to normal bcrypt algorithm return super(bcrypt_sha256, self)._calc_checksum(key) - @classmethod - def _check_truncate_policy(cls, secret): - # disable check performed by bcrypt(), since this doesn't truncate passwords. - pass - # XXX: have _needs_update() mark the $2a$ ones for upgrading? # so do that after we switch to hex encoding? diff --git a/passlib/handlers/django.py b/passlib/handlers/django.py index 2b9547e..ac80c7b 100644 --- a/passlib/handlers/django.py +++ b/passlib/handlers/django.py @@ -9,6 +9,7 @@ from hashlib import md5, sha1, sha256 import logging; log = logging.getLogger(__name__) # site # pkg +from passlib.handlers.bcrypt import _wrapped_bcrypt from passlib.hash import bcrypt, pbkdf2_sha1, pbkdf2_sha256 from passlib.utils import to_unicode, rng, getrandstr, BASE64_CHARS from passlib.utils.compat import str_to_uascii, uascii_to_str, unicode, u @@ -176,7 +177,7 @@ django_bcrypt = uh.PrefixWrapper("django_bcrypt", bcrypt, """) django_bcrypt.django_name = "bcrypt" -class django_bcrypt_sha256(bcrypt): +class django_bcrypt_sha256(_wrapped_bcrypt): """This class implements Django 1.6's Bcrypt+SHA256 hash, and follows the :ref:`password-hash-api`. It supports a variable-length salt, and a variable number of rounds. @@ -225,12 +226,6 @@ class django_bcrypt_sha256(bcrypt): secret = hexlify(self._digest(secret).digest()) return super(django_bcrypt_sha256, self)._calc_checksum(secret) - # patch set_backend so it modifies bcrypt class, not this one... - # else it would clobber our _calc_checksum() wrapper above. - @classmethod - def set_backend(cls, *args, **kwds): - return bcrypt.set_backend(*args, **kwds) - class django_pbkdf2_sha256(DjangoVariableHash): """This class implements Django's PBKDF2-HMAC-SHA256 hash, and follows the :ref:`password-hash-api`. diff --git a/passlib/tests/test_handlers_bcrypt.py b/passlib/tests/test_handlers_bcrypt.py index d0de16a..92fb978 100644 --- a/passlib/tests/test_handlers_bcrypt.py +++ b/passlib/tests/test_handlers_bcrypt.py @@ -215,6 +215,8 @@ class _bcrypt_test(HandlerCase): hash = IDENT_2A + hash[3:] if secret: secret = repeat_string(secret, 72) + elif hash.startswith(IDENT_2Y) and bcrypt.__version__ == "3.0.0": + hash = IDENT_2B + hash[4:] hash = to_bytes(hash) try: return bcrypt.hashpw(secret, hash) == hash |
