mirror of
https://github.com/yuzu-mirror/mbedtls.git
synced 2026-04-04 14:08:39 +00:00
Merge branch 'opaque_psk_implementation' into development-psa-proposed
This commit is contained in:
commit
485529952f
8 changed files with 1198 additions and 179 deletions
|
|
@ -61,6 +61,7 @@ int main( void )
|
|||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
|
@ -86,6 +87,7 @@ int main( void )
|
|||
#define DFL_KEY_FILE ""
|
||||
#define DFL_KEY_OPAQUE 0
|
||||
#define DFL_PSK ""
|
||||
#define DFL_PSK_OPAQUE 0
|
||||
#define DFL_PSK_IDENTITY "Client_identity"
|
||||
#define DFL_ECJPAKE_PW NULL
|
||||
#define DFL_EC_MAX_OPS -1
|
||||
|
|
@ -147,9 +149,24 @@ int main( void )
|
|||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
#define USAGE_PSK \
|
||||
#define USAGE_PSK_RAW \
|
||||
" psk=%%s default: \"\" (in hex, without 0x)\n" \
|
||||
" psk_identity=%%s default: \"Client_identity\"\n"
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define USAGE_PSK_SLOT \
|
||||
" psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
|
||||
" Enable this to store the PSK configured through command line\n" \
|
||||
" parameter `psk` in a PSA-based key slot.\n" \
|
||||
" Note: Currently only supported in conjunction with\n" \
|
||||
" the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
|
||||
" to force a particular PSK-only ciphersuite.\n" \
|
||||
" Note: This is to test integration of PSA-based opaque PSKs with\n" \
|
||||
" Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
|
||||
" with prepopulated key slots instead of importing raw key material.\n"
|
||||
#else
|
||||
#define USAGE_PSK_SLOT ""
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
|
||||
#else
|
||||
#define USAGE_PSK ""
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
|
||||
|
|
@ -347,6 +364,9 @@ struct options
|
|||
const char *crt_file; /* the file with the client certificate */
|
||||
const char *key_file; /* the file with the client key */
|
||||
int key_opaque; /* handle private key as if it were opaque */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
int psk_opaque;
|
||||
#endif
|
||||
const char *psk; /* the pre-shared key */
|
||||
const char *psk_identity; /* the pre-shared key identity */
|
||||
const char *ecjpake_pw; /* the EC J-PAKE password */
|
||||
|
|
@ -550,6 +570,13 @@ int main( int argc, char *argv[] )
|
|||
|
||||
const char *pers = "ssl_client2";
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_slot_t slot = 0;
|
||||
psa_algorithm_t alg = 0;
|
||||
psa_key_policy_t policy;
|
||||
psa_status_t status;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
|
||||
#endif
|
||||
|
|
@ -572,9 +599,6 @@ int main( int argc, char *argv[] )
|
|||
#endif
|
||||
char *p, *q;
|
||||
const int *list;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Make sure memory references are valid.
|
||||
|
|
@ -642,6 +666,9 @@ int main( int argc, char *argv[] )
|
|||
opt.key_file = DFL_KEY_FILE;
|
||||
opt.key_opaque = DFL_KEY_OPAQUE;
|
||||
opt.psk = DFL_PSK;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
opt.psk_opaque = DFL_PSK_OPAQUE;
|
||||
#endif
|
||||
opt.psk_identity = DFL_PSK_IDENTITY;
|
||||
opt.ecjpake_pw = DFL_ECJPAKE_PW;
|
||||
opt.ec_max_ops = DFL_EC_MAX_OPS;
|
||||
|
|
@ -746,6 +773,10 @@ int main( int argc, char *argv[] )
|
|||
#endif
|
||||
else if( strcmp( p, "psk" ) == 0 )
|
||||
opt.psk = q;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
else if( strcmp( p, "psk_opaque" ) == 0 )
|
||||
opt.psk_opaque = atoi( q );
|
||||
#endif
|
||||
else if( strcmp( p, "psk_identity" ) == 0 )
|
||||
opt.psk_identity = q;
|
||||
else if( strcmp( p, "ecjpake_pw" ) == 0 )
|
||||
|
|
@ -1030,57 +1061,6 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_debug_set_threshold( opt.debug_level );
|
||||
#endif
|
||||
|
||||
if( opt.force_ciphersuite[0] > 0 )
|
||||
{
|
||||
const mbedtls_ssl_ciphersuite_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_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_printf( "forced ciphersuite not allowed with this protocol version\n" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
/* 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 = ciphersuite_info->max_minor_ver;
|
||||
}
|
||||
if( opt.min_version < ciphersuite_info->min_minor_ver )
|
||||
{
|
||||
opt.min_version = ciphersuite_info->min_minor_ver;
|
||||
/* DTLS starts with TLS 1.1 */
|
||||
if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
||||
opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
|
||||
opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
|
||||
}
|
||||
|
||||
/* Enable RC4 if needed and not explicitly disabled */
|
||||
if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
|
||||
{
|
||||
if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
|
||||
{
|
||||
mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
/*
|
||||
* Unhexify the pre-shared key if any is given
|
||||
|
|
@ -1131,6 +1111,101 @@ int main( int argc, char *argv[] )
|
|||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
|
||||
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_opaque != 0 )
|
||||
{
|
||||
if( opt.psk == NULL )
|
||||
{
|
||||
mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( opt.force_ciphersuite[0] <= 0 )
|
||||
{
|
||||
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" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if( opt.force_ciphersuite[0] > 0 )
|
||||
{
|
||||
const mbedtls_ssl_ciphersuite_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_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_printf( "forced ciphersuite not allowed with this protocol version\n" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
/* 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 = ciphersuite_info->max_minor_ver;
|
||||
}
|
||||
if( opt.min_version < ciphersuite_info->min_minor_ver )
|
||||
{
|
||||
opt.min_version = ciphersuite_info->min_minor_ver;
|
||||
/* DTLS starts with TLS 1.1 */
|
||||
if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
||||
opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
|
||||
opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
|
||||
}
|
||||
|
||||
/* Enable RC4 if needed and not explicitly disabled */
|
||||
if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
|
||||
{
|
||||
if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
|
||||
{
|
||||
mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_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 ||
|
||||
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" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
/* Determine KDF algorithm the opaque PSK will be used in. */
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
|
||||
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
|
||||
else
|
||||
#endif /* MBEDTLS_SHA512_C */
|
||||
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( opt.curves != NULL )
|
||||
{
|
||||
|
|
@ -1515,6 +1590,45 @@ int main( int argc, char *argv[] )
|
|||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_opaque != 0 )
|
||||
{
|
||||
/* The algorithm has already been determined earlier. */
|
||||
status = mbedtls_psa_get_free_key_slot( &slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
psa_key_policy_init( &policy );
|
||||
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
|
||||
|
||||
status = psa_set_key_policy( slot, &policy );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
|
||||
(const unsigned char *) opt.psk_identity,
|
||||
strlen( opt.psk_identity ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
|
||||
(const unsigned char *) opt.psk_identity,
|
||||
strlen( opt.psk_identity ) ) ) != 0 )
|
||||
|
|
@ -1523,7 +1637,7 @@ int main( int argc, char *argv[] )
|
|||
ret );
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
|
||||
|
||||
if( opt.min_version != DFL_MIN_VERSION )
|
||||
mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
|
||||
|
|
@ -2157,6 +2271,26 @@ exit:
|
|||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
|
||||
defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_opaque != 0 )
|
||||
{
|
||||
/* This is ok even if the slot hasn't been
|
||||
* initialized (we might have jumed here
|
||||
* immediately because of bad cmd line params,
|
||||
* for example). */
|
||||
status = psa_destroy_key( slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
mbedtls_printf( "Failed to destroy key slot %u - error was %d",
|
||||
(unsigned) slot, (int) status );
|
||||
if( ret == 0 )
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
|
||||
MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ int main( void )
|
|||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
|
@ -123,6 +124,8 @@ int main( void )
|
|||
#define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
|
||||
#define DFL_ASYNC_PRIVATE_ERROR ( 0 )
|
||||
#define DFL_PSK ""
|
||||
#define DFL_PSK_OPAQUE 0
|
||||
#define DFL_PSK_LIST_OPAQUE 0
|
||||
#define DFL_PSK_IDENTITY "Client_identity"
|
||||
#define DFL_ECJPAKE_PW NULL
|
||||
#define DFL_PSK_LIST NULL
|
||||
|
|
@ -224,9 +227,36 @@ int main( void )
|
|||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
#define USAGE_PSK \
|
||||
#define USAGE_PSK_RAW \
|
||||
" psk=%%s default: \"\" (in hex, without 0x)\n" \
|
||||
" psk_identity=%%s default: \"Client_identity\"\n"
|
||||
" psk_identity=%%s default: \"Client_identity\"\n" \
|
||||
" psk_list=%%s default: \"\"\n" \
|
||||
" A list of (PSK identity, PSK value) pairs in (hex format, without 0x)\n" \
|
||||
" id1,psk1[,id2,psk2[,...]]\n"
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#define USAGE_PSK_SLOT \
|
||||
" psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
|
||||
" Enable this to store the PSK configured through command line\n" \
|
||||
" parameter `psk` in a PSA-based key slot.\n" \
|
||||
" Note: Currently only supported in conjunction with\n" \
|
||||
" the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
|
||||
" to force a particular PSK-only ciphersuite.\n" \
|
||||
" Note: This is to test integration of PSA-based opaque PSKs with\n" \
|
||||
" Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
|
||||
" with prepopulated key slots instead of importing raw key material.\n" \
|
||||
" psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
|
||||
" Enable this to store the list of dynamically chosen PSKs configured\n" \
|
||||
" through the command line parameter `psk_list` in PSA-based key slots.\n" \
|
||||
" Note: Currently only supported in conjunction with\n" \
|
||||
" the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
|
||||
" to force a particular PSK-only ciphersuite.\n" \
|
||||
" Note: This is to test integration of PSA-based opaque PSKs with\n" \
|
||||
" Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
|
||||
" with prepopulated key slots instead of importing raw key material.\n"
|
||||
#else
|
||||
#define USAGE_PSK_SLOT ""
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
|
||||
#else
|
||||
#define USAGE_PSK ""
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
|
||||
|
|
@ -453,6 +483,10 @@ struct options
|
|||
int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
|
||||
int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
|
||||
int async_private_error; /* inject error in async private callback */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
int psk_opaque;
|
||||
int psk_list_opaque;
|
||||
#endif
|
||||
const char *psk; /* the pre-shared key */
|
||||
const char *psk_identity; /* the pre-shared key identity */
|
||||
char *psk_list; /* list of PSK id/key pairs for callback */
|
||||
|
|
@ -771,22 +805,39 @@ struct _psk_entry
|
|||
const char *name;
|
||||
size_t key_len;
|
||||
unsigned char key[MBEDTLS_PSK_MAX_LEN];
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_slot_t slot;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
psk_entry *next;
|
||||
};
|
||||
|
||||
/*
|
||||
* Free a list of psk_entry's
|
||||
*/
|
||||
void psk_free( psk_entry *head )
|
||||
int psk_free( psk_entry *head )
|
||||
{
|
||||
psk_entry *next;
|
||||
|
||||
while( head != NULL )
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status;
|
||||
psa_key_slot_t const slot = head->slot;
|
||||
|
||||
if( slot != 0 )
|
||||
{
|
||||
status = psa_destroy_key( slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
return( status );
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
next = head->next;
|
||||
mbedtls_free( head );
|
||||
head = next;
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -844,6 +895,11 @@ int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
|
|||
if( name_len == strlen( cur->name ) &&
|
||||
memcmp( name, cur->name, name_len ) == 0 )
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( cur->slot != 0 )
|
||||
return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
|
||||
else
|
||||
#endif
|
||||
return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
|
||||
}
|
||||
|
||||
|
|
@ -1174,12 +1230,46 @@ int idle( mbedtls_net_context *fd,
|
|||
return( 0 );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
static psa_status_t psa_setup_psk_key_slot( psa_key_slot_t slot,
|
||||
psa_algorithm_t alg,
|
||||
unsigned char *psk,
|
||||
size_t psk_len )
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_key_policy_t policy;
|
||||
|
||||
psa_key_policy_init( &policy );
|
||||
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
|
||||
|
||||
status = psa_set_key_policy( slot, &policy );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
fprintf( stderr, "POLICY\n" );
|
||||
return( status );
|
||||
}
|
||||
|
||||
status = psa_import_key( slot, PSA_KEY_TYPE_DERIVE, psk, psk_len );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
fprintf( stderr, "IMPORT\n" );
|
||||
return( status );
|
||||
}
|
||||
|
||||
return( PSA_SUCCESS );
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0, len, written, frags, exchanges_left;
|
||||
int version_suites[4][2];
|
||||
unsigned char* buf = 0;
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm_t alg = 0;
|
||||
psa_key_slot_t psk_slot = 0;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
unsigned char psk[MBEDTLS_PSK_MAX_LEN];
|
||||
size_t psk_len = 0;
|
||||
psk_entry *psk_info = NULL;
|
||||
|
|
@ -1342,6 +1432,10 @@ int main( int argc, char *argv[] )
|
|||
opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
|
||||
opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
|
||||
opt.psk = DFL_PSK;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
opt.psk_opaque = DFL_PSK_OPAQUE;
|
||||
opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
|
||||
#endif
|
||||
opt.psk_identity = DFL_PSK_IDENTITY;
|
||||
opt.psk_list = DFL_PSK_LIST;
|
||||
opt.ecjpake_pw = DFL_ECJPAKE_PW;
|
||||
|
|
@ -1470,6 +1564,12 @@ int main( int argc, char *argv[] )
|
|||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
else if( strcmp( p, "psk" ) == 0 )
|
||||
opt.psk = q;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
else if( strcmp( p, "psk_opaque" ) == 0 )
|
||||
opt.psk_opaque = atoi( q );
|
||||
else if( strcmp( p, "psk_list_opaque" ) == 0 )
|
||||
opt.psk_list_opaque = atoi( q );
|
||||
#endif
|
||||
else if( strcmp( p, "psk_identity" ) == 0 )
|
||||
opt.psk_identity = q;
|
||||
else if( strcmp( p, "psk_list" ) == 0 )
|
||||
|
|
@ -1779,6 +1879,42 @@ int main( int argc, char *argv[] )
|
|||
goto exit;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_opaque != 0 )
|
||||
{
|
||||
if( strlen( opt.psk ) == 0 )
|
||||
{
|
||||
mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( opt.force_ciphersuite[0] <= 0 )
|
||||
{
|
||||
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" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
}
|
||||
|
||||
if( opt.psk_list_opaque != 0 )
|
||||
{
|
||||
if( opt.psk_list == NULL )
|
||||
{
|
||||
mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( opt.force_ciphersuite[0] <= 0 )
|
||||
{
|
||||
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" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if( opt.force_ciphersuite[0] > 0 )
|
||||
{
|
||||
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
||||
|
|
@ -1828,6 +1964,30 @@ int main( int argc, char *argv[] )
|
|||
|
||||
opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
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 ||
|
||||
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" );
|
||||
ret = 2;
|
||||
goto usage;
|
||||
}
|
||||
|
||||
/* Determine KDF algorithm the opaque PSK will be used in. */
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
|
||||
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
|
||||
else
|
||||
#endif /* MBEDTLS_SHA512_C */
|
||||
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
|
||||
if( opt.version_suites != NULL )
|
||||
|
|
@ -2501,12 +2661,42 @@ int main( int argc, char *argv[] )
|
|||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
|
||||
if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
|
||||
{
|
||||
ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
|
||||
(const unsigned char *) opt.psk_identity,
|
||||
strlen( opt.psk_identity ) );
|
||||
if( ret != 0 )
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_opaque != 0 )
|
||||
{
|
||||
status = mbedtls_psa_get_free_key_slot( &psk_slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
fprintf( stderr, "ALLOC FAIL\n" );
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* The algorithm has already been determined earlier. */
|
||||
status = psa_setup_psk_key_slot( psk_slot, alg, psk, psk_len );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
fprintf( stderr, "SETUP FAIL\n" );
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
|
||||
(const unsigned char *) opt.psk_identity,
|
||||
strlen( opt.psk_identity ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
|
||||
(const unsigned char *) opt.psk_identity,
|
||||
strlen( opt.psk_identity ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", - ret );
|
||||
goto exit;
|
||||
|
|
@ -2514,7 +2704,34 @@ int main( int argc, char *argv[] )
|
|||
}
|
||||
|
||||
if( opt.psk_list != NULL )
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_list_opaque != 0 )
|
||||
{
|
||||
psk_entry *cur_psk;
|
||||
for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
|
||||
{
|
||||
status = mbedtls_psa_get_free_key_slot( &cur_psk->slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
status = psa_setup_psk_key_slot( cur_psk->slot, alg,
|
||||
cur_psk->key,
|
||||
cur_psk->key_len );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_DHM_C)
|
||||
|
|
@ -3143,12 +3360,31 @@ exit:
|
|||
sni_free( sni_info );
|
||||
#endif
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
|
||||
psk_free( psk_info );
|
||||
if( ( ret = psk_free( psk_info ) ) != 0 )
|
||||
mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
|
||||
#endif
|
||||
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
|
||||
mbedtls_dhm_free( &dhm );
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
|
||||
defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( opt.psk_opaque != 0 )
|
||||
{
|
||||
/* This is ok even if the slot hasn't been
|
||||
* initialized (we might have jumed here
|
||||
* immediately because of bad cmd line params,
|
||||
* for example). */
|
||||
status = psa_destroy_key( psk_slot );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
mbedtls_printf( "Failed to destroy key slot %u - error was %d",
|
||||
(unsigned) psk_slot, (int) status );
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED &&
|
||||
MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
mbedtls_ssl_free( &ssl );
|
||||
mbedtls_ssl_config_free( &conf );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue