Skip to content

Commit b1765ca

Browse files
authored
Merge pull request #7785 from dgarske/asn_original
Fixes for ASN original
2 parents 5e58aff + c4f73f5 commit b1765ca

4 files changed

Lines changed: 53 additions & 18 deletions

File tree

configure.ac

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,10 +4762,10 @@ else
47624762
fi
47634763
if test "$ENABLED_ASN" = "yes"; then
47644764
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_ASN_TEMPLATE"
4765+
elif test "$ENABLED_ASN" = "original"; then
4766+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_ASN_ORIGINAL"
47654767
else
4766-
if test "$ENABLED_ASN" != "original"; then
4767-
AC_MSG_ERROR([Invalid asn option. Valid are: template or original. Seen: $ENABLED_ASN.])
4768-
fi
4768+
AC_MSG_ERROR([Invalid asn option. Valid are: template or original. Seen: $ENABLED_ASN.])
47694769
fi
47704770

47714771
# turn off ASN if leanpsk on

tests/suites.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,9 @@ int SuiteTest(int argc, char** argv)
10601060
#if defined(HAVE_ECC) && !defined(NO_SHA256) && defined(WOLFSSL_CUSTOM_CURVES) && \
10611061
defined(HAVE_ECC_KOBLITZ) && defined(HAVE_ECC_BRAINPOOL) && \
10621062
/* Intel QuickAssist and Cavium Nitrox do not support custom curves */ \
1063-
!defined(HAVE_INTEL_QA) && !defined(HAVE_CAVIUM_V)
1063+
!defined(HAVE_INTEL_QA) && !defined(HAVE_CAVIUM_V) && \
1064+
/* only supported with newer ASN template code */ \
1065+
defined(WOLFSSL_ASN_TEMPLATE)
10641066

10651067
/* TLS non-NIST curves (Koblitz / Brainpool) */
10661068
XSTRLCPY(argv0[1], "tests/test-ecc-cust-curves.conf", sizeof(argv0[1]));

wolfcrypt/src/asn.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

wolfcrypt/test/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18078,7 +18078,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
1807818078
#endif
1807918079
static const char* certBadOid =
1808018080
CERT_ROOT "test" CERT_PATH_SEP "cert-bad-oid.der";
18081-
#ifndef WOLFSSL_NO_ASN_STRICT
18081+
#if defined(WOLFSSL_ASN_TEMPLATE) && !defined(WOLFSSL_NO_ASN_STRICT)
1808218082
static const char* certBadUtf8 =
1808318083
CERT_ROOT "test" CERT_PATH_SEP "cert-bad-utf8.der";
1808418084
#endif
@@ -18383,7 +18383,7 @@ static wc_test_ret_t cert_bad_asn1_test(void)
1838318383
/* Subject name OID: 55 04 f4. Last byte with top bit set invalid. */
1838418384
ret = cert_load_bad(certBadOid, tmp, ASN_PARSE_E);
1838518385
}
18386-
#ifndef WOLFSSL_NO_ASN_STRICT
18386+
#if defined(WOLFSSL_ASN_TEMPLATE) && !defined(WOLFSSL_NO_ASN_STRICT)
1838718387
if (ret == 0) {
1838818388
/* Issuer name UTF8STRING: df 52 4e 44. Top bit of second byte not set.
1838918389
*/

0 commit comments

Comments
 (0)