2 #include "mbedtls/pk.h"
3 #include "mbedtls/pem.h"
4 #include "mbedtls/oid.h"
8 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
12 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
13 void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
15 mbedtls_pk_context ctx;
19 mbedtls_pk_init( &ctx );
21 if( strcmp( pwd, "NULL" ) == 0 )
24 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd );
26 TEST_ASSERT( res == result );
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 );
37 mbedtls_pk_free( &ctx );
41 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
42 void pk_parse_public_keyfile_rsa( char * key_file, int result )
44 mbedtls_pk_context ctx;
47 mbedtls_pk_init( &ctx );
49 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
51 TEST_ASSERT( res == result );
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 );
62 mbedtls_pk_free( &ctx );
66 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
67 void pk_parse_public_keyfile_ec( char * key_file, int result )
69 mbedtls_pk_context ctx;
72 mbedtls_pk_init( &ctx );
74 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
76 TEST_ASSERT( res == result );
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 );
87 mbedtls_pk_free( &ctx );
91 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
92 void pk_parse_keyfile_ec( char * key_file, char * password, int result )
94 mbedtls_pk_context ctx;
97 mbedtls_pk_init( &ctx );
99 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password );
101 TEST_ASSERT( res == result );
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 );
112 mbedtls_pk_free( &ctx );
116 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
117 void pk_parse_key( data_t * buf, char * result_str, int result )
119 mbedtls_pk_context pk;
120 unsigned char output[2000];
123 mbedtls_pk_init( &pk );
125 memset( output, 0, 2000 );
128 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0 ) == ( result ) );
129 if( ( result ) == 0 )
135 mbedtls_pk_free( &pk );