From 591f162bed106f5a225de4f6e193f19f3565273d Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Mon, 22 Jan 2018 12:30:04 +0200 Subject: [PATCH] support mki value Add support mki value in the DTLS-SRTP Signed-off-by: Johan Pascal --- include/mbedtls/ssl.h | 28 ++++++++++- library/ssl_cli.c | 72 +++++++++++++++++++-------- library/ssl_srv.c | 111 +++++++++++++++++++++++++++--------------- library/ssl_tls.c | 18 +++++++ 4 files changed, 168 insertions(+), 61 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index 3c84bb7d0..72ba687d3 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -214,6 +214,9 @@ #define MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED 1 #define MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED 0 +#define MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED 0 +#define MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED 1 + /* * Default range for DTLS retransmission timer value, in milliseconds. * RFC 6347 4.2.4.1 says from 1 second to 60 seconds. @@ -1182,7 +1185,7 @@ struct mbedtls_ssl_config #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ #if defined(MBEDTLS_SSL_DTLS_SRTP) unsigned int dtls_srtp_mki_support : 1; /* support having mki_value - in the use_srtp extension */ + in the use_srtp extension */ #endif }; @@ -3174,6 +3177,16 @@ const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ); #endif /* MBEDTLS_SSL_ALPN */ #if defined(MBEDTLS_SSL_DTLS_SRTP) +/** + * \brief Add support for mki value in use_srtp extension + * (Default: MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) + * + * \param conf SSL configuration + * \param truncate Enable or disable (MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED or + * MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) + */ +void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, int support_mki_value ); + /** * \brief Set the supported DTLS-SRTP protection profiles. * @@ -3185,6 +3198,17 @@ const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ); * \return 0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA. */ int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, const mbedtls_ssl_srtp_profile *profiles, size_t profiles_number); + +/** + * \brief Set the mki_value for the current dtls session. + * + * \param ssl SSL context + * \param mki_value MKI value to set + * \param mki_len MKI length + * + * \return 0 on success, MBEDTLS_ERR_SSL_BAD_INPUT_DATA or MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE + */ +int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, unsigned char* mki_value, size_t mki_len ); /** * \brief Get the negotiated DTLS-SRTP Protection Profile. * This function should be called after the handshake is @@ -3194,7 +3218,7 @@ int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, co * * \return Protection Profile enum member, MBEDTLS_SRTP_UNSET_PROFILE if no protocol was negotiated. */ -mbedtls_ssl_srtp_profile mbedtls_ssl_get_dtls_srtp_protection_profile( const mbedtls_ssl_context *ssl); +mbedtls_ssl_srtp_profile mbedtls_ssl_get_dtls_srtp_protection_profile( const mbedtls_ssl_context *ssl ); /** * \brief Get the generated DTLS-SRTP key material. diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 17cd4828b..8b9f2f0f7 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -762,6 +762,7 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, { unsigned char *p = buf; size_t protection_profiles_index = 0; + size_t mki_len = 0, i; *olen = 0; @@ -785,12 +786,15 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; * - * Note: srtp_mki is not supported */ - - /* Extension length = 2bytes for profiles lenght, ssl->conf->dtls_srtp_profile_list_len*2 (each profile is 2 bytes length ) + 1 byte for the non implemented srtp_mki vector length (always 0) */ - *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 ) >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 ) ) & 0xFF ); + if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED && + ssl->dtls_srtp_info.mki_len != 0 ) + { + mki_len = ssl->dtls_srtp_info.mki_len; + } + /* Extension length = 2bytes for profiles length, ssl->conf->dtls_srtp_profile_list_len*2 (each profile is 2 bytes length ) + 1 byte for srtp_mki vector length and the mki_len value */ + *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len ) >> 8 ) & 0xFF ); + *p++ = (unsigned char)( ( ( 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len ) ) & 0xFF ); /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ @@ -831,9 +835,18 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, } } - *p++ = 0x00; /* non implemented srtp_mki vector length is always 0 */ + *p++ = mki_len & 0xFF; + + if( mki_len != 0 ) + { + for( i=0; i < mki_len; i++ ) + { + *p++ = ssl->dtls_srtp_info.mki_value[i]; + } + } + /* total extension length: extension type (2 bytes) + extension length (2 bytes) + protection profile length (2 bytes) + 2*nb protection profiles + srtp_mki vector length(1 byte)*/ - *olen = 2 + 2 + 2 + 2*(ssl->conf->dtls_srtp_profile_list_len) + 1; + *olen = 2 + 2 + 2 + 2*( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len; } #endif /* MBEDTLS_SSL_DTLS_SRTP */ @@ -1804,7 +1817,7 @@ static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len ) { mbedtls_ssl_srtp_profile server_protection = MBEDTLS_SRTP_UNSET_PROFILE; - size_t i; + size_t i, mki_len = 0; uint16_t server_protection_profile_value = 0; /* If use_srtp is not configured, just ignore the extension */ @@ -1821,22 +1834,28 @@ static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; * - * Note: srtp_mki is not supported */ + if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED && + ssl->dtls_srtp_info.mki_len != 0 ) + { + mki_len = ssl->dtls_srtp_info.mki_len; + } - /* Length is 5 : one protection profile(2 bytes) + length(2 bytes) and potential srtp_mki which won't be parsed */ - if( len < 5 ) + /* Length is 5 and optional mki_value : one protection profile(2 bytes) + length(2 bytes) and srtp_mki */ + if( ( len != 5 ) && ( len != ( 5 + mki_len ) ) ) return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); /* * get the server protection profile */ - if (((uint16_t)(buf[0]<<8 | buf[1])) != 0x0002) { /* protection profile length must be 0x0002 as we must have only one protection profile in server Hello */ + if (((uint16_t)( ( buf[0]<<8 ) | buf[1] ) ) != 0x0002) { /* protection profile length must be 0x0002 as we must have only one protection profile in server Hello */ return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); } else { - server_protection_profile_value = buf[2]<<8 | buf[3]; + server_protection_profile_value = ( buf[2]<<8 ) | buf[3]; } + ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE; + /* * Check we have the server profile in our list */ @@ -1862,15 +1881,30 @@ static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) { ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i]; - return 0; + break; } } - /* If we get there, no match was found : server problem, it shall never answer with incompatible profile */ - ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE; - mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, - MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); - return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); + /* If no match was found : server problem, it shall never answer with incompatible profile */ + if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE ) + { + mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, + MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); + return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); + } + /* RFC5764: + * If the client detects a nonzero-length MKI in the server's response + * that is different than the one the client offered, then the client + * MUST abort the handshake and SHOULD send an invalid_parameter alert. + */ + if( len > 5 && + ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) + { + 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 ); + } + return 0; } #endif /* MBEDTLS_SSL_DTLS_SRTP */ diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 68afcbaef..f5229efb4 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -782,7 +782,7 @@ static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, { mbedtls_ssl_srtp_profile client_protection = MBEDTLS_SRTP_UNSET_PROFILE; size_t i,j; - uint16_t profile_length; + size_t profile_length; /* If use_srtp is not configured, just ignore the extension */ if( ( ssl->conf->dtls_srtp_profile_list == NULL ) || ( ssl->conf->dtls_srtp_profile_list_len == 0 ) ) @@ -798,53 +798,71 @@ static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl, * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>; * - * Note: srtp_mki is not supported */ /* Min length is 5 : at least one protection profile(2 bytes) and length(2 bytes) + srtp_mki length(1 byte) */ if( len < 5 ) return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); - /* - * Use our order of preference - */ - profile_length = buf[0]<<8|buf[1]; /* first 2 bytes are protection profile length(in bytes) */ - for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++) + ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE; + + profile_length = ( buf[0]<<8 ) | buf[1]; /* first 2 bytes are protection profile length(in bytes) */ + + + /* parse the extension list values are defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml */ + for( j=0; j < profile_length; j+=2 ) { - /* parse the extension list values are defined in http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml */ - for (j=0; jconf->dtls_srtp_profile_list[i]) { + switch ( protection_profile_value ) + { + case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80_IANA_VALUE: + client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_80; + break; + case MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32_IANA_VALUE: + client_protection = MBEDTLS_SRTP_AES128_CM_HMAC_SHA1_32; + break; + case MBEDTLS_SRTP_NULL_HMAC_SHA1_80_IANA_VALUE: + client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_80; + break; + case MBEDTLS_SRTP_NULL_HMAC_SHA1_32_IANA_VALUE: + client_protection = MBEDTLS_SRTP_NULL_HMAC_SHA1_32; + break; + default: + client_protection = MBEDTLS_SRTP_UNSET_PROFILE; + break; + } + /* check if suggested profile is in our list */ + for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++) + { + if( client_protection == ssl->conf->dtls_srtp_profile_list[i] ) + { ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i]; - return 0; + break; } } + if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_SRTP_UNSET_PROFILE ) + break; + } + if( ( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED ) && + ( len > ( profile_length + 2 ) ) ) + { + ssl->dtls_srtp_info.mki_len = buf[ profile_length + 2 ]; + if( ssl->dtls_srtp_info.mki_len > MBEDTLS_DTLS_SRTP_MAX_MKI_LENGTH ) + { + mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, + MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); + ssl->dtls_srtp_info.mki_len = 0; + return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO ); + } + + for( i=0; i < ssl->dtls_srtp_info.mki_len; i++ ) + { + ssl->dtls_srtp_info.mki_value[i] = buf[ profile_length + 2 + i ]; + } } - /* If we get there, no match was found */ - ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_SRTP_UNSET_PROFILE; - // mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, - // MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ); - return( 0 ); + return( 0 ); } #endif /* MBEDTLS_SSL_DTLS_SRTP */ @@ -2585,6 +2603,8 @@ static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, unsigned char *buf, size_t *olen ) { + size_t mki_len = 0, ext_len = 0, i; + if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_SRTP_UNSET_PROFILE ) { *olen = 0; @@ -2593,12 +2613,19 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding use_srtp extension" ) ); + if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED && + ssl->dtls_srtp_info.mki_len != 0 ) + { + mki_len = ssl->dtls_srtp_info.mki_len; + } + /* extension */ buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF ); buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF ); - /* total length (5: only one profile(2 bytes) and length(2bytes) and srtp_mki not supported so zero length(1byte) ) */ - buf[2] = 0x00; - buf[3] = 0x05; + /* total length 5 and mki value: only one profile(2 bytes) and length(2 bytes) and srtp_mki ) */ + ext_len = 5 + mki_len; + buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); + buf[3] = (unsigned char)( ext_len & 0xFF ); /* protection profile length: 2 */ buf[4] = 0x00; @@ -2625,9 +2652,13 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, return; } - buf[8] = 0x00; /* unsupported srtp_mki variable length vector set to 0 */ + buf[8] = mki_len & 0xFF; + for( i=0; i < mki_len; i++ ) + { + buf[ 9 + i ] = ssl->dtls_srtp_info.mki_value[i]; + } - *olen = 9; + *olen = 9 + mki_len; } #endif /* MBEDTLS_SSL_DTLS_SRTP */ diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 48ddd9a67..ecdd4f636 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4714,6 +4714,24 @@ const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ) #endif /* MBEDTLS_SSL_ALPN */ #if defined(MBEDTLS_SSL_DTLS_SRTP) +void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, int support_mki_value ) +{ + conf->dtls_srtp_mki_support = support_mki_value; +} + +int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, unsigned char* mki_value, size_t mki_len ) +{ + if ( mki_len > MBEDTLS_DTLS_SRTP_MAX_MKI_LENGTH ) + return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; + + if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED ) + return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; + + memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); + ssl->dtls_srtp_info.mki_len = mki_len; + return 0; +} + int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, const mbedtls_ssl_srtp_profile *profiles, size_t profiles_number) { size_t i;