blob: 6c904a39714efc24b7de2aec271c544a4904496e [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
Gilles Peskine157679c2024-02-09 19:29:44 +01009#if defined(MBEDTLS_PSA_CRYPTO_C)
10#include "test/psa_exercise_key.h"
11#endif
12
Waleed Elmelegy38202a22023-09-21 15:21:10 +010013#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
14#define HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der
15#endif
16
Gilles Peskine157679c2024-02-09 19:29:44 +010017#if defined(MBEDTLS_PSA_CRYPTO_C)
18static int test_psa_bridge(const mbedtls_pk_context *ctx,
19 psa_key_usage_t usage_flag)
20{
21 switch (usage_flag) {
22 case PSA_KEY_USAGE_SIGN_HASH:
23 mbedtls_test_set_step(0);
24 break;
25 case PSA_KEY_USAGE_SIGN_MESSAGE:
26 mbedtls_test_set_step(1);
27 break;
28 case PSA_KEY_USAGE_DECRYPT:
29 mbedtls_test_set_step(2);
30 break;
31 case PSA_KEY_USAGE_DERIVE:
32 mbedtls_test_set_step(3);
33 break;
34 case PSA_KEY_USAGE_VERIFY_HASH:
35 mbedtls_test_set_step(4);
36 break;
37 case PSA_KEY_USAGE_VERIFY_MESSAGE:
38 mbedtls_test_set_step(5);
39 break;
40 case PSA_KEY_USAGE_ENCRYPT:
41 mbedtls_test_set_step(6);
42 break;
43 }
44
45 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
46 mbedtls_svc_key_id_t psa_key = MBEDTLS_SVC_KEY_ID_INIT;
47 int ok = 0;
48
49 TEST_EQUAL(mbedtls_pk_get_psa_attributes(ctx, usage_flag, &attributes), 0);
50 TEST_EQUAL(mbedtls_pk_import_into_psa(ctx, &attributes, &psa_key), 0);
51 psa_algorithm_t exercise_usage = psa_get_key_usage_flags(&attributes);
52 psa_algorithm_t exercise_alg = psa_get_key_algorithm(&attributes);
53 TEST_ASSERT(mbedtls_test_psa_exercise_key(psa_key,
54 exercise_usage, exercise_alg));
55
56 mbedtls_test_set_step((unsigned long) -1);
57 ok = 1;
58
59exit:
60 psa_destroy_key(psa_key);
61 psa_reset_key_attributes(&attributes);
62 return ok;
63}
64
65/* Whether a pk key can do ECDSA. Opaque keys are not supported since this
66 * test suite does not create opaque keys. */
67static int pk_can_ecdsa(const mbedtls_pk_context *ctx)
68{
69 /* Check whether we have an EC key. Unfortunately this also accepts
70 * keys on Montgomery curves, which can only do ECDH, so we'll have
71 * to dig further. */
72 if (!mbedtls_pk_can_do(ctx, MBEDTLS_PK_ECDSA)) {
73 return 0;
74 }
75#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
76 return ctx->ec_family != PSA_ECC_FAMILY_MONTGOMERY;
77#elif defined(MBEDTLS_ECDSA_C)
78 return mbedtls_ecdsa_can_do(mbedtls_pk_ec_ro(*ctx)->grp.id);
79#else
80 return 0;
81#endif
82}
83#endif /* MBEDTLS_PSA_CRYPTO_C */
84
Paul Bakker1a7550a2013-09-15 13:01:22 +020085/* END_HEADER */
86
87/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +020088 * depends_on:MBEDTLS_PK_PARSE_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020089 * END_DEPENDENCIES
90 */
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010093void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 int res;
97 char *pwd = password;
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200100 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
107 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108
Gilles Peskine799befd2023-11-15 11:04:08 +0100109 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
114 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100115 TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100116
117#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100118 PSA_INIT();
119 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
120 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
121 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DECRYPT));
122 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
123 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
124 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100125#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 }
127
Paul Bakkerbd51b262014-07-10 15:26:12 +0200128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100130 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100132
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133/* END_CASE */
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139 int res;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200142 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145
Gilles Peskine799befd2023-11-15 11:04:08 +0100146 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
151 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100152 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100153
154#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100155 PSA_INIT();
156 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
157 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
158 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100159#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160 }
161
Paul Bakkerbd51b262014-07-10 15:26:12 +0200162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100164 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165}
166/* END_CASE */
167
Valerio Setti545a0d62023-06-14 14:56:48 +0200168/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 int res;
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800175 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178
Gilles Peskine799befd2023-11-15 11:04:08 +0100179 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200183#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
184 /* No need to check whether the parsed public point is on the curve or
185 * not because this is already done by the internal "pk_get_ecpubkey()"
186 * function */
187#else
188 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200189 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100190 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200191#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100192
193#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100194 PSA_INIT();
195 if (pk_can_ecdsa(&ctx)) {
196 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
197 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
198 }
Gilles Peskined0783862024-02-02 13:13:34 +0100199#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200200 }
201
Paul Bakkerbd51b262014-07-10 15:26:12 +0200202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100204 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200205}
206/* END_CASE */
207
Valerio Setti545a0d62023-06-14 14:56:48 +0200208/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212 int res;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800215 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
218 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200219
Gilles Peskine799befd2023-11-15 11:04:08 +0100220 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100224#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
225 /* PSA keys are already checked on import so nothing to do here. */
226#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200227 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100228 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100229#endif
230
231#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100232 PSA_INIT();
233 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DERIVE));
234 if (pk_can_ecdsa(&ctx)) {
235 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
236 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
237 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
238 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
239 }
Valerio Setti7237d5f2023-05-18 19:00:22 +0200240#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200241 }
242
Paul Bakkerbd51b262014-07-10 15:26:12 +0200243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100245 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200246}
247/* END_CASE */
248
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100249/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200255 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
258 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200259
Paul Bakkerbd51b262014-07-10 15:26:12 +0200260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200262 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200263}
264/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200265
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100266/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100267void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
268{
269 mbedtls_pk_context pk;
270
271 mbedtls_pk_init(&pk);
272 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100273
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100274 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100275 pass->x, pass->len,
276 mbedtls_test_rnd_std_rand,
277 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100278exit:
279 mbedtls_pk_free(&pk);
280 USE_PSA_DONE();
281}
282/* END_CASE */
283
Valerio Settid476faa2023-07-05 10:33:53 +0200284/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
285void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200286{
287 /* Montgomery keys have specific bits set to either 0 or 1 depending on
288 * their position. This is enforced during parsing (please see the implementation
289 * of mbedtls_ecp_read_key() for more details). The scope of this function
290 * is to verify this enforcing by feeding the parse algorithm with a x25519
291 * key which does not have those bits set properly. */
292 mbedtls_pk_context pk;
293 unsigned char *output_key = NULL;
294 size_t output_key_len = 0;
295
296 mbedtls_pk_init(&pk);
297 USE_PSA_INIT();
298
299 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
300 mbedtls_test_rnd_std_rand, NULL), 0);
301
302 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100303 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200304 /* output_key_len is updated with the real amount of data written to
305 * output_key buffer. */
306 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
307 TEST_ASSERT(output_key_len > 0);
308
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100309 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200310
311exit:
312 if (output_key != NULL) {
313 mbedtls_free(output_key);
314 }
315 mbedtls_pk_free(&pk);
316 USE_PSA_DONE();
317}
318/* END_CASE */