Skip to content

Commit a4c4740

Browse files
additional sanity check with session ticket size
1 parent 350706d commit a4c4740

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

src/internal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39181,6 +39181,10 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
3918139181
WOLFSSL_ERROR_VERBOSE(BAD_TICKET_MSG_SZ);
3918239182
return WOLFSSL_TICKET_RET_REJECT;
3918339183
}
39184+
if ((word32)inLen + WOLFSSL_TICKET_FIXED_SZ > len) {
39185+
WOLFSSL_ERROR_VERBOSE(BAD_TICKET_MSG_SZ);
39186+
return WOLFSSL_TICKET_RET_REJECT;
39187+
}
3918439188
outLen = (int)inLen; /* may be reduced by user padding */
3918539189

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

tests/api.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28083,6 +28083,103 @@ static int test_ticket_ret_create(void)
2808328083
}
2808428084
#endif
2808528085

28086+
/* Build a valid TLS 1.2 ticket by completing an initial handshake, then tamper
28087+
* with enc_len so it is larger than the true encrypted payload. */
28088+
#if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
28089+
!defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
28090+
!defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
28091+
!defined(NO_RSA) && defined(HAVE_ECC) && \
28092+
defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
28093+
28094+
static int test_ticket_enc_corrupted_cb(WOLFSSL* ssl,
28095+
byte key_name[WOLFSSL_TICKET_NAME_SZ], byte iv[WOLFSSL_TICKET_IV_SZ],
28096+
byte mac[WOLFSSL_TICKET_MAC_SZ], int enc, byte* ticket, int inLen,
28097+
int* outLen, void* userCtx)
28098+
{
28099+
(void)ssl;
28100+
(void)key_name;
28101+
(void)iv;
28102+
(void)mac;
28103+
(void)userCtx;
28104+
(void)outLen;
28105+
28106+
if (!enc) {
28107+
XMEMSET(ticket, 0, (size_t)inLen);
28108+
return WOLFSSL_TICKET_RET_REJECT; /* keep handshake progressing */
28109+
}
28110+
return WOLFSSL_TICKET_RET_CREATE;
28111+
}
28112+
28113+
static int test_ticket_enc_corrupted(void)
28114+
{
28115+
EXPECT_DECLS;
28116+
struct test_memio_ctx test_ctx;
28117+
WOLFSSL_CTX* ctx_c = NULL;
28118+
WOLFSSL_CTX* ctx_s = NULL;
28119+
WOLFSSL* ssl_c = NULL;
28120+
WOLFSSL* ssl_s = NULL;
28121+
WOLFSSL_SESSION* sess = NULL;
28122+
ExternalTicket* et;
28123+
word16 encLen;
28124+
28125+
XMEMSET(&test_ctx, 0, sizeof(test_ctx));
28126+
ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
28127+
wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
28128+
wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
28129+
wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
28130+
ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
28131+
ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
28132+
ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
28133+
if (sess != NULL) {
28134+
ExpectIntGT(sess->ticketLen, WOLFSSL_TICKET_FIXED_SZ);
28135+
28136+
/* Force enc_len to exceed actual encrypted ticket payload */
28137+
et = (ExternalTicket*)sess->ticket;
28138+
ato16(et->enc_len, &encLen);
28139+
encLen = (word16)(sess->ticketLen - WOLFSSL_TICKET_FIXED_SZ + 100);
28140+
ExpectIntLE((int)encLen, (int)WOLFSSL_TICKET_ENC_SZ);
28141+
c16toa(encLen, et->enc_len);
28142+
}
28143+
28144+
wolfSSL_free(ssl_c);
28145+
ssl_c = NULL;
28146+
wolfSSL_free(ssl_s);
28147+
ssl_s = NULL;
28148+
test_memio_clear_buffer(&test_ctx, 1);
28149+
test_memio_clear_buffer(&test_ctx, 0);
28150+
28151+
ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
28152+
wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
28153+
wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
28154+
ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
28155+
wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
28156+
wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
28157+
wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
28158+
wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
28159+
if (sess != NULL) {
28160+
ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
28161+
ExpectIntEQ(wolfSSL_CTX_set_TicketEncCb(ctx_s,
28162+
test_ticket_enc_corrupted_cb), WOLFSSL_SUCCESS);
28163+
ExpectTrue((wolfSSL_connect(ssl_c) ==
28164+
WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)) &&
28165+
(ssl_c->error == WC_NO_ERR_TRACE(WANT_READ)));
28166+
ExpectTrue((wolfSSL_accept(ssl_s) ==
28167+
WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR)) &&
28168+
(ssl_s->error == WC_NO_ERR_TRACE(WANT_READ)));
28169+
}
28170+
28171+
wolfSSL_SESSION_free(sess);
28172+
wolfSSL_free(ssl_c);
28173+
wolfSSL_free(ssl_s);
28174+
wolfSSL_CTX_free(ctx_c);
28175+
wolfSSL_CTX_free(ctx_s);
28176+
28177+
return EXPECT_RESULT();
28178+
}
28179+
#else
28180+
static int test_ticket_enc_corrupted(void) { return TEST_SKIPPED; }
28181+
#endif
28182+
2808628183
#if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
2808728184
defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
2808828185
defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
@@ -33238,6 +33335,7 @@ TEST_CASE testCases[] = {
3323833335
TEST_DECL(test_ticket_nonce_malloc),
3323933336
#endif
3324033337
TEST_DECL(test_ticket_ret_create),
33338+
TEST_DECL(test_ticket_enc_corrupted),
3324133339
TEST_DECL(test_wrong_cs_downgrade),
3324233340
TEST_DECL(test_extra_alerts_wrong_cs),
3324333341
TEST_DECL(test_extra_alerts_skip_hs),

0 commit comments

Comments
 (0)