1
2
3
4
5
6 """Abstract Syntax Notation One (ASN.1) parsing"""
7
8 from .codec import Parser
12 """
13 Parser and storage of ASN.1 DER encoded objects.
14
15 @type length: int
16 @ivar length: length of the value of the tag
17 @type value: bytearray
18 @ivar value: literal value of the tag
19 """
20
22 """Create an object from bytes.
23
24 @type bytes: bytearray
25 @param bytes: DER encoded ANS.1 object
26 """
27 p = Parser(bytes)
28 p.get(1)
29
30
31 self.length = self._getASN1Length(p)
32
33
34 self.value = p.getFixBytes(self.length)
35
37 """
38 Return n-th child assuming that the object is a SEQUENCE.
39
40 @type which: int
41 @param which: ordinal of the child to return
42
43 @rtype: ASN1Parser
44 @return: decoded child object
45 """
46 return ASN1Parser(self.getChildBytes(which))
47
49 """
50 Return raw encoding of n-th child, assume self is a SEQUENCE
51
52 @type which: int
53 @param which: ordinal of the child to return
54
55 @rtype: bytearray
56 @return: raw child object
57 """
58 p = Parser(self.value)
59 for _ in range(which+1):
60 markIndex = p.index
61 p.get(1)
62 length = self._getASN1Length(p)
63 p.getFixBytes(length)
64 return p.bytes[markIndex : p.index]
65
66 @staticmethod
68 """Decode the ASN.1 DER length field"""
69 firstLength = p.get(1)
70 if firstLength <= 127:
71 return firstLength
72 else:
73 lengthLength = firstLength & 0x7F
74 return p.get(lengthLength)
75