Visitar URL original
Remove quadratic constructed OCTET STRING and ANY reassembly · pyasn1/pyasn1@4ce0079 · GitHub
Skip to content

Commit 4ce0079

Browse files
committed
Remove quadratic constructed OCTET STRING and ANY reassembly
1 parent 73ba7b0 commit 4ce0079

4 files changed

Lines changed: 85 additions & 17 deletions

File tree

‎pyasn1/codec/ber/decoder.py‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -362,23 +362,20 @@ def valueDecoder(self, substrate, asn1Spec,
362362
if LOG:
363363
LOG('assembling constructed serialization')
364364

365-
# All inner fragments are of the same type, treat them as octet string
366-
substrateFun = self.substrateCollector
367-
368-
header = b''
365+
# Assemble the logical value of each fragment, including nested ones.
366+
fragments = []
369367

370368
original_position = substrate.tell()
371369
# head = popSubstream(substrate, length)
372370
while substrate.tell() - original_position < length:
373371
for component in decodeFun(
374-
substrate, self.protoComponent, substrateFun=substrateFun,
375-
**options):
372+
substrate, self.protoComponent, **options):
376373
if isinstance(component, SubstrateUnderrunError):
377374
yield component
378375

379-
header += component
376+
fragments.append(bytes(component))
380377

381-
yield self._createComponent(asn1Spec, tagSet, header, **options)
378+
yield self._createComponent(asn1Spec, tagSet, b''.join(fragments), **options)
382379

383380
def indefLenValueDecoder(self, substrate, asn1Spec,
384381
tagSet=None, length=None, state=None,
@@ -392,16 +389,13 @@ def indefLenValueDecoder(self, substrate, asn1Spec,
392389

393390
return
394391

395-
# All inner fragments are of the same type, treat them as octet string
396-
substrateFun = self.substrateCollector
397-
398-
header = b''
392+
# Assemble the logical value of each fragment, including nested ones.
393+
fragments = []
399394

400395
while True: # loop over fragments
401396

402397
for component in decodeFun(
403-
substrate, self.protoComponent, substrateFun=substrateFun,
404-
allowEoo=True, **options):
398+
substrate, self.protoComponent, allowEoo=True, **options):
405399

406400
if isinstance(component, SubstrateUnderrunError):
407401
yield component
@@ -412,9 +406,9 @@ def indefLenValueDecoder(self, substrate, asn1Spec,
412406
if component is eoo.endOfOctets:
413407
break
414408

415-
header += component
409+
fragments.append(bytes(component))
416410

417-
yield self._createComponent(asn1Spec, tagSet, header, **options)
411+
yield self._createComponent(asn1Spec, tagSet, b''.join(fragments), **options)
418412

419413

420414
class NullPayloadDecoder(AbstractSimplePayloadDecoder):
@@ -1431,6 +1425,8 @@ def indefLenValueDecoder(self, substrate, asn1Spec,
14311425
# All inner fragments are of the same type, treat them as octet string
14321426
substrateFun = self.substrateCollector
14331427

1428+
fragments = [chunk]
1429+
14341430
while True: # loop over fragments
14351431

14361432
for component in decodeFun(
@@ -1446,7 +1442,9 @@ def indefLenValueDecoder(self, substrate, asn1Spec,
14461442
if component is eoo.endOfOctets:
14471443
break
14481444

1449-
chunk += component
1445+
fragments.append(component)
1446+
1447+
chunk = b''.join(fragments)
14501448

14511449
if substrateFun:
14521450
yield chunk # TODO: Weird

‎tests/codec/ber/test_decoder.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,32 @@ def testIndefModeChunked(self):
244244
bytes((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0))
245245
) == (b'Quick brown fox', b'')
246246

247+
def testManyFragments(self):
248+
fragments = b'\x04\x02AB' * 256
249+
250+
for substrate in (b'\x24' + encode_length(len(fragments)) + fragments,
251+
b'\x24\x80' + fragments + b'\x00\x00'):
252+
for asn1Spec in (None, univ.OctetString()):
253+
value, rest = decoder.decode(substrate, asn1Spec=asn1Spec)
254+
assert value.asOctets() == b'AB' * 256
255+
assert rest == b''
256+
257+
def testNestedIndefModeChunked(self):
258+
substrate = bytes.fromhex('24 07 24 80 04 01 41 00 00')
259+
260+
for asn1Spec in (None, univ.OctetString()):
261+
value, rest = decoder.decode(substrate, asn1Spec=asn1Spec)
262+
assert value.asOctets() == b'A'
263+
assert rest == b''
264+
265+
def testNestedDefModeChunked(self):
266+
substrate = bytes.fromhex('24 05 24 03 04 01 41')
267+
268+
for asn1Spec in (None, univ.OctetString()):
269+
value, rest = decoder.decode(substrate, asn1Spec=asn1Spec)
270+
assert value.asOctets() == b'A'
271+
assert rest == b''
272+
247273
def testDefModeChunkedSubst(self):
248274
assert decoder.decode(
249275
bytes(
@@ -1876,6 +1902,14 @@ def testByUntaggedIndefMode(self):
18761902
bytes((4, 3, 102, 111, 120)), asn1Spec=self.s
18771903
) == (univ.Any('\004\003fox'), b'')
18781904

1905+
def testManyIndefiniteLengthComponents(self):
1906+
fragments = b'\x04\x01A' * 128
1907+
substrate = b'\x30\x80' + fragments + b'\x00\x00'
1908+
value, rest = decoder.decode(substrate, asn1Spec=self.s)
1909+
1910+
assert value == b'\x30\x80' + fragments
1911+
assert rest == b''
1912+
18791913
def testTaggedExIndefMode(self):
18801914
s = univ.Any('\004\003fox').subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
18811915
assert decoder.decode(bytes((164, 128, 4, 3, 102, 111, 120, 0, 0)), asn1Spec=s) == (s, b'')

‎tests/codec/cer/test_decoder.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ def testLongMode(self):
7474
bytes((36, 128, 4, 130, 3, 232) + (81,) * 1000 + (4, 1, 81, 0, 0))
7575
) == (b'Q' * 1001, b'')
7676

77+
def testManyFragments(self):
78+
fragments = b'\x04\x01A' * 64
79+
definite = b'\x24\x81\xc0' + fragments
80+
indefinite = b'\x24\x80' + fragments + b'\x00\x00'
81+
82+
for substrate in (definite, indefinite):
83+
value, rest = decoder.decode(substrate, asn1Spec=univ.OctetString())
84+
assert value.asOctets() == b'A' * 64
85+
assert rest == b''
86+
7787
# TODO: test failures on short chunked and long unchunked substrate samples
7888

7989

@@ -110,6 +120,16 @@ def testLongBinaryMantissa(self):
110120
assert rest == b''
111121

112122

123+
class AnyDecoderTestCase(BaseTestCase):
124+
def testManyIndefiniteLengthComponents(self):
125+
fragments = b'\x04\x01A' * 64
126+
substrate = b'\x30\x80' + fragments + b'\x00\x00'
127+
value, rest = decoder.decode(substrate, asn1Spec=univ.Any())
128+
129+
assert value == b'\x30\x80' + fragments
130+
assert rest == b''
131+
132+
113133
class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase):
114134
def setUp(self):
115135
openType = opentype.OpenType(

‎tests/codec/der/test_decoder.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from tests.base import BaseTestCase
1111

12+
from pyasn1.type import char
1213
from pyasn1.type import tag
1314
from pyasn1.type import namedtype
1415
from pyasn1.type import opentype
@@ -80,6 +81,21 @@ def testChunkedMode(self):
8081
else:
8182
assert 0, 'chunked encoding tolerated'
8283

84+
def testSchemaDrivenManyFragments(self):
85+
fragments = b'\x04\x01A' * 64
86+
value, rest = decoder.decode(b'\x24\x81\xc0' + fragments,
87+
asn1Spec=univ.OctetString())
88+
89+
assert value.asOctets() == b'A' * 64
90+
assert rest == b''
91+
92+
def testConstructedCharacterString(self):
93+
fragments = b'\x0c\x01A' * 64
94+
value, rest = decoder.decode(b'\x2c\x81\xc0' + fragments)
95+
96+
assert value == char.UTF8String(b'A' * 64)
97+
assert rest == b''
98+
8399

84100
class LargeTagDecoderTestCase(BaseTestCase):
85101
def testExcessiveLongTag(self):

0 commit comments

Comments
 (0)