blob: 14afef6e94b13ddeaffae298d70a535b76bb8ea2 [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
45#if defined(MBEDTLS_PSA_CRYPTO_C)
46 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
47 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
48 PSA_KEY_USAGE_SIGN_HASH,
49 &attributes), 0);
50 psa_reset_key_attributes(&attributes);
51 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
52 PSA_KEY_USAGE_SIGN_MESSAGE,
53 &attributes), 0);
54 psa_reset_key_attributes(&attributes);
55 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
56 PSA_KEY_USAGE_DECRYPT,
57 &attributes), 0);
58 psa_reset_key_attributes(&attributes);
59 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
60 PSA_KEY_USAGE_VERIFY_HASH,
61 &attributes), 0);
62 psa_reset_key_attributes(&attributes);
63 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
64 PSA_KEY_USAGE_VERIFY_MESSAGE,
65 &attributes), 0);
66 psa_reset_key_attributes(&attributes);
67 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
68 PSA_KEY_USAGE_ENCRYPT,
69 &attributes), 0);
70#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020071 }
72
Paul Bakkerbd51b262014-07-10 15:26:12 +020073exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010075 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020076}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010077
Paul Bakker1a7550a2013-09-15 13:01:22 +020078/* END_CASE */
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010081void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020084 int res;
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020087 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020090
Gilles Peskine799befd2023-11-15 11:04:08 +010091 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010095 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
96 rsa = mbedtls_pk_rsa(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +010097 TEST_EQUAL(mbedtls_rsa_check_pubkey(rsa), 0);
Gilles Peskined0783862024-02-02 13:13:34 +010098
99#if defined(MBEDTLS_PSA_CRYPTO_C)
100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
101 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
102 PSA_KEY_USAGE_ENCRYPT,
103 &attributes), 0);
104 psa_reset_key_attributes(&attributes);
105 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
106 PSA_KEY_USAGE_VERIFY_HASH,
107 &attributes), 0);
108 psa_reset_key_attributes(&attributes);
109 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
110 PSA_KEY_USAGE_VERIFY_MESSAGE,
111 &attributes), 0);
112#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113 }
114
Paul Bakkerbd51b262014-07-10 15:26:12 +0200115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +0100117 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118}
119/* END_CASE */
120
Valerio Setti545a0d62023-06-14 14:56:48 +0200121/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125 int res;
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800128 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131
Gilles Peskine799befd2023-11-15 11:04:08 +0100132 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +0200136#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
137 /* No need to check whether the parsed public point is on the curve or
138 * not because this is already done by the internal "pk_get_ecpubkey()"
139 * function */
140#else
141 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +0200142 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100143 TEST_EQUAL(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q), 0);
Valerio Setti483738e2023-05-17 15:37:29 +0200144#endif
Gilles Peskined0783862024-02-02 13:13:34 +0100145
146#if defined(MBEDTLS_PSA_CRYPTO_C)
147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
148 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
149 PSA_KEY_USAGE_VERIFY_HASH,
150 &attributes), 0);
151 psa_reset_key_attributes(&attributes);
152 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
153 PSA_KEY_USAGE_VERIFY_MESSAGE,
154 &attributes), 0);
155#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156 }
157
Paul Bakkerbd51b262014-07-10 15:26:12 +0200158exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_pk_free(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800160 MD_OR_USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161}
162/* END_CASE */
163
Valerio Setti545a0d62023-06-14 14:56:48 +0200164/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 int res;
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_pk_init(&ctx);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800171 MD_OR_USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
174 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175
Gilles Peskine799befd2023-11-15 11:04:08 +0100176 TEST_EQUAL(res, result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Gilles Peskined0783862024-02-02 13:13:34 +0100180#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
181 /* PSA keys are already checked on import so nothing to do here. */
182#else
Valerio Setti7237d5f2023-05-18 19:00:22 +0200183 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine799befd2023-11-15 11:04:08 +0100184 TEST_EQUAL(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d), 0);
Gilles Peskined0783862024-02-02 13:13:34 +0100185#endif
186
187#if defined(MBEDTLS_PSA_CRYPTO_C)
188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
189 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
190 PSA_KEY_USAGE_SIGN_HASH,
191 &attributes), 0);
192 psa_reset_key_attributes(&attributes);
193 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
194 PSA_KEY_USAGE_SIGN_MESSAGE,
195 &attributes), 0);
196 psa_reset_key_attributes(&attributes);
197 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
198 PSA_KEY_USAGE_DERIVE,
199 &attributes), 0);
200 psa_reset_key_attributes(&attributes);
201 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
202 PSA_KEY_USAGE_VERIFY_HASH,
203 &attributes), 0);
204 psa_reset_key_attributes(&attributes);
205 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&ctx,
206 PSA_KEY_USAGE_VERIFY_MESSAGE,
207 &attributes), 0);
Valerio Setti7237d5f2023-05-18 19:00:22 +0200208#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);
Pengyu Lvc5d4c462023-11-15 14:20:07 +0800213 MD_OR_USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214}
215/* END_CASE */
216
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100217/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200223 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
226 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200227
Paul Bakkerbd51b262014-07-10 15:26:12 +0200228exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200230 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200231}
232/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200233
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100234/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:HAVE_mbedtls_pk_parse_key_pkcs8_encrypted_der */
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100235void pk_parse_key_encrypted(data_t *buf, data_t *pass, int result)
236{
237 mbedtls_pk_context pk;
238
239 mbedtls_pk_init(&pk);
240 USE_PSA_INIT();
Waleed Elmelegy38202a22023-09-21 15:21:10 +0100241
Waleed Elmelegy9d4d8eb2023-09-21 08:27:39 +0100242 TEST_EQUAL(mbedtls_pk_parse_key_pkcs8_encrypted_der(&pk, buf->x, buf->len,
Waleed Elmelegy556a0792023-09-21 09:19:56 +0100243 pass->x, pass->len,
244 mbedtls_test_rnd_std_rand,
245 NULL), result);
Waleed Elmelegy1db5cda2023-09-20 18:00:48 +0100246exit:
247 mbedtls_pk_free(&pk);
248 USE_PSA_DONE();
249}
250/* END_CASE */
251
Valerio Settid476faa2023-07-05 10:33:53 +0200252/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
253void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200254{
255 /* Montgomery keys have specific bits set to either 0 or 1 depending on
256 * their position. This is enforced during parsing (please see the implementation
257 * of mbedtls_ecp_read_key() for more details). The scope of this function
258 * is to verify this enforcing by feeding the parse algorithm with a x25519
259 * key which does not have those bits set properly. */
260 mbedtls_pk_context pk;
261 unsigned char *output_key = NULL;
262 size_t output_key_len = 0;
263
264 mbedtls_pk_init(&pk);
265 USE_PSA_INIT();
266
267 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
268 mbedtls_test_rnd_std_rand, NULL), 0);
269
270 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100271 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200272 /* output_key_len is updated with the real amount of data written to
273 * output_key buffer. */
274 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
275 TEST_ASSERT(output_key_len > 0);
276
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100277 TEST_MEMORY_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200278
279exit:
280 if (output_key != NULL) {
281 mbedtls_free(output_key);
282 }
283 mbedtls_pk_free(&pk);
284 USE_PSA_DONE();
285}
286/* END_CASE */