Details
import time
from pyasn1.codec.ber import decoder as ber
from pyasn1.codec.cer import decoder as cer
from pyasn1.codec.der import decoder as der
from pyasn1.type import univ
def tlv(identifier, contents):
length = len(contents)
if length < 128:
return bytes((identifier, length)) + contents
encoded = length.to_bytes((length.bit_length() + 7) // 8, "big")
return bytes((identifier, 0x80 | len(encoded))) + encoded + contents
def measure(label, decode, substrate, asn1Spec=None):
start = time.process_time()
_, rest = decode(substrate, asn1Spec=asn1Spec)
assert not rest
print(f"{label}: {len(substrate)} input bytes, "
f"{time.process_time() - start:.3f} CPU seconds")
n = 100_000
octet_fragments = b"\x04\x02AA" * n
utf8_fragments = b"\x0c\x02AA" * n
measure("BER constructed OCTET STRING", ber.decode,
tlv(0x24, octet_fragments))
measure("BER indefinite ANY", ber.decode,
b"\x30\x80" + b"\x04\x01A" * n + b"\x00\x00",
univ.Any())
# CER uses indefinite length and 1,000-content-octet primitive fragments.
cer_fragment = tlv(0x04, b"A" * 1000)
measure("CER constructed OCTET STRING", cer.decode,
b"\x24\x80" + cer_fragment * 1000 + b"\x00\x00")
# These constructed encodings are accepted by pyasn1 but are invalid DER.
measure("DER OCTET STRING with schema", der.decode,
tlv(0x24, octet_fragments), univ.OctetString())
measure("DER UTF8String without schema", der.decode,
tlv(0x2c, utf8_fragments))
Patch
pyasn1 0.6.5 replaces the growing bytes accumulator with a list of fragments and joins them once after decoding in all three affected loops. The OCTET STRING base-class change also covers the inheriting character-string decoders. This removes the repeated copying while preserving the assembled decoded value.
Workaround
Until upgrading, enforce application-appropriate limits on the size of untrusted ASN.1 input *before* passing it to pyasn1, and limit the CPU resources available to decoding workers. Applications that require DER can also reject constructed string encodings before invoking the decoder. The appropriate byte limit depends on the protocol and expected valid values.
Credits and references
Reported by alimony. Independently reported by manus-pi, whose report identified the schema-driven DER route, inherited character-string decoders, and additional performance evidence.