Skip to content

Reject BIT STRINGs whose length exceeds the buffer - #393

Open
0xrichardlu wants to merge 1 commit into
tlsfuzzer:masterfrom
0xrichardlu:fix/bitstring-length-check
Open

0xrichardlu wants to merge 1 commit into
tlsfuzzer:masterfrom
0xrichardlu:fix/bitstring-length-check

Conversation

@0xrichardlu

Copy link
Copy Markdown

remove_bitstring() reads the declared length and then slices string[1 + llen : 1 + llen + length] without checking that the buffer is that long. remove_sequence, remove_octet_string, remove_integer, remove_constructed and the rest all raise UnexpectedDER("Length longer than the provided buffer") in this case. remove_bitstring didn't.

As a result, a SubjectPublicKeyInfo whose BIT STRING claims more bytes than are present is accepted:

from ecdsa import SigningKey, VerifyingKey, NIST256p
vk = SigningKey.from_secret_exponent(12345, NIST256p).verifying_key
d = bytearray(vk.to_der()); i = d.index(b"\x03\x42\x00\x04")
d[i + 1] = 0x7f          # BIT STRING now claims 127 bytes, only 66 present
VerifyingKey.from_der(bytes(d)) == vk     # True, no error

cryptography's load_der_public_key rejects the same bytes ("short data"). Separately, remove_bitstring(b"\x03\x01", 0) raised IndexError rather than UnexpectedDER.

The fix adds the same length check the other helpers use. There are two new tests in test_der.py, and both fail without the change. test_der.py and test_keys.py pass (274 tests).

remove_bitstring() sliced the body without checking the declared length,
so a truncated BIT STRING was silently accepted (VerifyingKey.from_der()
returned the key) and b'\x03\x01' raised IndexError instead of
UnexpectedDER. The other remove_* helpers already check this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant