Skip to content

[dnssec] Fix ASN.1 DER encoding of DSA and ECDSA signatures - #152

Merged
Flowdalic merged 1 commit into
MiniDNS:masterfrom
Flowdalic:dnssec-fix-ecdsa-der-encoding
Oct 18, 2026
Merged

Flowdalic merged 1 commit into
MiniDNS:masterfrom
Flowdalic:dnssec-fix-ecdsa-der-encoding

Conversation

@Flowdalic

Copy link
Copy Markdown
Collaborator

The conversion of the RRSIG signature into ASN.1 DER, as expected by java.security.Signature, did not produce the minimal encoding of the signature's r and s INTEGERs in all cases:

First, EcdsaSignatureVerifier never removed leading zero bytes. Hence, if r or s started with a zero byte followed by a byte without the high bit set, which happens for roughly one in 256 signatures, the encoding contained a redundant leading zero. Strict DER parsers, like the one of the SunEC provider, reject this with "Invalid encoding: redundant leading 0s", which caused the validation of perfectly valid ECDSAP256SHA256 and ECDSAP384SHA384 signatures to fail with "Signature is invalid.".

Secondly, DsaSignatureVerifier removed all leading zero bytes, but did not add back the zero byte required if the following byte has its high bit set, which results in r or s being encoded as negative INTEGER.

Both now use a shared helper which uses BigInteger to produce the minimal two's complement encoding.

Add ECDSA support to DnssecWorld and tests for ECDSA signatures, including two real-world signatures of the cloudflare.com A RRset with a redundant leading zero byte in r and s, respectively. Also fix DnssecWorld's conversion of DSA signatures from ASN.1 to RFC 2536, which produced garbage if r or s was shorter than 20 bytes.

Thanks to László Károlyi for reporting this and suggesting the fix.

Fixes #151.

The conversion of the RRSIG signature into ASN.1 DER, as expected by
java.security.Signature, did not produce the minimal encoding of the
signature's r and s INTEGERs in all cases:

First, EcdsaSignatureVerifier never removed leading zero bytes. Hence, if r
or s started with a zero byte followed by a byte without the high bit set,
which happens for roughly one in 256 signatures, the encoding contained a
redundant leading zero. Strict DER parsers, like the one of the SunEC
provider, reject this with "Invalid encoding: redundant leading 0s", which
caused the validation of perfectly valid ECDSAP256SHA256 and
ECDSAP384SHA384 signatures to fail with "Signature is invalid.".

Secondly, DsaSignatureVerifier removed all leading zero bytes, but did not
add back the zero byte required if the following byte has its high bit set,
which results in r or s being encoded as negative INTEGER.

Both now use a shared helper which uses BigInteger to produce the
minimal two's complement encoding.

Add ECDSA support to DnssecWorld and tests for ECDSA signatures,
including two real-world signatures of the cloudflare.com A RRset with
a redundant leading zero byte in r and s, respectively. Also fix
DnssecWorld's conversion of DSA signatures from ASN.1 to RFC 2536,
which produced garbage if r or s was shorter than 20 bytes.

Thanks to László Károlyi for reporting this and suggesting the fix.

Fixes MiniDNS#151.
@Flowdalic
Flowdalic force-pushed the dnssec-fix-ecdsa-der-encoding branch from b390813 to 0f0635f Compare October 18, 2026 10:56
@Flowdalic
Flowdalic merged commit 3fb9f35 into MiniDNS:master Oct 18, 2026
2 checks passed
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 39121214226

Coverage increased (+0.6%) to 60.009%

Details

  • Coverage increased (+0.6%) from the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • 5 coverage regressions across 3 files.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

5 previously-covered lines in 3 files lost coverage.

File Lines Losing Coverage Coverage
org/minidns/dnssec/algorithms/DsaSignatureVerifier.java 2 93.75%
org/minidns/dnssec/algorithms/EcdsaSignatureVerifier.java 2 94.12%
org/minidns/dnssec/algorithms/JavaSecSignatureVerifier.java 1 93.75%

Coverage Stats

Coverage Status
Relevant Lines: 4725
Covered Lines: 3002
Line Coverage: 63.53%
Relevant Branches: 1784
Covered Branches: 904
Branch Coverage: 50.67%
Branches in Coverage %: Yes
Coverage Strength: 2.85 hits per line

💛 - Coveralls

@Flowdalic
Flowdalic deleted the dnssec-fix-ecdsa-der-encoding branch October 19, 2026 07:51
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.

ECDSA signature verification fails with "Signature is invalid" due to non-minimal DER encoding of r/s

2 participants