Skip to content

reject undersized modulus for dh public keys in spki loader - #15121

Merged
alex merged 1 commit into
pyca:mainfrom
dxbjavid:dh-public-key-min-modulus
Jul 3, 2026
Merged

alex merged 1 commit into
pyca:mainfrom
dxbjavid:dh-public-key-min-modulus

Conversation

@dxbjavid

Copy link
Copy Markdown
Contributor

Loading a public key currently accepts Diffie-Hellman keys whose modulus is well below 512 bits, even though the private-key DER loader and the DHParameterNumbers constructor already reject anything under that minimum, so the public-key path is the one place a badly undersized group slips through. The check belongs in parse_public_key in the SPKI loader, where both DH OIDs (the X9.42 dhpublicnumber form and the PKCS#3 dhKeyAgreement form) get turned into keys. I've added the same num_bits comparison there for both arms so the behaviour lines up with the other two entry points.

@alex

alex commented Jun 30, 2026

Copy link
Copy Markdown
Member

If we're going to do this, the place to do this enforcement shoudl be in cryptography-rust, not on the parser side.

@dxbjavid
dxbjavid force-pushed the dh-public-key-min-modulus branch from 5d152b1 to 59e7726 Compare June 30, 2026 15:42
@dxbjavid

Copy link
Copy Markdown
Contributor Author

makes sense, moved it out of the parser. the check now lives in public_key_from_pkey in cryptography-rust (backend/dh.rs), right after check_dh_parameters, so it sits alongside the other DH key construction rather than in the SPKI loader. reverted the spki.rs changes and added a python regression test that loads a 248-bit DH SPKI and expects a ValueError.

@alex

alex commented Jul 1, 2026

Copy link
Copy Markdown
Member

Looks like this test isn't actually hitting the expected code path?

@dxbjavid

dxbjavid commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

you're right, it wasn't. two things were going on: the der i'd hand-built wasn't a valid group, and on openssl check_dh_parameters (DH_check) already rejects a sub-512 modulus itself with DH_MODULUS_TOO_SMALL, so the ValueError was coming from there rather than the new code.

i've switched the test to use the public half of the existing 256-bit dh_key_256.pem vector, which is a real group that clears DH_check, and moved the explicit num_bits check ahead of check_dh_parameters so it's the thing that actually rejects the key. that matches the private-key loader, which does its own num_bits check rather than leaning on openssl's, and keeps the behaviour consistent on backends whose DH_check doesn't enforce the minimum. the load now raises from the new check and the test genuinely hits it.

Comment thread src/rust/src/backend/dh.rs Outdated
Comment on lines +75 to +77
pyo3::exceptions::PyValueError::new_err("Invalid key"),
));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to put this check in check_dh_parameters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reason not to, that's tidier. moved the num_bits check into check_dh_parameters and reverted the special-case in public_key_from_pkey, so all the DH construction paths share the same gate now. it still runs ahead of dh.check_key() so it's the thing that rejects the undersized modulus, and the test hits it.

@alex alex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a merge conflict

@dxbjavid
dxbjavid force-pushed the dh-public-key-min-modulus branch from 7c1cc3a to f99accc Compare July 3, 2026 17:25
@dxbjavid

dxbjavid commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

rebased on main to clear the conflict too (it was just the changelog).

@alex
alex enabled auto-merge (squash) July 3, 2026 17:27
@alex
alex merged commit 5d49222 into pyca:main Jul 3, 2026
69 checks passed
@dxbjavid

dxbjavid commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @alex for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants