blob: ba93e77c9188532b6b524832c4d9734119cc5395 [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"
Paul Bakker1a7550a2013-09-15 13:01:22 +02008/* END_HEADER */
9
10/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020012 * END_DEPENDENCIES
13 */
14
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010016void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020019 int res;
20 char *pwd = password;
21
Gilles Peskine449bd832023-01-11 14:50:10 +010022 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020023 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020024
Gilles Peskine449bd832023-01-11 14:50:10 +010025 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +020026 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010027 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020028
Gilles Peskine449bd832023-01-11 14:50:10 +010029 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
30 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +020031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020033
Gilles Peskine449bd832023-01-11 14:50:10 +010034 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010036 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
37 rsa = mbedtls_pk_rsa(ctx);
38 TEST_ASSERT(mbedtls_rsa_check_privkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020039 }
40
Paul Bakkerbd51b262014-07-10 15:26:12 +020041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010042 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010043 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020044}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010045
Paul Bakker1a7550a2013-09-15 13:01:22 +020046/* END_CASE */
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010049void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020052 int res;
53
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020055 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010063 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
64 rsa = mbedtls_pk_rsa(ctx);
65 TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020066 }
67
Paul Bakkerbd51b262014-07-10 15:26:12 +020068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010070 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020071}
72/* END_CASE */
73
Valerio Setti545a0d62023-06-14 14:56:48 +020074/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +010075void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020078 int res;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020081 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +010088 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +020089#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
90 /* No need to check whether the parsed public point is on the curve or
91 * not because this is already done by the internal "pk_get_ecpubkey()"
92 * function */
93#else
94 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +020095 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +010096 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Valerio Setti483738e2023-05-17 15:37:29 +020097#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 }
99
Paul Bakkerbd51b262014-07-10 15:26:12 +0200100exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_pk_free(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200102 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103}
104/* END_CASE */
105
Valerio Setti545a0d62023-06-14 14:56:48 +0200106/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110 int res;
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200113 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
116 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (res == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti7237d5f2023-05-18 19:00:22 +0200122#if defined(MBEDTLS_ECP_C)
123 const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
Valerio Setti7237d5f2023-05-18 19:00:22 +0200125#else
126 /* PSA keys are already checked on import so nothing to do here. */
127#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128 }
129
Paul Bakkerbd51b262014-07-10 15:26:12 +0200130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_pk_free(&ctx);
Valerio Setti3fddf252023-04-04 10:49:28 +0200132 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133}
134/* END_CASE */
135
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100136/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200142 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
145 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146
Paul Bakkerbd51b262014-07-10 15:26:12 +0200147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200149 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150}
151/* END_CASE */
Valerio Settiaed87992023-07-04 19:58:43 +0200152
Valerio Settid476faa2023-07-05 10:33:53 +0200153/* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */
154void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output)
Valerio Settiaed87992023-07-04 19:58:43 +0200155{
156 /* Montgomery keys have specific bits set to either 0 or 1 depending on
157 * their position. This is enforced during parsing (please see the implementation
158 * of mbedtls_ecp_read_key() for more details). The scope of this function
159 * is to verify this enforcing by feeding the parse algorithm with a x25519
160 * key which does not have those bits set properly. */
161 mbedtls_pk_context pk;
162 unsigned char *output_key = NULL;
163 size_t output_key_len = 0;
164
165 mbedtls_pk_init(&pk);
166 USE_PSA_INIT();
167
168 TEST_EQUAL(mbedtls_pk_parse_key(&pk, input_key->x, input_key->len, NULL, 0,
169 mbedtls_test_rnd_std_rand, NULL), 0);
170
171 output_key_len = input_key->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100172 TEST_CALLOC(output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200173 /* output_key_len is updated with the real amount of data written to
174 * output_key buffer. */
175 output_key_len = mbedtls_pk_write_key_der(&pk, output_key, output_key_len);
176 TEST_ASSERT(output_key_len > 0);
177
Tom Cosgrove65cd8512023-07-20 16:46:01 +0100178 TEST_BUFFERS_EQUAL(exp_output->x, exp_output->len, output_key, output_key_len);
Valerio Settiaed87992023-07-04 19:58:43 +0200179
180exit:
181 if (output_key != NULL) {
182 mbedtls_free(output_key);
183 }
184 mbedtls_pk_free(&pk);
185 USE_PSA_DONE();
186}
187/* END_CASE */