blob: add225d7357664b6df93532e4265beae7d2da515 [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 Peskine34955672024-02-12 14:19:24 +010051
Gilles Peskine157679c2024-02-09 19:29:44 +010052 psa_algorithm_t exercise_usage = psa_get_key_usage_flags(&attributes);
53 psa_algorithm_t exercise_alg = psa_get_key_algorithm(&attributes);
Gilles Peskine34955672024-02-12 14:19:24 +010054 if (mbedtls_test_can_exercise_psa_algorithm(exercise_alg)) {
55 TEST_ASSERT(mbedtls_test_psa_exercise_key(psa_key,
56 exercise_usage,
57 exercise_alg));
58 }
Gilles Peskine157679c2024-02-09 19:29:44 +010059
60 mbedtls_test_set_step((unsigned long) -1);
61 ok = 1;
62
63exit:
64 psa_destroy_key(psa_key);
65 psa_reset_key_attributes(&attributes);
66 return ok;
67}
68
Gilles Peskine1d338762024-02-12 14:18:26 +010069#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine157679c2024-02-09 19:29:44 +010070/* Whether a pk key can do ECDSA. Opaque keys are not supported since this
71 * test suite does not create opaque keys. */
72static int pk_can_ecdsa(const mbedtls_pk_context *ctx)
73{
74 /* Check whether we have an EC key. Unfortunately this also accepts
75 * keys on Montgomery curves, which can only do ECDH, so we'll have
76 * to dig further. */
77 if (!mbedtls_pk_can_do(ctx, MBEDTLS_PK_ECDSA)) {
78 return 0;
79 }
80#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
81 return ctx->ec_family != PSA_ECC_FAMILY_MONTGOMERY;
82#elif defined(MBEDTLS_ECDSA_C)
83 return mbedtls_ecdsa_can_do(mbedtls_pk_ec_ro(*ctx)->grp.id);
84#else
85 return 0;
86#endif
87}
Gilles Peskine1d338762024-02-12 14:18:26 +010088#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
89#endif /* MBEDTLS_PSA_CRYPTO_C && && MBEDTLS_FS_IO */
Gilles Peskine157679c2024-02-09 19:29:44 +010090
Paul Bakker1a7550a2013-09-15 13:01:22 +020091/* END_HEADER */
92
93/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +020094 * depends_on:MBEDTLS_PK_PARSE_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020095 * END_DEPENDENCIES
96 */
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010099void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102 int res;
103 char *pwd = password;
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200106 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200109 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
113 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114
Gilles Peskine799befd2023-11-15 11:04:08 +0100115 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
120 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100121 TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100122
123#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100124 PSA_INIT();
125 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
126 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
127 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DECRYPT));
128 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
129 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
130 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100131#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132 }
133
Paul Bakkerbd51b262014-07-10 15:26:12 +0200134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100136 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100138
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139/* END_CASE */
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145 int res;
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200148 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
Gilles Peskine799befd2023-11-15 11:04:08 +0100152 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
157 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100158 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100159
160#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100161 PSA_INIT();
162 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
163 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
164 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_ENCRYPT));
Gilles Peskined0783862024-02-02 13:13:34 +0100165#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166 }
167
Paul Bakkerbd51b262014-07-10 15:26:12 +0200168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100170 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171}
172/* END_CASE */
173
Valerio Setti545a0d62023-06-14 14:56:48 +0200174/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178 int res;
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800181 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184
Gilles Peskine799befd2023-11-15 11:04:08 +0100185 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200189#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
190 /* No need to check whether the parsed public point is on the curve or
191 * not because this is already done by the internal "pk_get_ecpubkey()"
192 * function */
193#else
194 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200195 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100196 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200197#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100198
199#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100200 PSA_INIT();
201 if (pk_can_ecdsa(&ctx)) {
202 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
203 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
204 }
Gilles Peskined0783862024-02-02 13:13:34 +0100205#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206 }
207
Paul Bakkerbd51b262014-07-10 15:26:12 +0200208exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100210 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200211}
212/* END_CASE */
213
Valerio Setti545a0d62023-06-14 14:56:48 +0200214/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200218 int res;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800221 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
224 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200225
Gilles Peskine799befd2023-11-15 11:04:08 +0100226 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100230#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
231 /* PSA keys are already checked on import so nothing to do here. */
232#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200233 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100234 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100235#endif
236
237#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine157679c2024-02-09 19:29:44 +0100238 PSA_INIT();
239 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_DERIVE));
240 if (pk_can_ecdsa(&ctx)) {
241 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_HASH));
242 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_SIGN_MESSAGE));
243 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_HASH));
244 TEST_ASSERT(test_psa_bridge(&ctx, PSA_KEY_USAGE_VERIFY_MESSAGE));
245 }
Valerio Setti7237d5f2023-05-18 19:00:22 +0200246#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200247 }
248
Paul Bakkerbd51b262014-07-10 15:26:12 +0200249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_pk_free(&ctx);
Gilles Peskine157679c2024-02-09 19:29:44 +0100251 PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200252}
253/* END_CASE */
254
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100255/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200261 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
264 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200265
Paul Bakkerbd51b262014-07-10 15:26:12 +0200266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200268 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200269}
270/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200271
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100272/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100273void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
274{
275 mbedtls_pk_context pk;
276
277 mbedtls_pk_init(&pk);
278 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100279
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100280 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100281 pass->x, pass->len,
282 mbedtls_test_rnd_std_rand,
283 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100284exit:
285 mbedtls_pk_free(&pk);
286 USE_PSA_DONE();
287}
288/* END_CASE */
289
Valerio Settid476faa2023-07-05 10:33:53 +0200290/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
291void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200292{
293 /* Montgomery keys have specific bits set to either 0 or 1 depending on
294 * their position. This is enforced during parsing (please see the implementation
295 * of mbedtls_ecp_read_key() for more details). The scope of this function
296 * is to verify this enforcing by feeding the parse algorithm with a x25519
297 * key which does not have those bits set properly. */
298 mbedtls_pk_context pk;
299 unsigned char *output_key = NULL;
300 size_t output_key_len = 0;
301
302 mbedtls_pk_init(&pk);
303 USE_PSA_INIT();
304
305 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
306 mbedtls_test_rnd_std_rand, NULL), 0);
307
308 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100309 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200310 /* output_key_len is updated with the real amount of data written to
311 * output_key buffer. */
312 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
313 TEST_ASSERT(output_key_len > 0);
314
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100315 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200316
317exit:
318 if (output_key != NULL) {
319 mbedtls_free(output_key);
320 }
321 mbedtls_pk_free(&pk);
322 USE_PSA_DONE();
323}
324/* END_CASE */