blob: a4d9466c4e3bdcdd5c2d1b25d1d7da84b7c6c699 [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 );
96 TEST_ASSERT( uECC_valid_public_key( uecckey->public_key,
97 uECC_secp256r1() ) == 0 );
98#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +020099 }
100
Paul Bakkerbd51b262014-07-10 15:26:12 +0200101exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_pk_free( &ctx );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100103#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200104}
105/* END_CASE */
106
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100107/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100108void pk_parse_keyfile_ec( char * key_file, char * password, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200109{
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100110#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_TINYCRYPT)
111 ((void) key_file);
112 ((void) password);
113 ((void) result);
114#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116 int res;
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_pk_init( &ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121
122 TEST_ASSERT( res == result );
123
124 if( res == 0 )
125 {
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100126#if !defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_ecp_keypair *eckey;
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100128#else
129 mbedtls_uecc_keypair *uecckey;
130 unsigned char tmp_pubkey[ 2 * NUM_ECC_BYTES ];
131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100133
134#if !defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 eckey = mbedtls_pk_ec( ctx );
136 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100137#else
138 uecckey = mbedtls_pk_uecc( ctx );
139 TEST_ASSERT( uECC_valid_public_key( uecckey->public_key,
140 uECC_secp256r1() ) == 0 );
141 TEST_ASSERT( uECC_compute_public_key( uecckey->private_key,
142 tmp_pubkey,
143 uECC_secp256r1() ) != 0 );
144 TEST_ASSERT( memcmp( tmp_pubkey, uecckey->public_key,
145 sizeof( tmp_pubkey ) ) == 0 );
146#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147 }
148
Paul Bakkerbd51b262014-07-10 15:26:12 +0200149exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_pk_free( &ctx );
Hanno Beckerd84dbe52019-08-21 14:31:52 +0100151#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152}
153/* END_CASE */
154
Azim Khanf1aaec92017-05-30 14:23:15 +0100155/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
Azim Khan5fcca462018-06-29 11:05:32 +0100156void pk_parse_key( data_t * buf, char * result_str, int result )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159 unsigned char output[2000];
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160 ((void) result_str);
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_pk_init( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164 memset( output, 0, 2000 );
165
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166
Azim Khand30ca132017-06-09 04:32:58 +0100167 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf->x, buf->len, NULL, 0 ) == ( result ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 if( ( result ) == 0 )
169 {
170 TEST_ASSERT( 1 );
171 }
172
Paul Bakkerbd51b262014-07-10 15:26:12 +0200173exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_pk_free( &pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175}
176/* END_CASE */