blob: f275c324d5805a98efcb9a984cabe53bfa4cdda1 [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);
Gilles Peskine2ec141a2024-02-15 17:22:37 +010051 if (!mbedtls_test_key_consistency_psa_pk(psa_key, ctx)) {
52 goto exit;
53 }
Gilles Peskine34955672024-02-12 14:19:24 +010054
Gilles Peskine157679c2024-02-09 19:29:44 +010055 psa_algorithm_t exercise_usage = psa_get_key_usage_flags(&attributes);
56 psa_algorithm_t exercise_alg = psa_get_key_algorithm(&attributes);
Gilles Peskine34955672024-02-12 14:19:24 +010057 if (mbedtls_test_can_exercise_psa_algorithm(exercise_alg)) {
58 TEST_ASSERT(mbedtls_test_psa_exercise_key(psa_key,
59 exercise_usage,
60 exercise_alg));
61 }
Gilles Peskine157679c2024-02-09 19:29:44 +010062
63 mbedtls_test_set_step((unsigned long) -1);
64 ok = 1;
65
66exit:
67 psa_destroy_key(psa_key);
68 psa_reset_key_attributes(&attributes);
69 return ok;
70}
71
Gilles Peskine1d338762024-02-12 14:18:26 +010072#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine157679c2024-02-09 19:29:44 +010073/* Whether a pk key can do ECDSA. Opaque keys are not supported since this
74 * test suite does not create opaque keys. */
75static int pk_can_ecdsa(const mbedtls_pk_context *ctx)
76{
77 /* Check whether we have an EC key. Unfortunately this also accepts
78 * keys on Montgomery curves, which can only do ECDH, so we'll have
79 * to dig further. */
80 if (!mbedtls_pk_can_do(ctx, MBEDTLS_PK_ECDSA)) {
81 return 0;
82 }
83#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
84 return ctx->ec_family != PSA_ECC_FAMILY_MONTGOMERY;
85#elif defined(MBEDTLS_ECDSA_C)
86 return mbedtls_ecdsa_can_do(mbedtls_pk_ec_ro(*ctx)->grp.id);
87#else
88 return 0;
89#endif
90}
Gilles Peskine1d338762024-02-12 14:18:26 +010091#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
92#endif /* MBEDTLS_PSA_CRYPTO_C && && MBEDTLS_FS_IO */
Gilles Peskine157679c2024-02-09 19:29:44 +010093
Paul Bakker1a7550a2013-09-15 13:01:22 +020094/* END_HEADER */
95
96/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +020097 * depends_on:MBEDTLS_PK_PARSE_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 * END_DEPENDENCIES
99 */
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 int res;
106 char *pwd = password;
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200109 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
116 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117
Gilles Peskine799befd2023-11-15 11:04:08 +0100118 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
123 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100124 TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100125
126#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100127 PSA_INIT();
128 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
129 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
130 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DECRYPT));
131 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
132 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
133 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100134#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135 }
136
Paul Bakkerbd51b262014-07-10 15:26:12 +0200137exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100139 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100141
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142/* END_CASE */
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148 int res;
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200151 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154
Gilles Peskine799befd2023-11-15 11:04:08 +0100155 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
160 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100161 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100162
163#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100164 PSA_INIT();
165 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
166 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
167 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100168#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169 }
170
Paul Bakkerbd51b262014-07-10 15:26:12 +0200171exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100173 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174}
175/* END_CASE */
176
Valerio Setti545a0d62023-06-14 14:56:48 +0200177/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181 int res;
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800184 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187
Gilles Peskine799befd2023-11-15 11:04:08 +0100188 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200192#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
193 /* No need to check whether the parsed public point is on the curve or
194 * not because this is already done by the internal "pk_get_ecpubkey()"
195 * function */
196#else
197 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200198 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100199 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200200#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100201
202#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100203 PSA_INIT();
204 if (pk_can_ecdsa(&ctx)) {
205 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
206 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
207 }
Gilles Peskined0783862024-02-02 13:13:34 +0100208#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209 }
210
Paul Bakkerbd51b262014-07-10 15:26:12 +0200211exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100213 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214}
215/* END_CASE */
216
Valerio Setti545a0d62023-06-14 14:56:48 +0200217/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200221 int res;
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800224 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
227 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200228
Gilles Peskine799befd2023-11-15 11:04:08 +0100229 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100233#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
234 /* PSA keys are already checked on import so nothing to do here. */
235#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200236 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100237 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100238#endif
239
240#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100241 PSA_INIT();
242 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DERIVE));
243 if (pk_can_ecdsa(&ctx)) {
244 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
245 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
246 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
247 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
248 }
Valerio Setti7237d5f2023-05-18 19:00:22 +0200249#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200250 }
251
Paul Bakkerbd51b262014-07-10 15:26:12 +0200252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100254 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200255}
256/* END_CASE */
257
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100258/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200264 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
267 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200268
Paul Bakkerbd51b262014-07-10 15:26:12 +0200269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200271 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200272}
273/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200274
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100275/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100276void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
277{
278 mbedtls_pk_context pk;
279
280 mbedtls_pk_init(&pk);
281 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100282
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100283 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100284 pass->x, pass->len,
285 mbedtls_test_rnd_std_rand,
286 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100287exit:
288 mbedtls_pk_free(&pk);
289 USE_PSA_DONE();
290}
291/* END_CASE */
292
Valerio Settid476faa2023-07-05 10:33:53 +0200293/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
294void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200295{
296 /* Montgomery keys have specific bits set to either 0 or 1 depending on
297 * their position. This is enforced during parsing (please see the implementation
298 * of mbedtls_ecp_read_key() for more details). The scope of this function
299 * is to verify this enforcing by feeding the parse algorithm with a x25519
300 * key which does not have those bits set properly. */
301 mbedtls_pk_context pk;
302 unsigned char *output_key = NULL;
303 size_t output_key_len = 0;
304
305 mbedtls_pk_init(&pk);
306 USE_PSA_INIT();
307
308 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
309 mbedtls_test_rnd_std_rand, NULL), 0);
310
311 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100312 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200313 /* output_key_len is updated with the real amount of data written to
314 * output_key buffer. */
315 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
316 TEST_ASSERT(output_key_len > 0);
317
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100318 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200319
320exit:
321 if (output_key != NULL) {
322 mbedtls_free(output_key);
323 }
324 mbedtls_pk_free(&pk);
325 USE_PSA_DONE();
326}
327/* END_CASE */