blob: 64a3175bc2de38bedcc765c928ee154f9a36bb16 [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"
Valerio Settifa49a8e2023-01-26 10:00:55 +01005#include "mbedtls/ecp.h"
Valerio Settiaed87992023-07-04 19:58:43 +02006#include "mbedtls/psa_util.h"
Valerio Setti77a75682023-05-15 11:18:46 +02007#include "pk_internal.h"
Waleed Elmelegy38202a22023-09-21 15:21:10 +01008
9#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
10#define HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der
11#endif
12
Paul Bakker1a7550a2013-09-15 13:01:22 +020013/* END_HEADER */
14
15/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +020016 * depends_on:MBEDTLS_PK_PARSE_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020017 * END_DEPENDENCIES
18 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010021void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020024 int res;
25 char *pwd = password;
26
Gilles Peskine449bd832023-01-11 14:50:10 +010027 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020028 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Gilles Peskine449bd832023-01-11 14:50:10 +010030 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +020031 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010032 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020033
Gilles Peskine449bd832023-01-11 14:50:10 +010034 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
35 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +020036
Gilles Peskine449bd832023-01-11 14:50:10 +010037 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020038
Gilles Peskine449bd832023-01-11 14:50:10 +010039 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010041 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
42 rsa = mbedtls_pk_rsa(ctx);
43 TEST_ASSERT(mbedtls_rsa_check_privkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020044 }
45
Paul Bakkerbd51b262014-07-10 15:26:12 +020046exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010048 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020049}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010050
Paul Bakker1a7550a2013-09-15 13:01:22 +020051/* END_CASE */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010054void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020057 int res;
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020060 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010068 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
69 rsa = mbedtls_pk_rsa(ctx);
70 TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 }
72
Paul Bakkerbd51b262014-07-10 15:26:12 +020073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010075 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020076}
77/* END_CASE */
78
Valerio Setti545a0d62023-06-14 14:56:48 +020079/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +010080void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020083 int res;
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020086 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +010093 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +020094#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
95 /* No need to check whether the parsed public point is on the curve or
96 * not because this is already done by the internal "pk_get_ecpubkey()"
97 * function */
98#else
99 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200100 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200102#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 }
104
Paul Bakkerbd51b262014-07-10 15:26:12 +0200105exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_pk_free(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200107 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108}
109/* END_CASE */
110
Valerio Setti545a0d62023-06-14 14:56:48 +0200111/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113{
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200118 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
121 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti7237d5f2023-05-18 19:00:22 +0200127#if defined(MBEDTLS_ECP_C)
128 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
Valerio Setti7237d5f2023-05-18 19:00:22 +0200130#else
131 /* PSA keys are already checked on import so nothing to do here. */
132#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133 }
134
Paul Bakkerbd51b262014-07-10 15:26:12 +0200135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_pk_free(&ctx);
Valerio Setti3fddf252023-04-04 10:49:28 +0200137 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200138}
139/* END_CASE */
140
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100141/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200147 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
150 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
Paul Bakkerbd51b262014-07-10 15:26:12 +0200152exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200154 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155}
156/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200157
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100158/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100159void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
160{
161 mbedtls_pk_context pk;
162
163 mbedtls_pk_init(&pk);
164 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100165
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100166 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100167 pass->x, pass->len,
168 mbedtls_test_rnd_std_rand,
169 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100170exit:
171 mbedtls_pk_free(&pk);
172 USE_PSA_DONE();
173}
174/* END_CASE */
175
Valerio Settid476faa2023-07-05 10:33:53 +0200176/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
177void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200178{
179 /* Montgomery keys have specific bits set to either 0 or 1 depending on
180 * their position. This is enforced during parsing (please see the implementation
181 * of mbedtls_ecp_read_key() for more details). The scope of this function
182 * is to verify this enforcing by feeding the parse algorithm with a x25519
183 * key which does not have those bits set properly. */
184 mbedtls_pk_context pk;
185 unsigned char *output_key = NULL;
186 size_t output_key_len = 0;
187
188 mbedtls_pk_init(&pk);
189 USE_PSA_INIT();
190
191 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
192 mbedtls_test_rnd_std_rand, NULL), 0);
193
194 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100195 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200196 /* output_key_len is updated with the real amount of data written to
197 * output_key buffer. */
198 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
199 TEST_ASSERT(output_key_len > 0);
200
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100201 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200202
203exit:
204 if (output_key != NULL) {
205 mbedtls_free(output_key);
206 }
207 mbedtls_pk_free(&pk);
208 USE_PSA_DONE();
209}
210/* END_CASE */