From 45bdb19eb7df4b3780fe9c912c63e99bffc39dd9 Mon Sep 17 00:00:00 2001
From: Simon Pichugin <simon.pichugin@gmail.com>
Date: Wed, 8 Jul 2026 17:37:40 -0700
Subject: [PATCH] Merge commit from fork

---
 pyasn1/codec/ber/decoder.py     | 28 +++++++++++++------------
 pyasn1/codec/ber/encoder.py     | 24 +++++++++++-----------
 tests/codec/ber/test_decoder.py | 36 +++++++++++++++++++++++++++++++++
 tests/codec/ber/test_encoder.py | 20 ++++++++++++++++++
 4 files changed, 83 insertions(+), 25 deletions(-)

Index: pyasn1-0.5.0/pyasn1/codec/ber/decoder.py
===================================================================
--- pyasn1-0.5.0.orig/pyasn1/codec/ber/decoder.py
+++ pyasn1-0.5.0/pyasn1/codec/ber/decoder.py
@@ -425,14 +425,14 @@ class ObjectIdentifierPayloadDecoder(Abs
 
         chunk = octs2ints(chunk)
 
-        oid = ()
+        oid = []
         index = 0
         substrateLen = len(chunk)
         while index < substrateLen:
             subId = chunk[index]
             index += 1
             if subId < 128:
-                oid += (subId,)
+                oid.append(subId)
             elif subId > 128:
                 # Construct subid from a number of octets
                 nextSubId = subId
@@ -448,11 +448,11 @@ class ObjectIdentifierPayloadDecoder(Abs
                     subId = (subId << 7) + (nextSubId & 0x7F)
                     if index >= substrateLen:
                         raise error.SubstrateUnderrunError(
-                            'Short substrate for sub-OID past %s' % (oid,)
+                            'Short substrate for sub-OID past %s' % (tuple(oid),)
                         )
                     nextSubId = chunk[index]
                     index += 1
-                oid += ((subId << 7) + nextSubId,)
+                oid.append((subId << 7) + nextSubId)
             elif subId == 128:
                 # ASN.1 spec forbids leading zeros (0x80) in OID
                 # encoding, tolerating it opens a vulnerability. See
@@ -462,15 +462,17 @@ class ObjectIdentifierPayloadDecoder(Abs
 
         # Decode two leading arcs
         if 0 <= oid[0] <= 39:
-            oid = (0,) + oid
+            oid.insert(0, 0)
         elif 40 <= oid[0] <= 79:
-            oid = (1, oid[0] - 40) + oid[1:]
+            oid[0] -= 40
+            oid.insert(0, 1)
         elif oid[0] >= 80:
-            oid = (2, oid[0] - 80) + oid[1:]
+            oid[0] -= 80
+            oid.insert(0, 2)
         else:
             raise error.PyAsn1Error('Malformed first OID octet: %s' % chunk[0])
 
-        yield self._createComponent(asn1Spec, tagSet, oid, **options)
+        yield self._createComponent(asn1Spec, tagSet, tuple(oid), **options)
 
 
 class RealPayloadDecoder(AbstractSimplePayloadDecoder):
Index: pyasn1-0.5.0/pyasn1/codec/ber/encoder.py
===================================================================
--- pyasn1-0.5.0.orig/pyasn1/codec/ber/encoder.py
+++ pyasn1-0.5.0/pyasn1/codec/ber/encoder.py
@@ -327,30 +327,30 @@ class ObjectIdentifierEncoder(AbstractIt
         else:
             raise error.PyAsn1Error('Impossible first/second arcs at %s' % (value,))
 
-        octets = ()
+        octets = []
 
         # Cycle through subIds
         for subOid in oid:
             if 0 <= subOid <= 127:
                 # Optimize for the common case
-                octets += (subOid,)
+                octets.append(subOid)
 
             elif subOid > 127:
                 # Pack large Sub-Object IDs
-                res = (subOid & 0x7f,)
+                res = [subOid & 0x7f]
                 subOid >>= 7
 
                 while subOid:
-                    res = (0x80 | (subOid & 0x7f),) + res
+                    res.append(0x80 | (subOid & 0x7f))
                     subOid >>= 7
 
                 # Add packed Sub-Object ID to resulted Object ID
-                octets += res
+                octets.extend(reversed(res))
 
             else:
                 raise error.PyAsn1Error('Negative OID arc %s at %s' % (subOid, value))
 
-        return octets, False, False
+        return tuple(octets), False, False
 
 
 class RealEncoder(AbstractItemEncoder):
Index: pyasn1-0.5.0/tests/codec/ber/test_decoder.py
===================================================================
--- pyasn1-0.5.0.orig/tests/codec/ber/test_decoder.py
+++ pyasn1-0.5.0/tests/codec/ber/test_decoder.py
@@ -26,6 +26,14 @@ from pyasn1.compat.octets import ints2oc
 from pyasn1 import error
 
 
+def encode_length(length):
+    if length < 128:
+        return bytes([length])
+
+    lengthBytes = length.to_bytes((length.bit_length() + 7) // 8, 'big')
+    return bytes([0x80 | len(lengthBytes)]) + lengthBytes
+
+
 class LargeTagDecoderTestCase(BaseTestCase):
     def testLargeTag(self):
         assert decoder.decode(ints2octs((127, 141, 245, 182, 253, 47, 3, 2, 1, 1))) == (1, null)
@@ -433,6 +441,20 @@ class ObjectIdentifierDecoderTestCase(Ba
             ints2octs((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB6, 0x47))
         ) == ((2, 999, 18446744073709551535184467440737095), null)
 
+    def testManySingleByteArcs(self):
+        encodedArcCount = 4096
+        substrate = (
+            bytes([0x06]) +
+            encode_length(encodedArcCount) +
+            bytes([0x01] * encodedArcCount)
+        )
+
+        value, rest = decoder.decode(substrate)
+        assert rest == b''
+        assert len(value) == encodedArcCount + 1
+        assert tuple(value[:3]) == (0, 1, 1)
+        assert tuple(value[-3:]) == (1, 1, 1)
+
 
 class RealDecoderTestCase(BaseTestCase):
     def testChar(self):
Index: pyasn1-0.5.0/tests/codec/ber/test_encoder.py
===================================================================
--- pyasn1-0.5.0.orig/tests/codec/ber/test_encoder.py
+++ pyasn1-0.5.0/tests/codec/ber/test_encoder.py
@@ -349,6 +349,16 @@ class ObjectIdentifierEncoderTestCase(Ba
         ) == ints2octs((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6,
                         0xB8, 0xCB, 0xE2, 0xB6, 0x47))
 
+    def testManySingleByteArcs(self):
+        arcCount = 4096
+        substrate = encoder.encode(
+            univ.ObjectIdentifier((1, 3) + (1,) * arcCount)
+        )
+
+        assert substrate == (
+            bytes([0x06, 0x82, 0x10, 0x01, 0x2B]) + bytes([0x01] * arcCount)
+        )
+
 
 class ObjectIdentifierWithSchemaEncoderTestCase(BaseTestCase):
     def testOne(self):
