Repository navigation
[dnssec] Fix ASN.1 DER encoding of DSA and ECDSA signatures - #152
Merged
Flowdalic merged 1 commit intoOct 18, 2026
Merged
Conversation
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
force-pushed
the
dnssec-fix-ecdsa-der-encoding
branch
from
October 18, 2026 10:56
b390813 to
0f0635f
Compare
Coverage Report for CI Build 39121214226Coverage increased (+0.6%) to 60.009%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions5 previously-covered lines in 3 files lost coverage.
Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.