From 6cd6af222bb36726f95d486461589105840d883f Mon Sep 17 00:00:00 2001 From: elie Date: Sat, 25 Apr 2015 09:53:43 +0000 Subject: fix to BitString and OctetString decoders dealing with constructed encoding -- it used to be possible to embed other types in substrate --- CHANGES | 2 ++ pyasn1/codec/ber/decoder.py | 8 ++++---- test/codec/ber/test_decoder.py | 11 +++++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index 3ccae94..57666c1 100644 --- a/CHANGES +++ b/CHANGES @@ -52,6 +52,8 @@ Revision 0.1.8rc2 - Fix to REAL type binary decoder to handle different bases and scale factor. - Fix to TagSet.repr() to include [obsolete] baseTag information. - Fix to broken REAL type decoding handling. +- Fix to BitString and OctetString decoders dealing with constructed + encoding -- it used to be possible to embed other types in substrate. Revision 0.1.7 -------------- diff --git a/pyasn1/codec/ber/decoder.py b/pyasn1/codec/ber/decoder.py index 7bb09ea..2da4142 100644 --- a/pyasn1/codec/ber/decoder.py +++ b/pyasn1/codec/ber/decoder.py @@ -144,7 +144,7 @@ class BitStringDecoder(AbstractSimpleDecoder): if substrateFun: return substrateFun(r, substrate, length) while head: - component, head = decodeFun(head) + component, head = decodeFun(head, self.protoComponent) r = r + component return r, tail @@ -154,7 +154,7 @@ class BitStringDecoder(AbstractSimpleDecoder): if substrateFun: return substrateFun(r, substrate, length) while substrate: - component, substrate = decodeFun(substrate) + component, substrate = decodeFun(substrate, self.protoComponent) if eoo.endOfOctets.isSameTypeWith(component) and \ component == eoo.endOfOctets: break @@ -177,7 +177,7 @@ class OctetStringDecoder(AbstractSimpleDecoder): if substrateFun: return substrateFun(r, substrate, length) while head: - component, head = decodeFun(head) + component, head = decodeFun(head, self.protoComponent) r = r + component return r, tail @@ -187,7 +187,7 @@ class OctetStringDecoder(AbstractSimpleDecoder): if substrateFun: return substrateFun(r, substrate, length) while substrate: - component, substrate = decodeFun(substrate) + component, substrate = decodeFun(substrate, self.protoComponent) if eoo.endOfOctets.isSameTypeWith(component) and \ component == eoo.endOfOctets: break diff --git a/test/codec/ber/test_decoder.py b/test/codec/ber/test_decoder.py index 72ed77f..cde3ff8 100644 --- a/test/codec/ber/test_decoder.py +++ b/test/codec/ber/test_decoder.py @@ -99,7 +99,14 @@ class BitStringDecoderTestCase(unittest.TestCase): ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)), substrateFun=lambda a,b,c: (b,c) ) == (ints2octs((3, 2, 0, 169, 3, 2, 1, 138, 0, 0)), -1) - + def testTypeChecking(self): + try: + decoder.decode(ints2octs((35, 4, 2, 2, 42, 42))) + except PyAsn1Error: + pass + except: + assert 0, 'accepted mis-encoded bit-string constructed out of an integer' + class OctetStringDecoderTestCase(unittest.TestCase): def testDefMode(self): assert decoder.decode( @@ -387,7 +394,7 @@ class RealDecoderTestCase(unittest.TestCase): decoder.decode(ints2octs((9, 1, 131))) except PyAsn1Error: pass - else: + except: assert 0, 'accepted too-short real' class SequenceDecoderTestCase(unittest.TestCase): -- cgit v1.2.1