2013-09-15 11:01:22 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2013-09-15 11:01:22 +00:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
|
2013-09-15 11:01:22 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
|
2017-05-30 13:23:15 +00:00
|
|
|
void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
|
2013-09-15 11:01:22 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 11:01:22 +00:00
|
|
|
int res;
|
|
|
|
char *pwd = password;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
|
|
|
if( strcmp( pwd, "NULL" ) == 0 )
|
|
|
|
pwd = NULL;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00: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 11:01:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &ctx );
|
2013-09-15 11:01:22 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
|
2017-05-30 13:23:15 +00:00
|
|
|
void pk_parse_public_keyfile_rsa( char * key_file, int result )
|
2013-09-15 11:01:22 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 11:01:22 +00:00
|
|
|
int res;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00: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 11:01:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &ctx );
|
2013-09-15 11:01:22 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-08-21 13:31:52 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2017-05-30 13:23:15 +00:00
|
|
|
void pk_parse_public_keyfile_ec( char * key_file, int result )
|
2013-09-15 11:01:22 +00:00
|
|
|
{
|
2019-08-21 13:31:52 +00:00
|
|
|
#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_TINYCRYPT)
|
|
|
|
((void) key_file);
|
|
|
|
((void) result);
|
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 11:01:22 +00:00
|
|
|
int res;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2019-08-21 13:31:52 +00:00
|
|
|
#if !defined(MBEDTLS_USE_TINYCRYPT)
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_keypair *eckey;
|
2019-08-21 13:31:52 +00:00
|
|
|
#else
|
|
|
|
mbedtls_uecc_keypair *uecckey;
|
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
|
2019-08-21 13:31:52 +00:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_USE_TINYCRYPT)
|
2015-04-08 10:49:31 +00:00
|
|
|
eckey = mbedtls_pk_ec( ctx );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
|
2019-08-21 13:31:52 +00:00
|
|
|
#else
|
|
|
|
uecckey = mbedtls_pk_uecc( ctx );
|
2019-11-21 11:00:43 +00:00
|
|
|
TEST_ASSERT( uECC_valid_public_key( uecckey->public_key ) == 0 );
|
2019-08-21 13:31:52 +00:00
|
|
|
#endif /* MBEDTLS_USE_TINYCRYPT */
|
2013-09-15 11:01:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &ctx );
|
2019-08-21 13:31:52 +00:00
|
|
|
#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */
|
2013-09-15 11:01:22 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-08-21 13:31:52 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2017-05-30 13:23:15 +00:00
|
|
|
void pk_parse_keyfile_ec( char * key_file, char * password, int result )
|
2013-09-15 11:01:22 +00:00
|
|
|
{
|
2019-08-21 13:31:52 +00:00
|
|
|
#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_TINYCRYPT)
|
|
|
|
((void) key_file);
|
|
|
|
((void) password);
|
|
|
|
((void) result);
|
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_context ctx;
|
2013-09-15 11:01:22 +00:00
|
|
|
int res;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_init( &ctx );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
res = mbedtls_pk_parse_keyfile( &ctx, key_file, password );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( res == result );
|
|
|
|
|
|
|
|
if( res == 0 )
|
|
|
|
{
|
2019-08-21 13:31:52 +00:00
|
|
|
#if !defined(MBEDTLS_USE_TINYCRYPT)
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_ecp_keypair *eckey;
|
2019-08-21 13:31:52 +00:00
|
|
|
#else
|
|
|
|
mbedtls_uecc_keypair *uecckey;
|
|
|
|
unsigned char tmp_pubkey[ 2 * NUM_ECC_BYTES ];
|
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
|
2019-08-21 13:31:52 +00:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_USE_TINYCRYPT)
|
2015-04-08 10:49:31 +00:00
|
|
|
eckey = mbedtls_pk_ec( ctx );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
|
2019-08-21 13:31:52 +00:00
|
|
|
#else
|
|
|
|
uecckey = mbedtls_pk_uecc( ctx );
|
2019-11-21 11:00:43 +00:00
|
|
|
TEST_ASSERT( uECC_valid_public_key( uecckey->public_key ) == 0 );
|
2019-08-21 13:31:52 +00:00
|
|
|
TEST_ASSERT( uECC_compute_public_key( uecckey->private_key,
|
2019-11-25 12:06:05 +00:00
|
|
|
tmp_pubkey ) == UECC_SUCCESS );
|
2019-08-21 13:31:52 +00:00
|
|
|
TEST_ASSERT( memcmp( tmp_pubkey, uecckey->public_key,
|
|
|
|
sizeof( tmp_pubkey ) ) == 0 );
|
|
|
|
#endif /* MBEDTLS_USE_TINYCRYPT */
|
2013-09-15 11:01:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &ctx );
|
2019-08-21 13:31:52 +00:00
|
|
|
#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */
|
2013-09-15 11:01:22 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-05-30 13:23:15 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
|
2018-06-29 10:05:32 +00:00
|
|
|
void pk_parse_key( data_t * buf, char * result_str, int result )
|
2013-09-15 11:01:22 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_context pk;
|
2013-09-15 11:01:22 +00:00
|
|
|
unsigned char output[2000];
|
|
|
|
((void) result_str);
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_init( &pk );
|
2013-09-15 11:01:22 +00:00
|
|
|
|
|
|
|
memset( output, 0, 2000 );
|
|
|
|
|
|
|
|
|
2017-06-09 03:32:58 +00:00
|
|
|
TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0 ) == ( result ) );
|
2013-09-15 11:01:22 +00:00
|
|
|
if( ( result ) == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( 1 );
|
|
|
|
}
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_free( &pk );
|
2013-09-15 11:01:22 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|