ASN.1 DER decoding of lengths is too permissive, allowing undetected smuggling of arbitrary data
Originally disclosed on Mozilla
- Fixed in 2 months
- Paid in 35 days
- Disclosed after 2 months
Created attachment 8486180
Test demonstrating the problem (GTest)
+++ This bug was initially created as a clone of Bug #1064636 +++
In some contexts, such as when decoding the AlgorithmIdentifier within a PKCS#1 signature, it is critical that we minimize the variance of possible encodings that are accepted when we parse a DER-encoded data stream. Usually "minimize the variance" means "only accept the legal DER encoding of the data, and reject any overly-long encodings or any other variant." In some contexts, failing to restrict variance in accepted encodings completely undoes the cryptography. See bug 1064636, bug 351079, and bug 350640, for example.
See the attached test, where I demonstrate that SEC_QuickDERDecodeItem (a.k.a. QuickDER) accepts many malformed encodings of ASN.1 lengths. Note, in particular the test case where the length of the NULL item is encoded as { 0x90, <arbitrary junk>, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, which is wrongly interpreted by QuickDER as equivalent to { 0x00 } on both 32-bit and 64-bit platforms. I believe it is likely that this would facilitate the same types of attacks as described in the aforementioned bugs.
In quickder.c, the function definite_length_decoder is used to decode tags and lengths. It decodes the length as follows:
unsigned int data_len;
// [...]
data_len = buf[used_length++];
if (data_len&0x80)
{
int len_count = data_len & 0x7f;
data_len = 0;
while (len_count-- > 0)
{
if (used_length >= length)
{
return NULL;
}
data_len = (data_len << 8) | buf[used_length++];
}
}
Note that there is no check that the encoding of the length is the shortest necssary encoding. And also note the integer overflow of data_len.
In the case of PKCS#1 (i.e. the aforementioned bugs), we shouldn't even be doing DER decoding in the first place. However, it isn't obvious to me yet what contexts besides PKCS#1 are also problematic, so it prudent to fix QuickDER, regardless of the fix for the PKCS#1 decoder in bug 1064636.
The attached GTest integrates into Gecko's mozilla::pkix test suite, for my own convenience. However, it doesn't use any of the mozilla::pkix or Gecko GTest utilities, so it should be easy to integrate it into other GTest-based test suites.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A critical-severity security bug in Libraries. Mozilla made it public on 8 November 2014 as CVE-2014-1569.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.