Skip to content

Commit be0224a

Browse files
Merge pull request #6167 from yuhaoth/pr/finalize-tls13-session-tickets
2 parents 1716f06 + 6ee726e commit be0224a

9 files changed

+484
-146
lines changed

include/mbedtls/mbedtls_config.h

+20
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,26 @@
15491549
*/
15501550
//#define MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE
15511551

1552+
/**
1553+
* \def MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE
1554+
*
1555+
* Maximum time difference in milliseconds tolerated between the age of a
1556+
* ticket from the server and client point of view.
1557+
* From the client point of view, the age of a ticket is the time difference
1558+
* between the time when the client proposes to the server to use the ticket
1559+
* (time of writing of the Pre-Shared Key Extension including the ticket) and
1560+
* the time the client received the ticket from the server.
1561+
* From the server point of view, the age of a ticket is the time difference
1562+
* between the time when the server receives a proposition from the client
1563+
* to use the ticket and the time when the ticket was created by the server.
1564+
* The server age is expected to be always greater than the client one and
1565+
* MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE defines the
1566+
* maximum difference tolerated for the server to accept the ticket.
1567+
* This is not used in TLS 1.2.
1568+
*
1569+
*/
1570+
#define MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE 6000
1571+
15521572
/**
15531573
* \def MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH
15541574
*

library/ssl_misc.h

-10
Original file line numberDiff line numberDiff line change
@@ -2457,16 +2457,6 @@ int mbedtls_ssl_check_dtls_clihlo_cookie(
24572457
#endif
24582458

24592459
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
2460-
/* Check if we have any PSK to offer, returns 0 if PSK is available.
2461-
* Assign the psk and ticket if pointers are present.
2462-
*/
2463-
MBEDTLS_CHECK_RETURN_CRITICAL
2464-
int mbedtls_ssl_get_psk_to_offer(
2465-
const mbedtls_ssl_context *ssl,
2466-
int *psk_type,
2467-
const unsigned char **psk, size_t *psk_len,
2468-
const unsigned char **psk_identity, size_t *psk_identity_len );
2469-
24702460
/**
24712461
* \brief Given an SSL context and its associated configuration, write the TLS
24722462
* 1.3 specific Pre-Shared key extension.

library/ssl_tls13_client.c

+91-15
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,59 @@ static int ssl_tls13_write_psk_key_exchange_modes_ext( mbedtls_ssl_context *ssl,
664664
ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES;
665665
return ( 0 );
666666
}
667-
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
667+
668+
/* Check if we have any PSK to offer, returns 0 if a PSK is available. */
669+
MBEDTLS_CHECK_RETURN_CRITICAL
670+
static int ssl_tls13_get_psk_to_offer(
671+
const mbedtls_ssl_context *ssl,
672+
int *psk_type,
673+
const unsigned char **psk, size_t *psk_len,
674+
const unsigned char **psk_identity, size_t *psk_identity_len )
675+
{
676+
*psk = NULL;
677+
*psk_len = 0;
678+
*psk_identity = NULL;
679+
*psk_identity_len = 0;
680+
*psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
681+
682+
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
683+
/* Check if a ticket has been configured. */
684+
if( ssl->session_negotiate != NULL &&
685+
ssl->session_negotiate->ticket != NULL )
686+
{
687+
#if defined(MBEDTLS_HAVE_TIME)
688+
mbedtls_time_t now = mbedtls_time( NULL );
689+
if( ssl->session_negotiate->ticket_received <= now &&
690+
(uint64_t)( now - ssl->session_negotiate->ticket_received )
691+
<= ssl->session_negotiate->ticket_lifetime )
692+
{
693+
*psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
694+
*psk = ssl->session_negotiate->resumption_key;
695+
*psk_len = ssl->session_negotiate->resumption_key_len;
696+
*psk_identity = ssl->session_negotiate->ticket;
697+
*psk_identity_len = ssl->session_negotiate->ticket_len;
698+
return( 0 );
699+
}
700+
#endif /* MBEDTLS_HAVE_TIME */
701+
MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket expired" ) );
702+
}
703+
#endif
704+
705+
/* Check if an external PSK has been configured. */
706+
if( ssl->conf->psk != NULL )
707+
{
708+
*psk = ssl->conf->psk;
709+
*psk_len = ssl->conf->psk_len;
710+
*psk_identity = ssl->conf->psk_identity;
711+
*psk_identity_len = ssl->conf->psk_identity_len;
712+
return( 0 );
713+
}
714+
715+
return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
716+
}
668717

669718
/*
670-
* mbedtls_ssl_tls13_write_pre_shared_key_ext() structure:
719+
* mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
671720
*
672721
* struct {
673722
* opaque identity<1..2^16-1>;
@@ -689,9 +738,6 @@ static int ssl_tls13_write_psk_key_exchange_modes_ext( mbedtls_ssl_context *ssl,
689738
* } PreSharedKeyExtension;
690739
*
691740
*/
692-
693-
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
694-
695741
int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
696742
mbedtls_ssl_context *ssl,
697743
unsigned char *buf, unsigned char *end,
@@ -725,9 +771,8 @@ int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
725771
* configured, offer that.
726772
* - Otherwise, skip the PSK extension.
727773
*/
728-
729-
if( mbedtls_ssl_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
730-
&psk_identity, &psk_identity_len ) != 0 )
774+
if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
775+
&psk_identity, &psk_identity_len ) != 0 )
731776
{
732777
MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip pre_shared_key extensions" ) );
733778
return( 0 );
@@ -757,6 +802,26 @@ int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
757802
break;
758803
}
759804
}
805+
else
806+
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
807+
if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
808+
{
809+
#if defined(MBEDTLS_HAVE_TIME)
810+
mbedtls_time_t now = mbedtls_time( NULL );
811+
812+
obfuscated_ticket_age =
813+
( (uint32_t)( now - ssl->session_negotiate->ticket_received ) * 1000 )
814+
+ ssl->session_negotiate->ticket_age_add;
815+
#endif
816+
}
817+
else
818+
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
819+
{
820+
MBEDTLS_SSL_DEBUG_MSG( 1, ( "write_identities_of_pre_shared_key_ext: "
821+
"should never happen" ) );
822+
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
823+
}
824+
760825

761826
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
762827
ssl->session_negotiate->ciphersuite );
@@ -831,8 +896,8 @@ int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
831896
unsigned char transcript[MBEDTLS_MD_MAX_SIZE];
832897
size_t transcript_len;
833898

834-
if( mbedtls_ssl_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
835-
&psk_identity, &psk_identity_len ) != 0 )
899+
if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
900+
&psk_identity, &psk_identity_len ) != 0 )
836901
{
837902
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
838903
}
@@ -1266,15 +1331,15 @@ static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
12661331
size_t psk_len;
12671332
const unsigned char *psk_identity;
12681333
size_t psk_identity_len;
1269-
1334+
int psk_type;
12701335

12711336
/* Check which PSK we've offered.
12721337
*
12731338
* NOTE: Ultimately, we want to offer multiple PSKs, and in this
12741339
* case, we need to iterate over them here.
12751340
*/
1276-
if( mbedtls_ssl_get_psk_to_offer( ssl, NULL, &psk, &psk_len,
1277-
&psk_identity, &psk_identity_len ) != 0 )
1341+
if( ssl_tls13_get_psk_to_offer( ssl, &psk_type, &psk, &psk_len,
1342+
&psk_identity, &psk_identity_len ) != 0 )
12781343
{
12791344
/* If we haven't offered a PSK, the server must not send
12801345
* a PSK identity extension. */
@@ -1622,16 +1687,19 @@ static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
16221687
/* Only the pre_shared_key extension was received */
16231688
case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
16241689
handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1690+
MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk" ) );
16251691
break;
16261692

16271693
/* Only the key_share extension was received */
16281694
case MBEDTLS_SSL_EXT_KEY_SHARE:
16291695
handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1696+
MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: ephemeral" ) );
16301697
break;
16311698

16321699
/* Both the pre_shared_key and key_share extensions were received */
16331700
case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
16341701
handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1702+
MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk_ephemeral" ) );
16351703
break;
16361704

16371705
/* Neither pre_shared_key nor key_share extension was received */
@@ -1819,7 +1887,12 @@ static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
18191887
*/
18201888
switch( extension_type )
18211889
{
1890+
case MBEDTLS_TLS_EXT_SERVERNAME:
1891+
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found server_name extension" ) );
1892+
1893+
/* The server_name extension should be an empty extension */
18221894

1895+
break;
18231896
case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
18241897
MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
18251898
break;
@@ -2237,11 +2310,11 @@ static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
22372310
if( ret != 0 )
22382311
return( ret );
22392312

2240-
ret = mbedtls_ssl_tls13_generate_resumption_master_secret( ssl );
2313+
ret = mbedtls_ssl_tls13_compute_resumption_master_secret( ssl );
22412314
if( ret != 0 )
22422315
{
22432316
MBEDTLS_SSL_DEBUG_RET( 1,
2244-
"mbedtls_ssl_tls13_generate_resumption_master_secret ", ret );
2317+
"mbedtls_ssl_tls13_compute_resumption_master_secret ", ret );
22452318
return ( ret );
22462319
}
22472320

@@ -2405,6 +2478,9 @@ static int ssl_tls13_parse_new_session_ticket( mbedtls_ssl_context *ssl,
24052478
return( ret );
24062479
}
24072480

2481+
/* session has been updated, allow export */
2482+
session->exported = 0;
2483+
24082484
return( 0 );
24092485
}
24102486

library/ssl_tls13_generic.c

-37
Original file line numberDiff line numberDiff line change
@@ -1504,41 +1504,4 @@ int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
15041504
}
15051505
#endif /* MBEDTLS_ECDH_C */
15061506

1507-
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1508-
/* Check if we have any PSK to offer, returns 0 if PSK is available.
1509-
* Assign the psk and ticket if pointers are present.
1510-
*/
1511-
int mbedtls_ssl_get_psk_to_offer(
1512-
const mbedtls_ssl_context *ssl,
1513-
int *psk_type,
1514-
const unsigned char **psk, size_t *psk_len,
1515-
const unsigned char **psk_identity, size_t *psk_identity_len )
1516-
{
1517-
int ptrs_present = 0;
1518-
1519-
if( psk_type != NULL && psk != NULL && psk_len != NULL &&
1520-
psk_identity != NULL && psk_identity_len != NULL )
1521-
{
1522-
ptrs_present = 1;
1523-
}
1524-
1525-
/* Check if an external PSK has been configured. */
1526-
if( ssl->conf->psk != NULL )
1527-
{
1528-
if( ptrs_present )
1529-
{
1530-
*psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
1531-
*psk = ssl->conf->psk;
1532-
*psk_len = ssl->conf->psk_len;
1533-
*psk_identity = ssl->conf->psk_identity;
1534-
*psk_identity_len = ssl->conf->psk_identity_len;
1535-
}
1536-
1537-
return( 0 );
1538-
}
1539-
1540-
return( 1 );
1541-
}
1542-
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1543-
15441507
#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */

library/ssl_tls13_keys.c

+35-4
Original file line numberDiff line numberDiff line change
@@ -1504,12 +1504,43 @@ int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl )
15041504
return( ret );
15051505
}
15061506

1507-
int mbedtls_ssl_tls13_generate_resumption_master_secret(
1508-
mbedtls_ssl_context *ssl )
1507+
int mbedtls_ssl_tls13_compute_resumption_master_secret( mbedtls_ssl_context *ssl )
15091508
{
1509+
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1510+
mbedtls_md_type_t md_type;
1511+
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1512+
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1513+
size_t transcript_len;
1514+
1515+
MBEDTLS_SSL_DEBUG_MSG( 2,
1516+
( "=> mbedtls_ssl_tls13_compute_resumption_master_secret" ) );
1517+
1518+
md_type = handshake->ciphersuite_info->mac;
1519+
1520+
ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1521+
transcript, sizeof( transcript ),
1522+
&transcript_len );
1523+
if( ret != 0 )
1524+
return( ret );
1525+
1526+
ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
1527+
mbedtls_psa_translate_md( md_type ),
1528+
handshake->tls13_master_secrets.app,
1529+
transcript, transcript_len,
1530+
&ssl->session_negotiate->app_secrets );
1531+
if( ret != 0 )
1532+
return( ret );
1533+
15101534
/* Erase master secrets */
1511-
mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
1512-
sizeof( ssl->handshake->tls13_master_secrets ) );
1535+
mbedtls_platform_zeroize( &handshake->tls13_master_secrets,
1536+
sizeof( handshake->tls13_master_secrets ) );
1537+
1538+
MBEDTLS_SSL_DEBUG_BUF( 4, "Resumption master secret",
1539+
ssl->session_negotiate->app_secrets.resumption_master_secret,
1540+
PSA_HASH_LENGTH( mbedtls_psa_translate_md( md_type ) ) ) ;
1541+
1542+
MBEDTLS_SSL_DEBUG_MSG( 2,
1543+
( "<= mbedtls_ssl_tls13_compute_resumption_master_secret" ) );
15131544
return( 0 );
15141545
}
15151546

library/ssl_tls13_keys.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,7 @@ int mbedtls_ssl_tls13_generate_application_keys(
636636
* \returns A negative error code on failure.
637637
*/
638638
MBEDTLS_CHECK_RETURN_CRITICAL
639-
int mbedtls_ssl_tls13_generate_resumption_master_secret(
640-
mbedtls_ssl_context *ssl );
639+
int mbedtls_ssl_tls13_compute_resumption_master_secret( mbedtls_ssl_context *ssl );
641640

642641
/**
643642
* \brief Calculate the verify_data value for the client or server TLS 1.3

0 commit comments

Comments
 (0)