• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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            mbedtls_test_rnd_std_rand, NULL );
26
27    TEST_ASSERT( res == result );
28
29    if( res == 0 )
30    {
31        mbedtls_rsa_context *rsa;
32        TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
33        rsa = mbedtls_pk_rsa( ctx );
34        TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
35    }
36
37exit:
38    mbedtls_pk_free( &ctx );
39}
40/* END_CASE */
41
42/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
43void pk_parse_public_keyfile_rsa( char * key_file, int result )
44{
45    mbedtls_pk_context ctx;
46    int res;
47
48    mbedtls_pk_init( &ctx );
49
50    res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
51
52    TEST_ASSERT( res == result );
53
54    if( res == 0 )
55    {
56        mbedtls_rsa_context *rsa;
57        TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
58        rsa = mbedtls_pk_rsa( ctx );
59        TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
60    }
61
62exit:
63    mbedtls_pk_free( &ctx );
64}
65/* END_CASE */
66
67/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
68void pk_parse_public_keyfile_ec( char * key_file, int result )
69{
70    mbedtls_pk_context ctx;
71    int res;
72
73    mbedtls_pk_init( &ctx );
74
75    res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
76
77    TEST_ASSERT( res == result );
78
79    if( res == 0 )
80    {
81        mbedtls_ecp_keypair *eckey;
82        TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
83        eckey = mbedtls_pk_ec( ctx );
84        TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
85    }
86
87exit:
88    mbedtls_pk_free( &ctx );
89}
90/* END_CASE */
91
92/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
93void pk_parse_keyfile_ec( char * key_file, char * password, int result )
94{
95    mbedtls_pk_context ctx;
96    int res;
97
98    mbedtls_pk_init( &ctx );
99
100    res = mbedtls_pk_parse_keyfile( &ctx, key_file, password,
101            mbedtls_test_rnd_std_rand, NULL );
102
103    TEST_ASSERT( res == result );
104
105    if( res == 0 )
106    {
107        mbedtls_ecp_keypair *eckey;
108        TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
109        eckey = mbedtls_pk_ec( ctx );
110        TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
111    }
112
113exit:
114    mbedtls_pk_free( &ctx );
115}
116/* END_CASE */
117
118/* BEGIN_CASE */
119void pk_parse_key( data_t * buf, int result )
120{
121    mbedtls_pk_context pk;
122
123    mbedtls_pk_init( &pk );
124
125    TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0,
126                        mbedtls_test_rnd_std_rand, NULL ) == result );
127
128exit:
129    mbedtls_pk_free( &pk );
130}
131/* END_CASE */
132