Skip to content

Conversation

@facutuesca
Copy link
Contributor

@facutuesca facutuesca commented Dec 10, 2025

Similar to #13899, this PR extends the support for SIZE to BIT STRING

The logic to check the size constraint is moved to its own function (check_size_constraint()), so that it can be reused by all types that support SIZE annotations.

I've also moved the existing tests for SIZE to their own class.

Part of #12283

Signed-off-by: Facundo Tuesca <facundo.tuesca@trailofbits.com>
Copy link
Member

@alex alex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reaperhulk this uses bits as the length unit for a size annotation, WDYT?

On the on one hand, bits are stupid.

@facutuesca is that waht RFCs do when SIZE OF annotations are on BIT STRING?

let bitstring: asn1::BitString<'_> =
asn1::BitString::new(val.get().data.as_bytes(py), val.get().padding_bits)
.ok_or(asn1::WriteError::AllocationError)?;
let n_bits = bitstring.as_bytes().len() * 8 - (bitstring.padding_bits() as usize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use into() instead of as so its clear there's no truncation risk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed (with usize::from())

Comment on lines 242 to 244
"field invalid has a SIZE annotation, but SIZE "
"annotations are only supported for fields of types: "
"[SEQUENCE OF, BIT STRING]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably simplify this to just the first part of the error, so it doesn't need to be updated for every type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@facutuesca
Copy link
Contributor Author

@reaperhulk this uses bits as the length unit for a size annotation, WDYT?

On the on one hand, bits are stupid.

@facutuesca is that waht RFCs do when SIZE OF annotations are on BIT STRING?

They should. From ITU-T X.680:
image

Signed-off-by: Facundo Tuesca <facundo.tuesca@trailofbits.com>
Signed-off-by: Facundo Tuesca <facundo.tuesca@trailofbits.com>
Signed-off-by: Facundo Tuesca <facundo.tuesca@trailofbits.com>
@alex alex enabled auto-merge (squash) December 18, 2025 23:27
@alex alex merged commit c140f5c into pyca:main Dec 18, 2025
68 checks passed
@facutuesca facutuesca deleted the ft/asn1-size-bitstring branch December 18, 2025 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants