Skip to content

asn1: prepare fields for pluggable codecs#5049

Open
polybassa wants to merge 8 commits into
secdev:masterfrom
polybassa:asn1-generic-codec-hooks
Open

asn1: prepare fields for pluggable codecs#5049
polybassa wants to merge 8 commits into
secdev:masterfrom
polybassa:asn1-generic-codec-hooks

Conversation

@polybassa

Copy link
Copy Markdown
Contributor

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)

@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.30%. Comparing base (326cf5e) to head (8b93e5c).

Files with missing lines Patch % Lines
scapy/asn1fields.py 91.04% 6 Missing ⚠️
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     
Files with missing lines Coverage Δ
scapy/asn1/ber.py 96.13% <100.00%> (+13.41%) ⬆️
scapy/asn1fields.py 86.60% <91.04%> (+2.96%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_size for 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.

Comment thread scapy/asn1fields.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread scapy/asn1fields.py Outdated
Comment thread scapy/asn1fields.py Outdated
Comment thread scapy/asn1fields.py
Comment thread scapy/asn1fields.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread scapy/asn1/ber.py Outdated
Comment thread scapy/asn1fields.py Outdated
Comment thread scapy/asn1fields.py Outdated
Comment thread scapy/asn1fields.py
Comment thread scapy/asn1fields.py Outdated
Comment thread scapy/asn1fields.py Outdated
@guedou

guedou commented Jul 22, 2026

Copy link
Copy Markdown
Member

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?

Nils Weiss added 8 commits July 23, 2026 07:45
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)
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)
@polybassa
polybassa force-pushed the asn1-generic-codec-hooks branch from ad020a1 to 8b93e5c Compare July 23, 2026 10:52
@polybassa

Copy link
Copy Markdown
Contributor Author

@guedou Thanks for your feedback. I reduced the implementation to a minimum

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants