@@ -1210,7 +1210,7 @@ static int GetASN_ObjectId(const byte* input, word32 idx, int length)
12101210 /* Last octet of a sub-identifier has bit 8 clear. Last octet must be last
12111211 * of a subidentifier. Ensure last octet hasn't got top bit set.
12121212 */
1213- else if ((input[(int)idx + length - 1] & 0x80) != 0x00 ) {
1213+ else if ((input[(int)idx + length - 1] & 0x80) == 0x80 ) {
12141214 WOLFSSL_MSG("OID last octet has top bit set");
12151215 ret = ASN_PARSE_E;
12161216 }
@@ -2430,6 +2430,19 @@ static int GetASNHeader_ex(const byte* input, byte tag, word32* inOutIdx,
24302430 if ((ret == 0) && (GetLength_ex(input, &idx, &length, maxIdx, check) < 0)) {
24312431 ret = ASN_PARSE_E;
24322432 }
2433+ if (ret == 0 && tag == ASN_OBJECT_ID) {
2434+ if (length < 3) {
2435+ /* OID data must be at least 3 bytes. */
2436+ WOLFSSL_MSG("OID length less than 3");
2437+ ret = ASN_PARSE_E;
2438+ }
2439+ else if ((input[(int)idx + length - 1] & 0x80) == 0x80) {
2440+ /* Last octet of a sub-identifier has bit 8 clear. Last octet must be
2441+ * last of a subidentifier. Ensure last octet hasn't got top bit set. */
2442+ WOLFSSL_MSG("OID last octet has top bit set");
2443+ ret = ASN_PARSE_E;
2444+ }
2445+ }
24332446 if (ret == 0) {
24342447 /* Return the length of data and index after header. */
24352448 *len = length;
@@ -2691,14 +2704,15 @@ int GetASNInt(const byte* input, word32* inOutIdx, int* len,
26912704 return ret;
26922705
26932706 if (*len > 0) {
2694-
26952707#ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
26962708 /* check for invalid padding on negative integer.
26972709 * c.f. X.690 (ISO/IEC 8825-2:2003 (E)) 10.4.6; RFC 5280 4.1
26982710 */
26992711 if (*len > 1) {
2700- if ((input[*inOutIdx] == 0xff) && (input[*inOutIdx + 1] & 0x80))
2701- return ASN_PARSE_E;
2712+ if ((input[*inOutIdx] == 0xff) && (input[*inOutIdx + 1] & 0x80)) {
2713+ WOLFSSL_MSG("Bad INTEGER encoding of negative");
2714+ return ASN_EXPECT_0_E;
2715+ }
27022716 }
27032717#endif
27042718
@@ -2708,8 +2722,10 @@ int GetASNInt(const byte* input, word32* inOutIdx, int* len,
27082722 (*len)--;
27092723
27102724#ifndef WOLFSSL_ASN_INT_LEAD_0_ANY
2711- if (*len > 0 && (input[*inOutIdx] & 0x80) == 0)
2712- return ASN_PARSE_E;
2725+ if (*len > 0 && (input[*inOutIdx] & 0x80) == 0) {
2726+ WOLFSSL_MSG("INTEGER is negative");
2727+ return ASN_EXPECT_0_E;
2728+ }
27132729#endif
27142730 }
27152731 }
@@ -3474,7 +3490,7 @@ int CheckBitString(const byte* input, word32* inOutIdx, int* len,
34743490 }
34753491
34763492 b = input[idx];
3477- if (zeroBits && b != 0x00)
3493+ if (zeroBits && ( b != 0x00) )
34783494 return ASN_EXPECT_0_E;
34793495 if (b >= 0x08)
34803496 return ASN_PARSE_E;
@@ -6920,7 +6936,7 @@ int ToTraditionalInline_ex2(const byte* input, word32* inOutIdx, word32 sz,
69206936
69216937 if (tag == ASN_OBJECT_ID) {
69226938 if ((*algId == ECDSAk) && (eccOid != NULL)) {
6923- if (GetObjectId(input, &idx, eccOid, oidCurveType, maxIdx ) < 0)
6939+ if (GetObjectId(input, &idx, eccOid, oidCurveType, sz ) < 0)
69246940 return ASN_PARSE_E;
69256941 }
69266942 else {
@@ -11572,9 +11588,11 @@ static int GetCertHeader(DecodedCert* cert)
1157211588 cert->sigIndex) < 0)
1157311589 return ASN_PARSE_E;
1157411590
11575- if (wc_GetSerialNumber(cert->source, &cert->srcIdx, cert->serial,
11576- &cert->serialSz, cert->sigIndex) < 0)
11577- return ASN_PARSE_E;
11591+ ret = wc_GetSerialNumber(cert->source, &cert->srcIdx, cert->serial,
11592+ &cert->serialSz, cert->sigIndex);
11593+ if (ret < 0) {
11594+ return ret;
11595+ }
1157811596
1157911597 return ret;
1158011598}
@@ -18590,6 +18608,7 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
1859018608#ifndef WOLFSSL_ASN_TEMPLATE
1859118609 word32 idx = 0;
1859218610 int length = 0;
18611+ word32 numNames = 0;
1859318612
1859418613 WOLFSSL_ENTER("DecodeAltNames");
1859518614
@@ -18622,8 +18641,13 @@ static int DecodeAltNames(const byte* input, word32 sz, DecodedCert* cert)
1862218641 return BUFFER_E;
1862318642 }
1862418643
18625- current_byte = input[idx++];
18644+ numNames++;
18645+ if (numNames > WOLFSSL_MAX_ALT_NAMES) {
18646+ WOLFSSL_MSG("\tToo many subject alternative names");
18647+ return ASN_ALT_NAME_E;
18648+ }
1862618649
18650+ current_byte = input[idx++];
1862718651 length--;
1862818652
1862918653 /* Save DNS Type names in the altNames list. */
@@ -20153,6 +20177,7 @@ static int DecodeSubtree(const byte* input, word32 sz, Base_entry** head,
2015320177#ifndef WOLFSSL_ASN_TEMPLATE
2015420178 word32 idx = 0;
2015520179 int ret = 0;
20180+ word32 cnt = 0;
2015620181
2015720182 (void)heap;
2015820183
@@ -20161,6 +20186,14 @@ static int DecodeSubtree(const byte* input, word32 sz, Base_entry** head,
2016120186 word32 nameIdx;
2016220187 byte b, bType;
2016320188
20189+ if (limit > 0) {
20190+ cnt++;
20191+ if (cnt > limit) {
20192+ WOLFSSL_MSG("too many name constraints");
20193+ return ASN_NAME_INVALID_E;
20194+ }
20195+ }
20196+
2016420197 if (GetSequence(input, &idx, &seqLength, sz) < 0) {
2016520198 WOLFSSL_MSG("\tfail: should be a SEQUENCE");
2016620199 return ASN_PARSE_E;
0 commit comments