Skip to content

Commit 52ee001

Browse files
authored
Merge pull request #9528 from SparkiDev/tls13_missing_ext_fix
TLS 1.3 missing extension: return correct alert code
2 parents 901ddab + 44be44a commit 52ee001

File tree

3 files changed

+178
-6
lines changed

3 files changed

+178
-6
lines changed

src/tls.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16567,15 +16567,18 @@ int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
1656716567
* contain SupportedGroups and vice-versa. */
1656816568
if (IsAtLeastTLSv1_3(ssl->version) && msgType == client_hello && isRequest) {
1656916569
int hasKeyShare = !IS_OFF(seenType, TLSX_ToSemaphore(TLSX_KEY_SHARE));
16570-
int hasSupportedGroups = !IS_OFF(seenType, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
16570+
int hasSupportedGroups = !IS_OFF(seenType,
16571+
TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
1657116572

1657216573
if (hasKeyShare && !hasSupportedGroups) {
16573-
WOLFSSL_MSG("ClientHello with KeyShare extension missing required SupportedGroups extension");
16574-
return MISSING_HANDSHAKE_DATA;
16574+
WOLFSSL_MSG("ClientHello with KeyShare extension missing required "
16575+
"SupportedGroups extension");
16576+
return INCOMPLETE_DATA;
1657516577
}
1657616578
if (hasSupportedGroups && !hasKeyShare) {
16577-
WOLFSSL_MSG("ClientHello with SupportedGroups extension missing required KeyShare extension");
16578-
return MISSING_HANDSHAKE_DATA;
16579+
WOLFSSL_MSG("ClientHello with SupportedGroups extension missing "
16580+
"required KeyShare extension");
16581+
return INCOMPLETE_DATA;
1657916582
}
1658016583
}
1658116584
#endif

tests/api/test_tls13.c

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,3 +2318,168 @@ int test_tls13_hrr_different_cs(void)
23182318
#endif
23192319
return EXPECT_RESULT();
23202320
}
2321+
2322+
#if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER) && \
2323+
defined(HAVE_ECC)
2324+
/* Called when writing. */
2325+
static int MESend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
2326+
{
2327+
(void)ssl;
2328+
(void)buf;
2329+
(void)sz;
2330+
(void)ctx;
2331+
2332+
/* Force error return from wolfSSL_accept_TLSv13(). */
2333+
return WANT_WRITE;
2334+
}
2335+
/* Called when reading. */
2336+
static int MERecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
2337+
{
2338+
WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
2339+
int len = (int)msg->length;
2340+
2341+
(void)ssl;
2342+
(void)sz;
2343+
2344+
/* Pass back as much of message as will fit in buffer. */
2345+
if (len > sz)
2346+
len = sz;
2347+
XMEMCPY(buf, msg->buffer, len);
2348+
/* Move over returned data. */
2349+
msg->buffer += len;
2350+
msg->length -= len;
2351+
2352+
/* Amount actually copied. */
2353+
return len;
2354+
}
2355+
#endif
2356+
2357+
int test_tls13_sg_missing(void)
2358+
{
2359+
EXPECT_DECLS;
2360+
#if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER) && \
2361+
defined(HAVE_ECC)
2362+
WOLFSSL_CTX *ctx = NULL;
2363+
WOLFSSL *ssl = NULL;
2364+
byte clientHello[] = {
2365+
0x16, 0x03, 0x03, 0x00, 0xcb, 0x01, 0x00, 0x00,
2366+
0xc7, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
2367+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2368+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2369+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2370+
0x01, 0x01, 0x01, 0x20, 0x03, 0x03, 0x03, 0x03,
2371+
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
2372+
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
2373+
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
2374+
0x03, 0x03, 0x03, 0x03, 0x00, 0x02, 0x13, 0x01,
2375+
0x01, 0x00, 0x00, 0x7c, 0x00, 0x0d, 0x00, 0x06,
2376+
0x00, 0x04, 0x04, 0x01, 0x08, 0x04,
2377+
/* KeyShare */
2378+
0x00, 0x33,
2379+
0x00, 0x67, 0x00, 0x65, 0x00, 0x18, 0x00, 0x61,
2380+
0x04, 0x53, 0x3e, 0xe5, 0xbf, 0x40, 0xec, 0x2d,
2381+
0x67, 0x98, 0x8b, 0x77, 0xf3, 0x17, 0x48, 0x9b,
2382+
0xb6, 0xdf, 0x95, 0x29, 0x25, 0xc7, 0x09, 0xfc,
2383+
0x03, 0x81, 0x11, 0x1a, 0x59, 0x56, 0xf2, 0xd7,
2384+
0x58, 0x11, 0x0e, 0x59, 0xd3, 0xd7, 0xc1, 0x72,
2385+
0x9e, 0x2c, 0x0d, 0x70, 0xea, 0xf7, 0x73, 0xe6,
2386+
0x12, 0x01, 0x16, 0x42, 0x6d, 0xe2, 0x43, 0x6a,
2387+
0x2f, 0x5f, 0xdd, 0x7f, 0xe5, 0x4f, 0xaf, 0x95,
2388+
0x2b, 0x04, 0xfd, 0x13, 0xf5, 0x16, 0xce, 0x62,
2389+
0x7f, 0x89, 0xd2, 0x01, 0x9d, 0x4c, 0x87, 0x96,
2390+
0x95, 0x9e, 0x43, 0x33, 0xc7, 0x06, 0x5b, 0x49,
2391+
0x6c, 0xa6, 0x34, 0xd5, 0xdc, 0x63, 0xbd, 0xe9,
2392+
0x1f,
2393+
/* SupportedVersions */
2394+
0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04
2395+
/* Missing SupportedGroups. */
2396+
};
2397+
WOLFSSL_BUFFER_INFO msg;
2398+
WOLFSSL_ALERT_HISTORY h;
2399+
2400+
/* Set up wolfSSL context. */
2401+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
2402+
ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
2403+
CERT_FILETYPE));
2404+
ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
2405+
CERT_FILETYPE));
2406+
/* Read from 'msg'. */
2407+
wolfSSL_SetIORecv(ctx, MERecv);
2408+
/* No where to send to - dummy sender. */
2409+
wolfSSL_SetIOSend(ctx, MESend);
2410+
2411+
/* Test cipher suite list with many copies of a cipher suite. */
2412+
ExpectNotNull(ssl = wolfSSL_new(ctx));
2413+
msg.buffer = clientHello;
2414+
msg.length = (unsigned int)sizeof(clientHello);
2415+
wolfSSL_SetIOReadCtx(ssl, &msg);
2416+
2417+
ExpectIntEQ(wolfSSL_accept_TLSv13(ssl),
2418+
WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
2419+
ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
2420+
ExpectIntEQ(h.last_tx.code, missing_extension);
2421+
ExpectIntEQ(h.last_tx.level, alert_fatal);
2422+
wolfSSL_free(ssl);
2423+
wolfSSL_CTX_free(ctx);
2424+
#endif
2425+
return EXPECT_RESULT();
2426+
}
2427+
2428+
int test_tls13_ks_missing(void)
2429+
{
2430+
EXPECT_DECLS;
2431+
#if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER) && \
2432+
defined(HAVE_ECC)
2433+
WOLFSSL_CTX *ctx = NULL;
2434+
WOLFSSL *ssl = NULL;
2435+
byte clientHello[] = {
2436+
0x16, 0x03, 0x03, 0x00, 0x66, 0x01, 0x00, 0x00,
2437+
0x62, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
2438+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2439+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2440+
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2441+
0x01, 0x01, 0x01, 0x20, 0x03, 0x03, 0x03, 0x03,
2442+
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
2443+
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
2444+
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
2445+
0x03, 0x03, 0x03, 0x03, 0x00, 0x02, 0x13, 0x01,
2446+
0x01, 0x00, 0x00, 0x17, 0x00, 0x0d, 0x00, 0x06,
2447+
0x00, 0x04, 0x04, 0x01, 0x08, 0x04,
2448+
/* SupportedGroups */
2449+
0x00, 0x0a,
2450+
0x00, 0x02, 0x00, 0x18,
2451+
/* SupportedVersions */
2452+
0x00, 0x2b, 0x00, 0x03,
2453+
0x02, 0x03, 0x04
2454+
/* Missing KeyShare. */
2455+
};
2456+
WOLFSSL_BUFFER_INFO msg;
2457+
WOLFSSL_ALERT_HISTORY h;
2458+
2459+
/* Set up wolfSSL context. */
2460+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
2461+
ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
2462+
CERT_FILETYPE));
2463+
ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
2464+
CERT_FILETYPE));
2465+
/* Read from 'msg'. */
2466+
wolfSSL_SetIORecv(ctx, MERecv);
2467+
/* No where to send to - dummy sender. */
2468+
wolfSSL_SetIOSend(ctx, MESend);
2469+
2470+
/* Test cipher suite list with many copies of a cipher suite. */
2471+
ExpectNotNull(ssl = wolfSSL_new(ctx));
2472+
msg.buffer = clientHello;
2473+
msg.length = (unsigned int)sizeof(clientHello);
2474+
wolfSSL_SetIOReadCtx(ssl, &msg);
2475+
2476+
ExpectIntEQ(wolfSSL_accept_TLSv13(ssl),
2477+
WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
2478+
ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
2479+
ExpectIntEQ(h.last_tx.code, missing_extension);
2480+
ExpectIntEQ(h.last_tx.level, alert_fatal);
2481+
wolfSSL_free(ssl);
2482+
wolfSSL_CTX_free(ctx);
2483+
#endif
2484+
return EXPECT_RESULT();
2485+
}

tests/api/test_tls13.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ int test_tls13_pq_groups(void);
3232
int test_tls13_early_data(void);
3333
int test_tls13_same_ch(void);
3434
int test_tls13_hrr_different_cs(void);
35+
int test_tls13_sg_missing(void);
36+
int test_tls13_ks_missing(void);
3537

3638
#define TEST_TLS13_DECLS \
3739
TEST_DECL_GROUP("tls13", test_tls13_apis), \
@@ -41,6 +43,8 @@ int test_tls13_hrr_different_cs(void);
4143
TEST_DECL_GROUP("tls13", test_tls13_pq_groups), \
4244
TEST_DECL_GROUP("tls13", test_tls13_early_data), \
4345
TEST_DECL_GROUP("tls13", test_tls13_same_ch), \
44-
TEST_DECL_GROUP("tls13", test_tls13_hrr_different_cs)
46+
TEST_DECL_GROUP("tls13", test_tls13_hrr_different_cs), \
47+
TEST_DECL_GROUP("tls13", test_tls13_sg_missing), \
48+
TEST_DECL_GROUP("tls13", test_tls13_ks_missing)
4549

4650
#endif /* WOLFCRYPT_TEST_TLS13_H */

0 commit comments

Comments
 (0)