asn1: prepare fields for pluggable codecs#5049
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5049 +/- ##
==========================================
+ Coverage 80.23% 80.30% +0.06%
==========================================
Files 388 388
Lines 96517 96549 +32
==========================================
+ Hits 77442 77530 +88
+ Misses 19075 19019 -56
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR refactors ASN.1 field encode/decode paths to be codec-stem driven (rather than BER-hardcoded), enabling additional ASN.1 codecs to plug in tagging behavior and receive codec kwargs consistently across field operations.
Changes:
- Added codec-stem dispatch for tagging encode/decode and threaded codec kwargs through ASN1F field encode/decode paths.
- Updated BER implementation to respect
conf.ASN1_default_long_sizefor explicit-tag length encoding and to tolerate extra kwargs in decode APIs. - Added BER-focused regression/coverage tests for packets/fields and codec helpers, wired into
asn1.uts.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
test/scapy/layers/ber_packets.py |
Adds BER ASN1_Packet / ASN1F field roundtrip tests (explicit tags, fixed sizes, optional, choice, sequence-of). |
test/scapy/layers/ber_codec.py |
Adds targeted tests for BER helper functions, codecs, and error formatting. |
test/scapy/layers/asn1.uts |
Integrates the new BER packet/codec tests into the existing ASN.1 UTS suite. |
scapy/asn1fields.py |
Implements codec-stem tagging dispatch, passes codec kwargs through field encode/decode, and refactors SEQUENCE/CHOICE/SEQUENCE_OF internals. |
scapy/asn1/ber.py |
Makes BER explicit-tag length encoding respect conf.ASN1_default_long_size and updates codec APIs to accept passthrough kwargs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I made more comments. I am a bit concerned by the bug fixes. Is it possible to limit the changes or do we need to fix other things for this pluggable implementation? |
Dispatch tagging via codec stems and pass codec kwargs through ASN1F encode/decode so additional codecs can register without hardcoding BER in every field path. AI-Assisted: yes (Cursor)
AI-Assisted: yes (Cursor)
AI-Assisted: yes (Cursor)
Apply field tagging in SEQUENCE OF builds, honor size_len for ASN1_Object values, and trim redundant optional/tagging helpers. AI-Assisted: yes (Cursor)
… values (e.g. KRB_AuthenticatorChecksum in an ASN1F_STRING) used self_build() alone and skipped the BER OCTET STRING wrap (04 …). Optional cksum then failed to decode, so the stream still had 0xa3 when cusec (0xa4) was expected. AI-Assisted: yes (Cursor)
… so conf.ASN1_default_long_size padded every BER length — including INTEGER/STRING — instead of only SEQUENCE/SET. AI-Assisted: yes (Cursor)
1. Copilot: size_len=0 vs unspecified — Real bug. BERcodec_SEQUENCE.enc now defaults to size_len=None and only applies
conf.ASN1_default_long_size when size_len is None, so size_len=0 can force short-form lengths. Regression test added.
2. guedou: drop unused helpers — Removed set_absent, SEQUENCE/CHOICE _m2i_ber, _register_choice, choice_order/choice_list, and
unused _choice_* APIs. Logic inlined back into m2i / __init__ so this PR stays focused on codec-stem hooks.
Kept on purpose
• SEQUENCE OF holds_packets split — Needed for correct BER with tagged element fields (not for other encodings). Clarified the
comment.
• Prior encoding bugfixes (ASN1_Object/Packet in _encode_item) — Required so LDAP/Kerberos still work with the new encode path.
asn1.uts, kerberos.uts, and ldap.uts all pass. Changes are uncommitted if you want a commit next.
AI-Assisted: yes (Cursor)
ad020a1 to
8b93e5c
Compare
|
@guedou Thanks for your feedback. I reduced the implementation to a minimum |
Dispatch tagging via codec stems and pass codec kwargs through ASN1F encode/decode so additional codecs can register without hardcoding BER in every field path.
AI-Assisted: yes (Cursor)