Skip to content

Commit 5384dc2

Browse files
committed
Add config check utils functions
Check configuration parameter in structure setup function to make sure the config data is available and valid. Current implementation checks the version config. Available version configs are - tls1_3 only - tls1_2 only issues: Mbed-TLS#4844 Change-Id: Ia762bd3d817440ae130b45f19b80a2868afae924 Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
1 parent 2a572cf commit 5384dc2

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

library/ssl_misc.h

+46
Original file line numberDiff line numberDiff line change
@@ -1259,4 +1259,50 @@ void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl );
12591259
void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight );
12601260
#endif /* MBEDTLS_SSL_PROTO_DTLS */
12611261

1262+
/**
1263+
* ssl utils functions for checking configuration.
1264+
*/
1265+
1266+
#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1267+
static inline int mbedtls_ssl_conf_is_tls13_only(const mbedtls_ssl_config *conf)
1268+
{
1269+
if( conf->min_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1270+
conf->max_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1271+
conf->min_minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 &&
1272+
conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1273+
{
1274+
return( 1 );
1275+
}
1276+
return( 0 );
1277+
}
1278+
#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1279+
1280+
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1281+
static inline int mbedtls_ssl_conf_is_tls12_only(const mbedtls_ssl_config *conf)
1282+
{
1283+
if( conf->min_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1284+
conf->max_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1285+
conf->min_minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1286+
conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1287+
{
1288+
return( 1 );
1289+
}
1290+
return( 0 );
1291+
}
1292+
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1293+
1294+
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1295+
static inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13(const mbedtls_ssl_config *conf)
1296+
{
1297+
if( conf->min_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1298+
conf->max_major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 &&
1299+
conf->min_minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1300+
conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1301+
{
1302+
return( 1 );
1303+
}
1304+
return( 0 );
1305+
}
1306+
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL*/
1307+
12621308
#endif /* ssl_misc.h */

library/ssl_tls.c

+50
Original file line numberDiff line numberDiff line change
@@ -3142,6 +3142,53 @@ void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
31423142
memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
31433143
}
31443144

3145+
static int ssl_conf_version_check( const mbedtls_ssl_context *ssl )
3146+
{
3147+
#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3148+
if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) )
3149+
{
3150+
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3151+
{
3152+
MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) );
3153+
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3154+
}
3155+
MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) );
3156+
return( 0 );
3157+
}
3158+
#endif
3159+
3160+
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3161+
if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) )
3162+
{
3163+
MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) );
3164+
return( 0 );
3165+
}
3166+
#endif
3167+
3168+
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3169+
if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) )
3170+
{
3171+
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) );
3172+
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
3173+
}
3174+
#endif
3175+
3176+
MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ));
3177+
return( MBEDTLS_ERR_SSL_BAD_CONFIG );
3178+
}
3179+
3180+
static int ssl_conf_check(const mbedtls_ssl_context *ssl)
3181+
{
3182+
int ret;
3183+
ret = ssl_conf_version_check( ssl );
3184+
if( ret != 0 )
3185+
return( ret );
3186+
3187+
/* Space for further checks */
3188+
3189+
return( 0 );
3190+
}
3191+
31453192
/*
31463193
* Setup an SSL context
31473194
*/
@@ -3155,6 +3202,9 @@ int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
31553202

31563203
ssl->conf = conf;
31573204

3205+
if( ( ret = ssl_conf_check( ssl ) ) != 0 )
3206+
return( ret );
3207+
31583208
/*
31593209
* Prepare base structures
31603210
*/

0 commit comments

Comments
 (0)