blob: 735c12547cc06c77ac9878d5b32aa99cb44c385d [file] [log] [blame]
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001/* BEGIN_HEADER */
Valerio Setti639d5672024-01-17 11:04:56 +01002#include "pk_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00003#include "mbedtls/pem.h"
4#include "mbedtls/oid.h"
Valerio Setti17513412023-04-26 14:48:43 +02005#include "psa/crypto_sizes.h"
Valerio Setti89590952023-04-17 17:34:09 +02006
Valerio Settic9cb5322023-04-18 11:20:36 +02007typedef enum {
8 TEST_PEM,
9 TEST_DER
10} pkwrite_file_format_t;
11
Valerio Setti547b3a42023-04-24 10:24:37 +020012/* Helper function for removing "\r" chars from a buffer. */
Valerio Setti7bacaf82023-04-24 08:52:16 +020013static void fix_new_lines(unsigned char *in_str, size_t *len)
14{
15 size_t chars_left;
16 unsigned int i;
17
18 for (i = 0; (i < *len) && (*len > 0); i++) {
19 if (in_str[i] == '\r') {
20 if (i < (*len - 1)) {
21 chars_left = *len - i - 1;
Valerio Settibf974b92023-04-24 10:26:24 +020022 memmove(&in_str[i], &in_str[i+1], chars_left);
Valerio Setti7bacaf82023-04-24 08:52:16 +020023 } else {
24 in_str[i] = '\0';
25 }
26 *len = *len - 1;
27 }
28 }
29}
30
valeriof6853a82023-05-31 12:00:11 +020031static int pk_write_any_key(mbedtls_pk_context *pk, unsigned char **p,
32 size_t *buf_len, int is_public_key, int is_der)
33{
34 int ret = 0;
35
36 if (is_der) {
37 if (is_public_key) {
38 ret = mbedtls_pk_write_pubkey_der(pk, *p, *buf_len);
39 } else {
40 ret = mbedtls_pk_write_key_der(pk, *p, *buf_len);
41 }
42 if (ret <= 0) {
43 return ret;
44 }
45
46 *p = *p + *buf_len - ret;
47 *buf_len = ret;
48 } else {
49#if defined(MBEDTLS_PEM_WRITE_C)
50 if (is_public_key) {
51 ret = mbedtls_pk_write_pubkey_pem(pk, *p, *buf_len);
52 } else {
53 ret = mbedtls_pk_write_key_pem(pk, *p, *buf_len);
54 }
55 if (ret != 0) {
56 return ret;
57 }
58
59 *buf_len = strlen((char *) *p) + 1; /* +1 takes the string terminator into account */
60#else
61 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
62#endif
63 }
64
65 return 0;
66}
67
Valerio Settic60bc5e2023-04-17 18:43:06 +020068static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
Valerio Setti89590952023-04-17 17:34:09 +020069{
70 mbedtls_pk_context key;
Gilles Peskine21e46b32023-10-17 16:35:20 +020071 mbedtls_pk_init(&key);
Valerio Setti89590952023-04-17 17:34:09 +020072 unsigned char *buf = NULL;
73 unsigned char *check_buf = NULL;
Valerio Settic60bc5e2023-04-17 18:43:06 +020074 unsigned char *start_buf;
Valerio Setti3401b302023-04-18 10:42:53 +020075 size_t buf_len, check_buf_len;
valeriof6853a82023-05-31 12:00:11 +020076#if defined(MBEDTLS_USE_PSA_CRYPTO)
77 mbedtls_svc_key_id_t opaque_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +010078 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
valeriof6853a82023-05-31 12:00:11 +020079#endif /* MBEDTLS_USE_PSA_CRYPTO */
80
81 USE_PSA_INIT();
Valerio Setti89590952023-04-17 17:34:09 +020082
83 /* Note: if mbedtls_pk_load_file() successfully reads the file, then
84 it also allocates check_buf, which should be freed on exit */
Valerio Setti3401b302023-04-18 10:42:53 +020085 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
Valerio Setti89590952023-04-17 17:34:09 +020086 TEST_ASSERT(check_buf_len > 0);
87
Valerio Setti7bacaf82023-04-24 08:52:16 +020088 /* Windows' line ending is different from the Linux's one ("\r\n" vs "\n").
89 * Git treats PEM files as text, so when on Windows, it replaces new lines
90 * with "\r\n" on checkout.
91 * Unfortunately mbedtls_pk_load_file() loads files in binary format,
92 * while mbedtls_pk_write_pubkey_pem() goes through the I/O layer which
93 * uses "\n" for newlines in both Windows and Linux.
94 * Here we remove the extra "\r" so that "buf" and "check_buf" can be
95 * easily compared later. */
96 if (!is_der) {
97 fix_new_lines(check_buf, &check_buf_len);
98 }
99 TEST_ASSERT(check_buf_len > 0);
100
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100101 TEST_CALLOC(buf, check_buf_len);
Valerio Setti89590952023-04-17 17:34:09 +0200102
Valerio Setti89590952023-04-17 17:34:09 +0200103 if (is_public_key) {
Valerio Setti3401b302023-04-18 10:42:53 +0200104 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
Valerio Setti89590952023-04-17 17:34:09 +0200105 } else {
Valerio Setti3401b302023-04-18 10:42:53 +0200106 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
Valerio Setti22808952023-04-18 12:57:52 +0200107 mbedtls_test_rnd_std_rand, NULL), 0);
Valerio Setti89590952023-04-17 17:34:09 +0200108 }
Valerio Setti89590952023-04-17 17:34:09 +0200109
valeriof6853a82023-05-31 12:00:11 +0200110 start_buf = buf;
111 buf_len = check_buf_len;
112 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
113 is_der), 0);
Valerio Settic60bc5e2023-04-17 18:43:06 +0200114
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100115 TEST_MEMORY_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
Valerio Setti89590952023-04-17 17:34:09 +0200116
valeriof6853a82023-05-31 12:00:11 +0200117#if defined(MBEDTLS_USE_PSA_CRYPTO)
118 /* Verify that pk_write works also for opaque private keys */
119 if (!is_public_key) {
120 memset(buf, 0, check_buf_len);
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100121 /* Turn the key PK context into an opaque one.
122 * Note: set some practical usage for the key to make get_psa_attributes() happy. */
123 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&key, PSA_KEY_USAGE_SIGN_MESSAGE, &key_attr), 0);
124 TEST_EQUAL(mbedtls_pk_import_into_psa(&key, &key_attr, &opaque_id), 0);
125 mbedtls_pk_free(&key);
126 mbedtls_pk_init(&key);
127 TEST_EQUAL(mbedtls_pk_setup_opaque(&key, opaque_id), 0);
valeriof6853a82023-05-31 12:00:11 +0200128 start_buf = buf;
129 buf_len = check_buf_len;
130 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
131 is_der), 0);
132
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100133 TEST_MEMORY_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
valeriof6853a82023-05-31 12:00:11 +0200134 }
135#endif /* MBEDTLS_USE_PSA_CRYPTO */
136
Valerio Setti89590952023-04-17 17:34:09 +0200137exit:
valeriof6853a82023-05-31 12:00:11 +0200138#if defined(MBEDTLS_USE_PSA_CRYPTO)
139 psa_destroy_key(opaque_id);
140#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti89590952023-04-17 17:34:09 +0200141 mbedtls_free(buf);
142 mbedtls_free(check_buf);
143 mbedtls_pk_free(&key);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200144 USE_PSA_DONE();
Valerio Setti89590952023-04-17 17:34:09 +0200145}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200146/* END_HEADER */
147
148/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +0200149 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200150 * END_DEPENDENCIES
151 */
152
Valerio Setti15cac172023-04-18 11:25:30 +0200153/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200154void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200155{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200156 pk_write_check_common(key_file, 1, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200157 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200158}
159/* END_CASE */
160
Valerio Setti15cac172023-04-18 11:25:30 +0200161/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200162void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200163{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200164 pk_write_check_common(key_file, 0, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200165 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200166}
167/* END_CASE */
Valerio Setti17513412023-04-26 14:48:43 +0200168
169/* BEGIN_CASE */
Valerio Settif5451712023-04-27 10:52:57 +0200170void pk_write_public_from_private(char *priv_key_file, char *pub_key_file)
Valerio Setti17513412023-04-26 14:48:43 +0200171{
Valerio Settif5451712023-04-27 10:52:57 +0200172 mbedtls_pk_context priv_key;
173 uint8_t *derived_key_raw = NULL;
Valerio Setti17513412023-04-26 14:48:43 +0200174 size_t derived_key_len = 0;
Valerio Settif5451712023-04-27 10:52:57 +0200175 uint8_t *pub_key_raw = NULL;
Valerio Setti17513412023-04-26 14:48:43 +0200176 size_t pub_key_len = 0;
177#if defined(MBEDTLS_USE_PSA_CRYPTO)
178 mbedtls_svc_key_id_t opaque_key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100179 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti17513412023-04-26 14:48:43 +0200180#endif /* MBEDTLS_USE_PSA_CRYPTO */
181
182 mbedtls_pk_init(&priv_key);
Valerio Setti17513412023-04-26 14:48:43 +0200183 USE_PSA_INIT();
184
Valerio Setti17513412023-04-26 14:48:43 +0200185 TEST_EQUAL(mbedtls_pk_parse_keyfile(&priv_key, priv_key_file, NULL,
186 mbedtls_test_rnd_std_rand, NULL), 0);
Valerio Settif5451712023-04-27 10:52:57 +0200187 TEST_EQUAL(mbedtls_pk_load_file(pub_key_file, &pub_key_raw,
188 &pub_key_len), 0);
Valerio Setti17513412023-04-26 14:48:43 +0200189
Valerio Settif5451712023-04-27 10:52:57 +0200190 derived_key_len = pub_key_len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100191 TEST_CALLOC(derived_key_raw, derived_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200192
Valerio Setti9a855f22023-04-27 12:07:23 +0200193 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
194 derived_key_len), pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200195
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100196 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100197 pub_key_raw, pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200198
199#if defined(MBEDTLS_USE_PSA_CRYPTO)
Tom Cosgrove351a3912023-09-01 09:54:04 +0100200 mbedtls_platform_zeroize(derived_key_raw, derived_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200201
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100202 /* Turn the priv_key PK context into an opaque one. */
203 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&priv_key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
204 TEST_EQUAL(mbedtls_pk_import_into_psa(&priv_key, &key_attr, &opaque_key_id), 0);
205 mbedtls_pk_free(&priv_key);
206 mbedtls_pk_init(&priv_key);
207 TEST_EQUAL(mbedtls_pk_setup_opaque(&priv_key, opaque_key_id), 0);
Valerio Setti17513412023-04-26 14:48:43 +0200208
Valerio Setti9a855f22023-04-27 12:07:23 +0200209 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
210 derived_key_len), pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200211
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100212 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100213 pub_key_raw, pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200214#endif /* MBEDTLS_USE_PSA_CRYPTO */
215
216exit:
217#if defined(MBEDTLS_USE_PSA_CRYPTO)
218 psa_destroy_key(opaque_key_id);
219#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Settif5451712023-04-27 10:52:57 +0200220 mbedtls_free(derived_key_raw);
221 mbedtls_free(pub_key_raw);
Valerio Setti17513412023-04-26 14:48:43 +0200222 mbedtls_pk_free(&priv_key);
223 USE_PSA_DONE();
224}
225/* END_CASE */