From 6a4342c18ca0507f3d1842591553bddac6eb9189 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 7 Dec 2014 13:52:39 -0600 Subject: directly test r, s for integer-ness --- src/cryptography/hazmat/primitives/asymmetric/utils.py | 14 +++++++++----- tests/hazmat/primitives/test_asym_utils.py | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/cryptography/hazmat/primitives/asymmetric/utils.py b/src/cryptography/hazmat/primitives/asymmetric/utils.py index cf5973a0..71f4ff8e 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/utils.py +++ b/src/cryptography/hazmat/primitives/asymmetric/utils.py @@ -8,6 +8,8 @@ from pyasn1.codec.der import decoder, encoder from pyasn1.error import PyAsn1Error from pyasn1.type import namedtype, univ +import six + class _DSSSigValue(univ.Sequence): componentType = namedtype.NamedTypes( @@ -32,11 +34,13 @@ def decode_rfc6979_signature(signature): def encode_rfc6979_signature(r, s): - try: - sig = _DSSSigValue() - sig.setComponentByName('r', r) - sig.setComponentByName('s', s) - except PyAsn1Error: + if ( + not isinstance(r, six.integer_types) or + not isinstance(s, six.integer_types) + ): raise ValueError("Both r and s must be integers") + sig = _DSSSigValue() + sig.setComponentByName('r', r) + sig.setComponentByName('s', s) return encoder.encode(sig) diff --git a/tests/hazmat/primitives/test_asym_utils.py b/tests/hazmat/primitives/test_asym_utils.py index 3598f78a..9403669c 100644 --- a/tests/hazmat/primitives/test_asym_utils.py +++ b/tests/hazmat/primitives/test_asym_utils.py @@ -39,6 +39,7 @@ def test_rfc6979_signature(): def test_encode_rfc6979_non_integer(): with pytest.raises(ValueError): encode_rfc6979_signature("h", 3) + encode_rfc6979_signature("3", "2") encode_rfc6979_signature(3, "h") encode_rfc6979_signature(3.3, 1.2) encode_rfc6979_signature("hello", "world") -- cgit v1.2.3