Skip to content

Commit 130c70d

Browse files
committed
TLS 1.3, plaintext alert: ignore when expecting encrypted
In TLS 1.3, ignore valid unencrypted alerts that appear after encryption has started. Only ignore WOLFSSL_ALERT_COUNT_MAX-1 alerts.
1 parent 59f4fa5 commit 130c70d

File tree

3 files changed

+189
-36
lines changed

3 files changed

+189
-36
lines changed

src/internal.c

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21679,20 +21679,20 @@ static int DoAlert(WOLFSSL* ssl, byte* input, word32* inOutIdx, int* type)
2167921679
byte code;
2168021680
word32 dataSz = (word32)ssl->curSize;
2168121681

21682-
#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
21683-
if (ssl->hsInfoOn)
21684-
AddPacketName(ssl, "Alert");
21685-
if (ssl->toInfoOn) {
21686-
/* add record header back on to info + alert bytes level/code */
21687-
int ret = AddPacketInfo(ssl, "Alert", alert, input + *inOutIdx,
21688-
ALERT_SIZE, READ_PROTO, RECORD_HEADER_SZ, ssl->heap);
21689-
if (ret != 0)
21690-
return ret;
21691-
#ifdef WOLFSSL_CALLBACKS
21692-
AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo);
21693-
#endif
21694-
}
21695-
#endif
21682+
#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
21683+
if (ssl->hsInfoOn)
21684+
AddPacketName(ssl, "Alert");
21685+
if (ssl->toInfoOn) {
21686+
/* add record header back on to info + alert bytes level/code */
21687+
int ret = AddPacketInfo(ssl, "Alert", alert, input + *inOutIdx,
21688+
ALERT_SIZE, READ_PROTO, RECORD_HEADER_SZ, ssl->heap);
21689+
if (ret != 0)
21690+
return ret;
21691+
#ifdef WOLFSSL_CALLBACKS
21692+
AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo);
21693+
#endif
21694+
}
21695+
#endif
2169621696

2169721697
if (IsEncryptionOn(ssl, 0))
2169821698
dataSz -= ssl->keys.padSz;
@@ -21707,11 +21707,15 @@ static int DoAlert(WOLFSSL* ssl, byte* input, word32* inOutIdx, int* type)
2170721707

2170821708
level = input[(*inOutIdx)++];
2170921709
code = input[(*inOutIdx)++];
21710-
ssl->alert_history.last_rx.code = code;
21711-
ssl->alert_history.last_rx.level = level;
2171221710
*type = code;
21713-
if (level == alert_fatal) {
21714-
ssl->options.isClosed = 1; /* Don't send close_notify */
21711+
/* Don't process alert when TLS 1.3 and encrypting but plaintext alert. */
21712+
if (!IsAtLeastTLSv1_3(ssl->version) || !IsEncryptionOn(ssl, 0) ||
21713+
ssl->keys.decryptedCur) {
21714+
ssl->alert_history.last_rx.code = code;
21715+
ssl->alert_history.last_rx.level = level;
21716+
if (level == alert_fatal) {
21717+
ssl->options.isClosed = 1; /* Don't send close_notify */
21718+
}
2171521719
}
2171621720

2171721721
if (++ssl->options.alertCount >= WOLFSSL_ALERT_COUNT_MAX) {
@@ -21725,20 +21729,27 @@ static int DoAlert(WOLFSSL* ssl, byte* input, word32* inOutIdx, int* type)
2172521729
}
2172621730

2172721731
LogAlert(*type);
21728-
if (*type == close_notify) {
21729-
ssl->options.closeNotify = 1;
21732+
/* Ignore alert if TLS 1.3 and encrypting but was plaintext alert. */
21733+
if (IsAtLeastTLSv1_3(ssl->version) && IsEncryptionOn(ssl, 0) &&
21734+
!ssl->keys.decryptedCur) {
21735+
*type = invalid_alert;
21736+
level = alert_none;
2173021737
}
2173121738
else {
21732-
/*
21733-
* A close_notify alert doesn't mean there's been an error, so we only
21734-
* add other types of alerts to the error queue
21735-
*/
21736-
WOLFSSL_ERROR(*type);
21737-
}
21739+
if (*type == close_notify) {
21740+
ssl->options.closeNotify = 1;
21741+
}
21742+
else {
21743+
/*
21744+
* A close_notify alert doesn't mean there's been an error, so we
21745+
* only add other types of alerts to the error queue
21746+
*/
21747+
WOLFSSL_ERROR(*type);
21748+
}
2173821749

21739-
if (IsEncryptionOn(ssl, 0)) {
21740-
*inOutIdx += ssl->keys.padSz;
2174121750
}
21751+
if (IsEncryptionOn(ssl, 0))
21752+
*inOutIdx += ssl->keys.padSz;
2174221753

2174321754
return level;
2174421755
}
@@ -22489,7 +22500,8 @@ static int DoProcessReplyEx(WOLFSSL* ssl, int allowSocketErr)
2248922500
#ifdef WOLFSSL_TLS13
2249022501
if (IsAtLeastTLSv1_3(ssl->version) && IsEncryptionOn(ssl, 0) &&
2249122502
ssl->curRL.type != application_data &&
22492-
ssl->curRL.type != change_cipher_spec) {
22503+
ssl->curRL.type != change_cipher_spec &&
22504+
ssl->curRL.type != alert) {
2249322505
SendAlert(ssl, alert_fatal, unexpected_message);
2249422506
WOLFSSL_ERROR_VERBOSE(PARSE_ERROR);
2249522507
return PARSE_ERROR;
@@ -22597,9 +22609,9 @@ static int DoProcessReplyEx(WOLFSSL* ssl, int allowSocketErr)
2259722609
case decryptMessage:
2259822610

2259922611
if (IsEncryptionOn(ssl, 0) && ssl->keys.decryptedCur == 0 &&
22600-
(!IsAtLeastTLSv1_3(ssl->version) ||
22601-
ssl->curRL.type != change_cipher_spec))
22602-
{
22612+
(!IsAtLeastTLSv1_3(ssl->version) ||
22613+
(ssl->curRL.type != change_cipher_spec &&
22614+
ssl->curRL.type != alert))) {
2260322615
ret = DoDecrypt(ssl);
2260422616
#ifdef WOLFSSL_ASYNC_CRYPT
2260522617
if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
@@ -22676,9 +22688,9 @@ static int DoProcessReplyEx(WOLFSSL* ssl, int allowSocketErr)
2267622688
case verifyMessage:
2267722689

2267822690
if (IsEncryptionOn(ssl, 0) && ssl->keys.decryptedCur == 0 &&
22679-
(!IsAtLeastTLSv1_3(ssl->version) ||
22680-
ssl->curRL.type != change_cipher_spec))
22681-
{
22691+
(!IsAtLeastTLSv1_3(ssl->version) ||
22692+
(ssl->curRL.type != change_cipher_spec &&
22693+
ssl->curRL.type != alert))) {
2268222694
if (!atomicUser
2268322695
#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
2268422696
&& !ssl->options.startedETMRead

tests/api/test_tls13.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,3 +2318,142 @@ int test_tls13_hrr_different_cs(void)
23182318
#endif
23192319
return EXPECT_RESULT();
23202320
}
2321+
2322+
#if defined(WOLFSSL_TLS13) && !defined(NO_RSA) && defined(HAVE_ECC) && \
2323+
!defined(NO_WOLFSSL_SERVER)
2324+
/* Called when writing. */
2325+
static int MbSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
2326+
{
2327+
(void)ssl;
2328+
(void)buf;
2329+
(void)ctx;
2330+
2331+
return sz;
2332+
}
2333+
/* Called when reading. */
2334+
static int MbRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
2335+
{
2336+
WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
2337+
int len = (int)msg->length;
2338+
2339+
(void)ssl;
2340+
(void)sz;
2341+
2342+
/* Pass back as much of message as will fit in buffer. */
2343+
if (len > sz)
2344+
len = sz;
2345+
XMEMCPY(buf, msg->buffer, len);
2346+
/* Move over returned data. */
2347+
msg->buffer += len;
2348+
msg->length -= len;
2349+
2350+
/* Amount actually copied. */
2351+
return len;
2352+
}
2353+
#endif
2354+
2355+
int test_tls13_plaintext_alert(void)
2356+
{
2357+
EXPECT_DECLS;
2358+
#if defined(WOLFSSL_TLS13) && !defined(NO_RSA) && defined(HAVE_ECC) && \
2359+
!defined(NO_WOLFSSL_SERVER)
2360+
byte clientMsgs[] = {
2361+
/* Client Hello */
2362+
0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
2363+
0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
2364+
0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
2365+
0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
2366+
0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
2367+
0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
2368+
0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
2369+
0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
2370+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2371+
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
2372+
0x13, 0x02, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
2373+
0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
2374+
0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
2375+
0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
2376+
0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
2377+
0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
2378+
0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
2379+
0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
2380+
0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
2381+
0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
2382+
0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
2383+
0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
2384+
0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
2385+
0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
2386+
0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
2387+
0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
2388+
0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
2389+
0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
2390+
0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
2391+
0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
2392+
0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
2393+
0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
2394+
0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
2395+
0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
2396+
0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
2397+
0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
2398+
0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
2399+
0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
2400+
0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
2401+
0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
2402+
0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
2403+
0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
2404+
0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
2405+
0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
2406+
0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
2407+
0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2408+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2409+
0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
2410+
0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
2411+
0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
2412+
0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
2413+
0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c,
2414+
/* Plaintext alert - not allowed once encryption is started. */
2415+
0x15, 0x03, 0x03, 0x00, 0x02, 0x01, 0x00,
2416+
0x15, 0x03, 0x03, 0x00, 0x02, 0x02, 0x0a,
2417+
0x15, 0x03, 0x03, 0x00, 0x02, 0x01, 0x00,
2418+
0x15, 0x03, 0x03, 0x00, 0x02, 0x02, 0x00,
2419+
0x15, 0x03, 0x03, 0x00, 0x02, 0x01, 0x00
2420+
};
2421+
2422+
WOLFSSL_CTX* ctx = NULL;
2423+
WOLFSSL* ssl = NULL;
2424+
WOLFSSL_BUFFER_INFO msg;
2425+
2426+
/* Set up wolfSSL context. */
2427+
ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
2428+
ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
2429+
CERT_FILETYPE));
2430+
ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
2431+
CERT_FILETYPE));
2432+
if (EXPECT_SUCCESS()) {
2433+
wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
2434+
}
2435+
/* Read from 'msg'. */
2436+
wolfSSL_SetIORecv(ctx, MbRecv);
2437+
/* No where to send to - dummy sender. */
2438+
wolfSSL_SetIOSend(ctx, MbSend);
2439+
2440+
ExpectNotNull(ssl = wolfSSL_new(ctx));
2441+
msg.buffer = clientMsgs;
2442+
msg.length = (unsigned int)sizeof(clientMsgs);
2443+
if (EXPECT_SUCCESS()) {
2444+
wolfSSL_SetIOReadCtx(ssl, &msg);
2445+
}
2446+
/* Alert will be ignored until too many. */
2447+
/* Read all message include CertificateVerify with invalid signature
2448+
* algorithm. */
2449+
ExpectIntEQ(wolfSSL_accept(ssl), WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR));
2450+
/* Expect an invalid parameter error. */
2451+
ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FATAL_ERROR),
2452+
WC_NO_ERR_TRACE(ALERT_COUNT_E));
2453+
2454+
wolfSSL_free(ssl);
2455+
wolfSSL_CTX_free(ctx);
2456+
#endif
2457+
return EXPECT_RESULT();
2458+
}
2459+

tests/api/test_tls13.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ 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_plaintext_alert(void);
3536

3637
#define TEST_TLS13_DECLS \
3738
TEST_DECL_GROUP("tls13", test_tls13_apis), \
@@ -41,6 +42,7 @@ int test_tls13_hrr_different_cs(void);
4142
TEST_DECL_GROUP("tls13", test_tls13_pq_groups), \
4243
TEST_DECL_GROUP("tls13", test_tls13_early_data), \
4344
TEST_DECL_GROUP("tls13", test_tls13_same_ch), \
44-
TEST_DECL_GROUP("tls13", test_tls13_hrr_different_cs)
45+
TEST_DECL_GROUP("tls13", test_tls13_hrr_different_cs), \
46+
TEST_DECL_GROUP("tls13", test_tls13_plaintext_alert)
4547

4648
#endif /* WOLFCRYPT_TEST_TLS13_H */

0 commit comments

Comments
 (0)