blob: 54fa7af9027d9253111ce8d6637a54676867d449 [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"
Paul Bakker1a7550a2013-09-15 13:01:22 +02005/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
Paul Bakker1a7550a2013-09-15 13:01:22 +02009 * END_DEPENDENCIES
10 */
11
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010013void pk_parse_keyfile_rsa( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020016 int res;
17 char *pwd = password;
18
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020020
21 if( strcmp( pwd, "NULL" ) == 0 )
22 pwd = NULL;
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd );
Paul Bakker1a7550a2013-09-15 13:01:22 +020025
26 TEST_ASSERT( res == result );
27
28 if( res == 0 )
29 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 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 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020034 }
35
Paul Bakkerbd51b262014-07-10 15:26:12 +020036exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020038}
39/* END_CASE */
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010042void pk_parse_public_keyfile_rsa( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020045 int res;
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020050
51 TEST_ASSERT( res == result );
52
53 if( res == 0 )
54 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 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 );
Paul Bakker1a7550a2013-09-15 13:01:22 +020059 }
60
Paul Bakkerbd51b262014-07-10 15:26:12 +020061exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_pk_free( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020063}
64/* END_CASE */
65
Hanno Beckerd84dbe52019-08-21 14:31:52 +010066/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +010067void pk_parse_public_keyfile_ec( char * key_file, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +020068{
Hanno Beckerd84dbe52019-08-21 14:31:52 +010069#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_TINYCRYPT)
70 ((void) key_file);
71 ((void) result);
72#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020074 int res;
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
Paul Bakker1a7550a2013-09-15 13:01:22 +020079
80 TEST_ASSERT( res == result );
81
82 if( res == 0 )
83 {
Hanno Beckerd84dbe52019-08-21 14:31:52 +010084#if !defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_ecp_keypair *eckey;
Hanno Beckerd84dbe52019-08-21 14:31:52 +010086#else
87 mbedtls_uecc_keypair *uecckey;
88#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
Hanno Beckerd84dbe52019-08-21 14:31:52 +010090
91#if !defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 eckey = mbedtls_pk_ec( ctx );
93 TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
Hanno Beckerd84dbe52019-08-21 14:31:52 +010094#else
95 uecckey = mbedtls_pk_uecc( ctx );
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +010096 TEST_ASSERT( uECC_valid_public_key( uecckey->public_key ) == 0 );
Hanno Beckerd84dbe52019-08-21 14:31:52 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 }
99
Paul Bakkerbd51b262014-07-10 15:26:12 +0200100exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_pk_free( &ctx );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100102#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103}
104/* END_CASE */
105
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100106/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100107void pk_parse_keyfile_ec( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108{
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100109#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_TINYCRYPT)
110 ((void) key_file);
111 ((void) password);
112 ((void) result);
113#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200115 int res;
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120
121 TEST_ASSERT( res == result );
122
123 if( res == 0 )
124 {
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100125#if !defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_ecp_keypair *eckey;
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100127#else
128 mbedtls_uecc_keypair *uecckey;
129 unsigned char tmp_pubkey[ 2 * NUM_ECC_BYTES ];
130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100132
133#if !defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 eckey = mbedtls_pk_ec( ctx );
135 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100136#else
137 uecckey = mbedtls_pk_uecc( ctx );
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100138 TEST_ASSERT( uECC_valid_public_key( uecckey->public_key ) == 0 );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100139 TEST_ASSERT( uECC_compute_public_key( uecckey->private_key,
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100140 tmp_pubkey ) == UECC_SUCCESS );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100141 TEST_ASSERT( memcmp( tmp_pubkey, uecckey->public_key,
142 sizeof( tmp_pubkey ) ) == 0 );
143#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144 }
145
Paul Bakkerbd51b262014-07-10 15:26:12 +0200146exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_pk_free( &ctx );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100148#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149}
150/* END_CASE */
151
Azim Khanf1aaec92017-05-30 14:23:15 +0100152/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100153void pk_parse_key( data_t * buf, char * result_str, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156 unsigned char output[2000];
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157 ((void) result_str);
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_pk_init( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161 memset( output, 0, 2000 );
162
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163
Azim Khand30ca132017-06-09 04:32:58 +0100164 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0 ) == ( result ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165 if( ( result ) == 0 )
166 {
167 TEST_ASSERT( 1 );
168 }
169
Paul Bakkerbd51b262014-07-10 15:26:12 +0200170exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_pk_free( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172}
173/* END_CASE */