Skip to content

Commit 628e36e

Browse files
committed
Merge commit from fork
1 parent e60c691 commit 628e36e

6 files changed

Lines changed: 102 additions & 6 deletions

File tree

‎pyasn1/codec/ber/decoder.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
# Maximum number of continuation octets (high-bit set) allowed per OID arc.
3737
# 20 octets allows up to 140-bit integers, supporting UUID-based OIDs
3838
MAX_OID_ARC_CONTINUATION_OCTETS = 20
39+
40+
# Maximum number of octets in a long-form tag ID (20 octets = up to
41+
# 140-bit tag IDs, matching the OID arc limit)
42+
MAX_TAG_OCTETS = 20
3943
MAX_NESTING_DEPTH = 100
4044

4145
# Maximum number of bytes in a BER length field (8 bytes = up to 2^64-1)
@@ -1633,7 +1637,7 @@ def __call__(self, substrate, asn1Spec=None,
16331637

16341638
if tagId == 0x1F:
16351639
isShortTag = False
1636-
lengthOctetIdx = 0
1640+
tagOctetCount = 0
16371641
tagId = 0
16381642

16391643
while True:
@@ -1647,7 +1651,12 @@ def __call__(self, substrate, asn1Spec=None,
16471651
)
16481652

16491653
integerTag = ord(integerByte)
1650-
lengthOctetIdx += 1
1654+
tagOctetCount += 1
1655+
if tagOctetCount > MAX_TAG_OCTETS:
1656+
raise error.PyAsn1Error(
1657+
'Tag ID octet count exceeds limit (%d)' % (
1658+
MAX_TAG_OCTETS,)
1659+
)
16511660
tagId <<= 7
16521661
tagId |= (integerTag & 0x7F)
16531662

‎pyasn1/type/tag.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
tagCategoryUntagged = 0x04
3535

3636

37+
def _tagIdToStr(tagId):
38+
# Decimal rendering of a huge tag ID can exceed the interpreter's
39+
# integer-to-string conversion limit (sys.get_int_max_str_digits(),
40+
# Python 3.11+) and raise ValueError; hexadecimal is not limited
41+
try:
42+
return str(tagId)
43+
except ValueError:
44+
return hex(tagId)
45+
46+
3747
class Tag(object):
3848
"""Create ASN.1 tag
3949
@@ -56,7 +66,8 @@ class Tag(object):
5666
"""
5767
def __init__(self, tagClass, tagFormat, tagId):
5868
if tagId < 0:
59-
raise error.PyAsn1Error('Negative tag ID (%s) not allowed' % tagId)
69+
raise error.PyAsn1Error(
70+
'Negative tag ID (%s) not allowed' % _tagIdToStr(tagId))
6071
self.__tagClass = tagClass
6172
self.__tagFormat = tagFormat
6273
self.__tagId = tagId
@@ -65,7 +76,7 @@ def __init__(self, tagClass, tagFormat, tagId):
6576

6677
def __repr__(self):
6778
representation = '[%s:%s:%s]' % (
68-
self.__tagClass, self.__tagFormat, self.__tagId)
79+
self.__tagClass, self.__tagFormat, _tagIdToStr(self.__tagId))
6980
return '<%s object, tag %s>' % (
7081
self.__class__.__name__, representation)
7182

@@ -194,8 +205,9 @@ def __init__(self, baseTag=(), *superTags):
194205
self.__hash = hash(self.__superTagsClassId)
195206

196207
def __repr__(self):
197-
representation = '-'.join(['%s:%s:%s' % (x.tagClass, x.tagFormat, x.tagId)
198-
for x in self.__superTags])
208+
representation = '-'.join(
209+
['%s:%s:%s' % (x.tagClass, x.tagFormat, _tagIdToStr(x.tagId))
210+
for x in self.__superTags])
199211
if representation:
200212
representation = 'tags ' + representation
201213
else:

‎tests/codec/ber/test_decoder.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ def testLargeTag(self):
3333
def testLongTag(self):
3434
assert decoder.decode(bytes((0x1f, 2, 1, 0)))[0].tagSet == univ.Integer.tagSet
3535

36+
def testVeryLongTagRoundTrip(self):
37+
# (1 << 140) - 1 is the largest tag ID fitting the 20 octet limit
38+
for tagId in (1 << 77, (1 << 140) - 1):
39+
largeTag = tag.Tag(tag.tagClassContext, tag.tagFormatSimple, tagId)
40+
asn1Spec = univ.Integer().subtype(implicitTag=largeTag)
41+
value = univ.Integer(1).subtype(implicitTag=largeTag)
42+
43+
decoded, rest = decoder.decode(encoder.encode(value), asn1Spec=asn1Spec)
44+
45+
assert rest == b''
46+
assert decoded == 1
47+
48+
def testExcessiveLongTag(self):
49+
# 1 << 140 is the smallest tag ID needing 21 octets, one over the limit
50+
excessiveTag = tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1 << 140)
51+
asn1Spec = univ.Integer().subtype(implicitTag=excessiveTag)
52+
substrate = encoder.encode(univ.Integer(1).subtype(implicitTag=excessiveTag))
53+
54+
try:
55+
decoder.decode(substrate, asn1Spec=asn1Spec)
56+
except error.PyAsn1Error:
57+
pass
58+
else:
59+
assert 0, 'excessive long tag tolerated'
60+
3661
def testTagsEquivalence(self):
3762
integer = univ.Integer(2).subtype(implicitTag=tag.Tag(tag.tagClassContext, 0, 0))
3863
assert decoder.decode(bytes((0x9f, 0x80, 0x00, 0x02, 0x01, 0x02)), asn1Spec=integer) == decoder.decode(

‎tests/codec/cer/test_decoder.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ def testLongMode(self):
6666
# TODO: test failures on short chunked and long unchunked substrate samples
6767

6868

69+
class LargeTagDecoderTestCase(BaseTestCase):
70+
def testExcessiveLongTag(self):
71+
# 1 << 140 is the smallest tag ID needing 21 octets, one over the limit
72+
excessiveTag = tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1 << 140)
73+
asn1Spec = univ.Integer().subtype(implicitTag=excessiveTag)
74+
substrate = encoder.encode(univ.Integer(1).subtype(implicitTag=excessiveTag))
75+
76+
try:
77+
decoder.decode(substrate, asn1Spec=asn1Spec)
78+
except PyAsn1Error:
79+
pass
80+
else:
81+
assert 0, 'excessive long tag tolerated'
82+
83+
6984
class RealDecoderTestCase(BaseTestCase):
7085
def testLargeBinaryRoundTrip(self):
7186
substrate = encoder.encode(univ.Real((-1, 2, 76354972)))

‎tests/codec/der/test_decoder.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ def testChunkedMode(self):
7272
assert 0, 'chunked encoding tolerated'
7373

7474

75+
class LargeTagDecoderTestCase(BaseTestCase):
76+
def testExcessiveLongTag(self):
77+
# 1 << 140 is the smallest tag ID needing 21 octets, one over the limit
78+
excessiveTag = tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1 << 140)
79+
asn1Spec = univ.Integer().subtype(implicitTag=excessiveTag)
80+
substrate = encoder.encode(univ.Integer(1).subtype(implicitTag=excessiveTag))
81+
82+
try:
83+
decoder.decode(substrate, asn1Spec=asn1Spec)
84+
except PyAsn1Error:
85+
pass
86+
else:
87+
assert 0, 'excessive long tag tolerated'
88+
89+
7590
class RealDecoderTestCase(BaseTestCase):
7691
def testCanonicalLargeBinaryReal(self):
7792
substrate = encoder.encode(univ.Real((1, 2, 1000000)))

‎tests/type/test_tag.py‎

Lines changed: 20 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 import error
1213
from pyasn1.type import tag
1314

1415

@@ -23,6 +24,19 @@ class TagReprTestCase(TagTestCaseBase):
2324
def testRepr(self):
2425
assert 'Tag' in repr(self.t1)
2526

27+
def testReprHugeTagId(self):
28+
# must not hit the interpreter's int-to-str conversion limit
29+
hugeTag = tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1 << 100000)
30+
assert 'Tag' in repr(hugeTag)
31+
32+
def testNegativeHugeTagId(self):
33+
try:
34+
tag.Tag(tag.tagClassContext, tag.tagFormatSimple, -(1 << 100000))
35+
except error.PyAsn1Error:
36+
pass
37+
else:
38+
assert 0, 'negative tag ID tolerated'
39+
2640

2741
class TagCmpTestCase(TagTestCaseBase):
2842
def testCmp(self):
@@ -54,6 +68,12 @@ class TagSetReprTestCase(TagSetTestCaseBase):
5468
def testRepr(self):
5569
assert 'TagSet' in repr(self.ts1)
5670

71+
def testReprHugeTagId(self):
72+
# must not hit the interpreter's int-to-str conversion limit
73+
hugeTagSet = self.ts1.tagImplicitly(
74+
tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1 << 100000))
75+
assert 'TagSet' in repr(hugeTagSet)
76+
5777

5878
class TagSetCmpTestCase(TagSetTestCaseBase):
5979
def testCmp(self):

0 commit comments

Comments
 (0)