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
8 changes: 6 additions & 2 deletions scapy/asn1/ber.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,18 @@ def do_dec(cls, s, context=None, safe=False):
if context is None:
context = cls.tag.context
cls.check_string(s)
p, _ = BER_id_dec(s)
p, remainder = BER_id_dec(s)
if p not in context:
t = s
if len(t) > 18:
t = t[:15] + b"..."
raise BER_Decoding_Error("Unknown prefix [%02x] for [%r]" %
(p, t), remaining=s)
codec = context[p].get_codec(ASN1_Codecs.BER)
if codec == BERcodec_Object:
# Value type defined as Unknown
l, s = BER_num_dec(remainder)
return ASN1_BADTAG(s[:l]), s[l:]
return codec.dec(s, context, safe)

@classmethod
Expand All @@ -294,7 +298,7 @@ def safedec(cls, s, context=None):

@classmethod
def enc(cls, s):
if isinstance(s, six.string_types):
if isinstance(s, six.string_types + (bytes,)):
return BERcodec_STRING.enc(s)
else:
return BERcodec_INTEGER.enc(int(s))
Expand Down
14 changes: 14 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,20 @@ assert SNMP in z
x = UDP()/SNMP()
assert x.sport == x.dport == 161

= Basic SNMPvarbind build
~ SNMP ASN1
x = SNMPvarbind(oid=ASN1_OID("1.3.6.1.2.1.1.4.0"), value=RandBin())
x = SNMPvarbind(raw(x))
assert isinstance(x.value, ASN1_STRING)

= Failing SNMPvarbind dissection
~ SNMP ASN1
try:
SNMP('0a\x02\x01\x00\x04\x06public\xa3T\x02\x02D\xd0\x02\x01\x00\x02\x01\x000H0F\x06\x08+\x06\x01\x02\x01\x01\x05\x00\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D\x00\x03\x01\x02D')
assert False
except BER_Decoding_Error:
pass

= ASN1 - ASN1_Object
assert ASN1_Object(1) == ASN1_Object(1)
assert ASN1_Object(1) > ASN1_Object(0)
Expand Down