Skip to content

Commit 45bdb19

Browse files
committed
Merge commit from fork
1 parent 628e36e commit 45bdb19

4 files changed

Lines changed: 83 additions & 25 deletions

File tree

‎pyasn1/codec/ber/decoder.py‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,14 @@ def valueDecoder(self, substrate, asn1Spec,
427427
if not chunk:
428428
raise error.PyAsn1Error('Empty substrate')
429429

430-
oid = ()
430+
oid = []
431431
index = 0
432432
substrateLen = len(chunk)
433433
while index < substrateLen:
434434
subId = chunk[index]
435435
index += 1
436436
if subId < 128:
437-
oid += (subId,)
437+
oid.append(subId)
438438
elif subId > 128:
439439
# Construct subid from a number of octets
440440
nextSubId = subId
@@ -450,11 +450,11 @@ def valueDecoder(self, substrate, asn1Spec,
450450
subId = (subId << 7) + (nextSubId & 0x7F)
451451
if index >= substrateLen:
452452
raise error.SubstrateUnderrunError(
453-
'Short substrate for sub-OID past %s' % (oid,)
453+
'Short substrate for sub-OID past %s' % (tuple(oid),)
454454
)
455455
nextSubId = chunk[index]
456456
index += 1
457-
oid += ((subId << 7) + nextSubId,)
457+
oid.append((subId << 7) + nextSubId)
458458
elif subId == 128:
459459
# ASN.1 spec forbids leading zeros (0x80) in OID
460460
# encoding, tolerating it opens a vulnerability. See
@@ -464,15 +464,17 @@ def valueDecoder(self, substrate, asn1Spec,
464464

465465
# Decode two leading arcs
466466
if 0 <= oid[0] <= 39:
467-
oid = (0,) + oid
467+
oid.insert(0, 0)
468468
elif 40 <= oid[0] <= 79:
469-
oid = (1, oid[0] - 40) + oid[1:]
469+
oid[0] -= 40
470+
oid.insert(0, 1)
470471
elif oid[0] >= 80:
471-
oid = (2, oid[0] - 80) + oid[1:]
472+
oid[0] -= 80
473+
oid.insert(0, 2)
472474
else:
473475
raise error.PyAsn1Error('Malformed first OID octet: %s' % chunk[0])
474476

475-
yield self._createComponent(asn1Spec, tagSet, oid, **options)
477+
yield self._createComponent(asn1Spec, tagSet, tuple(oid), **options)
476478

477479

478480
class RelativeOIDPayloadDecoder(AbstractSimplePayloadDecoder):
@@ -492,14 +494,14 @@ def valueDecoder(self, substrate, asn1Spec,
492494
if not chunk:
493495
raise error.PyAsn1Error('Empty substrate')
494496

495-
reloid = ()
497+
reloid = []
496498
index = 0
497499
substrateLen = len(chunk)
498500
while index < substrateLen:
499501
subId = chunk[index]
500502
index += 1
501503
if subId < 128:
502-
reloid += (subId,)
504+
reloid.append(subId)
503505
elif subId > 128:
504506
# Construct subid from a number of octets
505507
nextSubId = subId
@@ -515,19 +517,19 @@ def valueDecoder(self, substrate, asn1Spec,
515517
subId = (subId << 7) + (nextSubId & 0x7F)
516518
if index >= substrateLen:
517519
raise error.SubstrateUnderrunError(
518-
'Short substrate for sub-OID past %s' % (reloid,)
520+
'Short substrate for sub-OID past %s' % (tuple(reloid),)
519521
)
520522
nextSubId = chunk[index]
521523
index += 1
522-
reloid += ((subId << 7) + nextSubId,)
524+
reloid.append((subId << 7) + nextSubId)
523525
elif subId == 128:
524526
# ASN.1 spec forbids leading zeros (0x80) in OID
525527
# encoding, tolerating it opens a vulnerability. See
526528
# https://www.esat.kuleuven.be/cosic/publications/article-1432.pdf
527529
# page 7
528530
raise error.PyAsn1Error('Invalid octet 0x80 in RELATIVE-OID encoding')
529531

530-
yield self._createComponent(asn1Spec, tagSet, reloid, **options)
532+
yield self._createComponent(asn1Spec, tagSet, tuple(reloid), **options)
531533

532534

533535
class RealPayloadDecoder(AbstractSimplePayloadDecoder):

‎pyasn1/codec/ber/encoder.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,30 +325,30 @@ def encodeValue(self, value, asn1Spec, encodeFun, **options):
325325
else:
326326
raise error.PyAsn1Error('Impossible first/second arcs at %s' % (value,))
327327

328-
octets = ()
328+
octets = []
329329

330330
# Cycle through subIds
331331
for subOid in oid:
332332
if 0 <= subOid <= 127:
333333
# Optimize for the common case
334-
octets += (subOid,)
334+
octets.append(subOid)
335335

336336
elif subOid > 127:
337337
# Pack large Sub-Object IDs
338-
res = (subOid & 0x7f,)
338+
res = [subOid & 0x7f]
339339
subOid >>= 7
340340

341341
while subOid:
342-
res = (0x80 | (subOid & 0x7f),) + res
342+
res.append(0x80 | (subOid & 0x7f))
343343
subOid >>= 7
344344

345345
# Add packed Sub-Object ID to resulted Object ID
346-
octets += res
346+
octets.extend(reversed(res))
347347

348348
else:
349349
raise error.PyAsn1Error('Negative OID arc %s at %s' % (subOid, value))
350350

351-
return octets, False, False
351+
return tuple(octets), False, False
352352

353353

354354
class RelativeOIDEncoder(AbstractItemEncoder):
@@ -358,30 +358,30 @@ def encodeValue(self, value, asn1Spec, encodeFun, **options):
358358
if asn1Spec is not None:
359359
value = asn1Spec.clone(value)
360360

361-
octets = ()
361+
octets = []
362362

363363
# Cycle through subIds
364364
for subOid in value.asTuple():
365365
if 0 <= subOid <= 127:
366366
# Optimize for the common case
367-
octets += (subOid,)
367+
octets.append(subOid)
368368

369369
elif subOid > 127:
370370
# Pack large Sub-Object IDs
371-
res = (subOid & 0x7f,)
371+
res = [subOid & 0x7f]
372372
subOid >>= 7
373373

374374
while subOid:
375-
res = (0x80 | (subOid & 0x7f),) + res
375+
res.append(0x80 | (subOid & 0x7f))
376376
subOid >>= 7
377377

378378
# Add packed Sub-Object ID to resulted RELATIVE-OID
379-
octets += res
379+
octets.extend(reversed(res))
380380

381381
else:
382382
raise error.PyAsn1Error('Negative RELATIVE-OID arc %s at %s' % (subOid, value))
383383

384-
return octets, False, False
384+
return tuple(octets), False, False
385385

386386

387387
class RealEncoder(AbstractItemEncoder):

‎tests/codec/ber/test_decoder.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
from pyasn1 import error
2727

2828

29+
def encode_length(length):
30+
if length < 128:
31+
return bytes([length])
32+
33+
lengthBytes = length.to_bytes((length.bit_length() + 7) // 8, 'big')
34+
return bytes([0x80 | len(lengthBytes)]) + lengthBytes
35+
36+
2937
class LargeTagDecoderTestCase(BaseTestCase):
3038
def testLargeTag(self):
3139
assert decoder.decode(bytes((127, 141, 245, 182, 253, 47, 3, 2, 1, 1))) == (1, b'')
@@ -475,6 +483,20 @@ def testLarge2(self):
475483
bytes((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB6, 0x47))
476484
) == ((2, 999, 18446744073709551535184467440737095), b'')
477485

486+
def testManySingleByteArcs(self):
487+
encodedArcCount = 4096
488+
substrate = (
489+
bytes([0x06]) +
490+
encode_length(encodedArcCount) +
491+
bytes([0x01] * encodedArcCount)
492+
)
493+
494+
value, rest = decoder.decode(substrate)
495+
assert rest == b''
496+
assert len(value) == encodedArcCount + 1
497+
assert tuple(value[:3]) == (0, 1, 1)
498+
assert tuple(value[-3:]) == (1, 1, 1)
499+
478500
def testExcessiveContinuationOctets(self):
479501
"""Test that OID arcs with excessive continuation octets are rejected."""
480502
# Create a payload with 25 continuation octets (exceeds 20 limit)
@@ -610,6 +632,20 @@ def testLarge(self):
610632
bytes((0x0D, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB6, 0x47))
611633
) == ((1079, 18446744073709551535184467440737095), b'')
612634

635+
def testManySingleByteArcs(self):
636+
arcCount = 4096
637+
substrate = (
638+
bytes([0x0d]) +
639+
encode_length(arcCount) +
640+
bytes([0x01] * arcCount)
641+
)
642+
643+
value, rest = decoder.decode(substrate)
644+
assert rest == b''
645+
assert len(value) == arcCount
646+
assert tuple(value[:3]) == (1, 1, 1)
647+
assert tuple(value[-3:]) == (1, 1, 1)
648+
613649
def testExcessiveContinuationOctets(self):
614650
"""Test that RELATIVE-OID arcs with excessive continuation octets are rejected."""
615651
# Create a payload with 25 continuation octets (exceeds 20 limit)

‎tests/codec/ber/test_encoder.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ def testLarge2(self):
348348
) == bytes((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6,
349349
0xB8, 0xCB, 0xE2, 0xB6, 0x47))
350350

351+
def testManySingleByteArcs(self):
352+
arcCount = 4096
353+
substrate = encoder.encode(
354+
univ.ObjectIdentifier((1, 3) + (1,) * arcCount)
355+
)
356+
357+
assert substrate == (
358+
bytes([0x06, 0x82, 0x10, 0x01, 0x2B]) + bytes([0x01] * arcCount)
359+
)
360+
351361

352362
class ObjectIdentifierWithSchemaEncoderTestCase(BaseTestCase):
353363
def testOne(self):
@@ -379,6 +389,16 @@ def testLarge(self):
379389
0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB,
380390
0xE2, 0xB6, 0x47))
381391

392+
def testManySingleByteArcs(self):
393+
arcCount = 4096
394+
substrate = encoder.encode(
395+
univ.RelativeOID((1,) * arcCount)
396+
)
397+
398+
assert substrate == (
399+
bytes([0x0D, 0x82, 0x10, 0x00]) + bytes([0x01] * arcCount)
400+
)
401+
382402

383403
class RelativeOIDWithSchemaEncoderTestCase(BaseTestCase):
384404
def testOne(self):

0 commit comments

Comments
 (0)