Skip to content

fix(keys): stop reading DH parameters, signatures and other non-key DER as RSA - #47

Open
bitwize-music wants to merge 1 commit into
nmatt0:masterfrom
bitwize-music:fix/nonkey-der-as-rsa
Open

bitwize-music wants to merge 1 commit into
nmatt0:masterfrom
bitwize-music:fix/nonkey-der-as-rsa

Conversation

@bitwize-music

Copy link
Copy Markdown

What

The key-weakness pass reported ASN.1 structures that are not keys as RSA public keys. Every bundled dhparam file (DH PARAMETERS) produced rsa-even-exponent [high]. DER (EC)DSA signature files produced rsa-tiny-modulus and cross-file shared-prime. SSL session files produced roca-key on a 1-bit "modulus". #46 has the repro, the full list of affected structures, and prevalence data.

Why it happened

parse_pub_der fell back to a bare PKCS#1 RSAPublicKey for any SEQUENCE that starts with two INTEGERs. Every PEM block other than CERTIFICATE / *PRIVATE KEY reached it, and so did any whole-file buffer starting with 0x30. SEQ { INTEGER, INTEGER } is also a PKCS#3 DHParameter {p, g} and an (EC)DSA-Sig-Value {r, s}. DSA and X9.42 DH parameters, a traditional DSAPublicKey, an SSL_SESSION and a multi-prime RSAPrivateKey all begin with it.

Fix

  • parse_pub_der takes the PEM label, as parse_priv_der has since fix(keys): parse traditional DSA private keys as DSA, not RSA #45. A bare RSAPublicKey is read only under RSA PUBLIC KEY / PUBLIC KEY. Under any other label only an SPKI is accepted, since its OID names the key type. So DH PARAMETERS, DSA PARAMETERS, SSL SESSION PARAMETERS, DSA PUBLIC KEY and similar blocks yield no key.
  • For unlabeled whole-file DER, plausible_bare_rsa requires the values to fit RSA:
    • exactly two INTEGERs;
    • n >= 512 bits (a signature's r is <= 521 bits);
    • e odd and < 2^256, per PKCS#1 / FIPS 186-5 (a DH generator is usually 2, and a signature's s is as wide as its r).
  • Known limitation: an unlabeled two-field DH group with an odd generator (3 or 5) still passes. Telling its prime from an RSA modulus needs a primality test, and one 2048-bit modexp takes about 5 s with the in-tree bignum. At >= 2048 bits no check fires on it anyway.
  • Unchanged: labeled RSA PUBLIC KEY / PUBLIC KEY, SPKI, certificates, private keys and SSH keys.

Test

  • tests/test_keys.py: 18 non-key fixtures must produce no finding. They cover 2- and 3-field DH, X9.42 DH, DSA parameters, a traditional DSA public key and an SSL session (PEM and DER), a DH PEM string inside a binary, and P-256/384/521 and DSA signature files with forced even r values. Unlabeled bare PKCS#1 keys (1024-bit e=65537, 1024-bit e=3, 512-bit) must still be flagged. On master the fixtures produce 38 findings and the test fails; it passes here.
  • tests/unit/unit_tests.cpp: the same SEQ { 65537, 65537 } body is a key under RSA PUBLIC KEY and not under DH PARAMETERS. Unlabeled, it is not a key.
  • Each rule is load-bearing. Disabling any one of them (the label rule, exactly two INTEGERs, n >= 512, e odd, e < 2^256, or the whole unlabeled check) makes test_keys fail.
  • Full suite green (ctest 15/15), also under -DMT_SANITIZE=ON. 7.5k mutated DER blobs ran through the ASan build with no report.
  • Corpus before/after (about 1.4M files): 71 findings removed, 0 added. Every removed finding traces to a non-key block (PEM DH / X9.42 DH / DSA / SSL SESSION PARAMETERS, or DER dhparams). The remaining RSA findings all come from certificates, PUBLIC KEY / PRIVATE KEY PEM, SSH keys, or DER SPKI and certs, except one OpenSSL test file that is the documented generator-5 case.

Closes #46

🤖 Generated with Claude Code

…ER as RSA

parse_pub_der fell back to a bare PKCS#1 RSAPublicKey for any SEQUENCE
whose first two elements are INTEGERs. Every PEM block that is not a
CERTIFICATE or a private key reached it, as did any whole-file buffer
starting with 0x30. But SEQ { INTEGER, INTEGER } is also a PKCS#3
DHParameter { p, g } and an (EC)DSA signature { r, s }, and DSA and X9.42
DH parameters, a traditional DSA public key, an SSL session and a
multi-prime RSAPrivateKey begin with it. So a bundled dh2048.pem was
reported as an RSA-2048 key with rsa-even-exponent (g = 2). A DER ECDSA
signature file became an RSA-256 key with rsa-tiny-modulus. An SSL
session's version field became a 1-bit modulus flagged roca-key. Signature
r values that shared a small factor were reported as shared-prime.

Pass the PEM label to parse_pub_der, as parse_priv_der has taken it since
nmatt0#45. A bare RSAPublicKey is read only under "RSA PUBLIC KEY" or "PUBLIC
KEY". An SPKI is still accepted under any label, because its OID names the
key type. Unlabeled whole-file DER is read as RSA only when the values fit
RSA: exactly two INTEGERs, n >= 512 bits, and e odd and < 2^256. An
unlabeled two-field DH group with an odd generator (3, 5) still passes,
since telling its prime from an RSA modulus needs a primality test.

test_keys gains 18 non-key fixtures and asserts that none produces a
finding: DH, X9.42 DH and DSA parameters, a traditional DSA public key, an
SSL session, a DH PEM string inside a binary, DER DH, DSA and session
blobs, and P-256/384/521 and DSA signature files. It also checks that
unlabeled bare PKCS#1 keys (1024-bit e=65537, 1024-bit e=3, 512-bit) are
still read as RSA. The unit tests check that the same SEQ body is a key
under RSA PUBLIC KEY but not under DH PARAMETERS, and that an unlabeled
17-bit "modulus" is not a key.

Closes nmatt0#46

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

keys: DH/DSA parameters, SSL sessions and DER signatures are reported as RSA public keys

1 participant