Skip to content

Commit 42788a0

Browse files
authored
Fix exchange with keys that had Q automatically computed (#11309)
fixes #10790 closes #10864 closes #11218
1 parent 2dbdfb8 commit 42788a0

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

Diff for: docs/development/test-vectors.rst

+4
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ Key exchange
224224
* ``vectors/cryptoraphy_vectors/asymmetric/ECDH/brainpool.txt`` contains
225225
Brainpool vectors from :rfc:`7027`.
226226

227+
* ``vectors/cryptography_vectors/asymmetric/DH/dhpub_cryptography_old.pem``
228+
contains a Diffie-Hellman public key generated with a previous version of
229+
``cryptography``.
230+
227231
X.509
228232
~~~~~
229233

Diff for: src/rust/cryptography-key-parsing/src/spki.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@ pub fn parse_public_key(
114114
let pub_key = openssl::bn::BigNum::from_slice(pub_key_int.as_bytes())?;
115115
let dh = dh.set_public_key(pub_key)?;
116116

117-
cfg_if::cfg_if! {
118-
if #[cfg(CRYPTOGRAPHY_IS_LIBRESSL)] {
119-
Ok(openssl::pkey::PKey::from_dh(dh)?)
120-
} else {
121-
Ok(openssl::pkey::PKey::from_dhx(dh)?)
122-
}
123-
}
117+
Ok(openssl::pkey::PKey::from_dh(dh)?)
124118
}
125119
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
126120
AlgorithmParameters::DhKeyAgreement(dh_params) => {

Diff for: src/rust/src/backend/dh.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,6 @@ pub(crate) fn public_key_from_pkey(
7070
}
7171
}
7272

73-
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
74-
fn pkey_from_dh<T: openssl::pkey::HasParams>(
75-
dh: openssl::dh::Dh<T>,
76-
) -> CryptographyResult<openssl::pkey::PKey<T>> {
77-
cfg_if::cfg_if! {
78-
if #[cfg(CRYPTOGRAPHY_IS_LIBRESSL)] {
79-
Ok(openssl::pkey::PKey::from_dh(dh)?)
80-
} else {
81-
if dh.prime_q().is_some() {
82-
Ok(openssl::pkey::PKey::from_dhx(dh)?)
83-
} else {
84-
Ok(openssl::pkey::PKey::from_dh(dh)?)
85-
}
86-
}
87-
}
88-
}
89-
9073
#[pyo3::pyfunction]
9174
#[pyo3(signature = (data, backend=None))]
9275
fn from_der_parameters(
@@ -214,7 +197,8 @@ impl DHPrivateKey {
214197
let orig_dh = self.pkey.dh().unwrap();
215198
let dh = clone_dh(&orig_dh)?;
216199

217-
let pkey = pkey_from_dh(dh.set_public_key(orig_dh.public_key().to_owned()?)?)?;
200+
let pkey =
201+
openssl::pkey::PKey::from_dh(dh.set_public_key(orig_dh.public_key().to_owned()?)?)?;
218202

219203
Ok(DHPublicKey { pkey })
220204
}
@@ -322,7 +306,7 @@ impl DHParameters {
322306
fn generate_private_key(&self) -> CryptographyResult<DHPrivateKey> {
323307
let dh = clone_dh(&self.dh)?.generate_key()?;
324308
Ok(DHPrivateKey {
325-
pkey: pkey_from_dh(dh)?,
309+
pkey: openssl::pkey::PKey::from_dh(dh)?,
326310
})
327311
}
328312

@@ -435,7 +419,7 @@ impl DHPrivateNumbers {
435419
));
436420
}
437421

438-
let pkey = pkey_from_dh(dh)?;
422+
let pkey = openssl::pkey::PKey::from_dh(dh)?;
439423
Ok(DHPrivateKey { pkey })
440424
}
441425

@@ -478,7 +462,7 @@ impl DHPublicNumbers {
478462

479463
let pub_key = utils::py_int_to_bn(py, self.y.bind(py))?;
480464

481-
let pkey = pkey_from_dh(dh.set_public_key(pub_key)?)?;
465+
let pkey = openssl::pkey::PKey::from_dh(dh.set_public_key(pub_key)?)?;
482466

483467
Ok(DHPublicKey { pkey })
484468
}

Diff for: tests/hazmat/primitives/test_dh.py

+10
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ def test_dh_vectors_with_q(self, backend, vector):
441441
assert int.from_bytes(symkey1, "big") == int(vector["z"], 16)
442442
assert int.from_bytes(symkey2, "big") == int(vector["z"], 16)
443443

444+
def test_exchange_old_key(self, backend):
445+
k = load_vectors_from_file(
446+
os.path.join("asymmetric", "DH", "dhpub_cryptography_old.pem"),
447+
lambda f: serialization.load_pem_public_key(f.read()),
448+
mode="rb",
449+
)
450+
assert isinstance(k, dh.DHPublicKey)
451+
# Ensure this doesn't raise.
452+
k.parameters().generate_private_key().exchange(k)
453+
444454
def test_public_key_equality(self, backend):
445455
key_bytes = load_vectors_from_file(
446456
os.path.join("asymmetric", "DH", "dhpub.pem"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIICJTCCARcGCSqGSIb3DQEDATCCAQgCggEBAP//////////yQ/aoiFowjTExmKL
3+
gNwc0SkCTgiKZ8x0Agu+pjsTmyJRSgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVt
4+
bVHCReSFtXZiXn7G9ExC6aY37WsL/1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR
5+
7ORbPcIAfLihY78FmNpINhxV05ppFj+o/STPX4NlXSPco62WHGLzViCFUrue1SkH
6+
cJaWbWcMNU5KvJgE8XRsCMoYIXwykF5GLjbOO+OedywYDoY DmyeDouwHoo+1xV3w
7+
b0xSyd4ry/aVWBcYOZVJfOqVauUV0iYYmPoFEBVyjlqKrKpo//////////8CAQID
8+
ggEGAAKCAQEAoely6vSHw+/Q3zGYLaJj7eeQkfd25K8SvtC+FMY9D7jwS4g71pyr
9+
U3FJ98Fi45Wdksh+d4u7U089trF5Xbgui29bZ0HcQZtfHEEz0Mh69tkipCm2/QIj
10+
6eDlo6sPk9hhhvgg4MMGiWKhCtHrub3x1FHdmf7KjOhrGeb5apiudo7blGFzGhZ3
11+
NFnbff+ArVNd+rdVmSoZn0aMhXRConlDu/44IYe5/24VLl7G+BzZlIZO4P2M83fd
12+
mBOvR13cmYssQjEFTbaZVQvQHa3t0+aywfdCgsXGmTTK6QDCBP8D+vf1bmhEswzs
13+
oYn1GLtJ3VyYyMBPDBomd2ctchZgTzsX1w==
14+
-----END PUBLIC KEY-----
15+

0 commit comments

Comments
 (0)