OLD | NEW |
| (Empty) |
1 """Class for parsing ASN.1""" | |
2 from compat import * | |
3 from codec import * | |
4 | |
5 #Takes a byte array which has a DER TLV field at its head | |
6 class ASN1Parser: | |
7 def __init__(self, bytes): | |
8 p = Parser(bytes) | |
9 p.get(1) #skip Type | |
10 | |
11 #Get Length | |
12 self.length = self._getASN1Length(p) | |
13 | |
14 #Get Value | |
15 self.value = p.getFixBytes(self.length) | |
16 | |
17 #Assuming this is a sequence... | |
18 def getChild(self, which): | |
19 return ASN1Parser(self.getChildBytes(which)) | |
20 | |
21 def getChildBytes(self, which): | |
22 p = Parser(self.value) | |
23 for x in range(which+1): | |
24 markIndex = p.index | |
25 p.get(1) #skip Type | |
26 length = self._getASN1Length(p) | |
27 p.getFixBytes(length) | |
28 return p.bytes[markIndex : p.index] | |
29 | |
30 #Decode the ASN.1 DER length field | |
31 def _getASN1Length(self, p): | |
32 firstLength = p.get(1) | |
33 if firstLength<=127: | |
34 return firstLength | |
35 else: | |
36 lengthLength = firstLength & 0x7F | |
37 return p.get(lengthLength) | |
OLD | NEW |