From 2c2d182cf781361117402f5dd0d8f9ee5387fd1a Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 24 Dec 2014 14:34:37 -0800 Subject: Fixes #1533 -- Initial work at parsing ECDSA public keys in OpenSSH format --- .../hazmat/primitives/serialization.py | 53 ++++++++++++++++++---- 1 file changed, 45 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py index 083f17e5..67f8a644 100644 --- a/src/cryptography/hazmat/primitives/serialization.py +++ b/src/cryptography/hazmat/primitives/serialization.py @@ -7,11 +7,10 @@ from __future__ import absolute_import, division, print_function import base64 import struct +import six + from cryptography.exceptions import UnsupportedAlgorithm -from cryptography.hazmat.primitives.asymmetric.dsa import ( - DSAParameterNumbers, DSAPublicNumbers -) -from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers +from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa def load_pem_private_key(data, password, backend): @@ -41,6 +40,10 @@ def load_ssh_public_key(data, backend): return _load_ssh_rsa_public_key(decoded_data, backend) elif key_type == b'ssh-dss': return _load_ssh_dss_public_key(decoded_data, backend) + elif key_type in [ + b'ecdsa-sha2-nistp256', b'ecdsa-sha2-nistp384', b'ecdsa-sha2-nistp521', + ]: + return _load_ssh_ecdsa_public_key(key_type, decoded_data, backend) else: raise UnsupportedAlgorithm( 'Only RSA and DSA keys are currently supported.' @@ -59,7 +62,7 @@ def _load_ssh_rsa_public_key(decoded_data, backend): if rest: raise ValueError('Key body contains extra bytes.') - return RSAPublicNumbers(e, n).public_key(backend) + return rsa.RSAPublicNumbers(e, n).public_key(backend) def _load_ssh_dss_public_key(decoded_data, backend): @@ -71,17 +74,51 @@ def _load_ssh_dss_public_key(decoded_data, backend): if key_type != b'ssh-dss': raise ValueError( - 'Key header and key body contain different key type values.') + 'Key header and key body contain different key type values.' + ) if rest: raise ValueError('Key body contains extra bytes.') - parameter_numbers = DSAParameterNumbers(p, q, g) - public_numbers = DSAPublicNumbers(y, parameter_numbers) + parameter_numbers = dsa.DSAParameterNumbers(p, q, g) + public_numbers = dsa.DSAPublicNumbers(y, parameter_numbers) return public_numbers.public_key(backend) +def _load_ssh_ecdsa_public_key(expected_key_type, decoded_data, backend): + key_type, rest = _read_next_string(decoded_data) + curve_name, rest = _read_next_string(rest) + data, rest = _read_next_string(rest) + + if key_type != expected_key_type != b"ecdsa-sha2" + curve_name: + raise ValueError( + 'Key header and key body contain different key type values.' + ) + + if rest: + raise ValueError('Key body contains extra bytes.') + + if key_type == "ecdsa-sha2-nistp256": + curve = ec.SECP256R1() + elif key_type == "ecdsa-sha2-nistp384": + curve = ec.SECP384R1() + elif key_type == "ecdsa-sha2-nistp521": + curve = ec.SECP521R1() + + if len(data) != 1 + 2 * (curve.key_size // 8): + raise ValueError("Malformed key bytes") + + if six.indexbytes(data, 0) != 4: + raise NotImplementedError( + "Compressed elliptic curve points are not supported" + ) + + x = _int_from_bytes(data[1:1 + curve.key_size // 8], byteorder='big') + y = _int_from_bytes(data[1 + curve.key_size // 8:], byteorder='big') + return ec.EllipticCurvePublicNumbers(x, y, curve).public_key(backend) + + def _read_next_string(data): """Retrieves the next RFC 4251 string value from the data.""" str_len, = struct.unpack('>I', data[:4]) -- cgit v1.2.3 From a4d59a9fea7ba549472b1a61af5f8c635f00d6c1 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 24 Dec 2014 14:41:53 -0800 Subject: Attempt to remove some duplication --- .../hazmat/primitives/serialization.py | 50 +++++++++++----------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py index 67f8a644..8a1c7ed6 100644 --- a/src/cryptography/hazmat/primitives/serialization.py +++ b/src/cryptography/hazmat/primitives/serialization.py @@ -29,6 +29,13 @@ def load_ssh_public_key(data, backend): 'Key is not in the proper format or contains extra data.') key_type = key_parts[0] + + if key_type not in [ + b'ssh-rsa', b'ssh-dss', b'ecdsa-sha2-nistp256', b'ecdsa-sha2-nistp384', + b'ecdsa-sha2-nistp521', + ]: + raise UnsupportedAlgorithm('Key type is not supported.') + key_body = key_parts[1] try: @@ -36,29 +43,27 @@ def load_ssh_public_key(data, backend): except TypeError: raise ValueError('Key is not in the proper format.') + inner_key_type, rest = _read_next_string(decoded_data) + + if inner_key_type != key_type: + raise ValueError( + 'Key header and key body contain different key type values.' + ) + if key_type == b'ssh-rsa': - return _load_ssh_rsa_public_key(decoded_data, backend) + return _load_ssh_rsa_public_key(rest, backend) elif key_type == b'ssh-dss': - return _load_ssh_dss_public_key(decoded_data, backend) + return _load_ssh_dss_public_key(rest, backend) elif key_type in [ b'ecdsa-sha2-nistp256', b'ecdsa-sha2-nistp384', b'ecdsa-sha2-nistp521', ]: - return _load_ssh_ecdsa_public_key(key_type, decoded_data, backend) - else: - raise UnsupportedAlgorithm( - 'Only RSA and DSA keys are currently supported.' - ) + return _load_ssh_ecdsa_public_key(key_type, rest, backend) def _load_ssh_rsa_public_key(decoded_data, backend): - key_type, rest = _read_next_string(decoded_data) - e, rest = _read_next_mpint(rest) + e, rest = _read_next_mpint(decoded_data) n, rest = _read_next_mpint(rest) - if key_type != b'ssh-rsa': - raise ValueError( - 'Key header and key body contain different key type values.') - if rest: raise ValueError('Key body contains extra bytes.') @@ -66,17 +71,11 @@ def _load_ssh_rsa_public_key(decoded_data, backend): def _load_ssh_dss_public_key(decoded_data, backend): - key_type, rest = _read_next_string(decoded_data) - p, rest = _read_next_mpint(rest) + p, rest = _read_next_mpint(decoded_data) q, rest = _read_next_mpint(rest) g, rest = _read_next_mpint(rest) y, rest = _read_next_mpint(rest) - if key_type != b'ssh-dss': - raise ValueError( - 'Key header and key body contain different key type values.' - ) - if rest: raise ValueError('Key body contains extra bytes.') @@ -87,11 +86,10 @@ def _load_ssh_dss_public_key(decoded_data, backend): def _load_ssh_ecdsa_public_key(expected_key_type, decoded_data, backend): - key_type, rest = _read_next_string(decoded_data) - curve_name, rest = _read_next_string(rest) + curve_name, rest = _read_next_string(decoded_data) data, rest = _read_next_string(rest) - if key_type != expected_key_type != b"ecdsa-sha2" + curve_name: + if expected_key_type != b"ecdsa-sha2-" + curve_name: raise ValueError( 'Key header and key body contain different key type values.' ) @@ -99,11 +97,11 @@ def _load_ssh_ecdsa_public_key(expected_key_type, decoded_data, backend): if rest: raise ValueError('Key body contains extra bytes.') - if key_type == "ecdsa-sha2-nistp256": + if curve_name == "nistp256": curve = ec.SECP256R1() - elif key_type == "ecdsa-sha2-nistp384": + elif curve_name == "nistp384": curve = ec.SECP384R1() - elif key_type == "ecdsa-sha2-nistp521": + elif curve_name == "nistp521": curve = ec.SECP521R1() if len(data) != 1 + 2 * (curve.key_size // 8): -- cgit v1.2.3 From d9aa3fbfa2fd15aa9c40a495ebbd972de2dde4de Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 24 Dec 2014 14:52:04 -0800 Subject: Reduce the duplication --- .../hazmat/primitives/serialization.py | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py index 8a1c7ed6..777bcc28 100644 --- a/src/cryptography/hazmat/primitives/serialization.py +++ b/src/cryptography/hazmat/primitives/serialization.py @@ -30,10 +30,15 @@ def load_ssh_public_key(data, backend): key_type = key_parts[0] - if key_type not in [ - b'ssh-rsa', b'ssh-dss', b'ecdsa-sha2-nistp256', b'ecdsa-sha2-nistp384', - b'ecdsa-sha2-nistp521', + if key_type == b'ssh-rsa': + loader = _load_ssh_rsa_public_key + elif key_type == b'ssh-dss': + loader = _load_ssh_dss_public_key + elif key_type in [ + b'ecdsa-sha2-nistp256', b'ecdsa-sha2-nistp384', b'ecdsa-sha2-nistp521', ]: + loader = _load_ssh_ecdsa_public_key + else: raise UnsupportedAlgorithm('Key type is not supported.') key_body = key_parts[1] @@ -50,17 +55,10 @@ def load_ssh_public_key(data, backend): 'Key header and key body contain different key type values.' ) - if key_type == b'ssh-rsa': - return _load_ssh_rsa_public_key(rest, backend) - elif key_type == b'ssh-dss': - return _load_ssh_dss_public_key(rest, backend) - elif key_type in [ - b'ecdsa-sha2-nistp256', b'ecdsa-sha2-nistp384', b'ecdsa-sha2-nistp521', - ]: - return _load_ssh_ecdsa_public_key(key_type, rest, backend) + return loader(key_type, rest, backend) -def _load_ssh_rsa_public_key(decoded_data, backend): +def _load_ssh_rsa_public_key(key_type, decoded_data, backend): e, rest = _read_next_mpint(decoded_data) n, rest = _read_next_mpint(rest) @@ -70,7 +68,7 @@ def _load_ssh_rsa_public_key(decoded_data, backend): return rsa.RSAPublicNumbers(e, n).public_key(backend) -def _load_ssh_dss_public_key(decoded_data, backend): +def _load_ssh_dss_public_key(key_type, decoded_data, backend): p, rest = _read_next_mpint(decoded_data) q, rest = _read_next_mpint(rest) g, rest = _read_next_mpint(rest) -- cgit v1.2.3 From 1a7d64e1bfd3e5d3877d3742388ac458bb64faa0 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 24 Dec 2014 14:54:36 -0800 Subject: THese are bytes for py3k --- src/cryptography/hazmat/primitives/serialization.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py index 777bcc28..448062d5 100644 --- a/src/cryptography/hazmat/primitives/serialization.py +++ b/src/cryptography/hazmat/primitives/serialization.py @@ -95,11 +95,11 @@ def _load_ssh_ecdsa_public_key(expected_key_type, decoded_data, backend): if rest: raise ValueError('Key body contains extra bytes.') - if curve_name == "nistp256": + if curve_name == b"nistp256": curve = ec.SECP256R1() - elif curve_name == "nistp384": + elif curve_name == b"nistp384": curve = ec.SECP384R1() - elif curve_name == "nistp521": + elif curve_name == b"nistp521": curve = ec.SECP521R1() if len(data) != 1 + 2 * (curve.key_size // 8): -- cgit v1.2.3 From 4b223434b4ca96de265334df036689264a82f9b8 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 24 Dec 2014 14:55:06 -0800 Subject: Improved docstring --- src/cryptography/hazmat/primitives/serialization.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py index 448062d5..b95ac1cd 100644 --- a/src/cryptography/hazmat/primitives/serialization.py +++ b/src/cryptography/hazmat/primitives/serialization.py @@ -116,7 +116,11 @@ def _load_ssh_ecdsa_public_key(expected_key_type, decoded_data, backend): def _read_next_string(data): - """Retrieves the next RFC 4251 string value from the data.""" + """ + Retrieves the next RFC 4251 string value from the data. + + While the RFC calls these strings, in Python they are bytes objects. + """ str_len, = struct.unpack('>I', data[:4]) return data[4:4 + str_len], data[4 + str_len:] -- cgit v1.2.3 From 8165db59374c7ce83e3ad34abf883195d1ec7b8b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Thu, 25 Dec 2014 08:34:42 -0800 Subject: Added test cases for NIST P-384 and 521. Fixed handling of key sizes which aren't divisibly by 8 --- src/cryptography/hazmat/primitives/serialization.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/cryptography/hazmat/primitives/serialization.py b/src/cryptography/hazmat/primitives/serialization.py index b95ac1cd..dad419fe 100644 --- a/src/cryptography/hazmat/primitives/serialization.py +++ b/src/cryptography/hazmat/primitives/serialization.py @@ -102,16 +102,18 @@ def _load_ssh_ecdsa_public_key(expected_key_type, decoded_data, backend): elif curve_name == b"nistp521": curve = ec.SECP521R1() - if len(data) != 1 + 2 * (curve.key_size // 8): - raise ValueError("Malformed key bytes") - if six.indexbytes(data, 0) != 4: raise NotImplementedError( "Compressed elliptic curve points are not supported" ) - x = _int_from_bytes(data[1:1 + curve.key_size // 8], byteorder='big') - y = _int_from_bytes(data[1 + curve.key_size // 8:], byteorder='big') + # key_size is in bits, and sometimes it's not evenly divisible by 8, so we + # add 7 to round up the number of bytes. + if len(data) != 1 + 2 * ((curve.key_size + 7) // 8): + raise ValueError("Malformed key bytes") + + x = _int_from_bytes(data[1:1 + (curve.key_size + 7) // 8], byteorder='big') + y = _int_from_bytes(data[1 + (curve.key_size + 7) // 8:], byteorder='big') return ec.EllipticCurvePublicNumbers(x, y, curve).public_key(backend) -- cgit v1.2.3