aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-12-07 11:44:04 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-12-07 11:44:04 -0600
commita43964a0e90d7788b81521c9e7b949cdc2b555a0 (patch)
tree137b67f9ca66d141e51060001a810f613a5e9331
parentae6db32351447bf41b809ea4b18f17641724dac1 (diff)
downloadcryptography-a43964a0e90d7788b81521c9e7b949cdc2b555a0.tar.gz
cryptography-a43964a0e90d7788b81521c9e7b949cdc2b555a0.tar.bz2
cryptography-a43964a0e90d7788b81521c9e7b949cdc2b555a0.zip
catch PyAsn1Error for encoding signature as well
-rw-r--r--src/cryptography/hazmat/primitives/asymmetric/utils.py10
-rw-r--r--tests/hazmat/primitives/test_asym_utils.py8
2 files changed, 15 insertions, 3 deletions
diff --git a/src/cryptography/hazmat/primitives/asymmetric/utils.py b/src/cryptography/hazmat/primitives/asymmetric/utils.py
index 08bb40c7..cf5973a0 100644
--- a/src/cryptography/hazmat/primitives/asymmetric/utils.py
+++ b/src/cryptography/hazmat/primitives/asymmetric/utils.py
@@ -32,7 +32,11 @@ def decode_rfc6979_signature(signature):
def encode_rfc6979_signature(r, s):
- sig = _DSSSigValue()
- sig.setComponentByName('r', r)
- sig.setComponentByName('s', s)
+ try:
+ sig = _DSSSigValue()
+ sig.setComponentByName('r', r)
+ sig.setComponentByName('s', s)
+ except PyAsn1Error:
+ raise ValueError("Both r and s must be integers")
+
return encoder.encode(sig)
diff --git a/tests/hazmat/primitives/test_asym_utils.py b/tests/hazmat/primitives/test_asym_utils.py
index 1a945f3a..3598f78a 100644
--- a/tests/hazmat/primitives/test_asym_utils.py
+++ b/tests/hazmat/primitives/test_asym_utils.py
@@ -36,6 +36,14 @@ def test_rfc6979_signature():
assert decode_rfc6979_signature(sig4) == (-1, 0)
+def test_encode_rfc6979_non_integer():
+ with pytest.raises(ValueError):
+ encode_rfc6979_signature("h", 3)
+ encode_rfc6979_signature(3, "h")
+ encode_rfc6979_signature(3.3, 1.2)
+ encode_rfc6979_signature("hello", "world")
+
+
def test_decode_rfc6979_trailing_bytes():
with pytest.raises(ValueError):
decode_rfc6979_signature(b"0\x06\x02\x01\x01\x02\x01\x01\x00\x00\x00")