scapy.asn1fields module

Classes that implement ASN.1 data structures.

class scapy.asn1fields.ASN1F_BIT_STRING(name: str, default: ASN1_BIT_STRING | AnyStr | None, default_readable: bool = True, context: Any | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None)[source]

Bases: ASN1F_field[str, ASN1_BIT_STRING]

ASN1_tag = <ASN1Tag BIT_STRING[3]>
randval() RandString[source]
class scapy.asn1fields.ASN1F_BIT_STRING_ENCAPS(name: str, default: ASN1_Packet | None, cls: Type[ASN1_Packet], context: Any | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None)[source]

Bases: ASN1F_BIT_STRING

We may emulate simple string encapsulation with explicit_tag=0x04, but we need a specific class for bit strings because of unused bits, etc.

ASN1_tag = <ASN1Tag BIT_STRING[3]>
i2m(pkt: ASN1_Packet, x: ASN1_BIT_STRING | None) bytes[source]
m2i(pkt: ASN1_Packet, s: bytes) Tuple[ASN1_Packet | None, bytes][source]
class scapy.asn1fields.ASN1F_BMP_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag BMP_STRING[30]>
class scapy.asn1fields.ASN1F_BOOLEAN(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_field[bool, ASN1_BOOLEAN]

ASN1_tag = <ASN1Tag BOOLEAN[1]>
randval() RandChoice[source]
class scapy.asn1fields.ASN1F_CHOICE(name: str, default: Any, *args: _CHOICE_T, **kwargs: Any)[source]

Bases: ASN1F_field[ASN1_Packet | Type[ASN1F_field[Any, Any]] | ASN1F_PACKET, ASN1_Object[Any]]

Multiple types are allowed: ASN1_Packet, ASN1F_field and ASN1F_PACKET(), See layers/x509.py for examples. Other ASN1F_field instances than ASN1F_PACKET instances must not be used.

ASN1_tag = <ASN1Tag ANY[0]>
holds_packets = 1
i2m(pkt: ASN1_Packet, x: Any) bytes[source]
m2i(pkt: ASN1_Packet, s: bytes) Tuple[ASN1_Object[Any], bytes][source]

First we have to retrieve the appropriate choice. Then we extract the field/packet, according to this choice.

randval() RandChoice[source]
class scapy.asn1fields.ASN1F_ENUMERATED(name: str, default: ASN1_INTEGER, enum: Dict[int, str], context: Any | None = None, implicit_tag: Any | None = None, explicit_tag: Any | None = None)[source]

Bases: ASN1F_enum_INTEGER

ASN1_tag = <ASN1Tag ENUMERATED[10]>
class scapy.asn1fields.ASN1F_FLAGS(name: str, default: str | None, mapping: List[str], context: Any | None = None, implicit_tag: int | None = None, explicit_tag: Any | None = None)[source]

Bases: ASN1F_BIT_STRING

any2i(pkt: ASN1_Packet, x: Any) str[source]
get_flags(pkt: ASN1_Packet) List[str][source]
i2repr(pkt: ASN1_Packet, x: Any) str[source]
class scapy.asn1fields.ASN1F_GENERALIZED_TIME(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag GENERALIZED_TIME[24]>
randval() GeneralizedTime[source]
class scapy.asn1fields.ASN1F_GENERAL_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag GENERAL_STRING[27]>
class scapy.asn1fields.ASN1F_IA5_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag IA5_STRING[22]>
class scapy.asn1fields.ASN1F_INTEGER(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_field[int, ASN1_INTEGER]

ASN1_tag = <ASN1Tag INTEGER[2]>
randval() RandNum[source]
class scapy.asn1fields.ASN1F_IPADDRESS(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag IPADDRESS[64]>
class scapy.asn1fields.ASN1F_ISO646_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag ISO646_STRING[26]>
class scapy.asn1fields.ASN1F_NULL(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_INTEGER

ASN1_tag = <ASN1Tag NULL[5]>
class scapy.asn1fields.ASN1F_NUMERIC_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag NUMERIC_STRING[18]>
class scapy.asn1fields.ASN1F_OID(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_field[str, ASN1_OID]

ASN1_tag = <ASN1Tag OID[6]>
randval() RandOID[source]
class scapy.asn1fields.ASN1F_PACKET(name: str, default: Optional[ASN1_Packet], cls: Type[ASN1_Packet], context: Optional[Any] = None, implicit_tag: Optional[int] = None, explicit_tag: Optional[int] = None, next_cls_cb: Optional[Callable[[ASN1_Packet], Type[ASN1_Packet]]]  # noqa: E501 = None)[source]

Bases: ASN1F_field[ASN1_Packet, ASN1_Packet | None]

any2i(pkt: ASN1_Packet, x: Union[bytes, ASN1_Packet, None, ASN1_Object[Optional[ASN1_Packet]]]  # noqa: E501) ASN1_Packet[source]
holds_packets = 1
i2m(pkt: ASN1_Packet, x: Union[bytes, ASN1_Packet, None, ASN1_Object[Optional[ASN1_Packet]]]  # noqa: E501) bytes[source]
m2i(pkt: ASN1_Packet, s: bytes) Tuple[Any, bytes][source]
randval() ASN1_Packet[source]
class scapy.asn1fields.ASN1F_PRINTABLE_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag PRINTABLE_STRING[19]>
class scapy.asn1fields.ASN1F_SEQUENCE(*seq: Any, **kwargs: Any)[source]

Bases: ASN1F_field[List[Any], List[Any]]

ASN1_tag = <ASN1Tag SEQUENCE[48]>
build(pkt: ASN1_Packet) bytes[source]
dissect(pkt: Any, s: bytes) bytes[source]
get_fields_list() List[ASN1F_field[Any, Any]][source]
holds_packets = 1
is_empty(pkt: ASN1_Packet) bool[source]
m2i(pkt: Any, s: bytes) Tuple[Any, bytes][source]

ASN1F_SEQUENCE behaves transparently, with nested ASN1_objects being dissected one by one. Because we use obj.dissect (see loop below) instead of obj.m2i (as we trust dissect to do the appropriate set_vals) we do not directly retrieve the list of nested objects. Thus m2i returns an empty list (along with the proper remainder). It is discarded by dissect() and should not be missed elsewhere.

class scapy.asn1fields.ASN1F_SEQUENCE_OF(name: str, default: Any, cls: _SEQ_T, context: Any | None = None, implicit_tag: Any | None = None, explicit_tag: Any | None = None)[source]

Bases: ASN1F_field[List[ASN1_Packet | Type[ASN1F_field[Any, Any]] | ASN1F_PACKET | ASN1F_field[Any, Any]], List[ASN1_Object[Any]]]

Two types are allowed as cls: ASN1_Packet, ASN1F_field

ASN1_tag = <ASN1Tag SEQUENCE[48]>
build(pkt: ASN1_Packet) bytes[source]
i2repr(pkt: ASN1_Packet, x: _I) str[source]
is_empty(pkt: ASN1_Packet) bool[source]
islist = 1
m2i(pkt: ASN1_Packet, s: bytes) Tuple[List[Any], bytes][source]
randval() Any[source]
class scapy.asn1fields.ASN1F_SET(*seq: Any, **kwargs: Any)[source]

Bases: ASN1F_SEQUENCE

ASN1_tag = <ASN1Tag SET[49]>
class scapy.asn1fields.ASN1F_SET_OF(name: str, default: Any, cls: _SEQ_T, context: Any | None = None, implicit_tag: Any | None = None, explicit_tag: Any | None = None)[source]

Bases: ASN1F_SEQUENCE_OF

ASN1_tag = <ASN1Tag SET[49]>
class scapy.asn1fields.ASN1F_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_field[str, ASN1_STRING]

ASN1_tag = <ASN1Tag STRING[4]>
randval() RandString[source]
class scapy.asn1fields.ASN1F_STRING_PacketField(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1F_STRING that holds packets.

any2i(pkt: ASN1_Packet, x: Any) Any[source]
holds_packets = 1
i2m(pkt: ASN1_Packet, val: Any) bytes[source]
class scapy.asn1fields.ASN1F_T61_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag T61_STRING[20]>
class scapy.asn1fields.ASN1F_TIME_TICKS(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_INTEGER

ASN1_tag = <ASN1Tag TIME_TICKS[67]>
class scapy.asn1fields.ASN1F_UNIVERSAL_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag UNIVERSAL_STRING[28]>
class scapy.asn1fields.ASN1F_UTC_TIME(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag UTC_TIME[23]>
randval() GeneralizedTime[source]
class scapy.asn1fields.ASN1F_UTF8_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag UTF8_STRING[12]>
class scapy.asn1fields.ASN1F_VIDEOTEX_STRING(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_STRING

ASN1_tag = <ASN1Tag VIDEOTEX_STRING[21]>
exception scapy.asn1fields.ASN1F_badsequence[source]

Bases: Exception

class scapy.asn1fields.ASN1F_element[source]

Bases: object

class scapy.asn1fields.ASN1F_enum_INTEGER(name: str, default: ASN1_INTEGER, enum: Dict[int, str], context: Any | None = None, implicit_tag: Any | None = None, explicit_tag: Any | None = None)[source]

Bases: ASN1F_INTEGER

i2m(pkt: ASN1_Packet, s: bytes | str | int | ASN1_INTEGER) bytes[source]
i2repr(pkt: ASN1_Packet, x: str | int) str[source]
class scapy.asn1fields.ASN1F_field(name: str, default: _A | None, context: Type[ASN1_Class] | None = None, implicit_tag: int | None = None, explicit_tag: int | None = None, flexible_tag: bool | None = False, size_len: int | None = None)[source]

Bases: ASN1F_element, Generic[_I, _A]

ASN1_tag = <ASN1Tag ANY[0]>
any2i(pkt: ASN1_Packet, x: Any) _I[source]
build(pkt: ASN1_Packet) bytes[source]
context[source]

alias of ASN1_Class_UNIVERSAL

copy() ASN1F_field[_I, _A][source]
dissect(pkt: ASN1_Packet, s: bytes) bytes[source]
do_copy(x: Any) Any[source]
extract_packet(cls: Type[ASN1_Packet], s: bytes, _underlayer: ASN1_Packet | None = None) Tuple[ASN1_Packet, bytes][source]
get_fields_list() List[ASN1F_field[Any, Any]][source]
holds_packets = 0
i2h(pkt: ASN1_Packet, x: _I) Any[source]
i2m(pkt: ASN1_Packet, x: bytes | _I | _A) bytes[source]
i2repr(pkt: ASN1_Packet, x: _I) str[source]
is_empty(pkt: ASN1_Packet) bool[source]
islist = 0
m2i(pkt: ASN1_Packet, s: bytes) Tuple[_A, bytes][source]

The good thing about safedec is that it may still decode ASN1 even if there is a mismatch between the expected tag (self.ASN1_tag) and the actual tag; the decoded ASN1 object will simply be put into an ASN1_BADTAG object. However, safedec prevents the raising of exceptions needed for ASN1F_optional processing. Thus we use ‘flexible_tag’, which should be False with ASN1F_optional.

Regarding other fields, we might need to know whether encoding went as expected or not. Noticeably, input methods from cert.py expect certain exceptions to be raised. Hence default flexible_tag is False.

randval() RandField[_I][source]
register_owner(cls: Type[ASN1_Packet]) None[source]
set_val(pkt: ASN1_Packet, val: Any) None[source]
class scapy.asn1fields.ASN1F_optional(field: ASN1F_field[Any, Any])[source]

Bases: ASN1F_element

any2i(pkt: ASN1_Packet, x: Any) Any[source]
build(pkt: ASN1_Packet) bytes[source]
dissect(pkt: ASN1_Packet, s: bytes) bytes[source]
i2repr(pkt: ASN1_Packet, x: Any) str[source]
m2i(pkt: ASN1_Packet, s: bytes) Tuple[Any, bytes][source]