From 1f02703e531343dbba9c5a89c9982d80e6b59d8f Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 5 Apr 2022 17:12:11 +0200 Subject: [PATCH 01/19] setup_psa_key_derivation(): add optional salt parameter Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 7804cb781..9f1cefd16 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4715,6 +4715,7 @@ static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* de psa_algorithm_t alg, const unsigned char* seed, size_t seed_length, const unsigned char* label, size_t label_length, + const unsigned char* salt, size_t salt_length, size_t capacity ) { psa_status_t status; @@ -4731,6 +4732,15 @@ static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* de if( status != PSA_SUCCESS ) return( status ); + if ( salt != NULL ) + { + status = psa_key_derivation_input_bytes( derivation, + PSA_KEY_DERIVATION_INPUT_SALT, + salt, salt_length ); + if( status != PSA_SUCCESS ) + return( status ); + } + if( mbedtls_svc_key_id_is_null( key ) ) { status = psa_key_derivation_input_bytes( @@ -4804,6 +4814,7 @@ static int tls_prf_generic( mbedtls_md_type_t md_type, random, rlen, (unsigned char const *) label, (size_t) strlen( label ), + NULL, 0, dlen ); if( status != PSA_SUCCESS ) { From ae4ed304351a61134459ea638935280b95e35824 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 5 Apr 2022 17:15:55 +0200 Subject: [PATCH 02/19] Fix naming: random bytes are the seed (not salt) in derivation process Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 9f1cefd16..7e35786fd 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5056,15 +5056,15 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, * is used. */ char const *lbl = "master secret"; - /* The salt for the KDF used for key expansion. + /* The seed for the KDF used for key expansion. * - If the Extended Master Secret extension is not used, * this is ClientHello.Random + ServerHello.Random * (see Sect. 8.1 in RFC 5246). * - If the Extended Master Secret extension is used, * this is the transcript of the handshake so far. * (see Sect. 4 in RFC 7627). */ - unsigned char const *salt = handshake->randbytes; - size_t salt_len = 64; + unsigned char const *seed = handshake->randbytes; + size_t seed_len = 64; #if !defined(MBEDTLS_DEBUG_C) && \ !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ @@ -5084,11 +5084,11 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) { lbl = "extended master secret"; - salt = session_hash; - handshake->calc_verify( ssl, session_hash, &salt_len ); + seed = session_hash; + handshake->calc_verify( ssl, session_hash, &seed_len ); MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", - session_hash, salt_len ); + session_hash, seed_len ); } #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ @@ -5115,7 +5115,7 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); status = setup_psa_key_derivation( &derivation, psk, alg, - salt, salt_len, + seed, seed_len, (unsigned char const *) lbl, (size_t) strlen( lbl ), master_secret_len ); @@ -5142,7 +5142,7 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, #endif { ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, - lbl, salt, salt_len, + lbl, seed, seed_len, master, master_secret_len ); if( ret != 0 ) From c2033409e307cc7564ed9f09f80ac3c3efa38dc4 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 5 Apr 2022 17:19:41 +0200 Subject: [PATCH 03/19] Add support for psa rsa-psk key exchange Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 7e35786fd..d3c99af7a 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5092,9 +5092,11 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, } #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ -#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ - defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) - if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && +#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ + ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) ) + if( ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || + handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) && ssl_use_opaque_psk( ssl ) == 1 ) { /* Perform PSK-to-MS expansion in a single step. */ @@ -5114,10 +5116,25 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, else alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); + size_t salt_len = 0; + unsigned char* salt = NULL; + + if ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + { + /* Provide other key as salt. + * For RSA-PKS other key length is always 48 bytes. + * Other key is stored in premaster, where first 2 bytes hold the + * length of the other key. Skip them. + */ + salt_len = 48; + salt = handshake->premaster + 2; + } + status = setup_psa_key_derivation( &derivation, psk, alg, seed, seed_len, (unsigned char const *) lbl, (size_t) strlen( lbl ), + salt, salt_len, master_secret_len ); if( status != PSA_SUCCESS ) { From f2534ba69b7285054e062307568e7c4b36da15ea Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 5 Apr 2022 17:21:14 +0200 Subject: [PATCH 04/19] tls12_client: skip PMS generation for opaque RSA-PSK Signed-off-by: Przemek Stekiel --- library/ssl_tls12_client.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/library/ssl_tls12_client.c b/library/ssl_tls12_client.c index dcc7dfb0d..aaffcd33f 100644 --- a/library/ssl_tls12_client.c +++ b/library/ssl_tls12_client.c @@ -3138,12 +3138,6 @@ ecdh_calc_secret: #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) { -#if defined(MBEDTLS_USE_PSA_CRYPTO) - /* Opaque PSKs are currently only supported for PSK-only suites. */ - if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); -#endif /* MBEDTLS_USE_PSA_CRYPTO */ - if( ( ret = ssl_write_encrypted_pms( ssl, header_len, &content_len, 2 ) ) != 0 ) return( ret ); @@ -3224,6 +3218,13 @@ ecdh_calc_secret: ( "skip PMS generation for opaque PSK" ) ); } else + if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK && + ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, + ( "skip PMS generation for opaque RSA-PSK" ) ); + } + else #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, From fc72e428ed559fce570e5e9324325d18f11860e6 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 5 Apr 2022 17:29:28 +0200 Subject: [PATCH 05/19] ssl_client2: Enable support for TLS 1.2 RSA-PSK opaque ciphersuite Signed-off-by: Przemek Stekiel --- programs/ssl/ssl_client2.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index cd6098682..66cf622aa 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -1406,13 +1406,16 @@ int main( int argc, char *argv[] ) #if defined (MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) if( opt.psk_opaque != 0 ) { - /* Ensure that the chosen ciphersuite is PSK-only; we must know + /* Ensure that the chosen ciphersuite is PSK-only or rsa-psk; we must know * the ciphersuite in advance to set the correct policy for the * PSK key slot. This limitation might go away in the future. */ - if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK || - opt.min_version != MBEDTLS_SSL_VERSION_TLS1_2 ) + if( ( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK && + ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_RSA_PSK ) || + opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 ) { - mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" ); + mbedtls_printf( "opaque PSKs are only supported in conjunction \ + with forcing TLS 1.2 and a PSK-only, RSA-PSK \ + ciphersuites through the 'force_ciphersuite' option.\n" ); ret = 2; goto usage; } From 8e0495e0f4f6645071e71ba287c89eedb2086786 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 5 Apr 2022 23:00:04 +0200 Subject: [PATCH 06/19] ssl-opt.sh: add tests for client psa opaque rsa-psk key exchange Signed-off-by: Przemek Stekiel --- tests/ssl-opt.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index d207e54cf..94b9f7d97 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -6452,6 +6452,66 @@ run_test "PSK callback: opaque psk on client, no callback, SHA-384, EMS" \ -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque rsa-psk on client, no callback" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA256 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque RSA-PSK"\ + -S "skip PMS generation for opaque RSA-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque rsa-psk on client, no callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque RSA-PSK"\ + -S "skip PMS generation for opaque RSA-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque rsa-psk on client, no callback, EMS" \ + "$P_SRV extended_ms=1 debug_level=3 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=1 debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque RSA-PSK"\ + -S "skip PMS generation for opaque RSA-PSK"\ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque rsa-psk on client, no callback, SHA-384, EMS" \ + "$P_SRV extended_ms=1 debug_level=3 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=1 debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque RSA-PSK"\ + -S "skip PMS generation for opaque RSA-PSK"\ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, static opaque on server, no callback" \ From aeb710fec5f9bdf6906486cea6513b7c47349af3 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Wed, 6 Apr 2022 11:40:30 +0200 Subject: [PATCH 07/19] Enable support for psa opaque RSA-PSK key exchange on the server side Signed-off-by: Przemek Stekiel --- library/ssl_tls12_server.c | 13 +++++++------ programs/ssl/ssl_server2.c | 9 ++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/library/ssl_tls12_server.c b/library/ssl_tls12_server.c index 6fd916f29..05d382b03 100644 --- a/library/ssl_tls12_server.c +++ b/library/ssl_tls12_server.c @@ -4047,18 +4047,19 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) return( ret ); } -#if defined(MBEDTLS_USE_PSA_CRYPTO) - /* Opaque PSKs are currently only supported for PSK-only. */ - if( ssl_use_opaque_psk( ssl ) == 1 ) - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); -#endif - if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret ); return( ret ); } +#if defined(MBEDTLS_USE_PSA_CRYPTO) + /* For opaque PSKs, we perform the PSK-to-MS derivation automatically + * and skip the intermediate PMS. */ + if( ssl_use_opaque_psk( ssl ) == 1 ) + MBEDTLS_SSL_DEBUG_MSG( 1, ( "skip PMS generation for opaque RSA-PSK" ) ); + else +#endif /* MBEDTLS_USE_PSA_CRYPTO */ if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl, ciphersuite_info->key_exchange ) ) != 0 ) { diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index a91af0eb5..13e340625 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -2210,10 +2210,13 @@ int main( int argc, char *argv[] ) /* Ensure that the chosen ciphersuite is PSK-only; we must know * the ciphersuite in advance to set the correct policy for the * PSK key slot. This limitation might go away in the future. */ - if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK || - opt.min_version != MBEDTLS_SSL_VERSION_TLS1_2 ) + if( ( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK && + ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_RSA_PSK ) || + opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 ) { - mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" ); + mbedtls_printf( "opaque PSKs are only supported in conjunction \ + with forcing TLS 1.2 and a PSK-only, RSA-PSK \ + ciphersuites through the 'force_ciphersuite' option.\n" ); ret = 2; goto usage; } From b270b56372a97e2f6fa39537025bcb21885a6ad8 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Wed, 6 Apr 2022 13:12:48 +0200 Subject: [PATCH 08/19] ssl-opt.sh: add tests for server psa opaque rsa-psk key exchange Signed-off-by: Przemek Stekiel --- tests/ssl-opt.sh | 124 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index 94b9f7d97..db5ab7365 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -6574,6 +6574,68 @@ run_test "PSK callback: raw psk on client, static opaque on server, no callba -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, static opaque on server, no callback" \ + "$P_SRV extended_ms=0 debug_level=5 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA" \ + "$P_CLI extended_ms=0 debug_level=5 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123" \ + 0 \ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, static opaque on server, no callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123" \ + 0 \ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, static opaque on server, no callback, EMS" \ + "$P_SRV debug_level=3 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123 extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, static opaque on server, no callback, EMS, SHA384" \ + "$P_SRV debug_level=3 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, no static PSK on server, opaque PSK from callback" \ @@ -6636,6 +6698,68 @@ run_test "PSK callback: raw psk on client, no static PSK on server, opaque PS -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, no static RSA-PSK on server, opaque RSA-PSK from callback" \ + "$P_SRV extended_ms=0 debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA" \ + "$P_CLI extended_ms=0 debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=def psk=beef" \ + 0 \ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, no static RSA-PSK on server, opaque RSA-PSK from callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384" \ + "$P_CLI extended_ms=0 debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=def psk=beef" \ + 0 \ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, no static RSA-PSK on server, opaque RSA-PSK from callback, EMS" \ + "$P_SRV debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=abc psk=dead extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw rsa-psk on client, no static RSA-PSK on server, opaque RSA-PSK from callback, EMS, SHA384" \ + "$P_SRV debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-RSA-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=abc psk=dead extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque RSA-PSK"\ + -s "skip PMS generation for opaque RSA-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, mismatching static raw PSK on server, opaque PSK from callback" \ From 51a1f36be082c5f24ac6859d4cf38dfae0d0ec1e Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Wed, 13 Apr 2022 08:57:06 +0200 Subject: [PATCH 09/19] setup_psa_key_derivation(): change salt parameter to other_secret Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index d3c99af7a..df6a793cf 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4715,7 +4715,8 @@ static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* de psa_algorithm_t alg, const unsigned char* seed, size_t seed_length, const unsigned char* label, size_t label_length, - const unsigned char* salt, size_t salt_length, + const unsigned char* other_secret, + size_t other_secret_length, size_t capacity ) { psa_status_t status; @@ -4732,11 +4733,11 @@ static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* de if( status != PSA_SUCCESS ) return( status ); - if ( salt != NULL ) + if ( other_secret != NULL ) { status = psa_key_derivation_input_bytes( derivation, - PSA_KEY_DERIVATION_INPUT_SALT, - salt, salt_length ); + PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, + other_secret, other_secret_length ); if( status != PSA_SUCCESS ) return( status ); } @@ -5116,25 +5117,25 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, else alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); - size_t salt_len = 0; - unsigned char* salt = NULL; + size_t other_secret_len = 0; + unsigned char* other_secret = NULL; if ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) { - /* Provide other key as salt. + /* Provide other key as other secret. * For RSA-PKS other key length is always 48 bytes. - * Other key is stored in premaster, where first 2 bytes hold the + * Other secret is stored in premaster, where first 2 bytes hold the * length of the other key. Skip them. */ - salt_len = 48; - salt = handshake->premaster + 2; + other_secret_len = 48; + other_secret = handshake->premaster + 2; } status = setup_psa_key_derivation( &derivation, psk, alg, seed, seed_len, (unsigned char const *) lbl, (size_t) strlen( lbl ), - salt, salt_len, + other_secret, other_secret_len, master_secret_len ); if( status != PSA_SUCCESS ) { From 19b80f8151c861138461e5ea31841d15d968f5c4 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Thu, 14 Apr 2022 08:29:31 +0200 Subject: [PATCH 10/19] Enable support for psa opaque ECDHE-PSK key exchange on the client side Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 30 ++++++++++++++-------- library/ssl_tls12_client.c | 52 +++++++++++++++++++++----------------- programs/ssl/ssl_client2.c | 8 +++--- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index df6a793cf..c60a8562c 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5093,11 +5093,13 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, } #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ -#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ - ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) ) +#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ + ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) ) if( ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || - handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) && + handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || + handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) && ssl_use_opaque_psk( ssl ) == 1 ) { /* Perform PSK-to-MS expansion in a single step. */ @@ -5120,15 +5122,23 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, size_t other_secret_len = 0; unsigned char* other_secret = NULL; - if ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) + switch( handshake->ciphersuite_info->key_exchange ) { - /* Provide other key as other secret. - * For RSA-PKS other key length is always 48 bytes. + /* Provide other secret. * Other secret is stored in premaster, where first 2 bytes hold the - * length of the other key. Skip them. + * length of the other key. */ - other_secret_len = 48; - other_secret = handshake->premaster + 2; + case MBEDTLS_KEY_EXCHANGE_RSA_PSK: + /* For RSA-PKS other key length is always 48 bytes. */ + other_secret_len = 48; + other_secret = handshake->premaster + 2; + break; + case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: + other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); + other_secret = handshake->premaster + 2; + break; + default: + break; } status = setup_psa_key_derivation( &derivation, psk, alg, diff --git a/library/ssl_tls12_client.c b/library/ssl_tls12_client.c index aaffcd33f..2868a84dc 100644 --- a/library/ssl_tls12_client.c +++ b/library/ssl_tls12_client.c @@ -2959,10 +2959,6 @@ ecdh_calc_secret: * ciphersuites we offered, so this should never happen. */ return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); - /* Opaque PSKs are currently only supported for PSK-only suites. */ - if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); - /* uint16 to store content length */ const size_t content_len_size = 2; @@ -3068,30 +3064,40 @@ ecdh_calc_secret: MBEDTLS_PUT_UINT16_BE( zlen, pms, 0 ); pms += zlen_size + zlen; - const unsigned char *psk = NULL; - size_t psk_len = 0; + /* In case of opaque psk skip writting psk to pms. + * Opaque key will be handled later. */ + if( ssl_conf_has_static_raw_psk( ssl->conf ) == 1 ) + { + const unsigned char *psk = NULL; + size_t psk_len = 0; - if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) - == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) - /* - * This should never happen because the existence of a PSK is always - * checked before calling this function - */ - return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); + if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) + == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) + /* + * This should never happen because the existence of a PSK is always + * checked before calling this function + */ + return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); - /* opaque psk<0..2^16-1>; */ - if( (size_t)( pms_end - pms ) < ( 2 + psk_len ) ) - return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); + /* opaque psk<0..2^16-1>; */ + if( (size_t)( pms_end - pms ) < ( 2 + psk_len ) ) + return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - /* Write the PSK length as uint16 */ - MBEDTLS_PUT_UINT16_BE( psk_len, pms, 0 ); - pms += 2; + /* Write the PSK length as uint16 */ + MBEDTLS_PUT_UINT16_BE( psk_len, pms, 0 ); + pms += 2; - /* Write the PSK itself */ - memcpy( pms, psk, psk_len ); - pms += psk_len; + /* Write the PSK itself */ + memcpy( pms, psk, psk_len ); + pms += psk_len; - ssl->handshake->pmslen = pms - ssl->handshake->premaster; + ssl->handshake->pmslen = pms - ssl->handshake->premaster; + } + else + { + MBEDTLS_SSL_DEBUG_MSG( 1, + ( "skip PMS generation for opaque ECDHE-PSK" ) ); + } } else #endif /* MBEDTLS_USE_PSA_CRYPTO && diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 66cf622aa..5ab13bee7 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -1406,11 +1406,13 @@ int main( int argc, char *argv[] ) #if defined (MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) if( opt.psk_opaque != 0 ) { - /* Ensure that the chosen ciphersuite is PSK-only or rsa-psk; we must know - * the ciphersuite in advance to set the correct policy for the + /* Ensure that the chosen ciphersuite is PSK-only, rsa-psk + or ecdhe-psk; we must know the ciphersuite in + advance to set the correct policy for the * PSK key slot. This limitation might go away in the future. */ 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_RSA_PSK && + ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) || opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 ) { mbedtls_printf( "opaque PSKs are only supported in conjunction \ From 14d11b0877ffecc42dae531afe17570f0e61b7e6 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Thu, 14 Apr 2022 08:33:29 +0200 Subject: [PATCH 11/19] Enable support for psa opaque ECDHE-PSK key exchange on the server side Signed-off-by: Przemek Stekiel --- library/ssl_tls12_server.c | 50 +++++++++++++++++++++----------------- programs/ssl/ssl_server2.c | 5 ++-- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/library/ssl_tls12_server.c b/library/ssl_tls12_server.c index 05d382b03..1adfe3e5f 100644 --- a/library/ssl_tls12_server.c +++ b/library/ssl_tls12_server.c @@ -4112,10 +4112,6 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED; uint8_t ecpoint_len; - /* Opaque PSKs are currently only supported for PSK-only. */ - if( ssl_use_opaque_psk( ssl ) == 1 ) - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); - mbedtls_ssl_handshake_params *handshake = ssl->handshake; if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) @@ -4188,28 +4184,38 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) const unsigned char *psk = NULL; size_t psk_len = 0; - if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) - == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) - /* - * This should never happen because the existence of a PSK is always - * checked before calling this function - */ - return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); + /* In case of opaque psk skip writting psk to pms. + * Opaque key will be handled later. */ + if( ssl_use_opaque_psk( ssl ) == 0 ) + { + if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) + == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) + /* + * This should never happen because the existence of a PSK is always + * checked before calling this function + */ + return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); - /* opaque psk<0..2^16-1>; */ - if( (size_t)( psm_end - psm ) < ( 2 + psk_len ) ) - return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); + /* opaque psk<0..2^16-1>; */ + if( (size_t)( psm_end - psm ) < ( 2 + psk_len ) ) + return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - /* Write the PSK length as uint16 */ - MBEDTLS_PUT_UINT16_BE( psk_len, psm, 0 ); - psm += 2; + /* Write the PSK length as uint16 */ + MBEDTLS_PUT_UINT16_BE( psk_len, psm, 0 ); + psm += 2; - /* Write the PSK itself */ - memcpy( psm, psk, psk_len ); - psm += psk_len; + /* Write the PSK itself */ + memcpy( psm, psk, psk_len ); + psm += psk_len; - ssl->handshake->pmslen = psm - ssl->handshake->premaster; -#else + ssl->handshake->pmslen = psm - ssl->handshake->premaster; + } + else + { + MBEDTLS_SSL_DEBUG_MSG( 1, + ( "skip PMS generation for opaque ECDHE-PSK" ) ); + } +#else /* MBEDTLS_USE_PSA_CRYPTO */ if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret ); diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index 13e340625..bc2aa6880 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -2211,11 +2211,12 @@ int main( int argc, char *argv[] ) * the ciphersuite in advance to set the correct policy for the * PSK key slot. This limitation might go away in the future. */ 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_RSA_PSK && + ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) || opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 ) { mbedtls_printf( "opaque PSKs are only supported in conjunction \ - with forcing TLS 1.2 and a PSK-only, RSA-PSK \ + with forcing TLS 1.2 and a PSK-only, RSA-PSK, ECDHE-PSK \ ciphersuites through the 'force_ciphersuite' option.\n" ); ret = 2; goto usage; From b6a0503dda9ae78643808481f99499f08fd635e3 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Thu, 14 Apr 2022 10:22:18 +0200 Subject: [PATCH 12/19] ssl-opt.sh: add tests for clent/server psa opaque ecdhe-psk key exchange Signed-off-by: Przemek Stekiel --- tests/ssl-opt.sh | 185 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index db5ab7365..15a19d53c 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -6512,6 +6512,67 @@ run_test "PSK callback: opaque rsa-psk on client, no callback, SHA-384, EMS" -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque ecdhe-psk on client, no callback" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA256 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque ECDHE-PSK"\ + -S "skip PMS generation for opaque ECDHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque ecdhe-psk on client, no callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque ECDHE-PSK"\ + -S "skip PMS generation for opaque ECDHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque ecdhe-psk on client, no callback, EMS" \ + "$P_SRV extended_ms=1 debug_level=3 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=1 debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque ECDHE-PSK"\ + -S "skip PMS generation for opaque ECDHE-PSK"\ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque ecdhe-psk on client, no callback, SHA-384, EMS" \ + "$P_SRV extended_ms=1 debug_level=3 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=1 debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque ECDHE-PSK"\ + -S "skip PMS generation for opaque ECDHE-PSK"\ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, static opaque on server, no callback" \ @@ -6636,6 +6697,68 @@ run_test "PSK callback: raw rsa-psk on client, static opaque on server, no ca -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, static opaque on server, no callback" \ + "$P_SRV extended_ms=0 debug_level=5 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA" \ + "$P_CLI extended_ms=0 debug_level=5 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123" \ + 0 \ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, static opaque on server, no callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123" \ + 0 \ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, static opaque on server, no callback, EMS" \ + "$P_SRV debug_level=3 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123 extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, static opaque on server, no callback, EMS, SHA384" \ + "$P_SRV debug_level=3 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, no static PSK on server, opaque PSK from callback" \ @@ -6760,6 +6883,68 @@ run_test "PSK callback: raw rsa-psk on client, no static RSA-PSK on server, o -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, no static ECDHE-PSK on server, opaque ECDHE-PSK from callback" \ + "$P_SRV extended_ms=0 debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA" \ + "$P_CLI extended_ms=0 debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=def psk=beef" \ + 0 \ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, no static ECDHE-PSK on server, opaque ECDHE-PSK from callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384" \ + "$P_CLI extended_ms=0 debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=def psk=beef" \ + 0 \ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, no static ECDHE-PSK on server, opaque ECDHE-PSK from callback, EMS" \ + "$P_SRV debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=abc psk=dead extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw ecdhe-psk on client, no static ECDHE-PSK on server, opaque ECDHE-PSK from callback, EMS, SHA384" \ + "$P_SRV debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-ECDHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=abc psk=dead extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque ECDHE-PSK"\ + -s "skip PMS generation for opaque ECDHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, mismatching static raw PSK on server, opaque PSK from callback" \ From b293aaa61b38cae1dfd58f3e56ea45cc2951f87b Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 19 Apr 2022 12:22:38 +0200 Subject: [PATCH 13/19] Enable support for psa opaque DHE-PSK key exchange on the client side Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 28 ++++++++++++++++++++++------ library/ssl_tls12_client.c | 6 ------ programs/ssl/ssl_client2.c | 16 ---------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index c60a8562c..294ed487e 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5099,7 +5099,8 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) ) if( ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || - handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) && + handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || + handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) && ssl_use_opaque_psk( ssl ) == 1 ) { /* Perform PSK-to-MS expansion in a single step. */ @@ -5134,6 +5135,7 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, other_secret = handshake->premaster + 2; break; case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: + case MBEDTLS_KEY_EXCHANGE_DHE_PSK: other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); other_secret = handshake->premaster + 2; break; @@ -5383,21 +5385,27 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch unsigned char *end = p + sizeof( ssl->handshake->premaster ); const unsigned char *psk = NULL; size_t psk_len = 0; + int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) (void) key_ex; #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ - if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) - == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) + if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) { /* * This should never happen because the existence of a PSK is always - * checked before calling this function + * checked before calling this function. + * + * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with + * the the shared secret without PSK. */ - MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); - return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); + if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); + return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); + } } /* @@ -5458,6 +5466,14 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch p += 2 + len; MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); + + /* For opaque PSK fill premaster with the the shared secret without PSK. */ + if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, + ( "skip PMS generation for opaque DHE-PSK" ) ); + return( 0 ); + } } else #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ diff --git a/library/ssl_tls12_client.c b/library/ssl_tls12_client.c index 2868a84dc..f6a21cbee 100644 --- a/library/ssl_tls12_client.c +++ b/library/ssl_tls12_client.c @@ -3153,12 +3153,6 @@ ecdh_calc_secret: #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) { -#if defined(MBEDTLS_USE_PSA_CRYPTO) - /* Opaque PSKs are currently only supported for PSK-only suites. */ - if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 ) - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); -#endif /* MBEDTLS_USE_PSA_CRYPTO */ - /* * ClientDiffieHellmanPublic public (DHM send G^X mod P) */ diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 5ab13bee7..2f33d8f59 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -1406,22 +1406,6 @@ int main( int argc, char *argv[] ) #if defined (MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) if( opt.psk_opaque != 0 ) { - /* Ensure that the chosen ciphersuite is PSK-only, rsa-psk - or ecdhe-psk; we must know the ciphersuite in - advance to set the correct policy for the - * PSK key slot. This limitation might go away in the future. */ - 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_ECDHE_PSK ) || - opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 ) - { - mbedtls_printf( "opaque PSKs are only supported in conjunction \ - with forcing TLS 1.2 and a PSK-only, RSA-PSK \ - ciphersuites through the 'force_ciphersuite' option.\n" ); - ret = 2; - goto usage; - } - /* Determine KDF algorithm the opaque PSK will be used in. */ #if defined(MBEDTLS_SHA384_C) if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) From cb322eac6bb67b1e6d2963310766448759defb9a Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 19 Apr 2022 12:23:37 +0200 Subject: [PATCH 14/19] Enable support for psa opaque DHE-PSK key exchange on the server side Signed-off-by: Przemek Stekiel --- library/ssl_tls12_server.c | 6 ------ programs/ssl/ssl_server2.c | 15 --------------- 2 files changed, 21 deletions(-) diff --git a/library/ssl_tls12_server.c b/library/ssl_tls12_server.c index 1adfe3e5f..5fa564322 100644 --- a/library/ssl_tls12_server.c +++ b/library/ssl_tls12_server.c @@ -4083,12 +4083,6 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) return( ret ); } -#if defined(MBEDTLS_USE_PSA_CRYPTO) - /* Opaque PSKs are currently only supported for PSK-only. */ - if( ssl_use_opaque_psk( ssl ) == 1 ) - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); -#endif - if( p != end ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) ); diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index bc2aa6880..46c6b7601 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -2207,21 +2207,6 @@ int main( int argc, char *argv[] ) #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 ) { - /* Ensure that the chosen ciphersuite is PSK-only; we must know - * the ciphersuite in advance to set the correct policy for the - * PSK key slot. This limitation might go away in the future. */ - 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_ECDHE_PSK ) || - opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 ) - { - mbedtls_printf( "opaque PSKs are only supported in conjunction \ - with forcing TLS 1.2 and a PSK-only, RSA-PSK, ECDHE-PSK \ - ciphersuites through the 'force_ciphersuite' option.\n" ); - ret = 2; - goto usage; - } - /* Determine KDF algorithm the opaque PSK will be used in. */ #if defined(MBEDTLS_SHA384_C) if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) From 85d46fe6cf16b72b3429ab8a7201cf7ed6a19f83 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Tue, 19 Apr 2022 12:47:48 +0200 Subject: [PATCH 15/19] ssl-opt.sh: add tests for clent/server psa opaque dhe-psk key exchange Signed-off-by: Przemek Stekiel --- tests/ssl-opt.sh | 183 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index 15a19d53c..deb4162f7 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -6572,6 +6572,65 @@ run_test "PSK callback: opaque ecdhe-psk on client, no callback, SHA-384, EMS -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque dhe-psk on client, no callback" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA256 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque DHE-PSK"\ + -S "skip PMS generation for opaque DHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque dhe-psk on client, no callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque DHE-PSK"\ + -S "skip PMS generation for opaque DHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque dhe-psk on client, no callback, EMS" \ + "$P_SRV extended_ms=1 debug_level=3 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=1 debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque DHE-PSK"\ + -S "skip PMS generation for opaque DHE-PSK"\ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: opaque dhe-psk on client, no callback, SHA-384, EMS" \ + "$P_SRV extended_ms=1 debug_level=3 psk=abc123 psk_identity=foo" \ + "$P_CLI extended_ms=1 debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 psk_opaque=1" \ + 0 \ + -c "skip PMS generation for opaque DHE-PSK"\ + -S "skip PMS generation for opaque DHE-PSK"\ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 @@ -6759,6 +6818,68 @@ run_test "PSK callback: raw ecdhe-psk on client, static opaque on server, no -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, static opaque on server, no callback" \ + "$P_SRV extended_ms=0 debug_level=5 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA" \ + "$P_CLI extended_ms=0 debug_level=5 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123" \ + 0 \ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, static opaque on server, no callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=1 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384" \ + "$P_CLI extended_ms=0 debug_level=1 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123" \ + 0 \ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, static opaque on server, no callback, EMS" \ + "$P_SRV debug_level=3 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=foo psk=abc123 extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, static opaque on server, no callback, EMS, SHA384" \ + "$P_SRV debug_level=3 psk=abc123 psk_identity=foo psk_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=foo psk=abc123 extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, no static PSK on server, opaque PSK from callback" \ @@ -6945,6 +7066,68 @@ run_test "PSK callback: raw ecdhe-psk on client, no static ECDHE-PSK on serve -S "SSL - Unknown identity received" \ -S "SSL - Verification of the message MAC failed" +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, no static DHE-PSK on server, opaque DHE-PSK from callback" \ + "$P_SRV extended_ms=0 debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA" \ + "$P_CLI extended_ms=0 debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=def psk=beef" \ + 0 \ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, no static DHE-PSK on server, opaque DHE-PSK from callback, SHA-384" \ + "$P_SRV extended_ms=0 debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384" \ + "$P_CLI extended_ms=0 debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=def psk=beef" \ + 0 \ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -C "session hash for extended master secret"\ + -S "session hash for extended master secret"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, no static DHE-PSK on server, opaque DHE-PSK from callback, EMS" \ + "$P_SRV debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-128-CBC-SHA \ + psk_identity=abc psk=dead extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 +run_test "PSK callback: raw dhe-psk on client, no static DHE-PSK on server, opaque DHE-PSK from callback, EMS, SHA384" \ + "$P_SRV debug_level=3 psk_list=abc,dead,def,beef psk_list_opaque=1 min_version=tls12 \ + force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 extended_ms=1" \ + "$P_CLI debug_level=3 min_version=tls12 force_ciphersuite=TLS-DHE-PSK-WITH-AES-256-CBC-SHA384 \ + psk_identity=abc psk=dead extended_ms=1" \ + 0 \ + -c "session hash for extended master secret"\ + -s "session hash for extended master secret"\ + -C "skip PMS generation for opaque DHE-PSK"\ + -s "skip PMS generation for opaque DHE-PSK"\ + -S "SSL - The handshake negotiation failed" \ + -S "SSL - Unknown identity received" \ + -S "SSL - Verification of the message MAC failed" + requires_config_enabled MBEDTLS_USE_PSA_CRYPTO requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 run_test "PSK callback: raw psk on client, mismatching static raw PSK on server, opaque PSK from callback" \ From 99114f3084784f8629fd95004e2cbd3c45c4a6b0 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Fri, 22 Apr 2022 11:20:09 +0200 Subject: [PATCH 16/19] Fix build flags for opaque/raw psk checks Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 23 ++++++++++++++++------- library/ssl_tls12_client.c | 8 ++++++-- library/ssl_tls12_server.c | 10 ++++++++-- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 294ed487e..441aa0f4e 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5001,8 +5001,13 @@ static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, return( 0 ); } -#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ - defined(MBEDTLS_USE_PSA_CRYPTO) + + +#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ + ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) ) static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) { if( ssl->conf->f_psk != NULL ) @@ -5021,7 +5026,10 @@ static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) return( 0 ); } #endif /* MBEDTLS_USE_PSA_CRYPTO && - MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ + ( MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || + MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || + MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || + MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ) */ /* * Compute master secret if needed @@ -5093,10 +5101,11 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, } #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ -#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ - ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) ) +#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ + ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) ) if( ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || diff --git a/library/ssl_tls12_client.c b/library/ssl_tls12_client.c index f6a21cbee..d286764c5 100644 --- a/library/ssl_tls12_client.c +++ b/library/ssl_tls12_client.c @@ -73,7 +73,9 @@ int mbedtls_ssl_conf_has_static_psk( mbedtls_ssl_config const *conf ) return( 0 ); } -#if defined(MBEDTLS_USE_PSA_CRYPTO) +#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ + ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ) || \ + defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) ) static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf ) { if( conf->psk_identity == NULL || @@ -87,7 +89,9 @@ static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf ) return( 0 ); } -#endif /* MBEDTLS_USE_PSA_CRYPTO */ +#endif /* MBEDTLS_USE_PSA_CRYPTO && + ( MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || + MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ) */ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ diff --git a/library/ssl_tls12_server.c b/library/ssl_tls12_server.c index 5fa564322..e9a505a5a 100644 --- a/library/ssl_tls12_server.c +++ b/library/ssl_tls12_server.c @@ -171,7 +171,10 @@ static int ssl_conf_has_psk_or_cb( mbedtls_ssl_config const *conf ) return( 0 ); } -#if defined(MBEDTLS_USE_PSA_CRYPTO) +#if defined(MBEDTLS_USE_PSA_CRYPTO) && \ + ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ) || \ + defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ + defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) ) static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) { if( ssl->conf->f_psk != NULL ) @@ -190,7 +193,10 @@ static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) return( 0 ); } -#endif /* MBEDTLS_USE_PSA_CRYPTO */ +#endif /* MBEDTLS_USE_PSA_CRYPTO && + ( MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || + MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || + MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) */ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl, From 8abcee9290905e8e5cf93faa68ac8dad9cb28c45 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Thu, 28 Apr 2022 09:16:28 +0200 Subject: [PATCH 17/19] Fix typos Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 441aa0f4e..d712debef 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5139,7 +5139,7 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, * length of the other key. */ case MBEDTLS_KEY_EXCHANGE_RSA_PSK: - /* For RSA-PKS other key length is always 48 bytes. */ + /* For RSA-PSK other key length is always 48 bytes. */ other_secret_len = 48; other_secret = handshake->premaster + 2; break; @@ -5408,7 +5408,7 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch * checked before calling this function. * * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with - * the the shared secret without PSK. + * the shared secret without PSK. */ if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) { From 8a4b7fd7c366a4153581f2ac6b4d3c5d048cfca0 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Thu, 28 Apr 2022 09:22:22 +0200 Subject: [PATCH 18/19] Optimize code Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index d712debef..3415c2805 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5004,10 +5004,7 @@ static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ - ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) ) + defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED ) static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) { if( ssl->conf->f_psk != NULL ) @@ -5102,14 +5099,8 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ - ( defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \ - defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) ) - if( ( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || - handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK || - handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || - handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) && + defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) + if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 && ssl_use_opaque_psk( ssl ) == 1 ) { /* Perform PSK-to-MS expansion in a single step. */ From 169bf0b8b073a5efde9875acf38e5a482749d028 Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Fri, 29 Apr 2022 07:53:29 +0200 Subject: [PATCH 19/19] Fix comments (#endif flags) Signed-off-by: Przemek Stekiel --- library/ssl_tls.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 3415c2805..e9e8b613f 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5023,10 +5023,7 @@ static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) return( 0 ); } #endif /* MBEDTLS_USE_PSA_CRYPTO && - ( MBEDTLS_KEY_EXCHANGE_PSK_ENABLED || - MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED || - MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || - MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ) */ + MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ /* * Compute master secret if needed @@ -5096,7 +5093,7 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", session_hash, seed_len ); } -#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ +#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)