diff --git a/src/rust/src/backend/ec.rs b/src/rust/src/backend/ec.rs index c1bb66fb2cdc..039afbe84ba6 100644 --- a/src/rust/src/backend/ec.rs +++ b/src/rust/src/backend/ec.rs @@ -45,7 +45,8 @@ fn curve_from_py_curve( } let py_curve_name = py_curve.getattr(pyo3::intern!(py, "name"))?; - let nid = match &*py_curve_name.extract::()? { + let curve_name = &*py_curve_name.extract::()?; + let nid = match curve_name { "secp192r1" => openssl::nid::Nid::X9_62_PRIME192V1, "secp224r1" => openssl::nid::Nid::SECP224R1, "secp256r1" => openssl::nid::Nid::X9_62_PRIME256V1, @@ -84,7 +85,12 @@ fn curve_from_py_curve( } }; - Ok(openssl::ec::EcGroup::from_curve_name(nid)?) + Ok(openssl::ec::EcGroup::from_curve_name(nid).map_err(|_| { + exceptions::UnsupportedAlgorithm::new_err(( + format!("Curve {curve_name} is not supported"), + exceptions::Reasons::UNSUPPORTED_ELLIPTIC_CURVE, + )) + })?) } fn py_curve_from_curve<'p>( diff --git a/tests/hazmat/primitives/test_ec.py b/tests/hazmat/primitives/test_ec.py index 1f0e5e696e74..f77cc041260e 100644 --- a/tests/hazmat/primitives/test_ec.py +++ b/tests/hazmat/primitives/test_ec.py @@ -331,6 +331,18 @@ def test_generate_unknown_curve(self, backend): is False ) + @pytest.mark.parametrize("curve", ec._CURVE_TYPES.values()) + def test_generate_unsupported_curve( + self, backend, curve: ec.EllipticCurve + ): + if backend.elliptic_curve_supported(curve): + return + + with raises_unsupported_algorithm( + exceptions._Reasons.UNSUPPORTED_ELLIPTIC_CURVE + ): + ec.generate_private_key(curve) + def test_unknown_signature_algoritm(self, backend): _skip_curve_unsupported(backend, ec.SECP192R1())