Skip to content

Commit 829520b

Browse files
authored
Add more robust processing for DH parameters. (#15016)
1 parent 0f05001 commit 829520b

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎src/rust/src/backend/dh.rs‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,20 @@ fn generate_parameters(
5555

5656
pub(crate) fn private_key_from_pkey(
5757
pkey: &openssl::pkey::PKeyRef<openssl::pkey::Private>,
58-
) -> DHPrivateKey {
59-
DHPrivateKey {
58+
) -> CryptographyResult<DHPrivateKey> {
59+
check_dh_parameters(&pkey.dh()?)?;
60+
Ok(DHPrivateKey {
6061
pkey: pkey.to_owned(),
61-
}
62+
})
6263
}
6364

6465
pub(crate) fn public_key_from_pkey(
6566
pkey: &openssl::pkey::PKeyRef<openssl::pkey::Public>,
66-
) -> DHPublicKey {
67-
DHPublicKey {
67+
) -> CryptographyResult<DHPublicKey> {
68+
check_dh_parameters(&pkey.dh()?)?;
69+
Ok(DHPublicKey {
6870
pkey: pkey.to_owned(),
69-
}
71+
})
7072
}
7173

7274
#[pyo3::pyfunction]
@@ -85,9 +87,9 @@ fn from_der_parameters(
8587
.transpose()?;
8688
let g = openssl::bn::BigNum::from_slice(asn1_params.g.as_bytes())?;
8789

88-
Ok(DHParameters {
89-
dh: openssl::dh::Dh::from_pqg(p, q, g)?,
90-
})
90+
let dh = openssl::dh::Dh::from_pqg(p, q, g)?;
91+
check_dh_parameters(&dh)?;
92+
Ok(DHParameters { dh })
9193
}
9294

9395
#[pyo3::pyfunction]
@@ -119,13 +121,19 @@ fn dh_parameters_from_numbers(
119121
let g = utils::py_int_to_bn(py, numbers.g.bind(py))?;
120122

121123
let dh = openssl::dh::Dh::from_pqg(p, q, g)?;
124+
check_dh_parameters(&dh)?;
125+
Ok(dh)
126+
}
122127

128+
fn check_dh_parameters<T: openssl::pkey::HasParams>(
129+
dh: &openssl::dh::Dh<T>,
130+
) -> CryptographyResult<()> {
123131
if !dh.check_key()? {
124132
return Err(CryptographyError::from(
125133
pyo3::exceptions::PyValueError::new_err("Invalid DH parameters"),
126134
));
127135
}
128-
Ok(dh)
136+
Ok(())
129137
}
130138

131139
fn clone_dh<T: openssl::pkey::HasParams>(

‎src/rust/src/backend/keys.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn private_key_from_pkey<'p>(
168168
openssl::pkey::Id::DSA => Ok(crate::backend::dsa::private_key_from_pkey(pkey)
169169
.into_pyobject(py)?
170170
.into_any()),
171-
openssl::pkey::Id::DH => Ok(crate::backend::dh::private_key_from_pkey(pkey)
171+
openssl::pkey::Id::DH => Ok(crate::backend::dh::private_key_from_pkey(pkey)?
172172
.into_pyobject(py)?
173173
.into_any()),
174174

@@ -177,7 +177,7 @@ fn private_key_from_pkey<'p>(
177177
CRYPTOGRAPHY_IS_BORINGSSL,
178178
CRYPTOGRAPHY_IS_AWSLC
179179
)))]
180-
openssl::pkey::Id::DHX => Ok(crate::backend::dh::private_key_from_pkey(pkey)
180+
openssl::pkey::Id::DHX => Ok(crate::backend::dh::private_key_from_pkey(pkey)?
181181
.into_pyobject(py)?
182182
.into_any()),
183183
#[cfg(any(
@@ -366,7 +366,7 @@ fn public_key_from_pkey<'p>(
366366
openssl::pkey::Id::DSA => Ok(crate::backend::dsa::public_key_from_pkey(pkey)
367367
.into_pyobject(py)?
368368
.into_any()),
369-
openssl::pkey::Id::DH => Ok(crate::backend::dh::public_key_from_pkey(pkey)
369+
openssl::pkey::Id::DH => Ok(crate::backend::dh::public_key_from_pkey(pkey)?
370370
.into_pyobject(py)?
371371
.into_any()),
372372

@@ -375,7 +375,7 @@ fn public_key_from_pkey<'p>(
375375
CRYPTOGRAPHY_IS_BORINGSSL,
376376
CRYPTOGRAPHY_IS_AWSLC
377377
)))]
378-
openssl::pkey::Id::DHX => Ok(crate::backend::dh::public_key_from_pkey(pkey)
378+
openssl::pkey::Id::DHX => Ok(crate::backend::dh::public_key_from_pkey(pkey)?
379379
.into_pyobject(py)?
380380
.into_any()),
381381
#[cfg(any(

0 commit comments

Comments
 (0)