1# SPDX-License-Identifier: GPL-2.0-only
2# This file is part of Scapy
3# See https://scapy.net/ for more information
4# Copyright (C) Philippe Biondi <phil@secdev.org>
5
6"""
7ASN.1 Packet
8
9Packet holding data in Abstract Syntax Notation (ASN.1).
10"""
11
12from scapy.base_classes import Packet_metaclass
13from scapy.packet import Packet
14
15from typing import (
16 Any,
17 Dict,
18 Tuple,
19 Type,
20 cast,
21 TYPE_CHECKING,
22)
23
24if TYPE_CHECKING:
25 from scapy.asn1fields import ASN1F_field # noqa: F401
26
27
28class ASN1Packet_metaclass(Packet_metaclass):
29 def __new__(cls,
30 name, # type: str
31 bases, # type: Tuple[type, ...]
32 dct # type: Dict[str, Any]
33 ):
34 # type: (...) -> Type[ASN1_Packet]
35 if dct["ASN1_root"] is not None:
36 dct["fields_desc"] = dct["ASN1_root"].get_fields_list()
37 return cast(
38 'Type[ASN1_Packet]',
39 super(ASN1Packet_metaclass, cls).__new__(cls, name, bases, dct),
40 )
41
42
43class ASN1_Packet(Packet, metaclass=ASN1Packet_metaclass):
44 ASN1_root = cast('ASN1F_field[Any, Any]', None)
45 ASN1_codec = None
46
47 def self_build(self):
48 # type: () -> bytes
49 if self.raw_packet_cache is not None:
50 return self.raw_packet_cache
51 return self.ASN1_root.build(self)
52
53 def do_dissect(self, x):
54 # type: (bytes) -> bytes
55 return self.ASN1_root.dissect(self, x)