2013-09-15 13:01:22 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2022-08-10 15:10:15 +02:00
|
|
|
#include "legacy_or_psa.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
|
2013-09-15 13:01:22 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 13:01:22 +02:00
|
|
|
int res;
|
|
|
|
char *pwd = password;
|
|
|
|
|
2022-09-01 15:23:09 +02:00
|
|
|
PSA_INIT_IF_NO_MD();
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( strcmp( pwd, "NULL" ) == 0 )
|
|
|
|
pwd = NULL;
|
|
|
|
|
2021-06-15 11:29:26 +02:00
|
|
|
res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
|
|
|
|
rsa = mbedtls_pk_rsa( ctx );
|
|
|
|
TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &ctx );
|
2022-09-01 15:23:09 +02:00
|
|
|
PSA_DONE_IF_NO_MD();
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void pk_parse_public_keyfile_rsa( char * key_file, int result )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 13:01:22 +02:00
|
|
|
int res;
|
|
|
|
|
2022-09-01 15:23:09 +02:00
|
|
|
PSA_INIT_IF_NO_MD();
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
|
|
|
|
rsa = mbedtls_pk_rsa( ctx );
|
|
|
|
TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &ctx );
|
2022-09-01 15:23:09 +02:00
|
|
|
PSA_DONE_IF_NO_MD();
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
|
2017-05-30 15:23:15 +02:00
|
|
|
void pk_parse_public_keyfile_ec( char * key_file, int result )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 13:01:22 +02:00
|
|
|
int res;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair *eckey;
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
|
|
|
|
eckey = mbedtls_pk_ec( ctx );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &ctx );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
|
2017-05-30 15:23:15 +02:00
|
|
|
void pk_parse_keyfile_ec( char * key_file, char * password, int result )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 13:01:22 +02:00
|
|
|
int res;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2021-06-15 11:29:26 +02:00
|
|
|
res = mbedtls_pk_parse_keyfile( &ctx, key_file, password,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair *eckey;
|
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
|
|
|
|
eckey = mbedtls_pk_ec( ctx );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &ctx );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-02-10 10:50:16 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void pk_parse_key( data_t * buf, int result )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_init( &pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2021-06-15 11:29:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL ) == result );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|