diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-09-24 16:24:21 -0500 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-09-26 16:02:36 -0500 |
commit | 84fc4e01afaae95d5a92703b045a37183be27988 (patch) | |
tree | c5c73b7078e9431120264e6ace63d375c3435c44 /cryptography | |
parent | 58f63ed781b73478ee3fe60ebe1cfdfd85df5186 (diff) | |
download | cryptography-84fc4e01afaae95d5a92703b045a37183be27988.tar.gz cryptography-84fc4e01afaae95d5a92703b045a37183be27988.tar.bz2 cryptography-84fc4e01afaae95d5a92703b045a37183be27988.zip |
Process curve name when loading EC keys. Fixes #1336
Diffstat (limited to 'cryptography')
-rw-r--r-- | cryptography/hazmat/backends/openssl/backend.py | 31 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/asymmetric/ec.py | 24 |
2 files changed, 52 insertions, 3 deletions
diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py index 0b129d1a..65fcbcb1 100644 --- a/cryptography/hazmat/backends/openssl/backend.py +++ b/cryptography/hazmat/backends/openssl/backend.py @@ -474,12 +474,28 @@ class Backend(object): assert dsa_cdata != self._ffi.NULL dsa_cdata = self._ffi.gc(dsa_cdata, self._lib.DSA_free) return _DSAPrivateKey(self, dsa_cdata) - elif self._lib.Cryptography_HAS_EC == 1 \ - and type == self._lib.EVP_PKEY_EC: + elif (self._lib.Cryptography_HAS_EC == 1 and + type == self._lib.EVP_PKEY_EC): ec_cdata = self._lib.EVP_PKEY_get1_EC_KEY(evp_pkey) assert ec_cdata != self._ffi.NULL ec_cdata = self._ffi.gc(ec_cdata, self._lib.EC_KEY_free) - return _EllipticCurvePrivateKey(self, ec_cdata, None) + group = self._lib.EC_KEY_get0_group(ec_cdata) + assert group != self._ffi.NULL + + nid = self._lib.EC_GROUP_get_curve_name(group) + assert nid != 0 + + curve_name = self._lib.OBJ_nid2sn(nid) + assert curve_name != self._ffi.NULL + + sn = self._ffi.string(curve_name).decode('ascii') + + curve = self._sn_to_elliptic_curve(sn) + + point = self._lib.EC_POINT_new(group) + assert point != self._ffi.NULL + point = self._ffi.gc(point, self._lib.EC_POINT_free) + return _EllipticCurvePrivateKey(self, ec_cdata, curve) else: raise UnsupportedAlgorithm("Unsupported key type.") @@ -1048,6 +1064,15 @@ class Backend(object): ) return curve_nid + def _sn_to_elliptic_curve(self, sn): + try: + return ec.CURVE_TYPES[sn]() + except KeyError: + raise UnsupportedAlgorithm( + "{0} is not a supported elliptic curve".format(sn), + _Reasons.UNSUPPORTED_ELLIPTIC_CURVE + ) + @contextmanager def _bn_ctx_manager(self): bn_ctx = self._lib.BN_CTX_new() diff --git a/cryptography/hazmat/primitives/asymmetric/ec.py b/cryptography/hazmat/primitives/asymmetric/ec.py index 220a419c..813e2c9e 100644 --- a/cryptography/hazmat/primitives/asymmetric/ec.py +++ b/cryptography/hazmat/primitives/asymmetric/ec.py @@ -184,6 +184,30 @@ class SECP192R1(object): return 192 +CURVE_TYPES = { + "prime192v1": SECP192R1, + "prime256v1": SECP256R1, + + "secp192r1": SECP192R1, + "secp224r1": SECP224R1, + "secp256r1": SECP256R1, + "secp384r1": SECP384R1, + "secp521r1": SECP521R1, + + "sect163k1": SECT163K1, + "sect233k1": SECT233K1, + "sect283k1": SECT283K1, + "sect409k1": SECT409K1, + "sect571k1": SECT571K1, + + "sect163r2": SECT163R2, + "sect233r1": SECT233R1, + "sect283r1": SECT283R1, + "sect409r1": SECT409R1, + "sect571r1": SECT571R1, +} + + @utils.register_interface(interfaces.EllipticCurveSignatureAlgorithm) class ECDSA(object): def __init__(self, algorithm): |