Reject empty extension sequences and generalize input-stream limit detection#143
Merged
Merged
Conversation
…tection Two independent ASN.1 hardening changes. 1. Minimum sequence-size restriction on X.509 extensions TCrlDistPoint and TExtendedKeyUsage now reject an empty sequence (< 1 element) in both their decode (IAsn1Sequence) and array constructors, via TAsn1Utilities.CheckSequenceSize(seq, 1, Int32.MaxValue). A malformed extension with no elements now raises EArgumentCryptoLibException instead of constructing a degenerate object. Both classes already stored the sequence and mapped on demand, so no storage change was needed. Adds TestCrlDistPointRejectsEmptySequence and TestExtendedKeyUsageRejectsEmptySequence to X509ExtensionsTests. 2. TStreamUtilities.TryGetAvailable and use in FindLimit Add TryGetAvailable(stream, out available): best-effort remaining bytes for a seekable stream (Max(0, Size - Position)), guarded so a stream that throws is treated as unavailable. TAsn1InputStream.FindLimit now uses it, replacing the TFixedBufferStream-specific branch (GetFixedBufferStreamLimit removed): any seekable stream gets an available-based limit, not just the fixed buffer. TFixedBufferStream is seekable, so its limit is unchanged; byte-array input still yields a tight bound. Corrects the InputStreamTests dummy: it modelled "unknown/non-seekable" but its Seek was a no-op returning 0, making it look like a seekable empty stream. It now inherits TBaseInputStream (CanSeek = False) with a raising Seek, so it honestly exercises FindLimit's fallback; TestNegativeMaxLimitClampsFindLimit uses it to actually reach the clamp path.
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.
Two independent ASN.1 hardening changes.
Minimum sequence-size restriction on X.509 extensions TCrlDistPoint and TExtendedKeyUsage now reject an empty sequence (< 1 element) in both their decode (IAsn1Sequence) and array constructors, via TAsn1Utilities.CheckSequenceSize(seq, 1, Int32.MaxValue). A malformed extension with no elements now raises EArgumentCryptoLibException instead of constructing a degenerate object. Both classes already stored the sequence and mapped on demand, so no storage change was needed. Adds TestCrlDistPointRejectsEmptySequence and TestExtendedKeyUsageRejectsEmptySequence to X509ExtensionsTests.
TStreamUtilities.TryGetAvailable and use in FindLimit Add TryGetAvailable(stream, out available): best-effort remaining bytes for a seekable stream (Max(0, Size - Position)), guarded so a stream that throws is treated as unavailable. TAsn1InputStream.FindLimit now uses it, replacing the TFixedBufferStream-specific branch (GetFixedBufferStreamLimit removed): any seekable stream gets an available-based limit, not just the fixed buffer. TFixedBufferStream is seekable, so its limit is unchanged; byte-array input still yields a tight bound.
Corrects the InputStreamTests dummy: it modelled "unknown/non-seekable" but its Seek was a no-op returning 0, making it look like a seekable empty stream. It now inherits TBaseInputStream (CanSeek = False) with a raising Seek, so it honestly exercises FindLimit's fallback; TestNegativeMaxLimitClampsFindLimit uses it to actually reach the clamp path.