From 65382f250d8595adcd087092c572d498aba89bc8 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 2 Jul 2019 13:51:57 +0100 Subject: [PATCH 01/22] Make ssl.h independent of ssl_ciphersuites.h Previously, ssl.h included ssl_ciphersuites.h to have access to the helper macros MBEDTLS_KEY_EXCHANGE_XXX_ENABLED, and for no other reason. This commit moves the definitions of these macros to ssl.h, thereby removing the dependency of ssl.h on ssl_ciphersuites.h. --- include/mbedtls/ssl.h | 82 +++++++++++++++++++++++++++- include/mbedtls/ssl_ciphersuites.h | 85 ------------------------------ 2 files changed, 80 insertions(+), 87 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index ad60e82c3..c72bbeb5a 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -33,8 +33,6 @@ #include "bignum.h" #include "ecp.h" -#include "ssl_ciphersuites.h" - #if defined(MBEDTLS_X509_CRT_PARSE_C) #include "x509_crt.h" #include "x509_crl.h" @@ -401,6 +399,84 @@ #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO 0xFF01 +/* + * Helper macros indicating whether certain classes + * of key exchanges are enabled in the configuration. + */ + +/* Key exchanges using a certificate */ +#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED +#endif + +/* Key exchanges allowing client certificate requests */ +#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED +#endif + +/* Key exchanges involving server signature in ServerKeyExchange */ +#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED +#endif + +/* Key exchanges using ECDH */ +#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED +#endif + +/* Key exchanges that don't involve ephemeral keys */ +#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED +#endif + +/* Key exchanges that involve ephemeral keys */ +#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED +#endif + +/* Key exchanges using a PSK */ +#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED +#endif + +/* Key exchanges using DHE */ +#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED +#endif + +/* Key exchanges using ECDHE */ +#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) +#define MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED +#endif + /* * Size defines */ @@ -3915,6 +3991,8 @@ void mbedtls_ssl_session_init( mbedtls_ssl_session *session ); */ void mbedtls_ssl_session_free( mbedtls_ssl_session *session ); +#include "ssl_ciphersuites.h" + #ifdef __cplusplus } #endif diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index 712678330..e3cd65bcc 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -304,79 +304,6 @@ typedef enum { MBEDTLS_KEY_EXCHANGE_ECJPAKE, } mbedtls_key_exchange_type_t; -/* Key exchanges using a certificate */ -#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED -#endif - -/* Key exchanges allowing client certificate requests */ -#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED -#endif - -/* Key exchanges involving server signature in ServerKeyExchange */ -#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED -#endif - -/* Key exchanges using ECDH */ -#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED -#endif - -/* Key exchanges that don't involve ephemeral keys */ -#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED -#endif - -/* Key exchanges that involve ephemeral keys */ -#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED -#endif - -/* Key exchanges using a PSK */ -#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED -#endif - -/* Key exchanges using DHE */ -#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED -#endif - -/* Key exchanges using ECDHE */ -#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) -#define MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED -#endif - typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; #define MBEDTLS_CIPHERSUITE_WEAK 0x01 /**< Weak ciphersuite flag */ @@ -417,7 +344,6 @@ mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( const mbedtls_ssl_ciphers int mbedtls_ssl_ciphersuite_uses_ec( const mbedtls_ssl_ciphersuite_t *info ); int mbedtls_ssl_ciphersuite_uses_psk( const mbedtls_ssl_ciphersuite_t *info ); -#if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) static inline int mbedtls_ssl_ciphersuite_has_pfs( const mbedtls_ssl_ciphersuite_t *info ) { switch( info->key_exchange ) @@ -434,9 +360,7 @@ static inline int mbedtls_ssl_ciphersuite_has_pfs( const mbedtls_ssl_ciphersuite return( 0 ); } } -#endif /* MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED */ -#if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) static inline int mbedtls_ssl_ciphersuite_no_pfs( const mbedtls_ssl_ciphersuite_t *info ) { switch( info->key_exchange ) @@ -452,9 +376,7 @@ static inline int mbedtls_ssl_ciphersuite_no_pfs( const mbedtls_ssl_ciphersuite_ return( 0 ); } } -#endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED) static inline int mbedtls_ssl_ciphersuite_uses_ecdh( const mbedtls_ssl_ciphersuite_t *info ) { switch( info->key_exchange ) @@ -467,7 +389,6 @@ static inline int mbedtls_ssl_ciphersuite_uses_ecdh( const mbedtls_ssl_ciphersui return( 0 ); } } -#endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDH_ENABLED */ static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( const mbedtls_ssl_ciphersuite_t *info ) { @@ -504,7 +425,6 @@ static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( const mbedtls_ssl_ciphe } } -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED) static inline int mbedtls_ssl_ciphersuite_uses_dhe( const mbedtls_ssl_ciphersuite_t *info ) { switch( info->key_exchange ) @@ -517,9 +437,7 @@ static inline int mbedtls_ssl_ciphersuite_uses_dhe( const mbedtls_ssl_ciphersuit return( 0 ); } } -#endif /* MBEDTLS_KEY_EXCHANGE__SOME__DHE_ENABLED) */ -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED) static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( const mbedtls_ssl_ciphersuite_t *info ) { switch( info->key_exchange ) @@ -533,9 +451,7 @@ static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( const mbedtls_ssl_ciphersu return( 0 ); } } -#endif /* MBEDTLS_KEY_EXCHANGE__SOME__ECDHE_ENABLED) */ -#if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) static inline int mbedtls_ssl_ciphersuite_uses_server_signature( const mbedtls_ssl_ciphersuite_t *info ) { switch( info->key_exchange ) @@ -549,7 +465,6 @@ static inline int mbedtls_ssl_ciphersuite_uses_server_signature( const mbedtls_s return( 0 ); } } -#endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */ #ifdef __cplusplus } From 473f98f2e013f116c0c48d02feff23b12e3e9733 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 26 Jun 2019 10:27:32 +0100 Subject: [PATCH 02/22] Introduce ciphersuite handle type This commit introduces an internal zero-cost abstraction layer for SSL ciphersuites: Instead of addressing ciphersuites via pointers to instances of mbedtls_ssl_ciphersuite_t and accessing their fields directly, this commit introduces an opaque type mbedtls_ssl_ciphersuite_handle_t, and getter functions mbedtls_ssl_suite_get_xxx() operating on ciphersuite handles. The role of NULL is played by a new macro constant MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE which results of functions returning handles can be checked against. (For example, when doing a lookup of a ciphersuite from a peer-provided ciphersuite ID in the per's Hello message). The getter functions have the validity of the handle as a precondition and are undefined if the handle is invalid. So far, there's only one implementation of this abstraction layer, namely mbedtls_ssl_ciphersuite_handle_t being mbedtls_ssl_ciphersuite_t const * and getter functions being field accesses. In subsequent commits, however, the abstraction layer will be useful to save code in the situation where only a single ciphersuite is enabled. --- include/mbedtls/ssl_ciphersuites.h | 132 ++++++++++++++++++++----- include/mbedtls/ssl_internal.h | 6 +- library/ssl_ciphersuites.c | 47 +++++---- library/ssl_cli.c | 154 +++++++++++++++++++---------- library/ssl_srv.c | 132 +++++++++++++++---------- library/ssl_tls.c | 50 +++++----- programs/ssl/ssl_client2.c | 17 ++-- programs/ssl/ssl_server2.c | 17 ++-- 8 files changed, 356 insertions(+), 199 deletions(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index e3cd65bcc..bb55a4b65 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -331,22 +331,80 @@ struct mbedtls_ssl_ciphersuite_t unsigned char flags; }; -const int *mbedtls_ssl_list_ciphersuites( void ); +typedef mbedtls_ssl_ciphersuite_t const * mbedtls_ssl_ciphersuite_handle_t; +#define MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ( (mbedtls_ssl_ciphersuite_handle_t) NULL ) -const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ); -const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_id( int ciphersuite_id ); +/* + * Getter functions for the extraction of ciphersuite attributes + * from a ciphersuite handle. + * + * These functions have the validity of the handle as a precondition! + * Their behaviour is undefined when MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE + * is passed. + */ -#if defined(MBEDTLS_PK_C) -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( const mbedtls_ssl_ciphersuite_t *info ); -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( const mbedtls_ssl_ciphersuite_t *info ); -#endif - -int mbedtls_ssl_ciphersuite_uses_ec( const mbedtls_ssl_ciphersuite_t *info ); -int mbedtls_ssl_ciphersuite_uses_psk( const mbedtls_ssl_ciphersuite_t *info ); - -static inline int mbedtls_ssl_ciphersuite_has_pfs( const mbedtls_ssl_ciphersuite_t *info ) +/* + * Implementation of getter functions when the ciphersuite handle + * is a pointer to the ciphersuite information structure. + * + * The precondition that the handle is valid means that + * we don't need to check that info != NULL. + */ +static inline int mbedtls_ssl_suite_get_id( + mbedtls_ssl_ciphersuite_handle_t const info ) { - switch( info->key_exchange ) + return( info->id ); +} +static inline const char* mbedtls_ssl_suite_get_name( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->name ); +} +static inline mbedtls_cipher_type_t mbedtls_ssl_suite_get_cipher( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->cipher ); +} +static inline mbedtls_md_type_t mbedtls_ssl_suite_get_mac( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->mac ); +} +static inline mbedtls_key_exchange_type_t mbedtls_ssl_suite_get_key_exchange( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->key_exchange ); +} +static inline int mbedtls_ssl_suite_get_min_major_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->min_major_ver ); +} +static inline int mbedtls_ssl_suite_get_min_minor_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->min_minor_ver ); +} +static inline int mbedtls_ssl_suite_get_max_major_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->max_major_ver ); +} +static inline int mbedtls_ssl_suite_get_max_minor_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->max_minor_ver ); +} +static inline unsigned char mbedtls_ssl_suite_get_flags( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + return( info->flags ); +} + +static inline int mbedtls_ssl_ciphersuite_has_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_DHE_RSA: case MBEDTLS_KEY_EXCHANGE_DHE_PSK: @@ -361,9 +419,10 @@ static inline int mbedtls_ssl_ciphersuite_has_pfs( const mbedtls_ssl_ciphersuite } } -static inline int mbedtls_ssl_ciphersuite_no_pfs( const mbedtls_ssl_ciphersuite_t *info ) +static inline int mbedtls_ssl_ciphersuite_no_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: @@ -377,9 +436,10 @@ static inline int mbedtls_ssl_ciphersuite_no_pfs( const mbedtls_ssl_ciphersuite_ } } -static inline int mbedtls_ssl_ciphersuite_uses_ecdh( const mbedtls_ssl_ciphersuite_t *info ) +static inline int mbedtls_ssl_ciphersuite_uses_ecdh( + mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: @@ -390,9 +450,10 @@ static inline int mbedtls_ssl_ciphersuite_uses_ecdh( const mbedtls_ssl_ciphersui } } -static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( const mbedtls_ssl_ciphersuite_t *info ) +static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( + mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_RSA: case MBEDTLS_KEY_EXCHANGE_DHE_RSA: @@ -407,9 +468,10 @@ static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( const mbedtls_ssl_ci } } -static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( const mbedtls_ssl_ciphersuite_t *info ) +static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( + mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_RSA: case MBEDTLS_KEY_EXCHANGE_RSA_PSK: @@ -425,9 +487,10 @@ static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( const mbedtls_ssl_ciphe } } -static inline int mbedtls_ssl_ciphersuite_uses_dhe( const mbedtls_ssl_ciphersuite_t *info ) +static inline int mbedtls_ssl_ciphersuite_uses_dhe( + mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_DHE_RSA: case MBEDTLS_KEY_EXCHANGE_DHE_PSK: @@ -438,9 +501,10 @@ static inline int mbedtls_ssl_ciphersuite_uses_dhe( const mbedtls_ssl_ciphersuit } } -static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( const mbedtls_ssl_ciphersuite_t *info ) +static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( + mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: @@ -452,9 +516,10 @@ static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( const mbedtls_ssl_ciphersu } } -static inline int mbedtls_ssl_ciphersuite_uses_server_signature( const mbedtls_ssl_ciphersuite_t *info ) +static inline int mbedtls_ssl_ciphersuite_uses_server_signature( + mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_DHE_RSA: case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: @@ -466,6 +531,19 @@ static inline int mbedtls_ssl_ciphersuite_uses_server_signature( const mbedtls_s } } +const int *mbedtls_ssl_list_ciphersuites( void ); + +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ); +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuite_id ); + +#if defined(MBEDTLS_PK_C) +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ); +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( mbedtls_ssl_ciphersuite_handle_t info ); +#endif + +int mbedtls_ssl_ciphersuite_uses_ec( mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_uses_psk( mbedtls_ssl_ciphersuite_handle_t info ); + #ifdef __cplusplus } #endif diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index 151589f59..b2382fab9 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -501,7 +501,7 @@ struct mbedtls_ssl_handshake_params const unsigned char *, size_t, unsigned char *, size_t); - mbedtls_ssl_ciphersuite_t const *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; size_t pmslen; /*!< premaster length */ @@ -918,7 +918,7 @@ int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ); int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ); void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, - const mbedtls_ssl_ciphersuite_t *ciphersuite_info ); + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info ); #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ); @@ -978,7 +978,7 @@ static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl ) * Return 0 if everything is OK, -1 if not. */ int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, - const mbedtls_ssl_ciphersuite_t *ciphersuite, + mbedtls_ssl_ciphersuite_handle_t ciphersuite, int cert_endpoint, uint32_t *flags ); #endif /* MBEDTLS_X509_CRT_PARSE_C */ diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index 518f7dde0..8378a9017 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -2182,18 +2182,19 @@ const int *mbedtls_ssl_list_ciphersuites( void ) static int supported_ciphersuites[MAX_CIPHERSUITES]; static int supported_init = 0; -static int ciphersuite_is_removed( const mbedtls_ssl_ciphersuite_t *cs_info ) +static int ciphersuite_is_removed( mbedtls_ssl_ciphersuite_handle_t cs_info ) { - (void)cs_info; + if( cs_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) + return( 1 ); #if defined(MBEDTLS_REMOVE_ARC4_CIPHERSUITES) - if( cs_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) + if( mbedtls_ssl_suite_get_cipher( cs_info ) == MBEDTLS_CIPHER_ARC4_128 ) return( 1 ); #endif /* MBEDTLS_REMOVE_ARC4_CIPHERSUITES */ #if defined(MBEDTLS_REMOVE_3DES_CIPHERSUITES) - if( cs_info->cipher == MBEDTLS_CIPHER_DES_EDE3_ECB || - cs_info->cipher == MBEDTLS_CIPHER_DES_EDE3_CBC ) + if( mbedtls_ssl_suite_get_cipher( cs_info ) == MBEDTLS_CIPHER_DES_EDE3_ECB || + mbedtls_ssl_suite_get_cipher( cs_info ) == MBEDTLS_CIPHER_DES_EDE3_CBC ) { return( 1 ); } @@ -2217,12 +2218,10 @@ const int *mbedtls_ssl_list_ciphersuites( void ) *p != 0 && q < supported_ciphersuites + MAX_CIPHERSUITES - 1; p++ ) { - const mbedtls_ssl_ciphersuite_t *cs_info; - if( ( cs_info = mbedtls_ssl_ciphersuite_from_id( *p ) ) != NULL && - !ciphersuite_is_removed( cs_info ) ) - { + mbedtls_ssl_ciphersuite_handle_t cs_info; + cs_info = mbedtls_ssl_ciphersuite_from_id( *p ); + if( !ciphersuite_is_removed( cs_info ) ) *(q++) = *p; - } } *q = 0; @@ -2233,10 +2232,10 @@ const int *mbedtls_ssl_list_ciphersuites( void ) } #endif /* MBEDTLS_SSL_CIPHERSUITES */ -const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_string( +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ) { - const mbedtls_ssl_ciphersuite_t *cur = ciphersuite_definitions; + mbedtls_ssl_ciphersuite_handle_t cur = ciphersuite_definitions; if( NULL == ciphersuite_name ) return( NULL ); @@ -2252,9 +2251,9 @@ const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_string( return( NULL ); } -const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_id( int ciphersuite ) +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuite ) { - const mbedtls_ssl_ciphersuite_t *cur = ciphersuite_definitions; + mbedtls_ssl_ciphersuite_handle_t cur = ciphersuite_definitions; while( cur->id != 0 ) { @@ -2269,7 +2268,7 @@ const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_id( int ciphersuit const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id ) { - const mbedtls_ssl_ciphersuite_t *cur; + mbedtls_ssl_ciphersuite_handle_t cur; cur = mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); @@ -2281,7 +2280,7 @@ const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id ) int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) { - const mbedtls_ssl_ciphersuite_t *cur; + mbedtls_ssl_ciphersuite_handle_t cur; cur = mbedtls_ssl_ciphersuite_from_string( ciphersuite_name ); @@ -2292,9 +2291,9 @@ int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) } #if defined(MBEDTLS_PK_C) -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( const mbedtls_ssl_ciphersuite_t *info ) +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_RSA: case MBEDTLS_KEY_EXCHANGE_DHE_RSA: @@ -2314,9 +2313,9 @@ mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( const mbedtls_ssl_ciph } } -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( const mbedtls_ssl_ciphersuite_t *info ) +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_RSA: case MBEDTLS_KEY_EXCHANGE_DHE_RSA: @@ -2335,9 +2334,9 @@ mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( const mbedtls_ssl_ciphers #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) -int mbedtls_ssl_ciphersuite_uses_ec( const mbedtls_ssl_ciphersuite_t *info ) +int mbedtls_ssl_ciphersuite_uses_ec( mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: @@ -2354,9 +2353,9 @@ int mbedtls_ssl_ciphersuite_uses_ec( const mbedtls_ssl_ciphersuite_t *info ) #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) -int mbedtls_ssl_ciphersuite_uses_psk( const mbedtls_ssl_ciphersuite_t *info ) +int mbedtls_ssl_ciphersuite_uses_psk( mbedtls_ssl_ciphersuite_handle_t info ) { - switch( info->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) { case MBEDTLS_KEY_EXCHANGE_PSK: case MBEDTLS_KEY_EXCHANGE_RSA_PSK: diff --git a/library/ssl_cli.c b/library/ssl_cli.c index a050adb3b..fa5b64ec5 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -780,34 +780,45 @@ static int ssl_generate_random( mbedtls_ssl_context *ssl ) * * \return 0 if valid, else 1 */ -static int ssl_validate_ciphersuite( const mbedtls_ssl_ciphersuite_t * suite_info, +static int ssl_validate_ciphersuite( mbedtls_ssl_ciphersuite_handle_t suite_info, const mbedtls_ssl_context * ssl, int min_minor_ver, int max_minor_ver ) { (void) ssl; - if( suite_info == NULL ) + if( suite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) return( 1 ); - if( suite_info->min_minor_ver > max_minor_ver || - suite_info->max_minor_ver < min_minor_ver ) + + if( mbedtls_ssl_suite_get_min_minor_ver( suite_info ) > max_minor_ver || + mbedtls_ssl_suite_get_max_minor_ver( suite_info ) < min_minor_ver ) + { return( 1 ); + } #if defined(MBEDTLS_SSL_PROTO_DTLS) if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && - ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) + ( mbedtls_ssl_suite_get_flags( suite_info ) & + MBEDTLS_CIPHERSUITE_NODTLS ) != 0 ) + { return( 1 ); + } #endif #if defined(MBEDTLS_ARC4_C) if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && - suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) + mbedtls_ssl_suite_get_cipher( suite_info ) == MBEDTLS_CIPHER_ARC4_128 ) + { return( 1 ); + } #endif #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && - mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) + if( mbedtls_ssl_suite_get_key_exchange( suite_info ) == + MBEDTLS_KEY_EXCHANGE_ECJPAKE && + mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) + { return( 1 ); + } #endif return( 0 ); @@ -821,7 +832,7 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) unsigned char *p, *q; unsigned char offer_compress; const int *ciphersuites; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) int uses_ec = 0; @@ -978,7 +989,7 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) continue; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x", - ciphersuites[i] ) ); + mbedtls_ssl_suite_get_id( ciphersuite_info ) ) ); #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) @@ -986,8 +997,10 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) #endif n++; - *p++ = (unsigned char)( ciphersuites[i] >> 8 ); - *p++ = (unsigned char)( ciphersuites[i] ); + *p++ = (unsigned char)( + mbedtls_ssl_suite_get_id( ciphersuite_info ) >> 8 ); + *p++ = (unsigned char)( + mbedtls_ssl_suite_get_id( ciphersuite_info ) ); } MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites (excluding SCSVs)", n ) ); @@ -1428,8 +1441,8 @@ static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, { int ret; - if( ssl->handshake->ciphersuite_info->key_exchange != - MBEDTLS_KEY_EXCHANGE_ECJPAKE ) + if( mbedtls_ssl_suite_get_key_exchange( + ssl->handshake->ciphersuite_info ) != MBEDTLS_KEY_EXCHANGE_ECJPAKE ) { MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); return( 0 ); @@ -1613,7 +1626,7 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) int extended_ms_seen = 0; #endif int handshake_failure = 0; - const mbedtls_ssl_ciphersuite_t *suite_info; + mbedtls_ssl_ciphersuite_handle_t suite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); @@ -1790,7 +1803,8 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) * Initialize update checksum functions */ ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); - if( ssl->handshake->ciphersuite_info == NULL ) + if( ssl->handshake->ciphersuite_info == + MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) ); mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, @@ -1888,10 +1902,12 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); } - MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", suite_info->name ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", + mbedtls_ssl_suite_get_name( suite_info ) ) ); #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) - if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && + if( mbedtls_ssl_suite_get_key_exchange( suite_info ) == + MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) { ssl->handshake->ecrs_enabled = 1; @@ -2555,14 +2571,15 @@ cleanup: static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) { int ret; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; unsigned char *p = NULL, *end = NULL; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_RSA ) { MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) ); ssl->state++; @@ -2574,8 +2591,10 @@ static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_ECDH_RSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) { if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 ) { @@ -2622,8 +2641,10 @@ static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) */ if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE ) { - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) { /* Current message is probably either * CertificateRequest or ServerHelloDone */ @@ -2650,10 +2671,14 @@ start_processing: MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p ); #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_RSA_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_DHE_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) { if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 ) { @@ -2667,16 +2692,22 @@ start_processing: #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + { ; /* nothing more to do */ + } else #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_DHE_RSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) { if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 ) { @@ -2692,9 +2723,12 @@ start_processing: #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) { if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 ) { @@ -2709,7 +2743,8 @@ start_processing: MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) { ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, p, end - p ); @@ -2922,7 +2957,7 @@ exit: #if ! defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); @@ -2944,7 +2979,7 @@ static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) unsigned char *buf; size_t n = 0; size_t cert_type_len = 0, dn_len = 0; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); @@ -3145,13 +3180,13 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) { int ret; size_t i, n; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) { /* * DHM key exchange -- send G^X mod P @@ -3195,10 +3230,14 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) { /* * ECDH key exchange -- send client public value @@ -3296,14 +3335,16 @@ ecdh_calc_secret: i += ssl->conf->psk_identity_len; #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_PSK ) { n = 0; } else #endif #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) { if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 ) return( ret ); @@ -3311,7 +3352,8 @@ ecdh_calc_secret: else #endif #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) { /* * ClientDiffieHellmanPublic public (DHM send G^X mod P) @@ -3342,7 +3384,8 @@ ecdh_calc_secret: else #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) { /* * ClientECDiffieHellmanPublic public; @@ -3368,7 +3411,7 @@ ecdh_calc_secret: } if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, - ciphersuite_info->key_exchange ) ) != 0 ) + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); return( ret ); @@ -3377,7 +3420,8 @@ ecdh_calc_secret: else #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_RSA ) { i = 4; if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 ) @@ -3386,7 +3430,8 @@ ecdh_calc_secret: else #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_ECJPAKE ) { i = 4; @@ -3438,7 +3483,7 @@ ecdh_calc_secret: #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; int ret; @@ -3464,7 +3509,7 @@ static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) { int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; size_t n = 0, offset = 0; unsigned char hash[48]; @@ -3570,7 +3615,8 @@ sign: * Reason: Otherwise we should have running hashes for SHA512 and SHA224 * in order to satisfy 'weird' needs from the server side. */ - if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) + if( mbedtls_ssl_suite_get_mac( ssl->handshake->ciphersuite_info ) + == MBEDTLS_MD_SHA384 ) { md_alg = MBEDTLS_MD_SHA384; ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384; diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 4fab4ed16..ec9201f8b 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -759,9 +759,8 @@ static int ssl_check_key_curve( mbedtls_pk_context *pk, * return 0 on success and -1 on failure. */ static int ssl_pick_cert( mbedtls_ssl_context *ssl, - const mbedtls_ssl_ciphersuite_t * ciphersuite_info, + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info, mbedtls_ecp_group_id const *acceptable_ec_grp_ids ) - { mbedtls_ssl_key_cert *cur, *list, *fallback = NULL; mbedtls_pk_type_t pk_alg = @@ -920,10 +919,10 @@ static int ssl_pick_cert( mbedtls_ssl_context *ssl, * Sets ciphersuite_info only if the suite matches. */ static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, - const mbedtls_ssl_ciphersuite_t **ciphersuite_info, + mbedtls_ssl_ciphersuite_handle_t *ciphersuite_info, mbedtls_ecp_group_id const *acceptable_ec_grp_ids ) { - const mbedtls_ssl_ciphersuite_t *suite_info; + mbedtls_ssl_ciphersuite_handle_t suite_info; #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) @@ -931,16 +930,17 @@ static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, #endif suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id ); - if( suite_info == NULL ) + if( suite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); } - MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", + mbedtls_ssl_suite_get_name( suite_info ) ) ); - if( suite_info->min_minor_ver > ssl->minor_ver || - suite_info->max_minor_ver < ssl->minor_ver ) + if( mbedtls_ssl_suite_get_min_minor_ver( suite_info ) > ssl->minor_ver || + mbedtls_ssl_suite_get_max_minor_ver( suite_info ) < ssl->minor_ver ) { MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) ); return( 0 ); @@ -948,13 +948,16 @@ static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, #if defined(MBEDTLS_SSL_PROTO_DTLS) if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && - ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) ) + ( mbedtls_ssl_suite_get_flags( suite_info ) & + MBEDTLS_CIPHERSUITE_NODTLS ) ) + { return( 0 ); + } #endif #if defined(MBEDTLS_ARC4_C) if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED && - suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) + mbedtls_ssl_suite_get_cipher( suite_info ) == MBEDTLS_CIPHER_ARC4_128 ) { MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) ); return( 0 ); @@ -962,7 +965,8 @@ static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, #endif #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && + if( mbedtls_ssl_suite_get_key_exchange( suite_info ) == + MBEDTLS_KEY_EXCHANGE_ECJPAKE && ( ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK ) == 0 ) { MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: ecjpake " @@ -1044,7 +1048,7 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) unsigned int ciph_len, sess_len, chal_len; unsigned char *buf, *p; const int *ciphersuites; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); @@ -1235,7 +1239,7 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) got_common_suite = 0; ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; - ciphersuite_info = NULL; + ciphersuite_info = MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) for( i = 0; ciphersuites[i] != 0; i++ ) @@ -1258,7 +1262,7 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) return( ret ); } - if( ciphersuite_info != NULL ) + if( ciphersuite_info != MBEDTLS_SSL_CIPHERSUITE_INVALD_HANDLE ) goto have_ciphersuite_v2; } @@ -1275,7 +1279,8 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) } have_ciphersuite_v2: - MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); + MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", + mbedtls_ssl_suite_get_name( ciphersuite_info ) ) ); ssl->session_negotiate->ciphersuite = ciphersuites[i]; ssl->handshake->ciphersuite_info = ciphersuite_info; @@ -1323,7 +1328,7 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) #endif int handshake_failure = 0; const int *ciphersuites; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; int major, minor; #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ @@ -2129,7 +2134,7 @@ read_record_header: */ got_common_suite = 0; ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; - ciphersuite_info = NULL; + ciphersuite_info = MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) for( i = 0; ciphersuites[i] != 0; i++ ) @@ -2151,7 +2156,7 @@ read_record_header: return( ret ); } - if( ciphersuite_info != NULL ) + if( ciphersuite_info != MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) goto have_ciphersuite; } @@ -2172,7 +2177,8 @@ read_record_header: } have_ciphersuite: - MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) ); + MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", + mbedtls_ssl_suite_get_name( ciphersuite_info ) ) ); ssl->session_negotiate->ciphersuite = ciphersuites[i]; ssl->handshake->ciphersuite_info = ciphersuite_info; @@ -2290,7 +2296,8 @@ static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, size_t *olen ) { unsigned char *p = buf; - const mbedtls_ssl_ciphersuite_t *suite = NULL; + mbedtls_ssl_ciphersuite_handle_t suite = + MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; const mbedtls_cipher_info_t *cipher = NULL; if( ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED || @@ -2306,9 +2313,17 @@ static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, * with Associated Data (AEAD) ciphersuite, it MUST NOT send an * encrypt-then-MAC response extension back to the client." */ - if( ( suite = mbedtls_ssl_ciphersuite_from_id( - ssl->session_negotiate->ciphersuite ) ) == NULL || - ( cipher = mbedtls_cipher_info_from_type( suite->cipher ) ) == NULL || + suite = mbedtls_ssl_ciphersuite_from_id( + ssl->session_negotiate->ciphersuite ); + if( suite == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) + { + *olen = 0; + return; + } + + cipher = mbedtls_cipher_info_from_type( + mbedtls_ssl_suite_get_cipher( suite ) ); + if( cipher == NULL || cipher->mode != MBEDTLS_MODE_CBC ) { *olen = 0; @@ -2491,7 +2506,7 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, *olen = 0; /* Skip costly computation if not needed */ - if( ssl->handshake->ciphersuite_info->key_exchange != + if( mbedtls_ssl_suite_get_key_exchange( ssl->handshake->ciphersuite_info ) != MBEDTLS_KEY_EXCHANGE_ECJPAKE ) return; @@ -2884,7 +2899,7 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); @@ -2903,7 +2918,7 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) { int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; size_t dn_size, total_dn_size; /* excluding length bytes */ size_t ct_len, sa_len; /* including length bytes */ @@ -3134,7 +3149,7 @@ static int ssl_resume_server_key_exchange( mbedtls_ssl_context *ssl, static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, size_t *signature_len ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) @@ -3160,7 +3175,8 @@ static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, * - ECJPAKE key exchanges */ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) { int ret; size_t len = 0; @@ -3188,8 +3204,8 @@ static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, **/ #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == MBEDTLS_KEY_EXCHANGE_DHE_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) { ssl->out_msg[ssl->out_msglen++] = 0x00; ssl->out_msg[ssl->out_msglen++] = 0x00; @@ -3353,7 +3369,8 @@ static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ defined(MBEDTLS_SSL_PROTO_TLS1_1) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ) { /* B: Default hash SHA1 */ md_alg = MBEDTLS_MD_SHA1; @@ -3495,7 +3512,7 @@ static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) int ret; size_t signature_len = 0; #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ @@ -3939,7 +3956,7 @@ static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned cha static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) { int ret; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; unsigned char *p, *end; ciphersuite_info = ssl->handshake->ciphersuite_info; @@ -3949,8 +3966,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \ ( defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) ) - if( ( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) && + if( ( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == MBEDTLS_KEY_EXCHANGE_RSA_PSK || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == MBEDTLS_KEY_EXCHANGE_RSA ) && ( ssl->handshake->async_in_progress != 0 ) ) { /* We've already read a record and there is an asynchronous @@ -3982,7 +3999,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) } #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) { if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 ) { @@ -4015,10 +4033,14 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \ defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || - ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDH_RSA || + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) + == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) { if( ( ret = mbedtls_ecdh_read_public( &ssl->handshake->ecdh_ctx, p, end - p) ) != 0 ) @@ -4050,7 +4072,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_PSK ) { if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) { @@ -4065,7 +4088,7 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) } if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, - ciphersuite_info->key_exchange ) ) != 0 ) + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); return( ret ); @@ -4074,7 +4097,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) else #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_RSA_PSK ) { #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) if ( ssl->handshake->async_in_progress != 0 ) @@ -4102,7 +4126,7 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) } if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, - ciphersuite_info->key_exchange ) ) != 0 ) + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); return( ret ); @@ -4111,7 +4135,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) else #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_DHE_PSK ) { if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) { @@ -4131,7 +4156,7 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) } if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, - ciphersuite_info->key_exchange ) ) != 0 ) + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); return( ret ); @@ -4140,7 +4165,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) else #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) { if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) { @@ -4159,7 +4185,7 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) MBEDTLS_DEBUG_ECDH_QP ); if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, - ciphersuite_info->key_exchange ) ) != 0 ) + mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); return( ret ); @@ -4168,7 +4194,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) else #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_RSA ) { if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 ) { @@ -4179,7 +4206,8 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) else #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_ECJPAKE ) { ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx, p, end - p ); @@ -4222,7 +4250,7 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) #if !defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED) static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); @@ -4249,7 +4277,7 @@ static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) mbedtls_pk_type_t pk_alg; #endif mbedtls_md_type_t md_alg; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; mbedtls_pk_context *peer_pk = NULL; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 84c78c48e..c245145b7 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -801,7 +801,7 @@ static int ssl_populate_transform( mbedtls_ssl_transform *transform, size_t mac_key_len; size_t iv_copy_len; unsigned keylen; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; const mbedtls_cipher_info_t *cipher_info; const mbedtls_md_info_t *md_info; @@ -823,26 +823,28 @@ static int ssl_populate_transform( mbedtls_ssl_transform *transform, * Get various info structures */ ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); - if( ciphersuite_info == NULL ) + if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", ciphersuite ) ); return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); } - cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); + cipher_info = mbedtls_cipher_info_from_type( + mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ); if( cipher_info == NULL ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", - ciphersuite_info->cipher ) ); + mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) ); return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); } - md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); + md_info = mbedtls_md_info_from_type( + mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); if( md_info == NULL ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", - ciphersuite_info->mac ) ); + mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) ); return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); } @@ -899,8 +901,8 @@ static int ssl_populate_transform( mbedtls_ssl_transform *transform, transform->maclen = 0; mac_key_len = 0; - transform->taglen = - ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; + transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) & + MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; /* All modes haves 96-bit IVs; * GCM and CCM has 4 implicit and 8 explicit bytes @@ -1338,15 +1340,15 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) { int ret; - const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t const ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); /* Set PRF, calc_verify and calc_finished function pointers */ ret = ssl_set_handshake_prfs( ssl->handshake, - ssl->minor_ver, - ciphersuite_info->mac ); + ssl->minor_ver, + mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); if( ret != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); @@ -6070,7 +6072,7 @@ static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) /* No certificate support -> dummy functions */ int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); @@ -6087,7 +6089,7 @@ int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); @@ -6110,7 +6112,7 @@ int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; size_t i, n; const mbedtls_x509_crt *crt; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); @@ -6474,7 +6476,7 @@ static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, int authmode ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) @@ -6483,8 +6485,11 @@ static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_SSL_SRV_C) if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) { - if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == + MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + { return( SSL_CERTIFICATE_SKIP ); + } if( authmode == MBEDTLS_SSL_VERIFY_NONE ) { @@ -6506,8 +6511,7 @@ static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, void *rs_ctx ) { int verify_ret; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = mbedtls_x509_crt *ca_chain; mbedtls_x509_crl *ca_crl; @@ -6973,7 +6977,7 @@ int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) } void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, - const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info ) { ((void) ciphersuite_info); @@ -6985,12 +6989,12 @@ void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, #endif #if defined(MBEDTLS_SSL_PROTO_TLS1_2) #if defined(MBEDTLS_SHA512_C) - if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) + if( mbedtls_ssl_suite_get_mac( ciphersuite_info ) == MBEDTLS_MD_SHA384 ) ssl->handshake->update_checksum = ssl_update_checksum_sha384; else #endif #if defined(MBEDTLS_SHA256_C) - if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) + if( mbedtls_ssl_suite_get_mac( ciphersuite_info ) != MBEDTLS_MD_SHA384 ) ssl->handshake->update_checksum = ssl_update_checksum_sha256; else #endif @@ -11242,7 +11246,7 @@ int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, #if defined(MBEDTLS_X509_CRT_PARSE_C) int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, - const mbedtls_ssl_ciphersuite_t *ciphersuite, + mbedtls_ssl_ciphersuite_handle_t ciphersuite, int cert_endpoint, uint32_t *flags ) { @@ -11266,7 +11270,7 @@ int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) { /* Server part of the key exchange */ - switch( ciphersuite->key_exchange ) + switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) ) { case MBEDTLS_KEY_EXCHANGE_RSA: case MBEDTLS_KEY_EXCHANGE_RSA_PSK: diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index c63b44550..56641f54d 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -56,6 +56,7 @@ int main( void ) #include "mbedtls/net_sockets.h" #include "mbedtls/ssl.h" +#include "mbedtls/ssl_ciphersuites.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/certs.h" @@ -1296,19 +1297,19 @@ int main( int argc, char *argv[] ) if( opt.force_ciphersuite[0] > 0 ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] ); if( opt.max_version != -1 && - ciphersuite_info->min_minor_ver > opt.max_version ) + mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) > opt.max_version ) { mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" ); ret = 2; goto usage; } if( opt.min_version != -1 && - ciphersuite_info->max_minor_ver < opt.min_version ) + mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) < opt.min_version ) { mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" ); ret = 2; @@ -1318,13 +1319,13 @@ int main( int argc, char *argv[] ) /* If the server selects a version that's not supported by * this suite, then there will be no common ciphersuite... */ if( opt.max_version == -1 || - opt.max_version > ciphersuite_info->max_minor_ver ) + opt.max_version > mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) ) { - opt.max_version = ciphersuite_info->max_minor_ver; + opt.max_version = mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ); } - if( opt.min_version < ciphersuite_info->min_minor_ver ) + if( opt.min_version < mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) ) { - opt.min_version = ciphersuite_info->min_minor_ver; + opt.min_version = mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ); /* DTLS starts with TLS 1.1 */ if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 ) @@ -1332,7 +1333,7 @@ int main( int argc, char *argv[] ) } /* Enable RC4 if needed and not explicitly disabled */ - if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) + if( mbedtls_ssl_suite_get_cipher( ciphersuite_info ) == MBEDTLS_CIPHER_ARC4_128 ) { if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED ) { diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 876a7a06c..4312629c5 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -55,6 +55,7 @@ int main( void ) #include "mbedtls/net_sockets.h" #include "mbedtls/ssl.h" +#include "mbedtls/ssl_ciphersuites.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/certs.h" @@ -2018,19 +2019,19 @@ int main( int argc, char *argv[] ) if( opt.force_ciphersuite[0] > 0 ) { - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] ); if( opt.max_version != -1 && - ciphersuite_info->min_minor_ver > opt.max_version ) + mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) > opt.max_version ) { mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" ); ret = 2; goto usage; } if( opt.min_version != -1 && - ciphersuite_info->max_minor_ver < opt.min_version ) + mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) < opt.min_version ) { mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" ); ret = 2; @@ -2040,13 +2041,13 @@ int main( int argc, char *argv[] ) /* If we select a version that's not supported by * this suite, then there will be no common ciphersuite... */ if( opt.max_version == -1 || - opt.max_version > ciphersuite_info->max_minor_ver ) + opt.max_version > mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ) ) { - opt.max_version = ciphersuite_info->max_minor_ver; + opt.max_version = mbedtls_ssl_suite_get_max_minor_ver( ciphersuite_info ); } - if( opt.min_version < ciphersuite_info->min_minor_ver ) + if( opt.min_version < mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ) ) { - opt.min_version = ciphersuite_info->min_minor_ver; + opt.min_version = mbedtls_ssl_suite_get_min_minor_ver( ciphersuite_info ); /* DTLS starts with TLS 1.1 */ if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 ) @@ -2054,7 +2055,7 @@ int main( int argc, char *argv[] ) } /* Enable RC4 if needed and not explicitly disabled */ - if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 ) + if( mbedtls_ssl_suite_get_cipher( ciphersuite_info ) == MBEDTLS_CIPHER_ARC4_128 ) { if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED ) { From b09132d33ac05afc204edde83d6b9f91908a67da Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 26 Jun 2019 10:53:02 +0100 Subject: [PATCH 03/22] Introduce framework for macro-based definitions of ciphersuites This commit is a step towards the goal of allowing to hardcode the choice of a single ciphersuite at compile-time. The hoped for benefit of this is that whereever a ciphersuite attribute is queried and checked against a compile-time constant, the check can be recognized as either true or false at compile-time, hence leading to a code-size reduction. For this to work, the ciphersuite attribute getter functions mbedtls_ssl_suite_get_xxx() will be modified to return something the compiler can recognize as a compile-time constant. In particular, in order to avoid relying on constant propagation abilities of the compiler, these functions should ideally return constant symbols (instead of, say, fields in a globally const structure instance). This puts us in the following situation: On the one hand, there's the array of ciphersuite information structures defining the attribute of those ciphersuites the stack knows about. On the other hand, we need direct access to those fields through constant symbols in the getter functions. In order to avoid any duplication of information, this commit exemplifies how ciphersuites can be conveniently defined on the basis of macro definitions, and how the corresponding instances of the ciphersuite information structure can be auto-generated from this. In the approach, to add support for a ciphersuite with official name NAME (such as TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8), the following macro constants need to be defined in ssl_ciphersuites.h: MBEDTLS_SUITE__ NAME __ID MBEDTLS_SUITE__ NAME __NAME MBEDTLS_SUITE__ NAME __CIPHER MBEDTLS_SUITE__ NAME __MAC ... To make check-names.sh happy, one also needs a dummy macro MBEDTLS_SUITE__ NAME() These ciphersuite attribute values can then be queried via MBEDTLS_SSL_SUITE_ID( NAME_MACRO ) ... where NAME_MACRO can be any macro expanding to a defined NAME. Further, a convenience macro MBEDTLS_SSL_SUITE_INFO( NAME_MACRO ) is provided that again takes a macro NAME_MACRO expanding to a defined NAME, and itself expands to an instance of mbedtls_ssl_ciphersuite_info_t using the macro attributes defined for NAME. This macro is then used in ssl_ciphersuites.c when defining the array of known ciphersuite information structures, (a) without duplicating the information, and (b) with increased readability, because there's only one line for each ciphersuite. --- include/mbedtls/ssl_ciphersuites.h | 68 ++++++++++++++++++++++++++++++ library/ssl_ciphersuites.c | 6 +-- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index bb55a4b65..3d3805c40 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -33,6 +33,7 @@ #include "pk.h" #include "cipher.h" #include "md.h" +#include "ssl.h" #ifdef __cplusplus extern "C" { @@ -311,6 +312,58 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; eg for CCM_8 */ #define MBEDTLS_CIPHERSUITE_NODTLS 0x04 /**< Can't be used with DTLS */ +/* + * Ciphersuite macro definitions + * + * This is highly incomplete and only contains those ciphersuites for + * which we need to be able to build the library with support for that + * ciphersuite only (currently MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 + * as an example). + */ + +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8() +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_ID MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_NAME "TLS-ECDHE-ECDSA-WITH-AES-128-CCM-8" +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_CIPHER MBEDTLS_CIPHER_AES_128_CCM +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_MAC MBEDTLS_MD_SHA256 +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_KEY_EXCHANGE MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_MIN_MAJOR_VER MBEDTLS_SSL_MAJOR_VERSION_3 +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_MIN_MINOR_VER MBEDTLS_SSL_MINOR_VERSION_3 +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_MAX_MAJOR_VER MBEDTLS_SSL_MAJOR_VERSION_3 +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_MAX_MINOR_VER MBEDTLS_SSL_MINOR_VERSION_3 +#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_FLAGS MBEDTLS_CIPHERSUITE_SHORT_TAG + +/* + * Helper macros to extract fields from ciphersuites. + */ + +#define MBEDTLS_SSL_SUITE_ID_T( SUITE ) SUITE ## _ID +#define MBEDTLS_SSL_SUITE_NAME_T( SUITE ) SUITE ## _NAME +#define MBEDTLS_SSL_SUITE_CIPHER_T( SUITE ) SUITE ## _CIPHER +#define MBEDTLS_SSL_SUITE_MAC_T( SUITE ) SUITE ## _MAC +#define MBEDTLS_SSL_SUITE_KEY_EXCHANGE_T( SUITE ) SUITE ## _KEY_EXCHANGE +#define MBEDTLS_SSL_SUITE_MIN_MAJOR_VER_T( SUITE ) SUITE ## _MIN_MAJOR_VER +#define MBEDTLS_SSL_SUITE_MIN_MINOR_VER_T( SUITE ) SUITE ## _MIN_MINOR_VER +#define MBEDTLS_SSL_SUITE_MAX_MAJOR_VER_T( SUITE ) SUITE ## _MAX_MAJOR_VER +#define MBEDTLS_SSL_SUITE_MAX_MINOR_VER_T( SUITE ) SUITE ## _MAX_MINOR_VER +#define MBEDTLS_SSL_SUITE_FLAGS_T( SUITE ) SUITE ## _FLAGS + +/* Wrapper around MBEDTLS_SSL_SUITE_XXX_T() which makes sure that + * the argument is macro-expanded before concatenated with the + * field name. This allows to call these macros as + * MBEDTLS_SSL_SUITE_XXX( MBEDTLS_SSL_SINGLE_CIPHERSUITE ), + * where MBEDTLS_SSL_SINGLE_CIPHERSUITE expands to MBEDTLS_SSL_SUITE_XXX. */ +#define MBEDTLS_SSL_SUITE_ID( SUITE ) MBEDTLS_SSL_SUITE_ID_T( SUITE ) +#define MBEDTLS_SSL_SUITE_NAME( SUITE ) MBEDTLS_SSL_SUITE_NAME_T( SUITE ) +#define MBEDTLS_SSL_SUITE_CIPHER( SUITE ) MBEDTLS_SSL_SUITE_CIPHER_T( SUITE ) +#define MBEDTLS_SSL_SUITE_MAC( SUITE ) MBEDTLS_SSL_SUITE_MAC_T( SUITE ) +#define MBEDTLS_SSL_SUITE_KEY_EXCHANGE( SUITE ) MBEDTLS_SSL_SUITE_KEY_EXCHANGE_T( SUITE ) +#define MBEDTLS_SSL_SUITE_MIN_MAJOR_VER( SUITE ) MBEDTLS_SSL_SUITE_MIN_MAJOR_VER_T( SUITE ) +#define MBEDTLS_SSL_SUITE_MIN_MINOR_VER( SUITE ) MBEDTLS_SSL_SUITE_MIN_MINOR_VER_T( SUITE ) +#define MBEDTLS_SSL_SUITE_MAX_MAJOR_VER( SUITE ) MBEDTLS_SSL_SUITE_MAX_MAJOR_VER_T( SUITE ) +#define MBEDTLS_SSL_SUITE_MAX_MINOR_VER( SUITE ) MBEDTLS_SSL_SUITE_MAX_MINOR_VER_T( SUITE ) +#define MBEDTLS_SSL_SUITE_FLAGS( SUITE ) MBEDTLS_SSL_SUITE_FLAGS_T( SUITE ) + /** * \brief This structure is used for storing ciphersuite information */ @@ -334,6 +387,21 @@ struct mbedtls_ssl_ciphersuite_t typedef mbedtls_ssl_ciphersuite_t const * mbedtls_ssl_ciphersuite_handle_t; #define MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ( (mbedtls_ssl_ciphersuite_handle_t) NULL ) +/** + * \brief This macro builds an instance of ::mbedtls_ssl_ciphersuite_t + * from an \c MBEDTLS_SUITE_XXX identifier. + */ +#define MBEDTLS_SSL_SUITE_INFO( SUITE ) \ + { MBEDTLS_SSL_SUITE_ID( SUITE ), \ + MBEDTLS_SSL_SUITE_NAME( SUITE ), \ + MBEDTLS_SSL_SUITE_CIPHER( SUITE ), \ + MBEDTLS_SSL_SUITE_MAC( SUITE ), \ + MBEDTLS_SSL_SUITE_KEY_EXCHANGE( SUITE ), \ + MBEDTLS_SSL_SUITE_MIN_MAJOR_VER( SUITE ), \ + MBEDTLS_SSL_SUITE_MIN_MINOR_VER( SUITE ), \ + MBEDTLS_SSL_SUITE_MAX_MAJOR_VER( SUITE ), \ + MBEDTLS_SSL_SUITE_MAX_MINOR_VER( SUITE ), \ + MBEDTLS_SSL_SUITE_FLAGS( SUITE ) } /* * Getter functions for the extraction of ciphersuite attributes * from a ciphersuite handle. diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index 8378a9017..ffe03412a 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -449,11 +449,7 @@ static const mbedtls_ssl_ciphersuite_t ciphersuite_definitions[] = MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3, 0 }, - { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, "TLS-ECDHE-ECDSA-WITH-AES-128-CCM-8", - MBEDTLS_CIPHER_AES_128_CCM, MBEDTLS_MD_SHA256, MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA, - MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3, - MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3, - MBEDTLS_CIPHERSUITE_SHORT_TAG }, + MBEDTLS_SSL_SUITE_INFO( MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 ), #endif /* MBEDTLS_CCM_C */ #endif /* MBEDTLS_AES_C */ From 5cce936e622e96f47b28d3ee5156398b71d8f6a1 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 26 Jun 2019 11:39:32 +0100 Subject: [PATCH 04/22] Add compile-time option to hardcode choice of single ciphersuite --- configs/baremetal.h | 1 + include/mbedtls/config.h | 11 ++++ include/mbedtls/ssl_ciphersuites.h | 82 ++++++++++++++++++++++++++++++ library/ssl_ciphersuites.c | 60 +++++++++++++++++++++- 4 files changed, 152 insertions(+), 2 deletions(-) diff --git a/configs/baremetal.h b/configs/baremetal.h index ee514b463..99d54106a 100644 --- a/configs/baremetal.h +++ b/configs/baremetal.h @@ -60,6 +60,7 @@ /* Key exchanges */ #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED #define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 +//#define MBEDTLS_SSL_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 /* Digests - just SHA-256 */ #define MBEDTLS_MD_C diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 91d25501f..2b7e9da1a 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -3641,6 +3641,17 @@ //#define MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET MBEDTLS_SSL_EXTENDED_MS_ENABLED //#define MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED +/* Set this to MBEDTLS_SUITE_{OFFICIAL_SUITE_NAME} to hardcode + * the choice of a fixed ciphersuite at compile-time. + * + * You need to make sure that the corresponding ciphersuite attributes + * are defined through macros in ssl_ciphersuites.h. See the definitions + * of MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_XXX for an example. + * + * If this option is set, the API mbedtls_ssl_conf_ciphersuites() is removed. + */ +//#define MBEDTLS_SSL_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 + /* \} SECTION: Compile-time SSL configuration */ /* Target and application specific configurations diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index 3d3805c40..2f31ceede 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -364,6 +364,7 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; #define MBEDTLS_SSL_SUITE_MAX_MINOR_VER( SUITE ) MBEDTLS_SSL_SUITE_MAX_MINOR_VER_T( SUITE ) #define MBEDTLS_SSL_SUITE_FLAGS( SUITE ) MBEDTLS_SSL_SUITE_FLAGS_T( SUITE ) +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) /** * \brief This structure is used for storing ciphersuite information */ @@ -402,6 +403,15 @@ typedef mbedtls_ssl_ciphersuite_t const * mbedtls_ssl_ciphersuite_handle_t; MBEDTLS_SSL_SUITE_MAX_MAJOR_VER( SUITE ), \ MBEDTLS_SSL_SUITE_MAX_MINOR_VER( SUITE ), \ MBEDTLS_SSL_SUITE_FLAGS( SUITE ) } + +#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + +typedef unsigned char mbedtls_ssl_ciphersuite_handle_t; +#define MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ( (mbedtls_ssl_ciphersuite_handle_t) 0 ) +#define MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ( (mbedtls_ssl_ciphersuite_handle_t) 1 ) + +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + /* * Getter functions for the extraction of ciphersuite attributes * from a ciphersuite handle. @@ -411,6 +421,7 @@ typedef mbedtls_ssl_ciphersuite_t const * mbedtls_ssl_ciphersuite_handle_t; * is passed. */ +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) /* * Implementation of getter functions when the ciphersuite handle * is a pointer to the ciphersuite information structure. @@ -468,6 +479,77 @@ static inline unsigned char mbedtls_ssl_suite_get_flags( { return( info->flags ); } +#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +/* + * Implementations of getter functions in the case of only + * a single possible ciphersuite. In this case, the handle + * is logically a boolean (either the invalid handle or the + * unique valid handle representing the single enabled + * ciphersuite), and the precondition that the handle is valid + * means that we can statically return the hardcoded attribute + * of the enabled ciphersuite. + */ +static inline int mbedtls_ssl_suite_get_id( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline const char* mbedtls_ssl_suite_get_name( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline mbedtls_cipher_type_t mbedtls_ssl_suite_get_cipher( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_CIPHER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline mbedtls_md_type_t mbedtls_ssl_suite_get_mac( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_MAC( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline mbedtls_key_exchange_type_t mbedtls_ssl_suite_get_key_exchange( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_KEY_EXCHANGE( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline int mbedtls_ssl_suite_get_min_major_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_MIN_MAJOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline int mbedtls_ssl_suite_get_min_minor_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_MIN_MINOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline int mbedtls_ssl_suite_get_max_major_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_MAX_MAJOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline int mbedtls_ssl_suite_get_max_minor_ver( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_MAX_MINOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +static inline unsigned char mbedtls_ssl_suite_get_flags( + mbedtls_ssl_ciphersuite_handle_t const info ) +{ + ((void) info); + return( MBEDTLS_SSL_SUITE_FLAGS( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ static inline int mbedtls_ssl_ciphersuite_has_pfs( mbedtls_ssl_ciphersuite_handle_t info ) diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index ffe03412a..d37d7a2e7 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -54,6 +54,7 @@ */ static const int ciphersuite_preference[] = { +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) #if defined(MBEDTLS_SSL_CIPHERSUITES) MBEDTLS_SSL_CIPHERSUITES, #else @@ -311,9 +312,13 @@ static const int ciphersuite_preference[] = MBEDTLS_TLS_PSK_WITH_NULL_SHA, #endif /* MBEDTLS_SSL_CIPHERSUITES */ +#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ), +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ 0 }; +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) static const mbedtls_ssl_ciphersuite_t ciphersuite_definitions[] = { #if defined(MBEDTLS_CHACHAPOLY_C) && \ @@ -2166,8 +2171,9 @@ static const mbedtls_ssl_ciphersuite_t ciphersuite_definitions[] = MBEDTLS_CIPHER_NONE, MBEDTLS_MD_NONE, MBEDTLS_KEY_EXCHANGE_NONE, 0, 0, 0, 0, 0 } }; +#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ -#if defined(MBEDTLS_SSL_CIPHERSUITES) +#if defined(MBEDTLS_SSL_CIPHERSUITES) || defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) const int *mbedtls_ssl_list_ciphersuites( void ) { return( ciphersuite_preference ); @@ -2226,8 +2232,9 @@ const int *mbedtls_ssl_list_ciphersuites( void ) return( supported_ciphersuites ); } -#endif /* MBEDTLS_SSL_CIPHERSUITES */ +#endif /* !( MBEDTLS_SSL_CIPHERSUITES || MBEDTLS_SSL_SINGLE_CIPHERSUITE ) */ +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ) { @@ -2286,6 +2293,55 @@ int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) return( cur->id ); } +#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( + const char *ciphersuite_name ) +{ + static const char * const single_suite_name = + MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + + if( strcmp( ciphersuite_name, single_suite_name ) == 0 ) + return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); + + return( MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ); +} + +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuite ) +{ + static const int single_suite_id = + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + + if( ciphersuite == single_suite_id ) + return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); + + return( MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ); +} + +const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id ) +{ + static const int single_suite_id = + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + + if( ciphersuite_id == single_suite_id ) + return( MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + + return( NULL ); +} + +int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) +{ + static const char * const single_suite_name = + MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + + if( strcmp( ciphersuite_name, single_suite_name ) == 0 ) + return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + + return( 0 ); +} + +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + #if defined(MBEDTLS_PK_C) mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ) { From 1499027d029e1d6a76d9264e280b72f59dd469ba Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 26 Jun 2019 11:47:15 +0100 Subject: [PATCH 05/22] Adapt ClientHello writing to case of single hardcoded ciphersuite This commit modifies the ClientHello writing routine ssl_write_client_hello in ssl_cli.c to switch between (a) listing all runtime configured ciphersuites (in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is not defined) (b) listing just the single hardcoded ciphersuite (in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is defined) The approach taken is to introduce a pair of helper macros MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE which when delimiting a block of code lead to that block of code being run once for each ciphersuite that's enabled in the context `ssl` and version `ver`, referenced through the (fresh) `info` variable. Internally, this is implemented either through a plain `for` loop traversing the runtime configured ciphersuite list (if MBEDTLS_SSL_SINGLE_CIPHERSUITE is disabled) or by just hardcoding `info` to the single enabled ciphersuite (if MBEDTLS_SSL_SINGLE_CIPHERSUITE is enabled). These helper macros will prove useful whereever previous code traversed the runtime configured ciphersuite list, but adaptations of those occasions outside ClientHello writing are left for later commits. --- include/mbedtls/ssl_internal.h | 37 ++++++++++++++ library/ssl_cli.c | 93 +++++++++++++++++++++------------- 2 files changed, 95 insertions(+), 35 deletions(-) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index b2382fab9..bca249d31 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -1430,4 +1430,41 @@ static inline unsigned int mbedtls_ssl_conf_get_ems_enforced( } #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ +/* + * Macros for the traversal of the list of all enabled ciphersuites. + * This is implemented as a plain loop in case we have a runtime + * configurable list of ciphersuites, and as a simple variable + * instantiation in case a single ciphersuite is enabled at + * compile-time. + */ +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) + +#define MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) \ + { \ + int const *__id_ptr; \ + for( __id_ptr=(ssl)->conf->ciphersuite_list[ (ver) ]; \ + *__id_ptr != 0; __id_ptr++ ) \ + { \ + const int __id = *__id_ptr; \ + mbedtls_ssl_ciphersuite_handle_t info; \ + info = mbedtls_ssl_ciphersuite_from_id( __id ); \ + if( info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) \ + continue; + +#define MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE \ + } \ + } + +#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + +#define MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) \ + { \ + const mbedtls_ssl_ciphersuite_handle_t info = \ + MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE; + +#define MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE \ + } + +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + #endif /* ssl_internal.h */ diff --git a/library/ssl_cli.c b/library/ssl_cli.c index fa5b64ec5..bf5ec11cf 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -831,8 +831,6 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) unsigned char *buf; unsigned char *p, *q; unsigned char offer_compress; - const int *ciphersuites; - mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) int uses_ec = 0; @@ -972,21 +970,27 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) /* * Ciphersuite list */ - ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; /* Skip writing ciphersuite length for now */ n = 0; q = p; p += 2; - for( i = 0; ciphersuites[i] != 0; i++ ) + MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, + ssl->minor_ver, + ciphersuite_info ) { - ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); - if( ssl_validate_ciphersuite( ciphersuite_info, ssl, ssl->conf->min_minor_ver, ssl->conf->max_minor_ver ) != 0 ) - continue; + { + /* Logically, we want to continue the ciphersuite iteration + * here, but We can't just use `continue` because + * MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE() + * doesn't unfold to a loop in case only a single + * ciphersuite is enabled. */ + goto next_suite; + } MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x", mbedtls_ssl_suite_get_id( ciphersuite_info ) ) ); @@ -1001,7 +1005,13 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) mbedtls_ssl_suite_get_id( ciphersuite_info ) >> 8 ); *p++ = (unsigned char)( mbedtls_ssl_suite_get_id( ciphersuite_info ) ); + + next_suite: + /* Need something here to avoid + * 'label at end of compound statement' error. */ + ((void) 0); } + MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites (excluding SCSVs)", n ) ); @@ -1626,7 +1636,9 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) int extended_ms_seen = 0; #endif int handshake_failure = 0; - mbedtls_ssl_ciphersuite_handle_t suite_info; + + /* The ciphersuite chosen by the server. */ + mbedtls_ssl_ciphersuite_handle_t server_suite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) ); @@ -1802,17 +1814,18 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) /* * Initialize update checksum functions */ - ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i ); - if( ssl->handshake->ciphersuite_info == - MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) + server_suite_info = mbedtls_ssl_ciphersuite_from_id( i ); +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) + ssl->handshake->ciphersuite_info = server_suite_info; +#endif + if( server_suite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) ); mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); } - - mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info ); + mbedtls_ssl_optimize_checksum( ssl, server_suite_info ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) ); MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n ); @@ -1875,38 +1888,48 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) /* * Perform cipher suite validation in same way as in ssl_write_client_hello. */ - i = 0; - while( 1 ) + MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, + ssl->minor_ver, + ciphersuite_info ) { - if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 ) + if( ssl_validate_ciphersuite( ciphersuite_info, ssl, + ssl->conf->min_minor_ver, + ssl->conf->max_minor_ver ) != 0 ) { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); - mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, - MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); - return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); + /* Logically, we want to continue the ciphersuite iteration + * here, but We can't just use `continue` because + * MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE() + * doesn't unfold to a loop in case only a single + * ciphersuite is enabled. */ + goto next_suite; } - if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] == - ssl->session_negotiate->ciphersuite ) - { - break; - } - } + if( ciphersuite_info != server_suite_info ) + goto next_suite; - suite_info = mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ); - if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver, ssl->minor_ver ) != 0 ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); - mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, - MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); - return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); + goto server_picked_valid_suite; + + next_suite: + /* Need something here to avoid + * 'label at end of compound statement' error. */ + ((void) 0); } + MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE + + /* If we reach this code-path, the server's chosen ciphersuite + * wasn't among those advertised by us. */ + MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) ); + mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, + MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER ); + return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); + +server_picked_valid_suite: MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", - mbedtls_ssl_suite_get_name( suite_info ) ) ); + mbedtls_ssl_suite_get_name( server_suite_info ) ) ); #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) - if( mbedtls_ssl_suite_get_key_exchange( suite_info ) == + if( mbedtls_ssl_suite_get_key_exchange( server_suite_info ) == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA && ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) { From 2d46b4f2a1270c5d252816683e7e2c5f3e849514 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Mon, 1 Jul 2019 11:06:34 +0100 Subject: [PATCH 06/22] Adapt ClientHello parsing to case of single hardcoded ciphersuite This commit adapts the ClientHello parsing routines in ssl_srv.c to use the ciphersuite traversal macros MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE introduced in the last commit, thereby making them work both with and without MBEDTLS_SSL_SINGLE_CIPHERSUITE. Another notable change concerns the ssl_ciphersuite_match: Previous, this function would take a ciphersuite ID and a pointer to a destination ciphersuite info structure as input and write eithe NULL or a valid ciphersuite info structure to that destination address, depending on whether the suite corresponding to the given ID was suitable or not. The function would always return 0 outside of a fatal error. This commit changes this to ssl_ciphersuite_is_match() which instead already takes a ciphersuite handle (which outside of a hardcoded ciphersuite is the same as the ptr to a ciphersuite info structure) and returns 0 or 1 (or a negative error code in case of a fatal error) indicating whether the suite corresponding to the handle was acceptable or not. The conversion of the ciphersuite ID to the ciphersuite info structure is done prior to calling ssl_ciphersuite_is_match(). --- library/ssl_srv.c | 146 +++++++++++++++++++++++++++++----------------- 1 file changed, 91 insertions(+), 55 deletions(-) diff --git a/library/ssl_srv.c b/library/ssl_srv.c index ec9201f8b..43664feb8 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -918,24 +918,15 @@ static int ssl_pick_cert( mbedtls_ssl_context *ssl, * Check if a given ciphersuite is suitable for use with our config/keys/etc * Sets ciphersuite_info only if the suite matches. */ -static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, - mbedtls_ssl_ciphersuite_handle_t *ciphersuite_info, - mbedtls_ecp_group_id const *acceptable_ec_grp_ids ) +static int ssl_ciphersuite_is_match( mbedtls_ssl_context *ssl, + mbedtls_ssl_ciphersuite_handle_t suite_info, + mbedtls_ecp_group_id const *acceptable_ec_grp_ids ) { - mbedtls_ssl_ciphersuite_handle_t suite_info; - #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) mbedtls_pk_type_t sig_type; #endif - suite_info = mbedtls_ssl_ciphersuite_from_id( suite_id ); - if( suite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); - return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); - } - MBEDTLS_SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", mbedtls_ssl_suite_get_name( suite_info ) ) ); @@ -1035,8 +1026,7 @@ static int ssl_ciphersuite_match( mbedtls_ssl_context *ssl, int suite_id, ((void) acceptable_ec_grp_ids); #endif - *ciphersuite_info = suite_info; - return( 0 ); + return( 1 ); } #if defined(MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO) @@ -1047,7 +1037,6 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) size_t n; unsigned int ciph_len, sess_len, chal_len; unsigned char *buf, *p; - const int *ciphersuites; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); @@ -1238,33 +1227,53 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) #endif /* MBEDTLS_SSL_FALLBACK_SCSV */ got_common_suite = 0; - ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; - ciphersuite_info = MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) - for( i = 0; ciphersuites[i] != 0; i++ ) -#else - for( i = 0; ciphersuites[i] != 0; i++ ) - for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) -#endif + { + MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ + ssl->minor_ver, \ + cur_info ) { +#else + MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ + ssl->minor_ver, \ + cur_info ) + { + for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 ) + { +#endif + const int ciphersuite_id = + mbedtls_ssl_suite_get_id( cur_info ); + if( p[0] != 0 || - p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || - p[2] != ( ( ciphersuites[i] ) & 0xFF ) ) - continue; + p[1] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || + p[2] != ( ( ciphersuite_id ) & 0xFF ) ) + { + goto next_suite; + } got_common_suite = 1; - if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], - &ciphersuite_info, - NULL ) ) ) + if( ssl_ciphersuite_is_match( ssl, cur_info, NULL ) ) { - return( ret ); + ciphersuite_info = cur_info; + goto have_ciphersuite_v2; } - if( ciphersuite_info != MBEDTLS_SSL_CIPHERSUITE_INVALD_HANDLE ) - goto have_ciphersuite_v2; + next_suite: + /* Need something here to avoid + * 'label at end of compound statement' error. */ + ((void) 0); + +#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) } + MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE + } +#else + } + } + MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE +#endif if( got_common_suite ) { @@ -1279,12 +1288,15 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) } have_ciphersuite_v2: - MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", - mbedtls_ssl_suite_get_name( ciphersuite_info ) ) ); - ssl->session_negotiate->ciphersuite = ciphersuites[i]; + ssl->session_negotiate->ciphersuite = + mbedtls_ssl_suite_get_id( ciphersuite_info ); ssl->handshake->ciphersuite_info = ciphersuite_info; + MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", + mbedtls_ssl_get_ciphersuite_name( + mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); + /* * SSLv2 Client Hello relevant renegotiation security checks */ @@ -1327,7 +1339,6 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) int extended_ms_seen = 0; #endif int handshake_failure = 0; - const int *ciphersuites; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; int major, minor; @@ -2133,32 +2144,53 @@ read_record_header: * and certificate from the SNI callback triggered by the SNI extension.) */ got_common_suite = 0; - ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver]; - ciphersuite_info = MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE; #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) - for( i = 0; ciphersuites[i] != 0; i++ ) -#else - for( i = 0; ciphersuites[i] != 0; i++ ) - for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) -#endif + { + MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ + ssl->minor_ver, \ + cur_info ) { - if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || - p[1] != ( ( ciphersuites[i] ) & 0xFF ) ) - continue; +#else + MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, \ + ssl->minor_ver, \ + cur_info ) + { + for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) + { +#endif + const int ciphersuite_id = + mbedtls_ssl_suite_get_id( cur_info ); + + if( p[0] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || + p[1] != ( ( ciphersuite_id ) & 0xFF ) ) + { + goto next_suite; + } got_common_suite = 1; - if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i], - &ciphersuite_info, - acceptable_ec_grp_ids ) ) != 0 ) + if( ssl_ciphersuite_is_match( ssl, cur_info, + acceptable_ec_grp_ids) ) { - return( ret ); + ciphersuite_info = cur_info; + goto have_ciphersuite; } - if( ciphersuite_info != MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) - goto have_ciphersuite; + next_suite: + /* Need something here to avoid + * 'label at end of compound statement' error. */ + ((void) 0); + +#if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) } + MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE + } +#else + } + } + MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE +#endif if( got_common_suite ) { @@ -2177,12 +2209,15 @@ read_record_header: } have_ciphersuite: - MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", - mbedtls_ssl_suite_get_name( ciphersuite_info ) ) ); - ssl->session_negotiate->ciphersuite = ciphersuites[i]; + ssl->session_negotiate->ciphersuite = + mbedtls_ssl_suite_get_id( ciphersuite_info ); ssl->handshake->ciphersuite_info = ciphersuite_info; + MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", + mbedtls_ssl_get_ciphersuite_name( + mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ); + ssl->state++; #if defined(MBEDTLS_SSL_PROTO_DTLS) @@ -2196,7 +2231,8 @@ have_ciphersuite: defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) { - mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( ciphersuite_info ); + mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg( + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ); if( sig_alg != MBEDTLS_PK_NONE ) { mbedtls_md_type_t md_alg = mbedtls_ssl_sig_hash_set_find( &ssl->handshake->hash_algs, From df6459673354a0e57e7c0588c8a8b4aa59a10cdf Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 26 Jun 2019 13:02:22 +0100 Subject: [PATCH 07/22] Remove ciphersuite from handshake params if single suite hardcoded If MBEDTLS_SSL_SINGLE_CIPHERSUITE is enabled, the type mbedtls_ssl_ciphersuite_handle_t is logically a boolean (concretely realized as `unsigned char`), containing the invalid handle and the unique valid handle, which represents the single enabled ciphersuite. The SSL handshake structure mbedtls_ssl_handshake_params contains an instance of mbedtls_ssl_ciphersuite_handle_t which is guaranteed to be valid, and which is hence redundant in any two-valued implementation of mbedtls_ssl_ciphersuite_handle_t. This commit replaces read-uses of mbedtls_ssl_handshake_params::ciphersuite_info by a getter functions which, and defines this getter function either by just reading the field from the handshake structure (in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is disabled), or by returning the single valid ciphersuite handle (in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is enabled) and removing the field from mbedtls_ssl_handshake_params in this case. --- include/mbedtls/ssl_internal.h | 17 +++++++++++++++++ library/ssl_cli.c | 18 ++++++++++-------- library/ssl_srv.c | 26 ++++++++++++++++---------- library/ssl_tls.c | 16 ++++++++++------ 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index bca249d31..0dbf60ec7 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -501,7 +501,9 @@ struct mbedtls_ssl_handshake_params const unsigned char *, size_t, unsigned char *, size_t); +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; +#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ size_t pmslen; /*!< premaster length */ @@ -556,6 +558,21 @@ static inline int mbedtls_ssl_hs_get_extended_ms( } #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +static inline mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_handshake_get_ciphersuite( + mbedtls_ssl_handshake_params const *handshake ) +{ + return( handshake->ciphersuite_info ); +} +#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +static inline mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_handshake_get_ciphersuite( + mbedtls_ssl_handshake_params const *handshake ) +{ + ((void) handshake); + return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); +} +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + typedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer; /* diff --git a/library/ssl_cli.c b/library/ssl_cli.c index bf5ec11cf..498bb79d8 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -1452,7 +1452,8 @@ static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl, int ret; if( mbedtls_ssl_suite_get_key_exchange( - ssl->handshake->ciphersuite_info ) != MBEDTLS_KEY_EXCHANGE_ECJPAKE ) + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ) + != MBEDTLS_KEY_EXCHANGE_ECJPAKE ) { MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) ); return( 0 ); @@ -2595,7 +2596,7 @@ static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl ) { int ret; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); unsigned char *p = NULL, *end = NULL; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) ); @@ -2981,7 +2982,7 @@ exit: static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) { mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); @@ -3003,7 +3004,7 @@ static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl ) size_t n = 0; size_t cert_type_len = 0, dn_len = 0; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) ); @@ -3204,7 +3205,7 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) int ret; size_t i, n; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) ); @@ -3507,7 +3508,7 @@ ecdh_calc_secret: static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) { mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); int ret; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) ); @@ -3533,7 +3534,7 @@ static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) { int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); size_t n = 0, offset = 0; unsigned char hash[48]; unsigned char *hash_start = hash; @@ -3638,7 +3639,8 @@ sign: * Reason: Otherwise we should have running hashes for SHA512 and SHA224 * in order to satisfy 'weird' needs from the server side. */ - if( mbedtls_ssl_suite_get_mac( ssl->handshake->ciphersuite_info ) + if( mbedtls_ssl_suite_get_mac( + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ) == MBEDTLS_MD_SHA384 ) { md_alg = MBEDTLS_MD_SHA384; diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 43664feb8..1963672ea 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1291,7 +1291,9 @@ have_ciphersuite_v2: ssl->session_negotiate->ciphersuite = mbedtls_ssl_suite_get_id( ciphersuite_info ); +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ssl->handshake->ciphersuite_info = ciphersuite_info; +#endif MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", mbedtls_ssl_get_ciphersuite_name( @@ -2212,7 +2214,9 @@ have_ciphersuite: ssl->session_negotiate->ciphersuite = mbedtls_ssl_suite_get_id( ciphersuite_info ); +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ssl->handshake->ciphersuite_info = ciphersuite_info; +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", mbedtls_ssl_get_ciphersuite_name( @@ -2542,9 +2546,12 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, *olen = 0; /* Skip costly computation if not needed */ - if( mbedtls_ssl_suite_get_key_exchange( ssl->handshake->ciphersuite_info ) != + if( mbedtls_ssl_suite_get_key_exchange( + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ) ) != MBEDTLS_KEY_EXCHANGE_ECJPAKE ) + { return; + } MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, ecjpake kkpp extension" ) ); @@ -2936,7 +2943,7 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) { mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) ); @@ -2955,7 +2962,7 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) { int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); size_t dn_size, total_dn_size; /* excluding length bytes */ size_t ct_len, sa_len; /* including length bytes */ unsigned char *buf, *p; @@ -3186,7 +3193,7 @@ static int ssl_prepare_server_key_exchange( mbedtls_ssl_context *ssl, size_t *signature_len ) { mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); #if defined(MBEDTLS_KEY_EXCHANGE__SOME_PFS__ENABLED) #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED) @@ -3549,7 +3556,7 @@ static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) size_t signature_len = 0; #if defined(MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); #endif /* MBEDTLS_KEY_EXCHANGE__SOME_NON_PFS__ENABLED */ MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) ); @@ -3992,11 +3999,10 @@ static int ssl_parse_client_psk_identity( mbedtls_ssl_context *ssl, unsigned cha static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) { int ret; - mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); unsigned char *p, *end; - ciphersuite_info = ssl->handshake->ciphersuite_info; - MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) ); #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \ @@ -4287,7 +4293,7 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) { mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); @@ -4314,7 +4320,7 @@ static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl ) #endif mbedtls_md_type_t md_alg; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); mbedtls_pk_context *peer_pk = NULL; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); diff --git a/library/ssl_tls.c b/library/ssl_tls.c index c245145b7..6792273d5 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -1340,8 +1340,8 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) { int ret; - mbedtls_ssl_ciphersuite_handle_t const ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t const ciphersuite_info = + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); @@ -6072,7 +6072,8 @@ static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) /* No certificate support -> dummy functions */ int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) { - mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); @@ -6089,7 +6090,8 @@ int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) { - mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); @@ -6112,7 +6114,8 @@ int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; size_t i, n; const mbedtls_x509_crt *crt; - mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = ssl->handshake->ciphersuite_info; + mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); @@ -6477,7 +6480,7 @@ static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, int authmode ) { mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = - ssl->handshake->ciphersuite_info; + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) return( SSL_CERTIFICATE_SKIP ); @@ -6512,6 +6515,7 @@ static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, { int verify_ret; mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = + mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); mbedtls_x509_crt *ca_chain; mbedtls_x509_crl *ca_crl; From 6ace4657b6d4d9e7a74ffdb8a21fb0c1b9ed747b Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 26 Jun 2019 15:14:50 +0100 Subject: [PATCH 08/22] Remove ciphersuite from SSL config if single suite hardcoded If MBEDTLS_SSL_SINGLE_CIPHERSUITE is enabled, it overwrites the runtime configuration of supported ciphersuites, which includes both the configuration API and the fields which are used to store the configuration. Both are therefore no longer needed and should be removed for the benefit of code-size, memory usage, and API clarity (no accidental hiccup of runtime vs. compile-time configuration possible). The configuration API mbedtls_ssl_conf_ciphersuites() has already been removed in case MBEDTLS_SSL_SINGLE_CIPHERSUITE, and this commit removes the field mbedtls_ssl_config::ciphersuite_list which it updates. --- configs/baremetal.h | 2 +- include/mbedtls/ssl.h | 60 ++++++++++++++++++++-------------- library/ssl_tls.c | 8 +++++ programs/ssl/ssl_client2.c | 2 ++ programs/ssl/ssl_mail_client.c | 2 ++ programs/ssl/ssl_server2.c | 4 +++ 6 files changed, 53 insertions(+), 25 deletions(-) diff --git a/configs/baremetal.h b/configs/baremetal.h index 99d54106a..ed5bdd951 100644 --- a/configs/baremetal.h +++ b/configs/baremetal.h @@ -60,7 +60,7 @@ /* Key exchanges */ #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED #define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 -//#define MBEDTLS_SSL_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 +#define MBEDTLS_SSL_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 /* Digests - just SHA-256 */ #define MBEDTLS_MD_C diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index c72bbeb5a..f9b95023e 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -972,7 +972,9 @@ struct mbedtls_ssl_config * Pointers */ +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) const int *ciphersuite_list[4]; /*!< allowed ciphersuites per version */ +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ /** Callback for printing debug output */ void (*f_dbg)(void *, int, const char *, int, const char *); @@ -2466,6 +2468,7 @@ int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, */ const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ); +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) /** * \brief Set the list of allowed ciphersuites and the preference * order. First in the list has the highest preference. @@ -2478,11 +2481,43 @@ const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_co * over the preference of the client unless * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined! * + * \note On constrained systems, support for a single ciphersuite + * (in all versions) can be fixed at compile-time through + * the configuration option MBEDTLS_SSL_SINGLE_CIPHERSUITE. + * * \param conf SSL configuration * \param ciphersuites 0-terminated list of allowed ciphersuites */ void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, - const int *ciphersuites ); + const int *ciphersuites ); + +/** + * \brief Set the list of allowed ciphersuites and the + * preference order for a specific version of the protocol. + * (Only useful on the server side) + * + * The ciphersuites array is not copied, and must remain + * valid for the lifetime of the ssl_config. + * + * \param conf SSL configuration + * \param ciphersuites 0-terminated list of allowed ciphersuites + * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 + * supported) + * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0, + * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2, + * MBEDTLS_SSL_MINOR_VERSION_3 supported) + * + * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 + * and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2 + * + * \note On constrained systems, support for a single ciphersuite + * (in all versions) can be fixed at compile-time through + * the configuration option MBEDTLS_SSL_SINGLE_CIPHERSUITE. + */ +void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, + const int *ciphersuites, + int major, int minor ); +#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #define MBEDTLS_SSL_UNEXPECTED_CID_IGNORE 0 #define MBEDTLS_SSL_UNEXPECTED_CID_FAIL 1 @@ -2531,29 +2566,6 @@ int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, size_t len, !MBEDTLS_SSL_CONF_CID_LEN && !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ -/** - * \brief Set the list of allowed ciphersuites and the - * preference order for a specific version of the protocol. - * (Only useful on the server side) - * - * The ciphersuites array is not copied, and must remain - * valid for the lifetime of the ssl_config. - * - * \param conf SSL configuration - * \param ciphersuites 0-terminated list of allowed ciphersuites - * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 - * supported) - * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0, - * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2, - * MBEDTLS_SSL_MINOR_VERSION_3 supported) - * - * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 - * and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2 - */ -void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, - const int *ciphersuites, - int major, int minor ); - #if defined(MBEDTLS_X509_CRT_PARSE_C) /** * \brief Set the X.509 security profile used for verification diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 6792273d5..afe32be57 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -8323,6 +8323,7 @@ int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session } #endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, const int *ciphersuites ) { @@ -8344,6 +8345,7 @@ void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, conf->ciphersuite_list[minor] = ciphersuites; } +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_X509_CRT_PARSE_C) void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, @@ -10803,11 +10805,13 @@ static int ssl_preset_default_hashes[] = { }; #endif +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) static int ssl_preset_suiteb_ciphersuites[] = { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 0 }; +#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) static int ssl_preset_suiteb_hashes[] = { @@ -10943,11 +10947,13 @@ int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ssl_preset_suiteb_ciphersuites; +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_X509_CRT_PARSE_C) conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; @@ -10982,11 +10988,13 @@ int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; #endif +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = mbedtls_ssl_list_ciphersuites(); +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_X509_CRT_PARSE_C) conf->cert_profile = &mbedtls_x509_crt_profile_default; diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 56641f54d..ac7810a43 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -1788,8 +1788,10 @@ int main( int argc, char *argv[] ) mbedtls_ssl_conf_session_tickets( &conf, opt.tickets ); #endif +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_ARC4_C) if( opt.arc4 != DFL_ARC4 ) diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c index 58804684e..3ceae8c74 100644 --- a/programs/ssl/ssl_mail_client.c +++ b/programs/ssl/ssl_mail_client.c @@ -621,8 +621,10 @@ int main( int argc, char *argv[] ) mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); +#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 ) diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 4312629c5..88c92b33a 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -2662,14 +2662,17 @@ int main( int argc, char *argv[] ) } #endif /* MBEDTLS_SSL_PROTO_DTLS */ +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); +#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_ARC4_C) if( opt.arc4 != DFL_ARC4 ) mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 ); #endif +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) if( opt.version_suites != NULL ) { mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0], @@ -2685,6 +2688,7 @@ int main( int argc, char *argv[] ) MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3 ); } +#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ #if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION) if( opt.allow_legacy != DFL_ALLOW_LEGACY ) From e02758c9c88d1200bc20f7d9514de3fc18ec8aa6 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 26 Jun 2019 15:31:31 +0100 Subject: [PATCH 09/22] Remove ciphersuite from SSL session if single suite hardcoded If MBEDTLS_SSL_SINGLE_CIPHERSUITE is enabled, the type mbedtls_ssl_ciphersuite_handle_t is logically a boolean (concretely realized as `unsigned char`), containing the invalid handle and the unique valid handle, which represents the single enabled ciphersuite. The SSL session structure mbedtls_ssl_session contains an instance of mbedtls_ssl_ciphersuite_handle_t which is guaranteed to be valid, and which is hence redundant in any two-valued implementation of mbedtls_ssl_ciphersuite_handle_t. This commit replaces read-uses of mbedtls_ssl_session::ciphersuite_info by a getter functions which, and defines this getter function either by just reading the field from the session structure (in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is disabled), or by returning the single valid ciphersuite handle (in case MBEDTLS_SSL_SINGLE_CIPHERSUITE is enabled) and removing the field from mbedtls_ssl_session in this case. --- include/mbedtls/ssl.h | 2 ++ include/mbedtls/ssl_ciphersuites.h | 15 ++++++++++ library/ssl_cache.c | 5 +++- library/ssl_cli.c | 4 ++- library/ssl_srv.c | 26 +++++++++++----- library/ssl_tls.c | 45 +++++++++++++++++++--------- tests/suites/test_suite_ssl.function | 4 +++ 7 files changed, 78 insertions(+), 23 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index f9b95023e..daa9d746f 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -923,7 +923,9 @@ struct mbedtls_ssl_session #if defined(MBEDTLS_HAVE_TIME) mbedtls_time_t start; /*!< starting time */ #endif +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) int ciphersuite; /*!< chosen ciphersuite */ +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ int compression; /*!< chosen compression */ size_t id_len; /*!< session id length */ unsigned char id[32]; /*!< session identifier */ diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index 2f31ceede..c8cfacde2 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -681,6 +681,21 @@ static inline int mbedtls_ssl_ciphersuite_uses_server_signature( } } +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +static inline int mbedtls_ssl_session_get_ciphersuite( + mbedtls_ssl_session const * session ) +{ + return( session->ciphersuite ); +} +#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +static inline int mbedtls_ssl_session_get_ciphersuite( + mbedtls_ssl_session const * session ) +{ + ((void) session); + return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); +} +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ + const int *mbedtls_ssl_list_ciphersuites( void ); mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ); diff --git a/library/ssl_cache.c b/library/ssl_cache.c index 62a0a2987..bcc2f59d1 100644 --- a/library/ssl_cache.c +++ b/library/ssl_cache.c @@ -84,10 +84,13 @@ int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session ) continue; #endif - if( session->ciphersuite != entry->session.ciphersuite || + if( mbedtls_ssl_session_get_ciphersuite( session ) != + mbedtls_ssl_session_get_ciphersuite( &entry->session ) || session->compression != entry->session.compression || session->id_len != entry->session.id_len ) + { continue; + } if( memcmp( session->id, entry->session.id, entry->session.id_len ) != 0 ) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 498bb79d8..f4d51dc99 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -1845,7 +1845,7 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) if( n == 0 || mbedtls_ssl_get_renego_status( ssl ) != MBEDTLS_SSL_INITIAL_HANDSHAKE || - ssl->session_negotiate->ciphersuite != i || + mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) != i || ssl->session_negotiate->compression != comp || ssl->session_negotiate->id_len != n || memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 ) @@ -1874,7 +1874,9 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_HAVE_TIME) ssl->session_negotiate->start = mbedtls_time( NULL ); #endif +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ssl->session_negotiate->ciphersuite = i; +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ ssl->session_negotiate->compression = comp; ssl->session_negotiate->id_len = n; memcpy( ssl->session_negotiate->id, buf + 35, n ); diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 1963672ea..a97bc3a3a 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1037,7 +1037,9 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) size_t n; unsigned int ciph_len, sess_len, chal_len; unsigned char *buf, *p; +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; +#endif MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) ); @@ -1256,7 +1258,9 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) if( ssl_ciphersuite_is_match( ssl, cur_info, NULL ) ) { +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ciphersuite_info = cur_info; +#endif goto have_ciphersuite_v2; } @@ -1289,9 +1293,9 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) have_ciphersuite_v2: +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ssl->session_negotiate->ciphersuite = mbedtls_ssl_suite_get_id( ciphersuite_info ); -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ssl->handshake->ciphersuite_info = ciphersuite_info; #endif @@ -1341,7 +1345,10 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) int extended_ms_seen = 0; #endif int handshake_failure = 0; + +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; +#endif int major, minor; #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ @@ -2175,7 +2182,9 @@ read_record_header: if( ssl_ciphersuite_is_match( ssl, cur_info, acceptable_ec_grp_ids) ) { +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ciphersuite_info = cur_info; +#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ goto have_ciphersuite; } @@ -2212,9 +2221,9 @@ read_record_header: have_ciphersuite: +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ssl->session_negotiate->ciphersuite = mbedtls_ssl_suite_get_id( ciphersuite_info ); -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) ssl->handshake->ciphersuite_info = ciphersuite_info; #endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ @@ -2354,7 +2363,7 @@ static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, * encrypt-then-MAC response extension back to the client." */ suite = mbedtls_ssl_ciphersuite_from_id( - ssl->session_negotiate->ciphersuite ); + mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ); if( suite == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) { *olen = 0; @@ -2695,6 +2704,7 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) mbedtls_time_t t; #endif int ret; + int ciphersuite; size_t olen, ext_len = 0, n; unsigned char *buf, *p; @@ -2844,12 +2854,13 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", mbedtls_ssl_handshake_get_resume( ssl->handshake ) ? "a" : "no" ) ); - *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 ); - *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite ); + ciphersuite = mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ); + *p++ = (unsigned char)( ciphersuite >> 8 ); + *p++ = (unsigned char)( ciphersuite ); *p++ = (unsigned char)( ssl->session_negotiate->compression ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", - mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) ); + mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X", ssl->session_negotiate->compression ) ); @@ -2898,7 +2909,8 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) if ( mbedtls_ssl_ciphersuite_uses_ec( - mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite ) ) ) + mbedtls_ssl_ciphersuite_from_id( + mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ) ) ) ) { ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen ); ext_len += olen; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index afe32be57..ab48cc4db 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -1378,24 +1378,24 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) /* Populate transform structure */ ret = ssl_populate_transform( ssl->transform_negotiate, - ssl->session_negotiate->ciphersuite, - ssl->session_negotiate->master, + mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ), + ssl->session_negotiate->master, #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) - ssl->session_negotiate->encrypt_then_mac, + ssl->session_negotiate->encrypt_then_mac, #endif #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) - ssl->session_negotiate->trunc_hmac, + ssl->session_negotiate->trunc_hmac, #endif #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ #if defined(MBEDTLS_ZLIB_SUPPORT) - ssl->session_negotiate->compression, + ssl->session_negotiate->compression, #endif - ssl->handshake->tls_prf, - ssl->handshake->randbytes, - ssl->minor_ver, - mbedtls_ssl_conf_get_endpoint( ssl->conf ), - ssl ); + ssl->handshake->tls_prf, + ssl->handshake->randbytes, + ssl->minor_ver, + mbedtls_ssl_conf_get_endpoint( ssl->conf ), + ssl ); if( ret != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); @@ -8968,10 +8968,13 @@ uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) { + int suite; + if( ssl == NULL || ssl->session == NULL ) return( NULL ); - return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); + suite = mbedtls_ssl_session_get_ciphersuite( ssl->session ); + return( mbedtls_ssl_get_ciphersuite_name( suite ) ); } const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) @@ -9393,8 +9396,10 @@ int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF ); + const int ciphersuite = + mbedtls_ssl_session_get_ciphersuite( session ); + *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF ); + *p++ = (unsigned char)( ( ciphersuite ) & 0xFF ); *p++ = (unsigned char)( session->compression & 0xFF ); @@ -9532,6 +9537,7 @@ static int ssl_session_load( mbedtls_ssl_session *session, { const unsigned char *p = buf; const unsigned char * const end = buf + len; + int ciphersuite; #if defined(MBEDTLS_HAVE_TIME) uint64_t start; #endif @@ -9578,12 +9584,23 @@ static int ssl_session_load( mbedtls_ssl_session *session, /* * Basic mandatory fields */ + if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - session->ciphersuite = ( p[0] << 8 ) | p[1]; + ciphersuite = ( p[0] << 8 ) | p[1]; p += 2; +#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) + session->ciphersuite = ciphersuite; +#else + if( ciphersuite != + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ) + { + return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); + } +#endif + session->compression = *p++; session->id_len = *p++; diff --git a/tests/suites/test_suite_ssl.function b/tests/suites/test_suite_ssl.function index 8271b232f..9707935ee 100644 --- a/tests/suites/test_suite_ssl.function +++ b/tests/suites/test_suite_ssl.function @@ -279,7 +279,9 @@ static int ssl_populate_session( mbedtls_ssl_session *session, #if defined(MBEDTLS_HAVE_TIME) session->start = mbedtls_time( NULL ) - 42; #endif +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) session->ciphersuite = 0xabcd; +#endif session->compression = 1; session->id_len = sizeof( session->id ); memset( session->id, 66, session->id_len ); @@ -698,7 +700,9 @@ void ssl_serialize_session_save_load( int ticket_len, char *crt_file ) #if defined(MBEDTLS_HAVE_TIME) TEST_ASSERT( original.start == restored.start ); #endif +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) TEST_ASSERT( original.ciphersuite == restored.ciphersuite ); +#endif TEST_ASSERT( original.compression == restored.compression ); TEST_ASSERT( original.id_len == restored.id_len ); TEST_ASSERT( memcmp( original.id, From 73f4cb126d003baa510fba6a95a5987f25f8455a Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 27 Jun 2019 13:51:07 +0100 Subject: [PATCH 10/22] Rename XXX_SINGLE_CIPHERSUITE -> XXX_CONF_SINGLE_CIPHERSUITE This is in line with the other configurations options used to hardcoded aspects of the SSL configuration. --- configs/baremetal.h | 2 +- include/mbedtls/config.h | 2 +- include/mbedtls/ssl.h | 16 +++++------ include/mbedtls/ssl_ciphersuites.h | 44 +++++++++++++++--------------- include/mbedtls/ssl_internal.h | 16 +++++------ library/ssl_ciphersuites.c | 34 +++++++++++------------ library/ssl_cli.c | 6 ++-- library/ssl_srv.c | 16 +++++------ library/ssl_tls.c | 20 +++++++------- programs/ssl/ssl_client2.c | 4 +-- programs/ssl/ssl_mail_client.c | 4 +-- programs/ssl/ssl_server2.c | 8 +++--- 12 files changed, 86 insertions(+), 86 deletions(-) diff --git a/configs/baremetal.h b/configs/baremetal.h index ed5bdd951..2e92e76ac 100644 --- a/configs/baremetal.h +++ b/configs/baremetal.h @@ -60,7 +60,7 @@ /* Key exchanges */ #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED #define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 -#define MBEDTLS_SSL_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 +#define MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 /* Digests - just SHA-256 */ #define MBEDTLS_MD_C diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 2b7e9da1a..8290c516d 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -3650,7 +3650,7 @@ * * If this option is set, the API mbedtls_ssl_conf_ciphersuites() is removed. */ -//#define MBEDTLS_SSL_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 +//#define MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 /* \} SECTION: Compile-time SSL configuration */ diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index daa9d746f..0a1d9d542 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -923,9 +923,9 @@ struct mbedtls_ssl_session #if defined(MBEDTLS_HAVE_TIME) mbedtls_time_t start; /*!< starting time */ #endif -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) int ciphersuite; /*!< chosen ciphersuite */ -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ int compression; /*!< chosen compression */ size_t id_len; /*!< session id length */ unsigned char id[32]; /*!< session identifier */ @@ -974,9 +974,9 @@ struct mbedtls_ssl_config * Pointers */ -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) const int *ciphersuite_list[4]; /*!< allowed ciphersuites per version */ -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ /** Callback for printing debug output */ void (*f_dbg)(void *, int, const char *, int, const char *); @@ -2470,7 +2470,7 @@ int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, */ const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ); -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) /** * \brief Set the list of allowed ciphersuites and the preference * order. First in the list has the highest preference. @@ -2485,7 +2485,7 @@ const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_co * * \note On constrained systems, support for a single ciphersuite * (in all versions) can be fixed at compile-time through - * the configuration option MBEDTLS_SSL_SINGLE_CIPHERSUITE. + * the configuration option MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE. * * \param conf SSL configuration * \param ciphersuites 0-terminated list of allowed ciphersuites @@ -2514,12 +2514,12 @@ void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, * * \note On constrained systems, support for a single ciphersuite * (in all versions) can be fixed at compile-time through - * the configuration option MBEDTLS_SSL_SINGLE_CIPHERSUITE. + * the configuration option MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE. */ void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, const int *ciphersuites, int major, int minor ); -#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #define MBEDTLS_SSL_UNEXPECTED_CID_IGNORE 0 #define MBEDTLS_SSL_UNEXPECTED_CID_FAIL 1 diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index c8cfacde2..281caa69b 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -351,8 +351,8 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; /* Wrapper around MBEDTLS_SSL_SUITE_XXX_T() which makes sure that * the argument is macro-expanded before concatenated with the * field name. This allows to call these macros as - * MBEDTLS_SSL_SUITE_XXX( MBEDTLS_SSL_SINGLE_CIPHERSUITE ), - * where MBEDTLS_SSL_SINGLE_CIPHERSUITE expands to MBEDTLS_SSL_SUITE_XXX. */ + * MBEDTLS_SSL_SUITE_XXX( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ), + * where MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE expands to MBEDTLS_SSL_SUITE_XXX. */ #define MBEDTLS_SSL_SUITE_ID( SUITE ) MBEDTLS_SSL_SUITE_ID_T( SUITE ) #define MBEDTLS_SSL_SUITE_NAME( SUITE ) MBEDTLS_SSL_SUITE_NAME_T( SUITE ) #define MBEDTLS_SSL_SUITE_CIPHER( SUITE ) MBEDTLS_SSL_SUITE_CIPHER_T( SUITE ) @@ -364,7 +364,7 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; #define MBEDTLS_SSL_SUITE_MAX_MINOR_VER( SUITE ) MBEDTLS_SSL_SUITE_MAX_MINOR_VER_T( SUITE ) #define MBEDTLS_SSL_SUITE_FLAGS( SUITE ) MBEDTLS_SSL_SUITE_FLAGS_T( SUITE ) -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) /** * \brief This structure is used for storing ciphersuite information */ @@ -404,13 +404,13 @@ typedef mbedtls_ssl_ciphersuite_t const * mbedtls_ssl_ciphersuite_handle_t; MBEDTLS_SSL_SUITE_MAX_MINOR_VER( SUITE ), \ MBEDTLS_SSL_SUITE_FLAGS( SUITE ) } -#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ typedef unsigned char mbedtls_ssl_ciphersuite_handle_t; #define MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ( (mbedtls_ssl_ciphersuite_handle_t) 0 ) #define MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ( (mbedtls_ssl_ciphersuite_handle_t) 1 ) -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ /* * Getter functions for the extraction of ciphersuite attributes @@ -421,7 +421,7 @@ typedef unsigned char mbedtls_ssl_ciphersuite_handle_t; * is passed. */ -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) /* * Implementation of getter functions when the ciphersuite handle * is a pointer to the ciphersuite information structure. @@ -479,7 +479,7 @@ static inline unsigned char mbedtls_ssl_suite_get_flags( { return( info->flags ); } -#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ /* * Implementations of getter functions in the case of only * a single possible ciphersuite. In this case, the handle @@ -493,63 +493,63 @@ static inline int mbedtls_ssl_suite_get_id( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline const char* mbedtls_ssl_suite_get_name( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline mbedtls_cipher_type_t mbedtls_ssl_suite_get_cipher( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_CIPHER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_CIPHER( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline mbedtls_md_type_t mbedtls_ssl_suite_get_mac( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_MAC( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_MAC( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline mbedtls_key_exchange_type_t mbedtls_ssl_suite_get_key_exchange( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_KEY_EXCHANGE( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_KEY_EXCHANGE( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline int mbedtls_ssl_suite_get_min_major_ver( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_MIN_MAJOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_MIN_MAJOR_VER( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline int mbedtls_ssl_suite_get_min_minor_ver( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_MIN_MINOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_MIN_MINOR_VER( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline int mbedtls_ssl_suite_get_max_major_ver( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_MAX_MAJOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_MAX_MAJOR_VER( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline int mbedtls_ssl_suite_get_max_minor_ver( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_MAX_MINOR_VER( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_MAX_MINOR_VER( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } static inline unsigned char mbedtls_ssl_suite_get_flags( mbedtls_ssl_ciphersuite_handle_t const info ) { ((void) info); - return( MBEDTLS_SSL_SUITE_FLAGS( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_FLAGS( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ static inline int mbedtls_ssl_ciphersuite_has_pfs( mbedtls_ssl_ciphersuite_handle_t info ) @@ -681,20 +681,20 @@ static inline int mbedtls_ssl_ciphersuite_uses_server_signature( } } -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) static inline int mbedtls_ssl_session_get_ciphersuite( mbedtls_ssl_session const * session ) { return( session->ciphersuite ); } -#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ static inline int mbedtls_ssl_session_get_ciphersuite( mbedtls_ssl_session const * session ) { ((void) session); - return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); } -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ const int *mbedtls_ssl_list_ciphersuites( void ); diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index 0dbf60ec7..a4d4eea14 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -501,9 +501,9 @@ struct mbedtls_ssl_handshake_params const unsigned char *, size_t, unsigned char *, size_t); -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; -#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ size_t pmslen; /*!< premaster length */ @@ -558,20 +558,20 @@ static inline int mbedtls_ssl_hs_get_extended_ms( } #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) static inline mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_handshake_get_ciphersuite( mbedtls_ssl_handshake_params const *handshake ) { return( handshake->ciphersuite_info ); } -#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ static inline mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_handshake_get_ciphersuite( mbedtls_ssl_handshake_params const *handshake ) { ((void) handshake); return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); } -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ typedef struct mbedtls_ssl_hs_buffer mbedtls_ssl_hs_buffer; @@ -1454,7 +1454,7 @@ static inline unsigned int mbedtls_ssl_conf_get_ems_enforced( * instantiation in case a single ciphersuite is enabled at * compile-time. */ -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) #define MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) \ { \ @@ -1472,7 +1472,7 @@ static inline unsigned int mbedtls_ssl_conf_get_ems_enforced( } \ } -#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #define MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) \ { \ @@ -1482,6 +1482,6 @@ static inline unsigned int mbedtls_ssl_conf_get_ems_enforced( #define MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE \ } -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #endif /* ssl_internal.h */ diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index d37d7a2e7..58e91796c 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -54,7 +54,7 @@ */ static const int ciphersuite_preference[] = { -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) #if defined(MBEDTLS_SSL_CIPHERSUITES) MBEDTLS_SSL_CIPHERSUITES, #else @@ -312,13 +312,13 @@ static const int ciphersuite_preference[] = MBEDTLS_TLS_PSK_WITH_NULL_SHA, #endif /* MBEDTLS_SSL_CIPHERSUITES */ -#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ - MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ), -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ), +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ 0 }; -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) static const mbedtls_ssl_ciphersuite_t ciphersuite_definitions[] = { #if defined(MBEDTLS_CHACHAPOLY_C) && \ @@ -2171,9 +2171,9 @@ static const mbedtls_ssl_ciphersuite_t ciphersuite_definitions[] = MBEDTLS_CIPHER_NONE, MBEDTLS_MD_NONE, MBEDTLS_KEY_EXCHANGE_NONE, 0, 0, 0, 0, 0 } }; -#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ -#if defined(MBEDTLS_SSL_CIPHERSUITES) || defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if defined(MBEDTLS_SSL_CIPHERSUITES) || defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) const int *mbedtls_ssl_list_ciphersuites( void ) { return( ciphersuite_preference ); @@ -2232,9 +2232,9 @@ const int *mbedtls_ssl_list_ciphersuites( void ) return( supported_ciphersuites ); } -#endif /* !( MBEDTLS_SSL_CIPHERSUITES || MBEDTLS_SSL_SINGLE_CIPHERSUITE ) */ +#endif /* !( MBEDTLS_SSL_CIPHERSUITES || MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) */ -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ) { @@ -2293,13 +2293,13 @@ int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) return( cur->id ); } -#else /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ) { static const char * const single_suite_name = - MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); if( strcmp( ciphersuite_name, single_suite_name ) == 0 ) return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); @@ -2310,7 +2310,7 @@ mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuite ) { static const int single_suite_id = - MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); if( ciphersuite == single_suite_id ) return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); @@ -2321,10 +2321,10 @@ mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuit const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id ) { static const int single_suite_id = - MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); if( ciphersuite_id == single_suite_id ) - return( MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); return( NULL ); } @@ -2332,15 +2332,15 @@ const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id ) int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) { static const char * const single_suite_name = - MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_SINGLE_CIPHERSUITE ); + MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); if( strcmp( ciphersuite_name, single_suite_name ) == 0 ) - return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ); + return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); return( 0 ); } -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_PK_C) mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index f4d51dc99..8774003be 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -1816,7 +1816,7 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) * Initialize update checksum functions */ server_suite_info = mbedtls_ssl_ciphersuite_from_id( i ); -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) ssl->handshake->ciphersuite_info = server_suite_info; #endif if( server_suite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) @@ -1874,9 +1874,9 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_HAVE_TIME) ssl->session_negotiate->start = mbedtls_time( NULL ); #endif -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) ssl->session_negotiate->ciphersuite = i; -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ ssl->session_negotiate->compression = comp; ssl->session_negotiate->id_len = n; memcpy( ssl->session_negotiate->id, buf + 35, n ); diff --git a/library/ssl_srv.c b/library/ssl_srv.c index a97bc3a3a..4ebd834cb 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1037,7 +1037,7 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) size_t n; unsigned int ciph_len, sess_len, chal_len; unsigned char *buf, *p; -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; #endif @@ -1258,7 +1258,7 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) if( ssl_ciphersuite_is_match( ssl, cur_info, NULL ) ) { -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) ciphersuite_info = cur_info; #endif goto have_ciphersuite_v2; @@ -1293,7 +1293,7 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) have_ciphersuite_v2: -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) ssl->session_negotiate->ciphersuite = mbedtls_ssl_suite_get_id( ciphersuite_info ); ssl->handshake->ciphersuite_info = ciphersuite_info; @@ -1346,7 +1346,7 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) #endif int handshake_failure = 0; -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; #endif int major, minor; @@ -2182,9 +2182,9 @@ read_record_header: if( ssl_ciphersuite_is_match( ssl, cur_info, acceptable_ec_grp_ids) ) { -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) ciphersuite_info = cur_info; -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ goto have_ciphersuite; } @@ -2221,11 +2221,11 @@ read_record_header: have_ciphersuite: -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) ssl->session_negotiate->ciphersuite = mbedtls_ssl_suite_get_id( ciphersuite_info ); ssl->handshake->ciphersuite_info = ciphersuite_info; -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ MBEDTLS_SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", mbedtls_ssl_get_ciphersuite_name( diff --git a/library/ssl_tls.c b/library/ssl_tls.c index ab48cc4db..e63c6880b 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -8323,7 +8323,7 @@ int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session } #endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, const int *ciphersuites ) { @@ -8345,7 +8345,7 @@ void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, conf->ciphersuite_list[minor] = ciphersuites; } -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_X509_CRT_PARSE_C) void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, @@ -9591,11 +9591,11 @@ static int ssl_session_load( mbedtls_ssl_session *session, ciphersuite = ( p[0] << 8 ) | p[1]; p += 2; -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) session->ciphersuite = ciphersuite; #else if( ciphersuite != - MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_SINGLE_CIPHERSUITE ) ) + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ) { return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); } @@ -10822,13 +10822,13 @@ static int ssl_preset_default_hashes[] = { }; #endif -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) static int ssl_preset_suiteb_ciphersuites[] = { MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 0 }; -#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) static int ssl_preset_suiteb_hashes[] = { @@ -10964,13 +10964,13 @@ int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ssl_preset_suiteb_ciphersuites; -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_X509_CRT_PARSE_C) conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; @@ -11005,13 +11005,13 @@ int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; #endif -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = mbedtls_ssl_list_ciphersuites(); -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_X509_CRT_PARSE_C) conf->cert_profile = &mbedtls_x509_crt_profile_default; diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index ac7810a43..d9cfa257e 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -1788,10 +1788,10 @@ int main( int argc, char *argv[] ) mbedtls_ssl_conf_session_tickets( &conf, opt.tickets ); #endif -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); -#endif /* MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_ARC4_C) if( opt.arc4 != DFL_ARC4 ) diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c index 3ceae8c74..63a3a2d3a 100644 --- a/programs/ssl/ssl_mail_client.c +++ b/programs/ssl/ssl_mail_client.c @@ -621,10 +621,10 @@ int main( int argc, char *argv[] ) mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); -#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 ) diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 88c92b33a..deaee9afe 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -2662,17 +2662,17 @@ int main( int argc, char *argv[] ) } #endif /* MBEDTLS_SSL_PROTO_DTLS */ -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); -#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if defined(MBEDTLS_ARC4_C) if( opt.arc4 != DFL_ARC4 ) mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 ); #endif -#if !defined(MBEDTLS_SSL_SINGLE_CIPHERSUITE) +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) if( opt.version_suites != NULL ) { mbedtls_ssl_conf_ciphersuites_for_version( &conf, version_suites[0], @@ -2688,7 +2688,7 @@ int main( int argc, char *argv[] ) MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3 ); } -#endif /* !MBEDTLS_SSL_SINGLE_CIPHERSUITE */ +#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION) if( opt.allow_legacy != DFL_ALLOW_LEGACY ) From 67fb16e59dabdf63964e61bb5293c8f88a508a06 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Fri, 28 Jun 2019 11:39:22 +0100 Subject: [PATCH 11/22] Make ciphersuite helpers static inline if only one suite enabled This commit restructures ssl_ciphersuites.h and ssl_ciphersuites.c to define all ciphersuite helper functions static inline in ssl_ciphersuites.h if MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE is set, and to otherwise put their definitions in ssl_ciphersuites.c. --- include/mbedtls/ssl_ciphersuites.h | 289 ++++++++++++++++++++++++++--- library/ssl_ciphersuites.c | 185 ++++++++---------- 2 files changed, 344 insertions(+), 130 deletions(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index 281caa69b..a7bf90434 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -34,6 +34,7 @@ #include "cipher.h" #include "md.h" #include "ssl.h" +#include #ifdef __cplusplus extern "C" { @@ -412,6 +413,21 @@ typedef unsigned char mbedtls_ssl_ciphersuite_handle_t; #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ +#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) +static inline int mbedtls_ssl_session_get_ciphersuite( + mbedtls_ssl_session const * session ) +{ + return( session->ciphersuite ); +} +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ +static inline int mbedtls_ssl_session_get_ciphersuite( + mbedtls_ssl_session const * session ) +{ + ((void) session); + return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); +} +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ + /* * Getter functions for the extraction of ciphersuite attributes * from a ciphersuite handle. @@ -551,7 +567,22 @@ static inline unsigned char mbedtls_ssl_suite_get_flags( } #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ -static inline int mbedtls_ssl_ciphersuite_has_pfs( +const int *mbedtls_ssl_list_ciphersuites( void ); + +/* + * Various small helper functions for ciphersuites. + * + * Like the getter functions, they assume that the provided ciphersuite + * handle is valid, and hence can be optimized in case there's only one + * ciphersuite enabled. + * + * To avoid code-duplication between inline and non-inline implementations + * of this, we define internal static inline versions of all functions first, + * and define wrappers around these either here or in ssl_ciphersuites.c, + * depending on whether MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE is defined. + */ + +static inline int mbedtls_ssl_ciphersuite_has_pfs_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -569,7 +600,7 @@ static inline int mbedtls_ssl_ciphersuite_has_pfs( } } -static inline int mbedtls_ssl_ciphersuite_no_pfs( +static inline int mbedtls_ssl_ciphersuite_no_pfs_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -586,7 +617,7 @@ static inline int mbedtls_ssl_ciphersuite_no_pfs( } } -static inline int mbedtls_ssl_ciphersuite_uses_ecdh( +static inline int mbedtls_ssl_ciphersuite_uses_ecdh_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -600,7 +631,7 @@ static inline int mbedtls_ssl_ciphersuite_uses_ecdh( } } -static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( +static inline int mbedtls_ssl_ciphersuite_cert_req_allowed_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -618,7 +649,7 @@ static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( } } -static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( +static inline int mbedtls_ssl_ciphersuite_uses_srv_cert_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -637,7 +668,7 @@ static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( } } -static inline int mbedtls_ssl_ciphersuite_uses_dhe( +static inline int mbedtls_ssl_ciphersuite_uses_dhe_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -651,7 +682,7 @@ static inline int mbedtls_ssl_ciphersuite_uses_dhe( } } -static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( +static inline int mbedtls_ssl_ciphersuite_uses_ecdhe_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -666,7 +697,7 @@ static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( } } -static inline int mbedtls_ssl_ciphersuite_uses_server_signature( +static inline int mbedtls_ssl_ciphersuite_uses_server_signature_internal( mbedtls_ssl_ciphersuite_handle_t info ) { switch( mbedtls_ssl_suite_get_key_exchange( info ) ) @@ -681,33 +712,237 @@ static inline int mbedtls_ssl_ciphersuite_uses_server_signature( } } +#if defined(MBEDTLS_PK_C) +static inline mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg_internal( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_RSA: + case MBEDTLS_KEY_EXCHANGE_DHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + case MBEDTLS_KEY_EXCHANGE_RSA_PSK: + return( MBEDTLS_PK_RSA ); + + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + return( MBEDTLS_PK_ECDSA ); + + case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: + return( MBEDTLS_PK_ECKEY ); + + default: + return( MBEDTLS_PK_NONE ); + } +} + +static inline mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg_internal( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_RSA: + case MBEDTLS_KEY_EXCHANGE_DHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + return( MBEDTLS_PK_RSA ); + + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + return( MBEDTLS_PK_ECDSA ); + + default: + return( MBEDTLS_PK_NONE ); + } +} + +#endif /* MBEDTLS_PK_C */ + +#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) +static inline int mbedtls_ssl_ciphersuite_uses_ec_internal( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: + case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: + case MBEDTLS_KEY_EXCHANGE_ECJPAKE: + return( 1 ); + + default: + return( 0 ); + } +} +#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ + +#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) +static inline int mbedtls_ssl_ciphersuite_uses_psk_internal( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_PSK: + case MBEDTLS_KEY_EXCHANGE_RSA_PSK: + case MBEDTLS_KEY_EXCHANGE_DHE_PSK: + case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: + return( 1 ); + + default: + return( 0 ); + } +} +#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ + +/* + * Wrappers around internal helper functions to be used by the rest of + * the library, either defined static inline here or in ssl_ciphersuites.c. + */ + #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) -static inline int mbedtls_ssl_session_get_ciphersuite( - mbedtls_ssl_session const * session ) -{ - return( session->ciphersuite ); -} -#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ -static inline int mbedtls_ssl_session_get_ciphersuite( - mbedtls_ssl_session const * session ) -{ - ((void) session); - return( MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); -} -#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ -const int *mbedtls_ssl_list_ciphersuites( void ); +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( + const char *ciphersuite_name ); +mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( + int ciphersuite_id ); -mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name ); -mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuite_id ); +int mbedtls_ssl_ciphersuite_has_pfs( mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_no_pfs( mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_uses_ecdh( mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_cert_req_allowed( + mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_uses_srv_cert( + mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_uses_dhe( mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_uses_ecdhe( mbedtls_ssl_ciphersuite_handle_t info ); +int mbedtls_ssl_ciphersuite_uses_server_signature( + mbedtls_ssl_ciphersuite_handle_t info ); #if defined(MBEDTLS_PK_C) -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ); -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( mbedtls_ssl_ciphersuite_handle_t info ); -#endif +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( + mbedtls_ssl_ciphersuite_handle_t info ); +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( + mbedtls_ssl_ciphersuite_handle_t info ); +#endif /* MBEDTLS_PK_C */ +#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) int mbedtls_ssl_ciphersuite_uses_ec( mbedtls_ssl_ciphersuite_handle_t info ); +#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || + MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ + +#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) int mbedtls_ssl_ciphersuite_uses_psk( mbedtls_ssl_ciphersuite_handle_t info ); +#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ + +#else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ + +static inline int mbedtls_ssl_ciphersuite_has_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_has_pfs_internal( info ) ); +} + +static inline int mbedtls_ssl_ciphersuite_no_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_no_pfs_internal( info ) ); +} + +static inline int mbedtls_ssl_ciphersuite_uses_ecdh( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_ecdh_internal( info ) ); +} + +static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_cert_req_allowed_internal( info ) ); +} + +static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_srv_cert_internal( info ) ); +} + +static inline int mbedtls_ssl_ciphersuite_uses_dhe( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_dhe_internal( info ) ); +} + +static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_ecdhe_internal( info ) ); +} + +static inline int mbedtls_ssl_ciphersuite_uses_server_signature( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_server_signature_internal( info ) ); +} + +#if defined(MBEDTLS_PK_C) +static inline mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_get_ciphersuite_sig_pk_alg_internal( info ) ); +} + +static inline mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_get_ciphersuite_sig_alg_internal( info ) ); +} +#endif /* MBEDTLS_PK_C */ + +#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) +static inline int mbedtls_ssl_ciphersuite_uses_ec( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_ec_internal( info ) ); +} +#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || + MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ + +#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) +static inline int mbedtls_ssl_ciphersuite_uses_psk( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_psk_internal( info ) ); +} +#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ + +static inline mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( + int ciphersuite ) +{ + static const int single_suite_id = + MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); + + if( ciphersuite == single_suite_id ) + return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); + + return( MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ); +} + +static inline mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( + const char *ciphersuite_name ) +{ + static const char * const single_suite_name = + MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); + + if( strcmp( ciphersuite_name, single_suite_name ) == 0 ) + return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); + + return( MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ); +} + +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #ifdef __cplusplus } diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index 58e91796c..4a9d3bf5a 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -2293,32 +2293,90 @@ int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) return( cur->id ); } +int mbedtls_ssl_ciphersuite_has_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_has_pfs_internal( info ) ); +} + +int mbedtls_ssl_ciphersuite_no_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_no_pfs_internal( info ) ); +} + +int mbedtls_ssl_ciphersuite_uses_ecdh( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_ecdh_internal( info ) ); +} + +int mbedtls_ssl_ciphersuite_cert_req_allowed( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_cert_req_allowed_internal( info ) ); +} + +int mbedtls_ssl_ciphersuite_uses_srv_cert( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_srv_cert_internal( info ) ); +} + +int mbedtls_ssl_ciphersuite_uses_dhe( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_dhe_internal( info ) ); +} + +int mbedtls_ssl_ciphersuite_uses_ecdhe( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_ecdhe_internal( info ) ); +} + +int mbedtls_ssl_ciphersuite_uses_server_signature( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_server_signature_internal( info ) ); +} + +#if defined(MBEDTLS_PK_C) +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_get_ciphersuite_sig_pk_alg_internal( info ) ); +} + +mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_get_ciphersuite_sig_alg_internal( info ) ); +} +#endif /* MBEDTLS_PK_C */ + +#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) +int mbedtls_ssl_ciphersuite_uses_ec( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_ec_internal( info ) ); +} +#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || + MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ + +#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) +int mbedtls_ssl_ciphersuite_uses_psk( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + return( mbedtls_ssl_ciphersuite_uses_psk_internal( info ) ); +} +#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ + #else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ -mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( - const char *ciphersuite_name ) -{ - static const char * const single_suite_name = - MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); - - if( strcmp( ciphersuite_name, single_suite_name ) == 0 ) - return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); - - return( MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ); -} - -mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuite ) -{ - static const int single_suite_id = - MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); - - if( ciphersuite == single_suite_id ) - return( MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE ); - - return( MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ); -} - -const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id ) +const char *mbedtls_ssl_get_ciphersuite_name( + const int ciphersuite_id ) { static const int single_suite_id = MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); @@ -2342,83 +2400,4 @@ int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ -#if defined(MBEDTLS_PK_C) -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_RSA: - case MBEDTLS_KEY_EXCHANGE_DHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - case MBEDTLS_KEY_EXCHANGE_RSA_PSK: - return( MBEDTLS_PK_RSA ); - - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - return( MBEDTLS_PK_ECDSA ); - - case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: - return( MBEDTLS_PK_ECKEY ); - - default: - return( MBEDTLS_PK_NONE ); - } -} - -mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg( mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_RSA: - case MBEDTLS_KEY_EXCHANGE_DHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - return( MBEDTLS_PK_RSA ); - - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - return( MBEDTLS_PK_ECDSA ); - - default: - return( MBEDTLS_PK_NONE ); - } -} - -#endif /* MBEDTLS_PK_C */ - -#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) -int mbedtls_ssl_ciphersuite_uses_ec( mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: - case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: - case MBEDTLS_KEY_EXCHANGE_ECJPAKE: - return( 1 ); - - default: - return( 0 ); - } -} -#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ - -#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) -int mbedtls_ssl_ciphersuite_uses_psk( mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_PSK: - case MBEDTLS_KEY_EXCHANGE_RSA_PSK: - case MBEDTLS_KEY_EXCHANGE_DHE_PSK: - case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: - return( 1 ); - - default: - return( 0 ); - } -} -#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ - #endif /* MBEDTLS_SSL_TLS_C */ From d89e8cb4278a4ab56c941d3bbf4685efd3c346b0 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Fri, 28 Jun 2019 13:09:41 +0100 Subject: [PATCH 12/22] Don't break comment lines prematurely in ssl_ciphersuites.h --- include/mbedtls/ssl_ciphersuites.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index a7bf90434..54491d719 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -497,13 +497,11 @@ static inline unsigned char mbedtls_ssl_suite_get_flags( } #else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ /* - * Implementations of getter functions in the case of only - * a single possible ciphersuite. In this case, the handle - * is logically a boolean (either the invalid handle or the - * unique valid handle representing the single enabled - * ciphersuite), and the precondition that the handle is valid - * means that we can statically return the hardcoded attribute - * of the enabled ciphersuite. + * Implementations of getter functions in the case of only a single possible + * ciphersuite. In this case, the handle is logically a boolean (either the + * invalid handle or the unique valid handle representing the single enabled + * ciphersuite), and the precondition that the handle is valid means that we + * can statically return the hardcoded attribute of the enabled ciphersuite. */ static inline int mbedtls_ssl_suite_get_id( mbedtls_ssl_ciphersuite_handle_t const info ) From c5db66af2f15ecbd80de16aaa142b8665f9b477d Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Mon, 1 Jul 2019 18:11:25 +0100 Subject: [PATCH 13/22] Update query_config.c --- programs/ssl/query_config.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/programs/ssl/query_config.c b/programs/ssl/query_config.c index cda71fd4e..574d4d7a5 100644 --- a/programs/ssl/query_config.c +++ b/programs/ssl/query_config.c @@ -2770,6 +2770,14 @@ int query_config( const char *config ) } #endif /* MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ +#if defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) + if( strcmp( "MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE", config ) == 0 ) + { + MACRO_EXPANSION_TO_STR( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ); + return( 0 ); + } +#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ + /* If the symbol is not found, return an error */ return( 1 ); } From f4d6b49352e752680d917b6dd6343190600506cb Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 2 Jul 2019 17:13:14 +0100 Subject: [PATCH 14/22] Allow use of continue in single-ciphersuite 'loops' --- include/mbedtls/ssl_internal.h | 4 ++-- library/ssl_cli.c | 26 +++----------------------- library/ssl_srv.c | 15 ++------------- 3 files changed, 7 insertions(+), 38 deletions(-) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index a4d4eea14..8a515772d 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -1475,12 +1475,12 @@ static inline unsigned int mbedtls_ssl_conf_get_ems_enforced( #else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ #define MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE( ssl, ver, info ) \ - { \ + do { \ const mbedtls_ssl_ciphersuite_handle_t info = \ MBEDTLS_SSL_CIPHERSUITE_UNIQUE_VALID_HANDLE; #define MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE \ - } + } while( 0 ); #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 8774003be..d45f3d3fe 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -984,12 +984,7 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) ssl->conf->min_minor_ver, ssl->conf->max_minor_ver ) != 0 ) { - /* Logically, we want to continue the ciphersuite iteration - * here, but We can't just use `continue` because - * MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE() - * doesn't unfold to a loop in case only a single - * ciphersuite is enabled. */ - goto next_suite; + continue; } MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x", @@ -1005,11 +1000,6 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) mbedtls_ssl_suite_get_id( ciphersuite_info ) >> 8 ); *p++ = (unsigned char)( mbedtls_ssl_suite_get_id( ciphersuite_info ) ); - - next_suite: - /* Need something here to avoid - * 'label at end of compound statement' error. */ - ((void) 0); } MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE @@ -1899,23 +1889,13 @@ static int ssl_parse_server_hello( mbedtls_ssl_context *ssl ) ssl->conf->min_minor_ver, ssl->conf->max_minor_ver ) != 0 ) { - /* Logically, we want to continue the ciphersuite iteration - * here, but We can't just use `continue` because - * MBEDTLS_SSL_BEGIN_FOR_EACH_CIPHERSUITE() - * doesn't unfold to a loop in case only a single - * ciphersuite is enabled. */ - goto next_suite; + continue; } if( ciphersuite_info != server_suite_info ) - goto next_suite; + continue; goto server_picked_valid_suite; - - next_suite: - /* Need something here to avoid - * 'label at end of compound statement' error. */ - ((void) 0); } MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 4ebd834cb..e743eff87 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1251,7 +1251,7 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) p[1] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || p[2] != ( ( ciphersuite_id ) & 0xFF ) ) { - goto next_suite; + continue; } got_common_suite = 1; @@ -1264,11 +1264,6 @@ static int ssl_parse_client_hello_v2( mbedtls_ssl_context *ssl ) goto have_ciphersuite_v2; } - next_suite: - /* Need something here to avoid - * 'label at end of compound statement' error. */ - ((void) 0); - #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) } MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE @@ -2174,7 +2169,7 @@ read_record_header: if( p[0] != ( ( ciphersuite_id >> 8 ) & 0xFF ) || p[1] != ( ( ciphersuite_id ) & 0xFF ) ) { - goto next_suite; + continue; } got_common_suite = 1; @@ -2187,12 +2182,6 @@ read_record_header: #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ goto have_ciphersuite; } - - next_suite: - /* Need something here to avoid - * 'label at end of compound statement' error. */ - ((void) 0); - #if defined(MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE) } MBEDTLS_SSL_END_FOR_EACH_CIPHERSUITE From 9b3ec12863a8f49b47cd0a53e24469c172318c28 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 2 Jul 2019 17:23:41 +0100 Subject: [PATCH 15/22] Add missing spaces at the end of comments --- include/mbedtls/ssl_ciphersuites.h | 2 +- library/ssl_ciphersuites.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index 54491d719..1c5ae5ba4 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -773,7 +773,7 @@ static inline int mbedtls_ssl_ciphersuite_uses_ec_internal( return( 0 ); } } -#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ +#endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) static inline int mbedtls_ssl_ciphersuite_uses_psk_internal( diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index 4a9d3bf5a..f9765c99f 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -2363,7 +2363,7 @@ int mbedtls_ssl_ciphersuite_uses_ec( return( mbedtls_ssl_ciphersuite_uses_ec_internal( info ) ); } #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || - MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED*/ + MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) int mbedtls_ssl_ciphersuite_uses_psk( From a1552ac37c4167e6d923be0fcca2bbcf5c1549f5 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 2 Jul 2019 17:23:51 +0100 Subject: [PATCH 16/22] Use "unknown" instead of NULL as name of unknown ciphersuite --- library/ssl_ciphersuites.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index f9765c99f..f03470476 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -2384,7 +2384,7 @@ const char *mbedtls_ssl_get_ciphersuite_name( if( ciphersuite_id == single_suite_id ) return( MBEDTLS_SSL_SUITE_NAME( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ); - return( NULL ); + return( "unknown" ); } int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) From 870bcd3a6e26aa696f22cb8c7f00c4afd3b93478 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 2 Jul 2019 17:24:12 +0100 Subject: [PATCH 17/22] Highlight precondition of ciphersuite getter functions --- include/mbedtls/ssl_ciphersuites.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index 1c5ae5ba4..9553ebccd 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -432,7 +432,7 @@ static inline int mbedtls_ssl_session_get_ciphersuite( * Getter functions for the extraction of ciphersuite attributes * from a ciphersuite handle. * - * These functions have the validity of the handle as a precondition! + * Warning: These functions have the validity of the handle as a precondition! * Their behaviour is undefined when MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE * is passed. */ From e4b355a1ede7889864049faad7b17b61a7e1eefd Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 2 Jul 2019 17:28:30 +0100 Subject: [PATCH 18/22] Remove redundant hardcoded ciphersuite identifier --- include/mbedtls/ssl_ciphersuites.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index 9553ebccd..b66c474f0 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -322,7 +322,7 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; * as an example). */ -#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8() +//#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_ID MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_NAME "TLS-ECDHE-ECDSA-WITH-AES-128-CCM-8" #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_CIPHER MBEDTLS_CIPHER_AES_128_CCM From 91900362f5d325892f573660588005fee3895363 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 3 Jul 2019 13:22:59 +0100 Subject: [PATCH 19/22] Modify default test in ssl-opt.sh to not expect particular primitive --- tests/ssl-opt.sh | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index 5f9e2ecdd..553ece426 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -164,6 +164,12 @@ requires_config_disabled() { fi } +requires_ciphersuite_enabled() { + if [ -z "$($P_CLI --help | grep "$1")" ]; then + SKIP_NEXT="YES" + fi +} + get_config_value_or_default() { # This function uses the query_config command line option to query the # required Mbed TLS compile time configuration from the ssl_server2 @@ -952,11 +958,25 @@ trap cleanup INT TERM HUP # Basic test +run_test "Default" \ + "$P_SRV debug_level=3" \ + "$P_CLI" \ + 0 + +run_test "Default, DTLS" \ + "$P_SRV dtls=1" \ + "$P_CLI dtls=1" \ + 0 + # Checks that: # - things work with all ciphersuites active (used with config-full in all.sh) # - the expected (highest security) parameters are selected # ("signature_algorithm ext: 6" means SHA-512 (highest common hash)) -run_test "Default" \ +requires_ciphersuite_enabled "TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256" +requires_config_enabled MBEDTLS_SHA512_C +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +requires_config_enabled MBEDTLS_ECP_DP_SECP521R1_ENABLED +run_test "Default, choose highest security suite and hash" \ "$P_SRV debug_level=3" \ "$P_CLI" \ 0 \ @@ -967,12 +987,18 @@ run_test "Default" \ -S "error" \ -C "error" -run_test "Default, DTLS" \ - "$P_SRV dtls=1" \ +requires_ciphersuite_enabled "TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256" +requires_config_enabled MBEDTLS_SHA512_C +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +requires_config_enabled MBEDTLS_ECP_DP_SECP521R1_ENABLED +run_test "Default, choose highest security suite and hash, DTLS" \ + "$P_SRV debug_level=3 dtls=1" \ "$P_CLI dtls=1" \ 0 \ -s "Protocol is DTLSv1.2" \ - -s "Ciphersuite is TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256" + -s "Ciphersuite is TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256" \ + -s "client hello v3, signature_algorithm ext: 6" \ + -s "ECDHE curve: secp521r1" # Test current time in ServerHello requires_config_enabled MBEDTLS_HAVE_TIME From efe1327fefe185d07d4f5b1a8f2639d561c52fcd Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 3 Jul 2019 13:32:01 +0100 Subject: [PATCH 20/22] Add test to all.sh exercising hardcoded ciphersuite --- tests/scripts/all.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index f626b55c3..8c53c6700 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -759,6 +759,21 @@ component_test_full_cmake_clang () { if_build_succeeded env OPENSSL_CMD="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA' } +component_test_hardcoded_ciphersuite_cmake_clang() { + msg "build: cmake, full config + MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE, clang" # ~ 50s + scripts/config.pl full + scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests + scripts/config.pl set MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 + CC=clang cmake -D CMAKE_BUILD_TYPE:String=Check -D ENABLE_TESTING=On . + make + + msg "test: main suites (full config + MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)" # ~ 5s + make test + + msg "test: ssl-opt.sh default (full config + MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)" # ~ 5s + if_build_succeeded tests/ssl-opt.sh -f '^Default$\|^Default, DTLS$' +} + component_build_deprecated () { msg "build: make, full config + DEPRECATED_WARNING, gcc -O" # ~ 30s scripts/config.pl full From 779d79dcd719b0bdc1ae40f51a7ba9a4e4c35769 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 3 Jul 2019 13:37:21 +0100 Subject: [PATCH 21/22] Restore static inline qualif'n of some helpers in ssl_ciphersuites.h --- include/mbedtls/ssl_ciphersuites.h | 321 ++++++++++++----------------- library/ssl_ciphersuites.c | 48 ----- 2 files changed, 131 insertions(+), 238 deletions(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index b66c474f0..f12ad25e7 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -580,136 +580,6 @@ const int *mbedtls_ssl_list_ciphersuites( void ); * depending on whether MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE is defined. */ -static inline int mbedtls_ssl_ciphersuite_has_pfs_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_DHE_RSA: - case MBEDTLS_KEY_EXCHANGE_DHE_PSK: - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - case MBEDTLS_KEY_EXCHANGE_ECJPAKE: - return( 1 ); - - default: - return( 0 ); - } -} - -static inline int mbedtls_ssl_ciphersuite_no_pfs_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: - case MBEDTLS_KEY_EXCHANGE_RSA: - case MBEDTLS_KEY_EXCHANGE_PSK: - case MBEDTLS_KEY_EXCHANGE_RSA_PSK: - return( 1 ); - - default: - return( 0 ); - } -} - -static inline int mbedtls_ssl_ciphersuite_uses_ecdh_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: - return( 1 ); - - default: - return( 0 ); - } -} - -static inline int mbedtls_ssl_ciphersuite_cert_req_allowed_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_RSA: - case MBEDTLS_KEY_EXCHANGE_DHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - return( 1 ); - - default: - return( 0 ); - } -} - -static inline int mbedtls_ssl_ciphersuite_uses_srv_cert_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_RSA: - case MBEDTLS_KEY_EXCHANGE_RSA_PSK: - case MBEDTLS_KEY_EXCHANGE_DHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - return( 1 ); - - default: - return( 0 ); - } -} - -static inline int mbedtls_ssl_ciphersuite_uses_dhe_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_DHE_RSA: - case MBEDTLS_KEY_EXCHANGE_DHE_PSK: - return( 1 ); - - default: - return( 0 ); - } -} - -static inline int mbedtls_ssl_ciphersuite_uses_ecdhe_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: - return( 1 ); - - default: - return( 0 ); - } -} - -static inline int mbedtls_ssl_ciphersuite_uses_server_signature_internal( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - switch( mbedtls_ssl_suite_get_key_exchange( info ) ) - { - case MBEDTLS_KEY_EXCHANGE_DHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: - case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: - return( 1 ); - - default: - return( 0 ); - } -} - #if defined(MBEDTLS_PK_C) static inline mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg_internal( mbedtls_ssl_ciphersuite_handle_t info ) @@ -805,18 +675,6 @@ mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_string( mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_id( int ciphersuite_id ); -int mbedtls_ssl_ciphersuite_has_pfs( mbedtls_ssl_ciphersuite_handle_t info ); -int mbedtls_ssl_ciphersuite_no_pfs( mbedtls_ssl_ciphersuite_handle_t info ); -int mbedtls_ssl_ciphersuite_uses_ecdh( mbedtls_ssl_ciphersuite_handle_t info ); -int mbedtls_ssl_ciphersuite_cert_req_allowed( - mbedtls_ssl_ciphersuite_handle_t info ); -int mbedtls_ssl_ciphersuite_uses_srv_cert( - mbedtls_ssl_ciphersuite_handle_t info ); -int mbedtls_ssl_ciphersuite_uses_dhe( mbedtls_ssl_ciphersuite_handle_t info ); -int mbedtls_ssl_ciphersuite_uses_ecdhe( mbedtls_ssl_ciphersuite_handle_t info ); -int mbedtls_ssl_ciphersuite_uses_server_signature( - mbedtls_ssl_ciphersuite_handle_t info ); - #if defined(MBEDTLS_PK_C) mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ); @@ -836,54 +694,6 @@ int mbedtls_ssl_ciphersuite_uses_psk( mbedtls_ssl_ciphersuite_handle_t info ); #else /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ -static inline int mbedtls_ssl_ciphersuite_has_pfs( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_has_pfs_internal( info ) ); -} - -static inline int mbedtls_ssl_ciphersuite_no_pfs( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_no_pfs_internal( info ) ); -} - -static inline int mbedtls_ssl_ciphersuite_uses_ecdh( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_ecdh_internal( info ) ); -} - -static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_cert_req_allowed_internal( info ) ); -} - -static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_srv_cert_internal( info ) ); -} - -static inline int mbedtls_ssl_ciphersuite_uses_dhe( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_dhe_internal( info ) ); -} - -static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_ecdhe_internal( info ) ); -} - -static inline int mbedtls_ssl_ciphersuite_uses_server_signature( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_server_signature_internal( info ) ); -} - #if defined(MBEDTLS_PK_C) static inline mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ) @@ -942,6 +752,137 @@ static inline mbedtls_ssl_ciphersuite_handle_t mbedtls_ssl_ciphersuite_from_stri #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ +static inline int mbedtls_ssl_ciphersuite_has_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_DHE_RSA: + case MBEDTLS_KEY_EXCHANGE_DHE_PSK: + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + case MBEDTLS_KEY_EXCHANGE_ECJPAKE: + return( 1 ); + + default: + return( 0 ); + } +} + +static inline int mbedtls_ssl_ciphersuite_no_pfs( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: + case MBEDTLS_KEY_EXCHANGE_RSA: + case MBEDTLS_KEY_EXCHANGE_PSK: + case MBEDTLS_KEY_EXCHANGE_RSA_PSK: + return( 1 ); + + default: + return( 0 ); + } +} + + +static inline int mbedtls_ssl_ciphersuite_uses_ecdh( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: + return( 1 ); + + default: + return( 0 ); + } +} + +static inline int mbedtls_ssl_ciphersuite_cert_req_allowed( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_RSA: + case MBEDTLS_KEY_EXCHANGE_DHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + return( 1 ); + + default: + return( 0 ); + } +} + +static inline int mbedtls_ssl_ciphersuite_uses_srv_cert( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_RSA: + case MBEDTLS_KEY_EXCHANGE_RSA_PSK: + case MBEDTLS_KEY_EXCHANGE_DHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + return( 1 ); + + default: + return( 0 ); + } +} + +static inline int mbedtls_ssl_ciphersuite_uses_dhe( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_DHE_RSA: + case MBEDTLS_KEY_EXCHANGE_DHE_PSK: + return( 1 ); + + default: + return( 0 ); + } +} + +static inline int mbedtls_ssl_ciphersuite_uses_ecdhe( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: + return( 1 ); + + default: + return( 0 ); + } +} + +static inline int mbedtls_ssl_ciphersuite_uses_server_signature( + mbedtls_ssl_ciphersuite_handle_t info ) +{ + switch( mbedtls_ssl_suite_get_key_exchange( info ) ) + { + case MBEDTLS_KEY_EXCHANGE_DHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: + case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: + return( 1 ); + + default: + return( 0 ); + } +} + #ifdef __cplusplus } #endif diff --git a/library/ssl_ciphersuites.c b/library/ssl_ciphersuites.c index f03470476..ad660079a 100644 --- a/library/ssl_ciphersuites.c +++ b/library/ssl_ciphersuites.c @@ -2293,54 +2293,6 @@ int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name ) return( cur->id ); } -int mbedtls_ssl_ciphersuite_has_pfs( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_has_pfs_internal( info ) ); -} - -int mbedtls_ssl_ciphersuite_no_pfs( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_no_pfs_internal( info ) ); -} - -int mbedtls_ssl_ciphersuite_uses_ecdh( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_ecdh_internal( info ) ); -} - -int mbedtls_ssl_ciphersuite_cert_req_allowed( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_cert_req_allowed_internal( info ) ); -} - -int mbedtls_ssl_ciphersuite_uses_srv_cert( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_srv_cert_internal( info ) ); -} - -int mbedtls_ssl_ciphersuite_uses_dhe( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_dhe_internal( info ) ); -} - -int mbedtls_ssl_ciphersuite_uses_ecdhe( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_ecdhe_internal( info ) ); -} - -int mbedtls_ssl_ciphersuite_uses_server_signature( - mbedtls_ssl_ciphersuite_handle_t info ) -{ - return( mbedtls_ssl_ciphersuite_uses_server_signature_internal( info ) ); -} - #if defined(MBEDTLS_PK_C) mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( mbedtls_ssl_ciphersuite_handle_t info ) From 9e720e07e135894756b1a3f0878335418b83fb4a Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Mon, 8 Jul 2019 11:24:36 +0100 Subject: [PATCH 22/22] Add note that def'n of MBEDTLS_SUITE_XXX is just for check-names.sh --- include/mbedtls/ssl_ciphersuites.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/mbedtls/ssl_ciphersuites.h b/include/mbedtls/ssl_ciphersuites.h index f12ad25e7..925f2808e 100644 --- a/include/mbedtls/ssl_ciphersuites.h +++ b/include/mbedtls/ssl_ciphersuites.h @@ -322,7 +322,6 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; * as an example). */ -//#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_ID MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_NAME "TLS-ECDHE-ECDSA-WITH-AES-128-CCM-8" #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_CIPHER MBEDTLS_CIPHER_AES_128_CCM @@ -334,6 +333,9 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t; #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_MAX_MINOR_VER MBEDTLS_SSL_MINOR_VERSION_3 #define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8_FLAGS MBEDTLS_CIPHERSUITE_SHORT_TAG +/* This is just to make check-names.sh happy -- don't uncomment. */ +//#define MBEDTLS_SUITE_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 + /* * Helper macros to extract fields from ciphersuites. */