blob: 829e789c06f67bda10609ec52c574b2d26d33b45 [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
Gilles Peskine069cec12024-02-12 16:59:17 +0100103 size_t bitlen = mbedtls_rsa_get_bitlen(rsa);
104 TEST_EQUAL(mbedtls_pk_get_bitlen(&ctx), bitlen);
105 TEST_EQUAL(mbedtls_pk_get_len(&ctx), (bitlen + 7) / 8);
106
Gilles Peskined0783862024-02-02 13:13:34 +0100107#if defined(MBEDTLS_PSA_CRYPTO_C)
108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
109 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
110 PSA_KEY_USAGE_ENCRYPT,
111 &attributes), 0);
112 psa_reset_key_attributes(&attributes);
113 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
114 PSA_KEY_USAGE_VERIFY_HASH,
115 &attributes), 0);
116 psa_reset_key_attributes(&attributes);
117 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
118 PSA_KEY_USAGE_VERIFY_MESSAGE,
119 &attributes), 0);
120#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121 }
122
Paul Bakkerbd51b262014-07-10 15:26:12 +0200123exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100125 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126}
127/* END_CASE */
128
Valerio Setti545a0d62023-06-14 14:56:48 +0200129/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133 int res;
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800136 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139
Gilles Peskine799befd2023-11-15 11:04:08 +0100140 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200144#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
145 /* No need to check whether the parsed public point is on the curve or
146 * not because this is already done by the internal "pk_get_ecpubkey()"
147 * function */
148#else
149 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200150 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100151 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200152#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100153
154#if defined(MBEDTLS_PSA_CRYPTO_C)
155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
157 PSA_KEY_USAGE_VERIFY_HASH,
158 &attributes), 0);
159 psa_reset_key_attributes(&attributes);
160 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
161 PSA_KEY_USAGE_VERIFY_MESSAGE,
162 &attributes), 0);
163#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164 }
165
Paul Bakkerbd51b262014-07-10 15:26:12 +0200166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_pk_free(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800168 MD_OR_USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169}
170/* END_CASE */
171
Valerio Setti545a0d62023-06-14 14:56:48 +0200172/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176 int res;
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800179 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
182 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200183
Gilles Peskine799befd2023-11-15 11:04:08 +0100184 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100188#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
189 /* PSA keys are already checked on import so nothing to do here. */
190#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200191 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100192 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100193#endif
194
195#if defined(MBEDTLS_PSA_CRYPTO_C)
196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
197 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
198 PSA_KEY_USAGE_SIGN_HASH,
199 &attributes), 0);
200 psa_reset_key_attributes(&attributes);
201 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
202 PSA_KEY_USAGE_SIGN_MESSAGE,
203 &attributes), 0);
204 psa_reset_key_attributes(&attributes);
205 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
206 PSA_KEY_USAGE_DERIVE,
207 &attributes), 0);
208 psa_reset_key_attributes(&attributes);
209 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
210 PSA_KEY_USAGE_VERIFY_HASH,
211 &attributes), 0);
212 psa_reset_key_attributes(&attributes);
213 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
214 PSA_KEY_USAGE_VERIFY_MESSAGE,
215 &attributes), 0);
Valerio Setti7237d5f2023-05-18 19:00:22 +0200216#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200217 }
218
Paul Bakkerbd51b262014-07-10 15:26:12 +0200219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_pk_free(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800221 MD_OR_USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200222}
223/* END_CASE */
224
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100225/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200231 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
234 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200235
Paul Bakkerbd51b262014-07-10 15:26:12 +0200236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200238 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200239}
240/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200241
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100242/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100243void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
244{
245 mbedtls_pk_context pk;
246
247 mbedtls_pk_init(&pk);
248 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100249
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100250 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100251 pass->x, pass->len,
252 mbedtls_test_rnd_std_rand,
253 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100254exit:
255 mbedtls_pk_free(&pk);
256 USE_PSA_DONE();
257}
258/* END_CASE */
259
Valerio Settid476faa2023-07-05 10:33:53 +0200260/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
261void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200262{
263 /* Montgomery keys have specific bits set to either 0 or 1 depending on
264 * their position. This is enforced during parsing (please see the implementation
265 * of mbedtls_ecp_read_key() for more details). The scope of this function
266 * is to verify this enforcing by feeding the parse algorithm with a x25519
267 * key which does not have those bits set properly. */
268 mbedtls_pk_context pk;
269 unsigned char *output_key = NULL;
270 size_t output_key_len = 0;
271
272 mbedtls_pk_init(&pk);
273 USE_PSA_INIT();
274
275 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
276 mbedtls_test_rnd_std_rand, NULL), 0);
277
278 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100279 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200280 /* output_key_len is updated with the real amount of data written to
281 * output_key buffer. */
282 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
283 TEST_ASSERT(output_key_len > 0);
284
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100285 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200286
287exit:
288 if (output_key != NULL) {
289 mbedtls_free(output_key);
290 }
291 mbedtls_pk_free(&pk);
292 USE_PSA_DONE();
293}
294/* END_CASE */