blob: f4bbb215a03f68a447a8f97c0ceefb5623e44ee8 [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 Peskine799befd2023-11-15 11:04:08 +010037 TEST_EQUAL(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);
Gilles Peskine799befd2023-11-15 11:04:08 +010043 TEST_EQUAL(mbedtls_rsa_check_privkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +010044
Gilles Peskine92fb6042024-02-01 22:33:06 +010045 size_t bitlen = mbedtls_rsa_get_bitlen(rsa);
46 TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen);
47 TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8);
48
Gilles Peskined0783862024-02-02 13:13:34 +010049#if defined(MBEDTLS_PSA_CRYPTO_C)
50 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
51 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
52 PSA_KEY_USAGE_SIGN_HASH,
53 &attributes), 0);
54 psa_reset_key_attributes(&attributes);
55 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
56 PSA_KEY_USAGE_SIGN_MESSAGE,
57 &attributes), 0);
58 psa_reset_key_attributes(&attributes);
59 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
60 PSA_KEY_USAGE_DECRYPT,
61 &attributes), 0);
62 psa_reset_key_attributes(&attributes);
63 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
64 PSA_KEY_USAGE_VERIFY_HASH,
65 &attributes), 0);
66 psa_reset_key_attributes(&attributes);
67 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
68 PSA_KEY_USAGE_VERIFY_MESSAGE,
69 &attributes), 0);
70 psa_reset_key_attributes(&attributes);
71 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
72 PSA_KEY_USAGE_ENCRYPT,
73 &attributes), 0);
74#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020075 }
76
Paul Bakkerbd51b262014-07-10 15:26:12 +020077exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010079 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020080}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010081
Paul Bakker1a7550a2013-09-15 13:01:22 +020082/* END_CASE */
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010085void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 int res;
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020091 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020094
Gilles Peskine799befd2023-11-15 11:04:08 +010095 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
100 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100101 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100102
103#if defined(MBEDTLS_PSA_CRYPTO_C)
104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
105 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
106 PSA_KEY_USAGE_ENCRYPT,
107 &attributes), 0);
108 psa_reset_key_attributes(&attributes);
109 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
110 PSA_KEY_USAGE_VERIFY_HASH,
111 &attributes), 0);
112 psa_reset_key_attributes(&attributes);
113 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
114 PSA_KEY_USAGE_VERIFY_MESSAGE,
115 &attributes), 0);
116#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117 }
118
Paul Bakkerbd51b262014-07-10 15:26:12 +0200119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100121 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122}
123/* END_CASE */
124
Valerio Setti545a0d62023-06-14 14:56:48 +0200125/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129 int res;
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800132 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135
Gilles Peskine799befd2023-11-15 11:04:08 +0100136 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200140#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
141 /* No need to check whether the parsed public point is on the curve or
142 * not because this is already done by the internal "pk_get_ecpubkey()"
143 * function */
144#else
145 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200146 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100147 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200148#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100149
150#if defined(MBEDTLS_PSA_CRYPTO_C)
151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
152 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
153 PSA_KEY_USAGE_VERIFY_HASH,
154 &attributes), 0);
155 psa_reset_key_attributes(&attributes);
156 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
157 PSA_KEY_USAGE_VERIFY_MESSAGE,
158 &attributes), 0);
159#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);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800164 MD_OR_USE_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_keyfile_ec(char *key_file, char *password, 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_keyfile(&ctx, key_file, password,
178 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Gilles Peskine799befd2023-11-15 11:04:08 +0100180 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100184#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
185 /* PSA keys are already checked on import so nothing to do here. */
186#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200187 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100188 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100189#endif
190
191#if defined(MBEDTLS_PSA_CRYPTO_C)
192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
193 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
194 PSA_KEY_USAGE_SIGN_HASH,
195 &attributes), 0);
196 psa_reset_key_attributes(&attributes);
197 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
198 PSA_KEY_USAGE_SIGN_MESSAGE,
199 &attributes), 0);
200 psa_reset_key_attributes(&attributes);
201 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
202 PSA_KEY_USAGE_DERIVE,
203 &attributes), 0);
204 psa_reset_key_attributes(&attributes);
205 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
206 PSA_KEY_USAGE_VERIFY_HASH,
207 &attributes), 0);
208 psa_reset_key_attributes(&attributes);
209 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
210 PSA_KEY_USAGE_VERIFY_MESSAGE,
211 &attributes), 0);
Valerio Setti7237d5f2023-05-18 19:00:22 +0200212#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200213 }
214
Paul Bakkerbd51b262014-07-10 15:26:12 +0200215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_pk_free(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800217 MD_OR_USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200218}
219/* END_CASE */
220
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100221/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200227 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
230 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200231
Paul Bakkerbd51b262014-07-10 15:26:12 +0200232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200234 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200235}
236/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200237
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100238/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100239void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
240{
241 mbedtls_pk_context pk;
242
243 mbedtls_pk_init(&pk);
244 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100245
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100246 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100247 pass->x, pass->len,
248 mbedtls_test_rnd_std_rand,
249 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100250exit:
251 mbedtls_pk_free(&pk);
252 USE_PSA_DONE();
253}
254/* END_CASE */
255
Valerio Settid476faa2023-07-05 10:33:53 +0200256/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
257void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200258{
259 /* Montgomery keys have specific bits set to either 0 or 1 depending on
260 * their position. This is enforced during parsing (please see the implementation
261 * of mbedtls_ecp_read_key() for more details). The scope of this function
262 * is to verify this enforcing by feeding the parse algorithm with a x25519
263 * key which does not have those bits set properly. */
264 mbedtls_pk_context pk;
265 unsigned char *output_key = NULL;
266 size_t output_key_len = 0;
267
268 mbedtls_pk_init(&pk);
269 USE_PSA_INIT();
270
271 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
272 mbedtls_test_rnd_std_rand, NULL), 0);
273
274 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100275 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200276 /* output_key_len is updated with the real amount of data written to
277 * output_key buffer. */
278 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
279 TEST_ASSERT(output_key_len > 0);
280
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100281 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200282
283exit:
284 if (output_key != NULL) {
285 mbedtls_free(output_key);
286 }
287 mbedtls_pk_free(&pk);
288 USE_PSA_DONE();
289}
290/* END_CASE */