Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/asn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@
__version__ = "2.7.1"


class Numbers(IntEnum):
class HexEnum(IntEnum):
def __repr__(self):
return '<{cls}.{name}: 0x{value:02x}>'.format(
cls=type(self).__name__,
name=self.name,
value=self.value
)


class Numbers(HexEnum):
Boolean = 0x01
Integer = 0x02
BitString = 0x03
Expand All @@ -47,12 +56,12 @@ class Numbers(IntEnum):
UnicodeString = 0x1e


class Types(IntEnum):
class Types(HexEnum):
Constructed = 0x20
Primitive = 0x00


class Classes(IntEnum):
class Classes(HexEnum):
Universal = 0x00
Application = 0x40
Context = 0x80
Expand Down Expand Up @@ -551,6 +560,19 @@ def _read_tag(self): # type: () -> Tag
nr = (nr << 7) | (byte & 0x7f)
if not byte & 0x80:
break
try:
typ = Types(typ)
except ValueError:
pass
try:
cls = Classes(cls)
except ValueError:
pass
if cls == Classes.Universal:
try:
nr = Numbers(nr)
except ValueError:
pass
return Tag(nr=nr, typ=typ, cls=cls)

def _read_length(self): # type: () -> int
Expand Down
16 changes: 16 additions & 0 deletions tests/test_asn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def test_boolean(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Boolean, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Boolean: 0x01>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert isinstance(val, int)
assert val
Expand All @@ -412,6 +413,7 @@ def test_integer(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Integer, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Integer: 0x02>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert isinstance(val, int)
assert val == 1
Expand Down Expand Up @@ -462,6 +464,7 @@ def test_octet_string(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.OctetString, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.OctetString: 0x04>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert val == b'foo'

Expand All @@ -471,6 +474,7 @@ def test_printable_string(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.PrintableString, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.PrintableString: 0x13>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert val == u'foo'

Expand All @@ -480,6 +484,7 @@ def test_bitstring(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.BitString, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.BitString: 0x03>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert val == b'\x12\x34\x56'

Expand All @@ -489,6 +494,7 @@ def test_bitstring_unused_bits(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.BitString, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.BitString: 0x03>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert val == b'\x01\x23\x45'

Expand All @@ -498,6 +504,7 @@ def test_unicode_printable_string(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.PrintableString, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.PrintableString: 0x13>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert val == u'fooé'

Expand All @@ -507,6 +514,7 @@ def test_null(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Null, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Null: 0x05>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert val is None

Expand Down Expand Up @@ -552,6 +560,7 @@ def test_enumerated(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Enumerated, asn1.Types.Primitive, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Enumerated: 0x0a>, typ=<Types.Primitive: 0x00>, cls=<Classes.Universal: 0x00>)"
tag, val = dec.read()
assert isinstance(val, int)
assert val == 1
Expand All @@ -562,6 +571,7 @@ def test_sequence(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Sequence, asn1.Types.Constructed, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Sequence: 0x10>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
dec.enter()
tag, val = dec.read()
assert val == 1
Expand All @@ -574,6 +584,7 @@ def test_sequence_of(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Sequence, asn1.Types.Constructed, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Sequence: 0x10>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
dec.enter()
tag, val = dec.read()
assert val == 1
Expand All @@ -586,6 +597,7 @@ def test_set(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Set, asn1.Types.Constructed, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Set: 0x11>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
dec.enter()
tag, val = dec.read()
assert val == 1
Expand All @@ -598,6 +610,7 @@ def test_set_of(self):
dec.start(buf)
tag = dec.peek()
assert tag == (asn1.Numbers.Set, asn1.Types.Constructed, asn1.Classes.Universal)
assert str(tag) == "Tag(nr=<Numbers.Set: 0x11>, typ=<Types.Constructed: 0x20>, cls=<Classes.Universal: 0x00>)"
dec.enter()
tag, val = dec.read()
assert val == 1
Expand All @@ -610,6 +623,7 @@ def test_context(self):
dec.start(buf)
tag = dec.peek()
assert tag == (1, asn1.Types.Constructed, asn1.Classes.Context)
assert str(tag) == "Tag(nr=1, typ=<Types.Constructed: 0x20>, cls=<Classes.Context: 0x80>)"
dec.enter()
tag, val = dec.read()
assert val == 1
Expand All @@ -620,6 +634,7 @@ def test_application(self):
dec.start(buf)
tag = dec.peek()
assert tag == (1, asn1.Types.Constructed, asn1.Classes.Application)
assert str(tag) == "Tag(nr=1, typ=<Types.Constructed: 0x20>, cls=<Classes.Application: 0x40>)"
dec.enter()
tag, val = dec.read()
assert val == 1
Expand All @@ -630,6 +645,7 @@ def test_private(self):
dec.start(buf)
tag = dec.peek()
assert tag == (1, asn1.Types.Constructed, asn1.Classes.Private)
assert str(tag) == "Tag(nr=1, typ=<Types.Constructed: 0x20>, cls=<Classes.Private: 0xc0>)"
dec.enter()
tag, val = dec.read()
assert val == 1
Expand Down