-
Notifications
You must be signed in to change notification settings - Fork 4
/
ASN1.py
executable file
·109 lines (79 loc) · 3.71 KB
/
ASN1.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
try:
from struct import *
from Peach.transformer import Transformer
from pyasn1.type import univ
from pyasn1.codec import der, ber, cer
class DerEncodeOctetString(Transformer):
"""DER encode an octect string ASN.1 style."""
def realEncode(self, data):
return der.encoder.encode(univ.OctetString(data))
class DerEncodeBitString(Transformer):
"""DER encode a bit string ASN.1 style."""
def realEncode(self, data):
return der.encoder.encode(univ.BitString(data))
class DerEncodeInteger(Transformer):
"""DER encode an integer ASN.1 style."""
def realEncode(self, data):
return der.encoder.encode(univ.Integer(int(data)))
class DerEncodeBoolean(Transformer):
"""DER encode a boolean ASN.1 style. Expects 0 or 1."""
def realEncode(self, data):
data = int(data)
if data != 0 and data != 1:
raise Exception("DerEncodeBoolean transformer expects 0 or 1")
return der.encoder.encode(univ.Boolean(data))
class DerEncodeObjectIdentifier(Transformer):
"""DER encode an object identifierASN.1 style."""
def realEncode(self, data):
return der.encoder.encode(univ.ObjectIdentifier(data))
class BerEncodeOctetString(Transformer):
"""BER encode a string ASN.1 style."""
def realEncode(self, data):
return ber.encoder.encode(univ.OctetString(data))
class BerEncodeBitString(Transformer):
"""BER encode a bit string ASN.1 style."""
def realEncode(self, data):
return ber.encoder.encode(univ.BitString(data))
class BerEncodeInteger(Transformer):
"""BER encode an integer ASN.1 style."""
def realEncode(self, data):
return ber.encoder.encode(univ.Integer(int(data)))
class BerEncodeBoolean(Transformer):
"""BER encode a boolean ASN.1 style. Expects 0 or 1."""
def realEncode(self, data):
data = int(data)
if data != 0 and data != 1:
raise Exception("BerEncodeBoolean transformer expects 0 or 1")
return ber.encoder.encode(univ.Boolean(data))
class BerEncodeObjectIdentifier(Transformer):
"""BER encode an object identifierASN.1 style."""
def realEncode(self, data):
return ber.encoder.encode(univ.ObjectIdentifier(data))
class CerEncodeOctetString(Transformer):
"""CER encode a string ASN.1 style."""
def realEncode(self, data):
return cer.encoder.encode(univ.OctetString(data))
class CerEncodeBitString(Transformer):
"""CER encode a bit string ASN.1 style."""
def realEncode(self, data):
return cer.encoder.encode(univ.BitString(data))
class CerEncodeInteger(Transformer):
"""CER encode an integer ASN.1 style."""
def realEncode(self, data):
return cer.encoder.encode(univ.Integer(int(data)))
class CerEncodeBoolean(Transformer):
"""CER encode a boolean ASN.1 style. Expects 0 or 1."""
def realEncode(self, data):
data = int(data)
if data != 0 and data != 1:
raise Exception("CerEncodeBoolean transformer expects 0 or 1")
return cer.encoder.encode(univ.Boolean(data))
class CerEncodeObjectIdentifier(Transformer):
"""CER encode an object identifierASN.1 style."""
def realEncode(self, data):
return cer.encoder.encode(univ.ObjectIdentifier(data))
except Exception as e:
pass