1/* BEGIN_HEADER */ 2#include "mbedtls/pk.h" 3#include "mbedtls/pem.h" 4#include "mbedtls/oid.h" 5/* END_HEADER */ 6 7/* BEGIN_DEPENDENCIES 8 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C 9 * END_DEPENDENCIES 10 */ 11 12/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ 13void pk_parse_keyfile_rsa( char *key_file, char *password, int result ) 14{ 15 mbedtls_pk_context ctx; 16 int res; 17 char *pwd = password; 18 19 mbedtls_pk_init( &ctx ); 20 21 if( strcmp( pwd, "NULL" ) == 0 ) 22 pwd = NULL; 23 24 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd ); 25 26 TEST_ASSERT( res == result ); 27 28 if( res == 0 ) 29 { 30 mbedtls_rsa_context *rsa; 31 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) ); 32 rsa = mbedtls_pk_rsa( ctx ); 33 TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 ); 34 } 35 36exit: 37 mbedtls_pk_free( &ctx ); 38} 39/* END_CASE */ 40 41/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ 42void pk_parse_public_keyfile_rsa( char *key_file, int result ) 43{ 44 mbedtls_pk_context ctx; 45 int res; 46 47 mbedtls_pk_init( &ctx ); 48 49 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file ); 50 51 TEST_ASSERT( res == result ); 52 53 if( res == 0 ) 54 { 55 mbedtls_rsa_context *rsa; 56 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) ); 57 rsa = mbedtls_pk_rsa( ctx ); 58 TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 ); 59 } 60 61exit: 62 mbedtls_pk_free( &ctx ); 63} 64/* END_CASE */ 65 66/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */ 67void pk_parse_public_keyfile_ec( char *key_file, int result ) 68{ 69 mbedtls_pk_context ctx; 70 int res; 71 72 mbedtls_pk_init( &ctx ); 73 74 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file ); 75 76 TEST_ASSERT( res == result ); 77 78 if( res == 0 ) 79 { 80 mbedtls_ecp_keypair *eckey; 81 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) ); 82 eckey = mbedtls_pk_ec( ctx ); 83 TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 ); 84 } 85 86exit: 87 mbedtls_pk_free( &ctx ); 88} 89/* END_CASE */ 90 91/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */ 92void pk_parse_keyfile_ec( char *key_file, char *password, int result ) 93{ 94 mbedtls_pk_context ctx; 95 int res; 96 97 mbedtls_pk_init( &ctx ); 98 99 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password ); 100 101 TEST_ASSERT( res == result ); 102 103 if( res == 0 ) 104 { 105 mbedtls_ecp_keypair *eckey; 106 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) ); 107 eckey = mbedtls_pk_ec( ctx ); 108 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 ); 109 } 110 111exit: 112 mbedtls_pk_free( &ctx ); 113} 114/* END_CASE */ 115 116/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ 117void pk_parse_key_rsa( char *key_data, char *result_str, int result ) 118{ 119 mbedtls_pk_context pk; 120 unsigned char buf[2000]; 121 unsigned char output[2000]; 122 int data_len; 123 ((void) result_str); 124 125 mbedtls_pk_init( &pk ); 126 127 memset( buf, 0, 2000 ); 128 memset( output, 0, 2000 ); 129 130 data_len = unhexify( buf, key_data ); 131 132 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf, data_len, NULL, 0 ) == ( result ) ); 133 if( ( result ) == 0 ) 134 { 135 TEST_ASSERT( 1 ); 136 } 137 138exit: 139 mbedtls_pk_free( &pk ); 140} 141/* END_CASE */ 142