From f83d31260db0739308537f54237ff00f3759434b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 8 Nov 2018 09:52:25 +0100 Subject: [PATCH] Implement key_opaque option to ssl_client2 --- programs/ssl/ssl_client2.c | 16 ++++++++++++++-- tests/ssl-opt.sh | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 8d15b4f8f..f3bf495bb 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -566,6 +566,9 @@ int main( int argc, char *argv[] ) mbedtls_x509_crt cacert; mbedtls_x509_crt clicert; mbedtls_pk_context pkey; +#if defined(MBEDTLS_USE_PSA_CRYPTO) + psa_key_slot_t key_slot = 0; /* invalid key slot */ +#endif #endif char *p, *q; const int *list; @@ -1327,11 +1330,17 @@ int main( int argc, char *argv[] ) #if defined(MBEDTLS_USE_PSA_CRYPTO) if( opt.key_opaque != 0 ) { - /* coming soon: load key to key slot */ + if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot, + PSA_ALG_SHA_256 ) ) != 0 ) + { + mbedtls_printf( " failed\n ! " + "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", -ret ); + goto exit; + } } #endif /* MBEDTLS_USE_PSA_CRYPTO */ - mbedtls_printf( " ok\n" ); + mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) ); #endif /* MBEDTLS_X509_CRT_PARSE_C */ /* @@ -2138,6 +2147,9 @@ exit: mbedtls_x509_crt_free( &clicert ); mbedtls_x509_crt_free( &cacert ); mbedtls_pk_free( &pkey ); +#if defined(MBEDTLS_USE_PSA_CRYPTO) + psa_destroy_key( key_slot ); +#endif #endif mbedtls_ssl_session_free( &saved_session ); mbedtls_ssl_free( &ssl ); diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index ce9aee28a..5cded213e 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -865,6 +865,21 @@ run_test "Default, DTLS" \ -s "Protocol is DTLSv1.2" \ -s "Ciphersuite is TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256" +# Test using an opaque private key for client authentication +requires_config_enabled MBEDTLS_USE_PSA_CRYPTO +requires_config_enabled MBEDTLS_X509_CRT_PARSE_C +requires_config_enabled MBEDTLS_ECDSA_C +requires_config_enabled MBEDTLS_SHA256_C +run_test "Opaque key for client authentication" \ + "$P_SRV auth_mode=required" \ + "$P_CLI key_opaque=1 crt_file=data_files/server5.crt \ + key_file=data_files/server5.key" \ + 0 \ + -c "key type: Opaque" \ + -s "Verifying peer X.509 certificate... ok" \ + -S "error" \ + -C "error" + # Test current time in ServerHello requires_config_enabled MBEDTLS_HAVE_TIME run_test "ServerHello contains gmt_unix_time" \