Skip to content

Fix loading PKCS#3 DH parameters with privateValueLength#15045

Merged
reaperhulk merged 1 commit into
pyca:mainfrom
alex:dh-pkcs3-private-value-length
Jun 15, 2026
Merged

Fix loading PKCS#3 DH parameters with privateValueLength#15045
reaperhulk merged 1 commit into
pyca:mainfrom
alex:dh-pkcs3-private-value-length

Conversation

@alex

@alex alex commented Jun 15, 2026

Copy link
Copy Markdown
Member

PKCS#3 DH PARAMETERS may carry an optional trailing INTEGER, privateValueLength. The loader parsed every DH parameters blob with the X9.42-shaped struct (p, g, q?), so it misread privateValueLength as the subprime q. Since #15016 added a check_key() validation, loading such a file now fails with ValueError: Invalid DH parameters (previously it silently exposed a bogus q).

This routes the PEM loader by tag:

  • DH PARAMETERS (PKCS#3) parses with BasicDHParams, ignoring privateValueLength.
  • X9.42 DH PARAMETERS parses with DHParams, keeping q.

The DER loader stays X9.42-permissive: DER carries no tag to disambiguate PKCS#3 from X9.42, and the existing dhp_rfc5114_2.der vectors require q to be parsed (its 224-bit q can't fit privateValueLength's u32).

Adds a dhp_privatevaluelength.pem vector (documented in test-vectors.rst) and a regression test.

🤖 Generated with Claude Code

A PKCS#3 "DH PARAMETERS" structure may carry an optional trailing
INTEGER, privateValueLength. The loader parsed every DH parameters blob
with the X9.42-shaped struct (p, g, q?), so it misread privateValueLength
as the subprime q. Since pyca#15016 added a check_key() validation, this now
fails with "Invalid DH parameters".

Route the PEM loader by tag: "DH PARAMETERS" (PKCS#3) ignores
privateValueLength, while "X9.42 DH PARAMETERS" keeps q. The DER loader
stays X9.42-permissive since DER carries no tag to disambiguate and the
existing rfc5114 DER vectors require q to be parsed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@reaperhulk reaperhulk merged commit 5d695c9 into pyca:main Jun 15, 2026
63 checks passed
@reaperhulk

Copy link
Copy Markdown
Member

I think we're going to need a backport and 49.0.1 around this, sigh.

@garbusbeach

Copy link
Copy Markdown

hi @reaperhulk, is there an schedule for 49.0.1 release? :D

@reaperhulk

Copy link
Copy Markdown
Member

There is not, but we'll likely do it this upcoming week once we know what all we want to backport.

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.

DH parameters generated by OpenSSL rejected by load_pem_parameters in cryptography 49.x

3 participants