summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2013-01-27 09:22:16 +0000
committerelie <elie>2013-01-27 09:22:16 +0000
commitc475ace4b826203dcda8099a2006ac2c06eb7bc5 (patch)
tree1b414611f28e5566c7739803c2c136e77d5982e1
parent1892011c1c643dbf31b76d333069fe46b0d2cfe3 (diff)
downloadpyasn1-c475ace4b826203dcda8099a2006ac2c06eb7bc5.tar.gz
fix to decoder to fail in cases where tagFormat indicates inappropriate
format for the type (e.g. BOOLEAN is always PRIMITIVE, SET is always CONSTRUCTED and OCTET STRING is either of the two)
-rw-r--r--CHANGES7
-rw-r--r--pyasn1/codec/ber/decoder.py11
-rw-r--r--test/codec/ber/decoder.py47
3 files changed, 65 insertions, 0 deletions
diff --git a/CHANGES b/CHANGES
index 4ed4a74..b99a207 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,10 @@
+Revision 0.1.7
+--------------
+
+- Fix to decoder to fail in cases where tagFormat indicates inappropriate
+ format for the type (e.g. BOOLEAN is always PRIMITIVE, SET is always
+ CONSTRUCTED and OCTET STRING is either of the two)
+
Revision 0.1.6
--------------
diff --git a/pyasn1/codec/ber/decoder.py b/pyasn1/codec/ber/decoder.py
index 997b3e7..2209ef0 100644
--- a/pyasn1/codec/ber/decoder.py
+++ b/pyasn1/codec/ber/decoder.py
@@ -15,7 +15,10 @@ class AbstractDecoder:
raise error.PyAsn1Error('Indefinite length mode decoder not implemented for %s' % (tagSet,))
class AbstractSimpleDecoder(AbstractDecoder):
+ tagFormats = (tag.tagFormatSimple,)
def _createComponent(self, asn1Spec, tagSet, value=None):
+ if tagSet[0][1] not in self.tagFormats:
+ raise error.PyAsn1Error('Invalid tag format %r for %r' % (tagSet[0], self.protoComponent,))
if asn1Spec is None:
return self.protoComponent.clone(value, tagSet)
elif value is None:
@@ -24,7 +27,10 @@ class AbstractSimpleDecoder(AbstractDecoder):
return asn1Spec.clone(value)
class AbstractConstructedDecoder(AbstractDecoder):
+ tagFormats = (tag.tagFormatConstructed,)
def _createComponent(self, asn1Spec, tagSet, value=None):
+ if tagSet[0][1] not in self.tagFormats:
+ raise error.PyAsn1Error('Invalid tag format %r for %r' % (tagSet[0], self.protoComponent,))
if asn1Spec is None:
return self.protoComponent.clone(tagSet)
else:
@@ -37,6 +43,7 @@ class EndOfOctetsDecoder(AbstractSimpleDecoder):
class ExplicitTagDecoder(AbstractSimpleDecoder):
protoComponent = univ.Any('')
+ tagFormats = (tag.tagFormatConstructed,)
def valueDecoder(self, fullSubstrate, substrate, asn1Spec, tagSet,
length, state, decodeFun, substrateFun):
if substrateFun:
@@ -108,6 +115,7 @@ class BooleanDecoder(IntegerDecoder):
class BitStringDecoder(AbstractSimpleDecoder):
protoComponent = univ.BitString(())
+ tagFormats = (tag.tagFormatSimple, tag.tagFormatConstructed)
def valueDecoder(self, fullSubstrate, substrate, asn1Spec, tagSet, length,
state, decodeFun, substrateFun):
head, tail = substrate[:length], substrate[length:]
@@ -157,6 +165,7 @@ class BitStringDecoder(AbstractSimpleDecoder):
class OctetStringDecoder(AbstractSimpleDecoder):
protoComponent = univ.OctetString('')
+ tagFormats = (tag.tagFormatSimple, tag.tagFormatConstructed)
def valueDecoder(self, fullSubstrate, substrate, asn1Spec, tagSet, length,
state, decodeFun, substrateFun):
head, tail = substrate[:length], substrate[length:]
@@ -396,6 +405,7 @@ class SetOfDecoder(SequenceOfDecoder):
class ChoiceDecoder(AbstractConstructedDecoder):
protoComponent = univ.Choice()
+ tagFormats = (tag.tagFormatSimple, tag.tagFormatConstructed)
def valueDecoder(self, fullSubstrate, substrate, asn1Spec, tagSet,
length, state, decodeFun, substrateFun):
head, tail = substrate[:length], substrate[length:]
@@ -421,6 +431,7 @@ class ChoiceDecoder(AbstractConstructedDecoder):
class AnyDecoder(AbstractSimpleDecoder):
protoComponent = univ.Any()
+ tagFormats = (tag.tagFormatSimple, tag.tagFormatConstructed)
def valueDecoder(self, fullSubstrate, substrate, asn1Spec, tagSet,
length, state, decodeFun, substrateFun):
if asn1Spec is None or \
diff --git a/test/codec/ber/decoder.py b/test/codec/ber/decoder.py
index 3df0b99..115d811 100644
--- a/test/codec/ber/decoder.py
+++ b/test/codec/ber/decoder.py
@@ -44,6 +44,13 @@ class IntegerDecoderTestCase(unittest.TestCase):
assert decoder.decode(
ints2octs((2, 1, 12)), asn1Spec=univ.Integer()
) == (12, null)
+ def testTagFormat(self):
+ try:
+ decoder.decode(ints2octs((34, 1, 12)))
+ except PyAsn1Error:
+ pass
+ else:
+ assert 0, 'wrong tagFormat worked out'
class BooleanDecoderTestCase(unittest.TestCase):
def testTrue(self):
@@ -54,6 +61,13 @@ class BooleanDecoderTestCase(unittest.TestCase):
assert decoder.decode(ints2octs((1, 1, 1, 0, 120, 50, 50))) == (1, ints2octs((0, 120, 50, 50)))
def testFalse(self):
assert decoder.decode(ints2octs((1, 1, 0))) == (0, null)
+ def testTagFormat(self):
+ try:
+ decoder.decode(ints2octs((33, 1, 1)))
+ except PyAsn1Error:
+ pass
+ else:
+ assert 0, 'wrong tagFormat worked out'
class BitStringDecoderTestCase(unittest.TestCase):
def testDefMode(self):
@@ -154,6 +168,13 @@ class ExpTaggedOctetStringDecoderTestCase(unittest.TestCase):
class NullDecoderTestCase(unittest.TestCase):
def testNull(self):
assert decoder.decode(ints2octs((5, 0))) == (null, null)
+ def testTagFormat(self):
+ try:
+ decoder.decode(ints2octs((37, 0)))
+ except PyAsn1Error:
+ pass
+ else:
+ assert 0, 'wrong tagFormat worked out'
class ObjectIdentifierDecoderTestCase(unittest.TestCase):
def testOID(self):
@@ -191,6 +212,14 @@ class ObjectIdentifierDecoderTestCase(unittest.TestCase):
else:
assert 1, 'Leading 0x80 tolarated'
+ def testTagFormat(self):
+ try:
+ decoder.decode(ints2octs((38, 1, 239)))
+ except PyAsn1Error:
+ pass
+ else:
+ assert 0, 'wrong tagFormat worked out'
+
class RealDecoderTestCase(unittest.TestCase):
def testChar(self):
assert decoder.decode(
@@ -222,6 +251,14 @@ class RealDecoderTestCase(unittest.TestCase):
ints2octs((9, 0))
) == (univ.Real(0.0), null)
+ def testTagFormat(self):
+ try:
+ decoder.decode(ints2octs((41, 0)))
+ except PyAsn1Error:
+ pass
+ else:
+ assert 0, 'wrong tagFormat worked out'
+
class SequenceDecoderTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.Sequence(componentType=namedtype.NamedTypes(
@@ -266,6 +303,16 @@ class SequenceDecoderTestCase(unittest.TestCase):
substrateFun=lambda a,b,c: (b,c)
) == (ints2octs((5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), -1)
+ def testTagFormat(self):
+ try:
+ decoder.decode(
+ ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
+ )
+ except PyAsn1Error:
+ pass
+ else:
+ assert 0, 'wrong tagFormat worked out'
+
class GuidedSequenceDecoderTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.Sequence(componentType=namedtype.NamedTypes(