Support encode_der for asn1.TLV values#15113
Open
alex wants to merge 2 commits into
Open
Conversation
Previously, calling `asn1.encode_der` on a value decoded with the `asn1.TLV` specifier raised `NotImplementedError`. This made it impossible to parse an arbitrary ASN.1 element as a `TLV` and then serialize just that element back to DER. A `TLV`'s `full_data` is a complete, already-validated DER element, so encoding re-parses it and writes it back out verbatim, honoring an EXPLICIT annotation when present (IMPLICIT annotations on TLV fields remain rejected at definition time). Closes #15109 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016iPBDfr6FxfNSm6nzakgwh
reaperhulk
reviewed
Jul 1, 2026
| assert isinstance(tlv, asn1.TLV) | ||
| assert asn1.encode_der(tlv) == original | ||
|
|
||
| def test_ok_encode_tlv_issue_example(self) -> None: |
Member
There was a problem hiding this comment.
This test name (and associated comment) isn't going to age well
Renames `test_ok_encode_tlv_issue_example` to `test_ok_encode_tlv_roundtrip_from_value` and rewords its comment to describe the behavior under test rather than referencing the original feature request, which won't age well. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016iPBDfr6FxfNSm6nzakgwh
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.
Closes #15109.
Previously, calling
asn1.encode_deron a value decoded with theasn1.TLVspecifier raisedNotImplementedError, making it impossible to parse an arbitrary ASN.1 element as aTLVand then serialize just that element back to DER:Change
Implements the
Type::Tlv()arm of the declarative ASN.1 encoder. ATLV'sfull_datais a complete, already-validated DER element, so encoding re-parses it into anasn1::Tlvand writes it back out verbatim:ANYfield) → written as-is.The re-parse is necessary because the
TLVPython object stores the element's raw DER bytes rather than a liveasn1::Tlv<'_>(which borrows its input and can't be held in a pyclass); the writer needs anAsn1Writable, andasn1::Tlvhas no public constructor, so re-parsingfull_datais how we recover one. It only reads the tag/length header and can't fail in practice, since the bytes came from a successful decode.Tests
test_ok_encode_tlvround-trips several elements (INTEGER, BIT STRING, SEQUENCE, NULL) throughdecode_der(TLV, ...)→encode_der.test_ok_encode_tlv_issue_examplecovers the exact example from the issue.test_sequence_with_tlv_with_explicit_annotationto assert the EXPLICIT-tagged TLV fields re-encode to the original DER.test_encode_implicit_tlv) covering the defensive implicit-encoding branch, for parity with the decode side.🤖 Generated with Claude Code
Generated by Claude Code