Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/pk.h" |
| 3 | #include "mbedtls/pem.h" |
| 4 | #include "mbedtls/oid.h" |
Valerio Setti | fa49a8e | 2023-01-26 10:00:55 +0100 | [diff] [blame] | 5 | #include "mbedtls/ecp.h" |
Valerio Setti | aed8799 | 2023-07-04 19:58:43 +0200 | [diff] [blame] | 6 | #include "mbedtls/psa_util.h" |
Valerio Setti | 77a7568 | 2023-05-15 11:18:46 +0200 | [diff] [blame] | 7 | #include "pk_internal.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 8 | /* END_HEADER */ |
| 9 | |
| 10 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 12 | * END_DEPENDENCIES |
| 13 | */ |
| 14 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 15 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 16 | void pk_parse_keyfile_rsa(char *key_file, char *password, int result) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 17 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | mbedtls_pk_context ctx; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 19 | int res; |
| 20 | char *pwd = password; |
| 21 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 22 | mbedtls_pk_init(&ctx); |
Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 23 | MD_PSA_INIT(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 24 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 25 | if (strcmp(pwd, "NULL") == 0) { |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 26 | pwd = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 28 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 29 | res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd, |
| 30 | mbedtls_test_rnd_std_rand, NULL); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 31 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 32 | TEST_ASSERT(res == result); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 33 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | if (res == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | mbedtls_rsa_context *rsa; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | 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 Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 41 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 42 | mbedtls_pk_free(&ctx); |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 43 | MD_PSA_DONE(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 44 | } |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 45 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 46 | /* END_CASE */ |
| 47 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | void pk_parse_public_keyfile_rsa(char *key_file, int result) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 50 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | mbedtls_pk_context ctx; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 52 | int res; |
| 53 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | mbedtls_pk_init(&ctx); |
Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 55 | MD_PSA_INIT(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 56 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | res = mbedtls_pk_parse_public_keyfile(&ctx, key_file); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 58 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | TEST_ASSERT(res == result); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 60 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | if (res == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_rsa_context *rsa; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | 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 Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 68 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | mbedtls_pk_free(&ctx); |
Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 70 | MD_PSA_DONE(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 71 | } |
| 72 | /* END_CASE */ |
| 73 | |
Valerio Setti | 545a0d6 | 2023-06-14 14:56:48 +0200 | [diff] [blame] | 74 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | void pk_parse_public_keyfile_ec(char *key_file, int result) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 76 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | mbedtls_pk_context ctx; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 78 | int res; |
| 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | mbedtls_pk_init(&ctx); |
Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 81 | USE_PSA_INIT(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 82 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | res = mbedtls_pk_parse_public_keyfile(&ctx, key_file); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | TEST_ASSERT(res == result); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 86 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | if (res == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY)); |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 89 | #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 Setti | 77a7568 | 2023-05-15 11:18:46 +0200 | [diff] [blame] | 95 | eckey = mbedtls_pk_ec_ro(ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0); |
Valerio Setti | 483738e | 2023-05-17 15:37:29 +0200 | [diff] [blame] | 97 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 100 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | mbedtls_pk_free(&ctx); |
Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 102 | USE_PSA_DONE(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 103 | } |
| 104 | /* END_CASE */ |
| 105 | |
Valerio Setti | 545a0d6 | 2023-06-14 14:56:48 +0200 | [diff] [blame] | 106 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_PK_HAVE_ECC_KEYS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | void pk_parse_keyfile_ec(char *key_file, char *password, int result) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 108 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | mbedtls_pk_context ctx; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 110 | int res; |
| 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | mbedtls_pk_init(&ctx); |
Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 113 | USE_PSA_INIT(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | res = mbedtls_pk_parse_keyfile(&ctx, key_file, password, |
| 116 | mbedtls_test_rnd_std_rand, NULL); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | TEST_ASSERT(res == result); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | if (res == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY)); |
Valerio Setti | 7237d5f | 2023-05-18 19:00:22 +0200 | [diff] [blame] | 122 | #if defined(MBEDTLS_ECP_C) |
| 123 | const mbedtls_ecp_keypair *eckey = mbedtls_pk_ec_ro(ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0); |
Valerio Setti | 7237d5f | 2023-05-18 19:00:22 +0200 | [diff] [blame] | 125 | #else |
| 126 | /* PSA keys are already checked on import so nothing to do here. */ |
| 127 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 130 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | mbedtls_pk_free(&ctx); |
Valerio Setti | 3fddf25 | 2023-04-04 10:49:28 +0200 | [diff] [blame] | 132 | USE_PSA_DONE(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 133 | } |
| 134 | /* END_CASE */ |
| 135 | |
Manuel Pégourié-Gonnard | b65370f | 2020-02-10 10:50:16 +0100 | [diff] [blame] | 136 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | void pk_parse_key(data_t *buf, int result) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 138 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | mbedtls_pk_context pk; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | mbedtls_pk_init(&pk); |
Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 142 | USE_PSA_INIT(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0, |
| 145 | mbedtls_test_rnd_std_rand, NULL) == result); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 146 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 147 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | mbedtls_pk_free(&pk); |
Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 149 | USE_PSA_DONE(); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 150 | } |
| 151 | /* END_CASE */ |
Valerio Setti | aed8799 | 2023-07-04 19:58:43 +0200 | [diff] [blame] | 152 | |
Valerio Setti | d476faa | 2023-07-05 10:33:53 +0200 | [diff] [blame] | 153 | /* BEGIN_CASE depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_PK_WRITE_C */ |
| 154 | void pk_parse_fix_montgomery(data_t *input_key, data_t *exp_output) |
Valerio Setti | aed8799 | 2023-07-04 19:58:43 +0200 | [diff] [blame] | 155 | { |
| 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; |
| 172 | ASSERT_ALLOC(output_key, output_key_len); |
| 173 | /* 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 | |
| 178 | ASSERT_COMPARE(exp_output->x, exp_output->len, output_key, output_key_len); |
| 179 | |
| 180 | exit: |
| 181 | if (output_key != NULL) { |
| 182 | mbedtls_free(output_key); |
| 183 | } |
| 184 | mbedtls_pk_free(&pk); |
| 185 | USE_PSA_DONE(); |
| 186 | } |
| 187 | /* END_CASE */ |