blob: 3227ce9ac41634adc7598466d83a95ea689888da [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 Peskine1d338762024-02-12 14:18:26 +010017#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_FS_IO)
Gilles Peskine157679c2024-02-09 19:29:44 +010018static 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
Gilles Peskine1d338762024-02-12 14:18:26 +010065#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine157679c2024-02-09 19:29:44 +010066/* Whether a pk key can do ECDSA. Opaque keys are not supported since this
67 * test suite does not create opaque keys. */
68static int pk_can_ecdsa(const mbedtls_pk_context *ctx)
69{
70 /* Check whether we have an EC key. Unfortunately this also accepts
71 * keys on Montgomery curves, which can only do ECDH, so we'll have
72 * to dig further. */
73 if (!mbedtls_pk_can_do(ctx, MBEDTLS_PK_ECDSA)) {
74 return 0;
75 }
76#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
77 return ctx->ec_family != PSA_ECC_FAMILY_MONTGOMERY;
78#elif defined(MBEDTLS_ECDSA_C)
79 return mbedtls_ecdsa_can_do(mbedtls_pk_ec_ro(*ctx)->grp.id);
80#else
81 return 0;
82#endif
83}
Gilles Peskine1d338762024-02-12 14:18:26 +010084#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
85#endif /* MBEDTLS_PSA_CRYPTO_C && && MBEDTLS_FS_IO */
Gilles Peskine157679c2024-02-09 19:29:44 +010086
Paul Bakker1a7550a2013-09-15 13:01:22 +020087/* END_HEADER */
88
89/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +020090 * depends_on:MBEDTLS_PK_PARSE_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020091 * END_DEPENDENCIES
92 */
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 int res;
99 char *pwd = password;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200102 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
109 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110
Gilles Peskine799befd2023-11-15 11:04:08 +0100111 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
116 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100117 TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100118
119#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100120 PSA_INIT();
121 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
122 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
123 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DECRYPT));
124 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
125 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
126 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100127#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128 }
129
Paul Bakkerbd51b262014-07-10 15:26:12 +0200130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100132 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100134
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135/* END_CASE */
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 int res;
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200144 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147
Gilles Peskine799befd2023-11-15 11:04:08 +0100148 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
153 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100154 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100155
156#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100157 PSA_INIT();
158 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
159 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
160 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100161#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162 }
163
Paul Bakkerbd51b262014-07-10 15:26:12 +0200164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100166 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167}
168/* END_CASE */
169
Valerio Setti545a0d62023-06-14 14:56:48 +0200170/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174 int res;
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800177 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200180
Gilles Peskine799befd2023-11-15 11:04:08 +0100181 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200185#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
186 /* No need to check whether the parsed public point is on the curve or
187 * not because this is already done by the internal "pk_get_ecpubkey()"
188 * function */
189#else
190 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200191 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100192 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200193#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100194
195#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100196 PSA_INIT();
197 if (pk_can_ecdsa(&ctx)) {
198 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
199 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
200 }
Gilles Peskined0783862024-02-02 13:13:34 +0100201#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200202 }
203
Paul Bakkerbd51b262014-07-10 15:26:12 +0200204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100206 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207}
208/* END_CASE */
209
Valerio Setti545a0d62023-06-14 14:56:48 +0200210/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214 int res;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800217 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
220 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200221
Gilles Peskine799befd2023-11-15 11:04:08 +0100222 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100226#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
227 /* PSA keys are already checked on import so nothing to do here. */
228#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200229 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100230 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100231#endif
232
233#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100234 PSA_INIT();
235 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DERIVE));
236 if (pk_can_ecdsa(&ctx)) {
237 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
238 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
239 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
240 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
241 }
Valerio Setti7237d5f2023-05-18 19:00:22 +0200242#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200243 }
244
Paul Bakkerbd51b262014-07-10 15:26:12 +0200245exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100247 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200248}
249/* END_CASE */
250
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100251/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200257 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
260 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200261
Paul Bakkerbd51b262014-07-10 15:26:12 +0200262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200264 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200265}
266/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200267
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100268/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100269void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
270{
271 mbedtls_pk_context pk;
272
273 mbedtls_pk_init(&pk);
274 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100275
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100276 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100277 pass->x, pass->len,
278 mbedtls_test_rnd_std_rand,
279 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100280exit:
281 mbedtls_pk_free(&pk);
282 USE_PSA_DONE();
283}
284/* END_CASE */
285
Valerio Settid476faa2023-07-05 10:33:53 +0200286/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
287void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200288{
289 /* Montgomery keys have specific bits set to either 0 or 1 depending on
290 * their position. This is enforced during parsing (please see the implementation
291 * of mbedtls_ecp_read_key() for more details). The scope of this function
292 * is to verify this enforcing by feeding the parse algorithm with a x25519
293 * key which does not have those bits set properly. */
294 mbedtls_pk_context pk;
295 unsigned char *output_key = NULL;
296 size_t output_key_len = 0;
297
298 mbedtls_pk_init(&pk);
299 USE_PSA_INIT();
300
301 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
302 mbedtls_test_rnd_std_rand, NULL), 0);
303
304 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100305 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200306 /* output_key_len is updated with the real amount of data written to
307 * output_key buffer. */
308 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
309 TEST_ASSERT(output_key_len > 0);
310
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100311 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200312
313exit:
314 if (output_key != NULL) {
315 mbedtls_free(output_key);
316 }
317 mbedtls_pk_free(&pk);
318 USE_PSA_DONE();
319}
320/* END_CASE */