summaryrefslogtreecommitdiff
path: root/python/examples/kaitai/asn1_der.py
blob: cd92a75e8c5ac3be0026549229623cbee8873599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

from pkg_resources import parse_version
from .kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
from enum import Enum
import collections


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

class Asn1Der(KaitaiStruct):
    """ASN.1 (Abstract Syntax Notation One) DER (Distinguished Encoding
    Rules) is a standard-backed serialization scheme used in many
    different use-cases. Particularly popular usage scenarios are X.509
    certificates and some telecommunication / networking protocols.
    
    DER is self-describing encoding scheme which allows representation
    of simple, atomic data elements, such as strings and numbers, and
    complex objects, such as sequences of other elements.
    
    DER is a subset of BER (Basic Encoding Rules), with an emphasis on
    being non-ambiguous: there's always exactly one canonical way to
    encode a data structure defined in terms of ASN.1 using DER.
    
    This spec allows full parsing of format syntax, but to understand
    the semantics, one would typically require a dictionary of Object
    Identifiers (OIDs), to match OID bodies against some human-readable
    list of constants. OIDs are covered by many different standards,
    so typically it's simpler to use a pre-compiled list of them, such
    as:
    
    * https://www.cs.auckland.ac.nz/~pgut001/dumpasn1.cfg
    * http://oid-info.com/
    * https://www.alvestrand.no/objectid/top.html
    
    .. seealso::
       Source - https://www.itu.int/rec/T-REC-X.690-201508-I/en
    """

    class TypeTag(Enum):
        end_of_content = 0
        boolean = 1
        integer = 2
        bit_string = 3
        octet_string = 4
        null_value = 5
        object_id = 6
        object_descriptor = 7
        external = 8
        real = 9
        enumerated = 10
        embedded_pdv = 11
        utf8string = 12
        relative_oid = 13
        sequence_10 = 16
        printable_string = 19
        ia5string = 22
        sequence_30 = 48
        set = 49
    SEQ_FIELDS = ["type_tag", "len", "body"]
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._debug = collections.defaultdict(dict)

    def _read(self):
        self._debug['type_tag']['start'] = self._io.pos()
        self.type_tag = KaitaiStream.resolve_enum(self._root.TypeTag, self._io.read_u1())
        self._debug['type_tag']['end'] = self._io.pos()
        self._debug['len']['start'] = self._io.pos()
        self.len = self._root.LenEncoded(self._io, self, self._root)
        self.len._read()
        self._debug['len']['end'] = self._io.pos()
        self._debug['body']['start'] = self._io.pos()
        _on = self.type_tag
        if _on == self._root.TypeTag.printable_string:
            self._raw_body = self._io.read_bytes(self.len.result)
            io = KaitaiStream(BytesIO(self._raw_body))
            self.body = self._root.BodyPrintableString(io, self, self._root)
            self.body._read()
        elif _on == self._root.TypeTag.sequence_10:
            self._raw_body = self._io.read_bytes(self.len.result)
            io = KaitaiStream(BytesIO(self._raw_body))
            self.body = self._root.BodySequence(io, self, self._root)
            self.body._read()
        elif _on == self._root.TypeTag.set:
            self._raw_body = self._io.read_bytes(self.len.result)
            io = KaitaiStream(BytesIO(self._raw_body))
            self.body = self._root.BodySequence(io, self, self._root)
            self.body._read()
        elif _on == self._root.TypeTag.sequence_30:
            self._raw_body = self._io.read_bytes(self.len.result)
            io = KaitaiStream(BytesIO(self._raw_body))
            self.body = self._root.BodySequence(io, self, self._root)
            self.body._read()
        elif _on == self._root.TypeTag.utf8string:
            self._raw_body = self._io.read_bytes(self.len.result)
            io = KaitaiStream(BytesIO(self._raw_body))
            self.body = self._root.BodyUtf8string(io, self, self._root)
            self.body._read()
        elif _on == self._root.TypeTag.object_id:
            self._raw_body = self._io.read_bytes(self.len.result)
            io = KaitaiStream(BytesIO(self._raw_body))
            self.body = self._root.BodyObjectId(io, self, self._root)
            self.body._read()
        else:
            self.body = self._io.read_bytes(self.len.result)
        self._debug['body']['end'] = self._io.pos()

    class BodySequence(KaitaiStruct):
        SEQ_FIELDS = ["entries"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['entries']['start'] = self._io.pos()
            self.entries = []
            i = 0
            while not self._io.is_eof():
                if not 'arr' in self._debug['entries']:
                    self._debug['entries']['arr'] = []
                self._debug['entries']['arr'].append({'start': self._io.pos()})
                _t_entries = Asn1Der(self._io)
                _t_entries._read()
                self.entries.append(_t_entries)
                self._debug['entries']['arr'][len(self.entries) - 1]['end'] = self._io.pos()
                i += 1

            self._debug['entries']['end'] = self._io.pos()


    class BodyUtf8string(KaitaiStruct):
        SEQ_FIELDS = ["str"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['str']['start'] = self._io.pos()
            self.str = (self._io.read_bytes_full()).decode(u"UTF-8")
            self._debug['str']['end'] = self._io.pos()


    class BodyObjectId(KaitaiStruct):
        """
        .. seealso::
           Source - https://docs.microsoft.com/en-us/windows/desktop/SecCertEnroll/about-object-identifier
        """
        SEQ_FIELDS = ["first_and_second", "rest"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['first_and_second']['start'] = self._io.pos()
            self.first_and_second = self._io.read_u1()
            self._debug['first_and_second']['end'] = self._io.pos()
            self._debug['rest']['start'] = self._io.pos()
            self.rest = self._io.read_bytes_full()
            self._debug['rest']['end'] = self._io.pos()

        @property
        def first(self):
            if hasattr(self, '_m_first'):
                return self._m_first if hasattr(self, '_m_first') else None

            self._m_first = self.first_and_second // 40
            return self._m_first if hasattr(self, '_m_first') else None

        @property
        def second(self):
            if hasattr(self, '_m_second'):
                return self._m_second if hasattr(self, '_m_second') else None

            self._m_second = (self.first_and_second % 40)
            return self._m_second if hasattr(self, '_m_second') else None


    class LenEncoded(KaitaiStruct):
        SEQ_FIELDS = ["b1", "int2", "int1"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['b1']['start'] = self._io.pos()
            self.b1 = self._io.read_u1()
            self._debug['b1']['end'] = self._io.pos()
            if self.b1 == 130:
                self._debug['int2']['start'] = self._io.pos()
                self.int2 = self._io.read_u2be()
                self._debug['int2']['end'] = self._io.pos()

            if self.b1 == 129:
                self._debug['int1']['start'] = self._io.pos()
                self.int1 = self._io.read_u1()
                self._debug['int1']['end'] = self._io.pos()


        @property
        def result(self):
            if hasattr(self, '_m_result'):
                return self._m_result if hasattr(self, '_m_result') else None

            self._m_result = (self.int1 if self.b1 == 129 else (self.int2 if self.b1 == 130 else self.b1))
            return self._m_result if hasattr(self, '_m_result') else None


    class BodyPrintableString(KaitaiStruct):
        SEQ_FIELDS = ["str"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['str']['start'] = self._io.pos()
            self.str = (self._io.read_bytes_full()).decode(u"ASCII")
            self._debug['str']['end'] = self._io.pos()