blob: 8ca3aca79e2875a89f173055487861b3824dba4b [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/pk.h"
3#include "mbedtls/pem.h"
4#include "mbedtls/oid.h"
Przemek Stekielbc0509a2022-08-10 15:10:15 +02005#include "legacy_or_psa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02006/* END_HEADER */
7
8/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020010 * END_DEPENDENCIES
11 */
12
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010014void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020017 int res;
18 char *pwd = password;
19
Andrzej Kurek7a320722022-09-01 09:23:09 -040020 PSA_INIT_IF_NO_MD();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020022
23 if( strcmp( pwd, "NULL" ) == 0 )
24 pwd = NULL;
25
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020026 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd,
27 mbedtls_test_rnd_std_rand, NULL );
Paul Bakker1a7550a2013-09-15 13:01:22 +020028
29 TEST_ASSERT( res == result );
30
31 if( res == 0 )
32 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_rsa_context *rsa;
34 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
35 rsa = mbedtls_pk_rsa( ctx );
36 TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020037 }
38
Paul Bakkerbd51b262014-07-10 15:26:12 +020039exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_pk_free( &ctx );
Andrzej Kurek7a320722022-09-01 09:23:09 -040041 PSA_DONE_IF_NO_MD();
Paul Bakker1a7550a2013-09-15 13:01:22 +020042}
43/* END_CASE */
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010046void pk_parse_public_keyfile_rsa( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020049 int res;
50
Andrzej Kurek7a320722022-09-01 09:23:09 -040051 PSA_INIT_IF_NO_MD();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020055
56 TEST_ASSERT( res == result );
57
58 if( res == 0 )
59 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_rsa_context *rsa;
61 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
62 rsa = mbedtls_pk_rsa( ctx );
63 TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020064 }
65
Paul Bakkerbd51b262014-07-10 15:26:12 +020066exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_pk_free( &ctx );
Andrzej Kurek7a320722022-09-01 09:23:09 -040068 PSA_DONE_IF_NO_MD();
Paul Bakker1a7550a2013-09-15 13:01:22 +020069}
70/* END_CASE */
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010073void pk_parse_public_keyfile_ec( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020076 int res;
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020081
82 TEST_ASSERT( res == result );
83
84 if( res == 0 )
85 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_ecp_keypair *eckey;
87 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
88 eckey = mbedtls_pk_ec( ctx );
89 TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 }
91
Paul Bakkerbd51b262014-07-10 15:26:12 +020092exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020094}
95/* END_CASE */
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Azim Khanf1aaec92017-05-30 14:23:15 +010098void pk_parse_keyfile_ec( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200101 int res;
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200104
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200105 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password,
106 mbedtls_test_rnd_std_rand, NULL );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107
108 TEST_ASSERT( res == result );
109
110 if( res == 0 )
111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_ecp_keypair *eckey;
113 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
114 eckey = mbedtls_pk_ec( ctx );
115 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116 }
117
Paul Bakkerbd51b262014-07-10 15:26:12 +0200118exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120}
121/* END_CASE */
122
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100123/* BEGIN_CASE */
124void pk_parse_key( data_t * buf, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_pk_init( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200130 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0,
131 mbedtls_test_rnd_std_rand, NULL ) == result );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Paul Bakkerbd51b262014-07-10 15:26:12 +0200133exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_pk_free( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135}
136/* END_CASE */