Skip to content

Conversation

@facutuesca
Copy link
Contributor

This PR adds support for IMPLICIT and EXPLICIT fields to the ASN.1 API. The field types must be annotated with asn1.Implicit(tag=X) or asn1.Explicit(tag=Y):

@asn1.sequence
class Example
  a: Annotated[typing.Union[int, None], asn1.Implicit(0)]
  b: Annotated[typing.Union[int, None], asn1.Implicit(1)]

Part of #12283

Signed-off-by: Facundo Tuesca <facundo.tuesca@trailofbits.com>
@facutuesca facutuesca force-pushed the ft/asn1-implicit-explicit branch from 54a0c20 to 0a47aaa Compare October 24, 2025 21:28
Signed-off-by: Facundo Tuesca <facundo.tuesca@trailofbits.com>
@facutuesca facutuesca force-pushed the ft/asn1-implicit-explicit branch from 30fc662 to 4b76630 Compare October 24, 2025 23:12
@facutuesca facutuesca marked this pull request as ready for review October 24, 2025 23:26
Comment on lines 63 to 70
encoding = None
for raw_annotation in metadata:
if isinstance(raw_annotation, Default):
default = raw_annotation.value
elif isinstance(raw_annotation, Explicit):
encoding = declarative_asn1.Encoding.Explicit(raw_annotation.tag)
elif isinstance(raw_annotation, Implicit):
encoding = declarative_asn1.Encoding.Implicit(raw_annotation.tag)
Copy link
Member

Choose a reason for hiding this comment

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

You need to prohibit duplicates (I guess this is true of defaults as well!)

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!

value: U


@dataclasses.dataclass(frozen=True)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need these, or could we expose Encoding.Explicit and Encoding.Implicit as these types?

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!

Comment on lines 158 to 159
// Since for optional types the annotations are enforced to be associated with the Option
// (instead of the inner type), when decoding the inner type we add the annotations of the Option
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I modified the comment to try to make it clearer:

// For optional types, annotations will always be associated to the `Optional` type
// i.e: `Annotated[Optional[T], annotation]`, as opposed to the inner `T` type.
// Therefore, when decoding the inner type `T` we must pass the annotation of the `Optional`

@reaperhulk
Copy link
Member

@facutuesca Just a friendly ping since we're probably only a few weeks away from 47.0.0. If we need to slip to 48 that's fine of course.

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 merged commit 6acfd70 into pyca:main Nov 19, 2025
68 checks passed
@facutuesca facutuesca deleted the ft/asn1-implicit-explicit branch November 19, 2025 13:23
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.

3 participants