Skip to content

Report the correct length in Asn1Parser.parseLength() error messages - #1090

Closed
michael-o wants to merge 1 commit into
apache:mainfrom
michael-o:fix/asn1parser-length-messages
Closed

michael-o wants to merge 1 commit into
apache:mainfrom
michael-o:fix/asn1parser-length-messages

Conversation

@michael-o

Copy link
Copy Markdown
Member

Asn1Parser.parseLength()'s bounds-check guards (added in commits baee26a) all throw IllegalArgumentException via asn1Parser.lengthInvalid, but hardcode the reported length to -1 in every
case, even when the actual decoded value is known and would be useful for diagnosing the failure.

This was discovered while upgrading tomcatspnegoad from Tomcat 9.0.118 to 9.0.119, where its OtherNameAsn1Parser test suite (a thin wrapper around
Asn1Parser used to decode the SAN:otherName field from X.509 client certificates, to extract an MS-UPN for Active Directory authentication) started failing due to the changed exception messages.

This PR fixes two issues:

  1. When the declared length is valid (correctly decoded, non-negative, no overflow) but exceeds the remaining input, the real length is now reported instead of -1.
  2. The bytes > 4 octet-count cap is replaced with a BigInteger-based check on the actual decoded magnitude. Per X.690 §8.1.3.5, a BER/DER length may use up to 126 octets and is always unsigned — the only
    real constraint here is that this implementation returns an int, so values exceeding Integer.MAX_VALUE cannot be represented. The old cap rejected some encodings (e.g. a non-minimal 5-octet length for a
    small value) that are valid per spec and now parse correctly, while cases that genuinely exceed Integer.MAX_VALUE get a new, accurately-worded message (asn1Parser.lengthUnsupported) instead of being
    misreported as "invalid."

No behavior change for well-formed, in-range input.

@rmaucher

Copy link
Copy Markdown
Contributor

I merged a variation I got after review. Fixed the log message, in particular. Thanks.

@rmaucher rmaucher closed this Oct 17, 2026
@michael-o
michael-o deleted the fix/asn1parser-length-messages branch October 17, 2026 09:39
@michael-o

Copy link
Copy Markdown
Member Author

I merged a variation I got after review. Fixed the log message, in particular. Thanks.

Merci !

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.

2 participants