Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .config/codespell_ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ wan
wanna
webp
widgits
uper
UPER
uPER
acn
ACN
30 changes: 17 additions & 13 deletions scapy/asn1/asn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,17 @@ def register_stem(cls, stem):
# type: (Type[BERcodec_Object[Any]]) -> None
cls._stem = stem

def register_tagging(cls, enc, dec):
# type: (Any, Any) -> None
# Codec-level implicit/explicit tagging (BER/OER) or identity (UPER/PER).
cls._tagging_enc = enc
cls._tagging_dec = dec

def tagging_enc(cls, s, **kwargs):
# type: (bytes, **Any) -> bytes
return cls._tagging_enc(s, **kwargs) # type: ignore

def tagging_dec(cls, s, **kwargs):
# type: (bytes, **Any) -> Tuple[Optional[int], bytes]
return cls._tagging_dec(s, **kwargs) # type: ignore
def register_hooks(cls, **hooks):
# type: (**Any) -> None
# Field operations a codec does its own way: the tagging of a field
# (BER) and the compound fields (SEQUENCE/CHOICE/… in OER and PER).
ASN1_Codecs.hooks.setdefault(cls, {}).update(hooks)

def hook(cls, name):
# type: (str) -> Any
# Hooks are optional and may be partial: missing entries mean that
# asn1fields keeps its default implementation.
return ASN1_Codecs.hooks.get(cls, {}).get(name)

def dec(cls, s, context=None, _depth=0):
# type: (bytes, Optional[Type[ASN1_Class]], int) -> ASN1_Object[Any]
Expand Down Expand Up @@ -168,6 +166,9 @@ class ASN1_Codecs(metaclass=ASN1_Codecs_metaclass):
SER = cast(ASN1Codec, 8)
XER = cast(ASN1Codec, 9)

# The field hooks of every codec, by codec then by field operation.
hooks = {} # type: Dict[ASN1Codec, Dict[str, Any]]


class ASN1Tag(EnumElement):
def __init__(self,
Expand Down Expand Up @@ -315,6 +316,9 @@ def __new__(cls,

class ASN1_Object(Generic[_K], metaclass=ASN1_Object_metaclass):
tag = ASN1_Class_UNIVERSAL.ANY
# Alternative a value was dissected as, when it comes from a CHOICE that
# holds several of its type. See ASN1F_CHOICE.record_alternative.
asn1_choice_index = None # type: Optional[int]

def __init__(self, val):
# type: (_K) -> None
Expand Down
6 changes: 5 additions & 1 deletion scapy/asn1/ber.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,11 @@ def enc(cls, s, size_len=0, **_kwargs):


ASN1_Codecs.BER.register_stem(BERcodec_Object)
ASN1_Codecs.BER.register_tagging(BER_tagging_enc, BER_tagging_dec)
# BER is the one codec that puts the tag of a field on the wire.
ASN1_Codecs.BER.register_hooks(
tagging_enc=BER_tagging_enc,
tagging_dec=BER_tagging_dec,
)


##########################
Expand Down
Loading
Loading