Skip to content

Commit ed8f67c

Browse files
authored
Merge pull request wolfSSL#9858 from JacobBarthelmeh/ticket
additional sanity check with session ticket size
2 parents 8a5c1c7 + e7e1d0e commit ed8f67c

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/internal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39204,6 +39204,10 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
3920439204
WOLFSSL_ERROR_VERBOSE(BAD_TICKET_MSG_SZ);
3920539205
return WOLFSSL_TICKET_RET_REJECT;
3920639206
}
39207+
if ((word32)inLen + WOLFSSL_TICKET_FIXED_SZ > len) {
39208+
WOLFSSL_ERROR_VERBOSE(BAD_TICKET_MSG_SZ);
39209+
return WOLFSSL_TICKET_RET_REJECT;
39210+
}
3920739211
outLen = (int)inLen; /* may be reduced by user padding */
3920839212

3920939213
if (ssl->ctx->ticketEncCb == NULL

tests/api.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28291,6 +28291,122 @@ static int test_ticket_ret_create(void)
2829128291
}
2829228292
#endif
2829328293

28294+
/* Build a valid TLS 1.2 ticket by completing an initial handshake, then tamper
28295+
* with enc_len so it is larger than the true encrypted payload. */
28296+
#if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
28297+
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
28298+
!defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
28299+
!defined(NO_RSA) && defined(HAVE_ECC) && \
28300+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
28301+
28302+
static int test_ticket_enc_corrupted_cb(WOLFSSL* ssl,
28303+
byte key_name[WOLFSSL_TICKET_NAME_SZ], byte iv[WOLFSSL_TICKET_IV_SZ],
28304+
byte mac[WOLFSSL_TICKET_MAC_SZ], int enc, byte* ticket, int inLen,
28305+
int* outLen, void* userCtx)
28306+
{
28307+
(void)ssl;
28308+
(void)key_name;
28309+
(void)iv;
28310+
(void)mac;
28311+
(void)userCtx;
28312+
(void)outLen;
28313+
28314+
if (!enc) {
28315+
XMEMSET(ticket, 0, (size_t)inLen);
28316+
return WOLFSSL_TICKET_RET_REJECT; /* keep handshake progressing */
28317+
}
28318+
return WOLFSSL_TICKET_RET_CREATE;
28319+
}
28320+
28321+
static int test_ticket_enc_corrupted(void)
28322+
{
28323+
EXPECT_DECLS;
28324+
struct test_memio_ctx test_ctx;
28325+
WOLFSSL_CTX* ctx_c = NULL;
28326+
WOLFSSL_CTX* ctx_s = NULL;
28327+
WOLFSSL* ssl_c = NULL;
28328+
WOLFSSL* ssl_s = NULL;
28329+
WOLFSSL_SESSION* sess = NULL;
28330+
ExternalTicket* et;
28331+
word16 encLen;
28332+
int actualEncLen;
28333+
int craftedBadLen = 0;
28334+
28335+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
28336+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
28337+
wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
28338+
wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
28339+
wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
28340+
ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
28341+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
28342+
ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
28343+
if (sess != NULL) {
28344+
ExpectIntGT(sess->ticketLen, WOLFSSL_TICKET_FIXED_SZ);
28345+
28346+
/* Force enc_len to exceed actual encrypted ticket payload while still
28347+
* staying <= WOLFSSL_TICKET_ENC_SZ, so callback is reached. */
28348+
et = (ExternalTicket*)sess->ticket;
28349+
ato16(et->enc_len, &encLen);
28350+
actualEncLen = (int)(sess->ticketLen - WOLFSSL_TICKET_FIXED_SZ);
28351+
if (actualEncLen + 100 <= (int)WOLFSSL_TICKET_ENC_SZ) {
28352+
encLen = (word16)(actualEncLen + 100);
28353+
c16toa(encLen, et->enc_len);
28354+
craftedBadLen = 1;
28355+
}
28356+
else if (actualEncLen + 1 <= (int)WOLFSSL_TICKET_ENC_SZ) {
28357+
encLen = (word16)(actualEncLen + 1);
28358+
c16toa(encLen, et->enc_len);
28359+
craftedBadLen = 1;
28360+
}
28361+
}
28362+
28363+
wolfSSL_free(ssl_c);
28364+
ssl_c = NULL;
28365+
wolfSSL_free(ssl_s);
28366+
ssl_s = NULL;
28367+
test_memio_clear_buffer(&test_ctx, 1);
28368+
test_memio_clear_buffer(&test_ctx, 0);
28369+
28370+
ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
28371+
wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
28372+
wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
28373+
ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
28374+
wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
28375+
wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
28376+
wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
28377+
wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
28378+
if (sess != NULL) {
28379+
if (!craftedBadLen) {
28380+
wolfSSL_SESSION_free(sess);
28381+
wolfSSL_free(ssl_c);
28382+
wolfSSL_free(ssl_s);
28383+
wolfSSL_CTX_free(ctx_c);
28384+
wolfSSL_CTX_free(ctx_s);
28385+
return TEST_SKIPPED;
28386+
}
28387+
ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
28388+
ExpectIntEQ(wolfSSL_CTX_set_TicketEncCb(ctx_s,
28389+
test_ticket_enc_corrupted_cb), WOLFSSL_SUCCESS);
28390+
ExpectTrue((wolfSSL_connect(ssl_c) ==
28391+
WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)) &&
28392+
(ssl_c->error == WC_NO_ERR_TRACE(WANT_READ)));
28393+
ExpectTrue((wolfSSL_accept(ssl_s) ==
28394+
WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)) &&
28395+
(ssl_s->error == WC_NO_ERR_TRACE(WANT_READ)));
28396+
}
28397+
28398+
wolfSSL_SESSION_free(sess);
28399+
wolfSSL_free(ssl_c);
28400+
wolfSSL_free(ssl_s);
28401+
wolfSSL_CTX_free(ctx_c);
28402+
wolfSSL_CTX_free(ctx_s);
28403+
28404+
return EXPECT_RESULT();
28405+
}
28406+
#else
28407+
static int test_ticket_enc_corrupted(void) { return TEST_SKIPPED; }
28408+
#endif
28409+
2829428410
#if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
2829528411
defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
2829628412
defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
@@ -33456,6 +33572,7 @@ TEST_CASE testCases[] = {
3345633572
TEST_DECL(test_ticket_nonce_malloc),
3345733573
#endif
3345833574
TEST_DECL(test_ticket_ret_create),
33575+
TEST_DECL(test_ticket_enc_corrupted),
3345933576
TEST_DECL(test_wrong_cs_downgrade),
3346033577
TEST_DECL(test_extra_alerts_wrong_cs),
3346133578
TEST_DECL(test_extra_alerts_skip_hs),

0 commit comments

Comments
 (0)