Skip to content

Commit b874f71

Browse files
authored
Merge pull request wolfSSL#11295 from julek-wolfssl/tls13-0rtt-single-use-bound
TLS 1.3: gate 0-RTT on a session cache hit
2 parents bc808cb + 72222b8 commit b874f71

8 files changed

Lines changed: 588 additions & 16 deletions

File tree

‎src/internal.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44214,12 +44214,22 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
4421444214
/* Restore the fake ID */
4421544215
XMEMCPY(ssl->session->sessionID, bogusID, ID_LEN);
4421644216
ssl->session->sessionIDSz= bogusIDSz;
44217+
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_EARLY_DATA) && \
44218+
!defined(NO_SESSION_CACHE)
44219+
/* A stateful ticket only resumes on a cache hit. RFC 8446 Sect. 8:
44220+
* CheckPreSharedKeys() accepts 0-RTT once, then evicts. */
44221+
ssl->options.ticketCacheHit = 1;
44222+
#endif
4421744223
}
4421844224
#ifdef WOLFSSL_TICKET_HAVE_ID
4421944225
else {
4422044226
if (wolfSSL_GetSession(ssl, NULL, 1) != NULL) {
4422144227
WOLFSSL_MSG("Found session matching the session id"
4422244228
" found in the ticket");
44229+
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_EARLY_DATA) && \
44230+
!defined(NO_SESSION_CACHE)
44231+
ssl->options.ticketCacheHit = 1;
44232+
#endif
4422344233
}
4422444234
else {
4422544235
WOLFSSL_MSG("Can't find session matching the session id"

‎src/ssl.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5931,6 +5931,9 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out,
59315931
ssl->options.pskKeModes = 0;
59325932
ssl->options.pskKeModesRecvd = 0;
59335933
#endif
5934+
#if defined(WOLFSSL_EARLY_DATA) && !defined(NO_SESSION_CACHE)
5935+
ssl->options.ticketCacheHit = 0;
5936+
#endif
59345937
#endif
59355938
ssl->options.rejectTicket = 0;
59365939
#endif

‎src/ssl_sess.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,90 @@ WOLFSSL_SESSION* wolfSSL_GetSession(WOLFSSL* ssl, byte* masterSecret,
26222622

26232623
#endif /* NO_SESSION_CACHE */
26242624

2625+
#if !defined(NO_TLS) && defined(WOLFSSL_TLS13) && \
2626+
defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
2627+
#if defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
2628+
/* Does freeing a session's ex_data call anything? Only then can two cache
2629+
* entries holding the same ex_data free it twice. */
2630+
static int SessionExDataHasFree(void)
2631+
{
2632+
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
2633+
return 1;
2634+
#elif defined(HAVE_EX_DATA_CRYPTO)
2635+
const CRYPTO_EX_cb_ctx* cb;
2636+
2637+
for (cb = crypto_ex_cb_ctx_session; cb != NULL; cb = cb->next) {
2638+
if (cb->free_func != NULL)
2639+
return 1;
2640+
}
2641+
return 0;
2642+
#else
2643+
return 0;
2644+
#endif
2645+
}
2646+
2647+
/* Move ex_data ownership from the cache entry of ssl->session to the
2648+
* session. When the entry is gone or no longer owns it, another owner holds
2649+
* the ex_data, so drop the stale copy and own an empty one. */
2650+
static void SessionTakeExData(WOLFSSL* ssl)
2651+
{
2652+
WOLFSSL_SESSION* session = ssl->session;
2653+
WOLFSSL_SESSION* sess = NULL;
2654+
const byte* id;
2655+
word32 row = 0;
2656+
int taken = 0;
2657+
2658+
if (session->ownExData || SslSessionCacheOff(ssl, session) ||
2659+
!SessionExDataHasFree()) {
2660+
return;
2661+
}
2662+
#ifdef HAVE_EXT_CACHE
2663+
if (ssl->options.internalCacheOff)
2664+
return;
2665+
#endif
2666+
2667+
id = session->sessionID;
2668+
if (session->haveAltSessionID)
2669+
id = session->altSessionID;
2670+
2671+
if (TlsSessionCacheGetAndWrLock(id, &sess, &row, ssl->options.side) == 0 &&
2672+
sess != NULL) {
2673+
if (sess->ownExData) {
2674+
XMEMCPY(&session->ex_data, &sess->ex_data,
2675+
sizeof(WOLFSSL_CRYPTO_EX_DATA));
2676+
XMEMSET(&sess->ex_data, 0, sizeof(WOLFSSL_CRYPTO_EX_DATA));
2677+
sess->ownExData = 0;
2678+
taken = 1;
2679+
}
2680+
TlsSessionCacheUnlockRow(row);
2681+
}
2682+
if (!taken)
2683+
XMEMSET(&session->ex_data, 0, sizeof(WOLFSSL_CRYPTO_EX_DATA));
2684+
session->ownExData = 1;
2685+
}
2686+
#endif /* HAVE_EX_DATA && !NO_SESSION_CACHE */
2687+
2688+
/* Give ssl->session a new random ID to cache it under. The entry for the old
2689+
* ID may own the ex_data the session holds a copy of. Move it to the session
2690+
* first so the entry added for the new ID is the only owner. */
2691+
int SessionNewAltId(WOLFSSL* ssl)
2692+
{
2693+
byte id[ID_LEN];
2694+
int ret;
2695+
2696+
ret = wc_RNG_GenerateBlock(ssl->rng, id, ID_LEN);
2697+
if (ret != 0)
2698+
return ret;
2699+
#if defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
2700+
SessionTakeExData(ssl);
2701+
#endif
2702+
XMEMCPY(ssl->session->altSessionID, id, ID_LEN);
2703+
ssl->session->haveAltSessionID = 1;
2704+
return 0;
2705+
}
2706+
#endif /* !NO_TLS && WOLFSSL_TLS13 && HAVE_SESSION_TICKET &&
2707+
* !NO_WOLFSSL_SERVER */
2708+
26252709
#ifdef OPENSSL_EXTRA
26262710

26272711
/* returns previous set cache size which stays constant */
@@ -3381,6 +3465,9 @@ static void SESSION_ex_data_cache_update(WOLFSSL_SESSION* session, int idx,
33813465
&& session->side == cacheSession->side
33823466
&& (IsAtLeastTLSv1_3(session->version) ==
33833467
IsAtLeastTLSv1_3(cacheSession->version))
3468+
/* An entry that gave its ex_data to a reissued ticket no
3469+
* longer holds this session's ex_data. */
3470+
&& cacheSession->ownExData
33843471
) {
33853472
if (get) {
33863473
if (getRet) {

‎src/tls13.c‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6927,6 +6927,11 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
69276927

69286928
#if defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_EARLY_DATA)
69296929
ssl->options.ticketPredatesCtx = 0;
6930+
#ifndef NO_SESSION_CACHE
6931+
/* Re-derived from this ClientHello's ticket. A stale value from the first
6932+
* hello of an HRR exchange must not gate 0-RTT for the second. */
6933+
ssl->options.ticketCacheHit = 0;
6934+
#endif
69306935
#endif
69316936

69326937
ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
@@ -7452,14 +7457,17 @@ static int CheckPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz,
74527457
&& !hasCertWithExternPsk
74537458
#endif
74547459
#if defined(HAVE_SESSION_TICKET) && !defined(NO_SESSION_CACHE)
7455-
/* RFC 8446 section 8: evict the session from the cache.
7456-
* Accept 0-RTT only when the eviction found the entry
7457-
* (single-use). */
7458-
&& wolfSSL_SSL_CTX_remove_session(ssl->ctx, ssl->session)
7459-
== 1
7460+
/* RFC 8446 section 8: accept 0-RTT once per ticket. Consuming
7461+
* the cache entry is what bounds the replay, so only accept
7462+
* when the entry was really evicted. Best effort. */
7463+
&& ssl->options.ticketCacheHit
7464+
&& wolfSSL_SSL_CTX_remove_session(ssl->ctx, ssl->session) == 1
74607465
#endif
74617466
) {
74627467
extEarlyData->resp = 1;
7468+
#if defined(HAVE_SESSION_TICKET) && !defined(NO_SESSION_CACHE)
7469+
WOLFSSL_MSG("Accepting early data for a cached ticket session");
7470+
#endif
74637471

74647472
/* Derive early data decryption key. */
74657473
ret = DeriveTls13Keys(ssl, early_data_key, DECRYPT_SIDE_ONLY,
@@ -14919,6 +14927,7 @@ static int SendTls13NewSessionTicket(WOLFSSL* ssl)
1491914927
word32 length;
1492014928
int sendSz;
1492114929
word16 extSz;
14930+
int genAltSessionID;
1492214931
word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
1492314932

1492414933
WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_SEND);
@@ -14968,14 +14977,28 @@ static int SendTls13NewSessionTicket(WOLFSSL* ssl)
1496814977
ssl->session->ticketNonce.data[0]++;
1496914978
}
1497014979

14971-
if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0) {
14972-
/* In this case we only send the ID as the ticket. Let's generate a new
14973-
* ID for the new ticket so that we don't overwrite any old ones */
14974-
ret = wc_RNG_GenerateBlock(ssl->rng, ssl->session->altSessionID,
14975-
ID_LEN);
14980+
/* Generate a new ID for the new ticket so that we don't overwrite any old
14981+
* ones. Only an ID ticket is cached without WOLFSSL_TICKET_HAVE_ID. */
14982+
genAltSessionID = (ssl->options.mask & WOLFSSL_OP_NO_TICKET) != 0;
14983+
#if defined(WOLFSSL_TICKET_HAVE_ID) && defined(WOLFSSL_EARLY_DATA) && \
14984+
!defined(NO_SESSION_CACHE)
14985+
/* A 0-RTT capable ticket needs a cache entry of its own to back the
14986+
* single-use bound of RFC 8446 Sect. 8. Sessions that cannot send early
14987+
* data keep one ID, so state bound to the cache entry (ex_data) survives
14988+
* a resumption. */
14989+
if (ssl->options.maxEarlyDataSz > 0)
14990+
genAltSessionID = 1;
14991+
#endif
14992+
#ifdef WOLFSSL_ASYNC_CRYPT
14993+
/* CreateTicket() already copied the ID into the ticket being encrypted,
14994+
* so keep it across an async retry. */
14995+
if (ssl->error == WC_NO_ERR_TRACE(WC_PENDING_E))
14996+
genAltSessionID = 0;
14997+
#endif
14998+
if (genAltSessionID) {
14999+
ret = SessionNewAltId(ssl);
1497615000
if (ret != 0)
1497715001
return ret;
14978-
ssl->session->haveAltSessionID = 1;
1497915002
}
1498015003

1498115004
if (!ssl->options.noTicketTls13) {

0 commit comments

Comments
 (0)