Skip to content

Commit d7596d0

Browse files
authored
fixes #12257 -- raise the correct error on an unsupported curve (#12271)
1 parent b1535a0 commit d7596d0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/rust/src/backend/ec.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ fn curve_from_py_curve(
4545
}
4646

4747
let py_curve_name = py_curve.getattr(pyo3::intern!(py, "name"))?;
48-
let nid = match &*py_curve_name.extract::<pyo3::pybacked::PyBackedStr>()? {
48+
let curve_name = &*py_curve_name.extract::<pyo3::pybacked::PyBackedStr>()?;
49+
let nid = match curve_name {
4950
"secp192r1" => openssl::nid::Nid::X9_62_PRIME192V1,
5051
"secp224r1" => openssl::nid::Nid::SECP224R1,
5152
"secp256r1" => openssl::nid::Nid::X9_62_PRIME256V1,
@@ -84,7 +85,12 @@ fn curve_from_py_curve(
8485
}
8586
};
8687

87-
Ok(openssl::ec::EcGroup::from_curve_name(nid)?)
88+
Ok(openssl::ec::EcGroup::from_curve_name(nid).map_err(|_| {
89+
exceptions::UnsupportedAlgorithm::new_err((
90+
format!("Curve {curve_name} is not supported"),
91+
exceptions::Reasons::UNSUPPORTED_ELLIPTIC_CURVE,
92+
))
93+
})?)
8894
}
8995

9096
fn py_curve_from_curve<'p>(

tests/hazmat/primitives/test_ec.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,21 @@ def test_generate_unknown_curve(self, backend):
331331
is False
332332
)
333333

334+
@pytest.mark.skip_fips(
335+
reason="Some FIPS curves aren't supported but work anyways"
336+
)
337+
@pytest.mark.parametrize("curve", ec._CURVE_TYPES.values())
338+
def test_generate_unsupported_curve(
339+
self, backend, curve: ec.EllipticCurve
340+
):
341+
if backend.elliptic_curve_supported(curve):
342+
return
343+
344+
with raises_unsupported_algorithm(
345+
exceptions._Reasons.UNSUPPORTED_ELLIPTIC_CURVE
346+
):
347+
ec.generate_private_key(curve)
348+
334349
def test_unknown_signature_algoritm(self, backend):
335350
_skip_curve_unsupported(backend, ec.SECP192R1())
336351

0 commit comments

Comments
 (0)