blob: 491bc489aa7914c3b0161593fa7e21e84c3d7632 [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 Setti5f1c8a72024-10-11 14:55:24 +02005#include "mbedtls/base64.h"
Valerio Setti17513412023-04-26 14:48:43 +02006#include "psa/crypto_sizes.h"
Valerio Setti89590952023-04-17 17:34:09 +02007
Valerio Settic9cb5322023-04-18 11:20:36 +02008typedef enum {
9 TEST_PEM,
10 TEST_DER
11} pkwrite_file_format_t;
12
Valerio Setti547b3a42023-04-24 10:24:37 +020013/* Helper function for removing "\r" chars from a buffer. */
Valerio Setti7bacaf82023-04-24 08:52:16 +020014static void fix_new_lines(unsigned char *in_str, size_t *len)
15{
16 size_t chars_left;
17 unsigned int i;
18
19 for (i = 0; (i < *len) && (*len > 0); i++) {
20 if (in_str[i] == '\r') {
21 if (i < (*len - 1)) {
22 chars_left = *len - i - 1;
Valerio Settibf974b92023-04-24 10:26:24 +020023 memmove(&in_str[i], &in_str[i+1], chars_left);
Valerio Setti7bacaf82023-04-24 08:52:16 +020024 } else {
25 in_str[i] = '\0';
26 }
27 *len = *len - 1;
28 }
29 }
30}
31
valeriof6853a82023-05-31 12:00:11 +020032static int pk_write_any_key(mbedtls_pk_context *pk, unsigned char **p,
33 size_t *buf_len, int is_public_key, int is_der)
34{
35 int ret = 0;
36
37 if (is_der) {
38 if (is_public_key) {
39 ret = mbedtls_pk_write_pubkey_der(pk, *p, *buf_len);
40 } else {
41 ret = mbedtls_pk_write_key_der(pk, *p, *buf_len);
42 }
43 if (ret <= 0) {
44 return ret;
45 }
46
47 *p = *p + *buf_len - ret;
48 *buf_len = ret;
49 } else {
50#if defined(MBEDTLS_PEM_WRITE_C)
51 if (is_public_key) {
52 ret = mbedtls_pk_write_pubkey_pem(pk, *p, *buf_len);
53 } else {
54 ret = mbedtls_pk_write_key_pem(pk, *p, *buf_len);
55 }
56 if (ret != 0) {
57 return ret;
58 }
59
60 *buf_len = strlen((char *) *p) + 1; /* +1 takes the string terminator into account */
61#else
62 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
63#endif
64 }
65
66 return 0;
67}
68
Valerio Settic60bc5e2023-04-17 18:43:06 +020069static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
Valerio Setti89590952023-04-17 17:34:09 +020070{
71 mbedtls_pk_context key;
Gilles Peskine21e46b32023-10-17 16:35:20 +020072 mbedtls_pk_init(&key);
Valerio Setti89590952023-04-17 17:34:09 +020073 unsigned char *buf = NULL;
74 unsigned char *check_buf = NULL;
Valerio Settic60bc5e2023-04-17 18:43:06 +020075 unsigned char *start_buf;
Valerio Setti3b9f4be2024-10-14 09:44:06 +020076 size_t buf_len, check_buf_len;
Valerio Setti5f1c8a72024-10-11 14:55:24 +020077 int expected_result;
valeriof6853a82023-05-31 12:00:11 +020078#if defined(MBEDTLS_USE_PSA_CRYPTO)
79 mbedtls_svc_key_id_t opaque_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +010080 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
valeriof6853a82023-05-31 12:00:11 +020081#endif /* MBEDTLS_USE_PSA_CRYPTO */
82
83 USE_PSA_INIT();
Valerio Setti89590952023-04-17 17:34:09 +020084
85 /* Note: if mbedtls_pk_load_file() successfully reads the file, then
86 it also allocates check_buf, which should be freed on exit */
Valerio Setti3401b302023-04-18 10:42:53 +020087 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
Valerio Setti89590952023-04-17 17:34:09 +020088 TEST_ASSERT(check_buf_len > 0);
89
Valerio Setti7bacaf82023-04-24 08:52:16 +020090 /* Windows' line ending is different from the Linux's one ("\r\n" vs "\n").
91 * Git treats PEM files as text, so when on Windows, it replaces new lines
92 * with "\r\n" on checkout.
93 * Unfortunately mbedtls_pk_load_file() loads files in binary format,
94 * while mbedtls_pk_write_pubkey_pem() goes through the I/O layer which
95 * uses "\n" for newlines in both Windows and Linux.
96 * Here we remove the extra "\r" so that "buf" and "check_buf" can be
97 * easily compared later. */
98 if (!is_der) {
99 fix_new_lines(check_buf, &check_buf_len);
100 }
101 TEST_ASSERT(check_buf_len > 0);
102
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100103 TEST_CALLOC(buf, check_buf_len);
Valerio Setti89590952023-04-17 17:34:09 +0200104
Valerio Setti89590952023-04-17 17:34:09 +0200105 if (is_public_key) {
Valerio Setti3401b302023-04-18 10:42:53 +0200106 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
Valerio Setti89590952023-04-17 17:34:09 +0200107 } else {
Valerio Setti3401b302023-04-18 10:42:53 +0200108 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
Valerio Setti22808952023-04-18 12:57:52 +0200109 mbedtls_test_rnd_std_rand, NULL), 0);
Valerio Setti89590952023-04-17 17:34:09 +0200110 }
Valerio Setti89590952023-04-17 17:34:09 +0200111
valeriof6853a82023-05-31 12:00:11 +0200112 start_buf = buf;
113 buf_len = check_buf_len;
Valerio Setti5f1c8a72024-10-11 14:55:24 +0200114 if (is_der) {
115 expected_result = MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
116 } else {
117 expected_result = MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
118 }
119 /* Intentionally pass a wrong size for the provided output buffer and check
120 * that the writing functions fails as expected. */
Valerio Setti3b9f4be2024-10-14 09:44:06 +0200121 for (size_t i = 1; i < buf_len; i++) {
122 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &i, is_public_key,
123 is_der), expected_result);
124 }
valeriof6853a82023-05-31 12:00:11 +0200125 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
126 is_der), 0);
Valerio Settic60bc5e2023-04-17 18:43:06 +0200127
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100128 TEST_MEMORY_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
Valerio Setti89590952023-04-17 17:34:09 +0200129
valeriof6853a82023-05-31 12:00:11 +0200130#if defined(MBEDTLS_USE_PSA_CRYPTO)
131 /* Verify that pk_write works also for opaque private keys */
132 if (!is_public_key) {
133 memset(buf, 0, check_buf_len);
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100134 /* Turn the key PK context into an opaque one.
135 * Note: set some practical usage for the key to make get_psa_attributes() happy. */
136 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&key, PSA_KEY_USAGE_SIGN_MESSAGE, &key_attr), 0);
137 TEST_EQUAL(mbedtls_pk_import_into_psa(&key, &key_attr, &opaque_id), 0);
138 mbedtls_pk_free(&key);
139 mbedtls_pk_init(&key);
140 TEST_EQUAL(mbedtls_pk_setup_opaque(&key, opaque_id), 0);
valeriof6853a82023-05-31 12:00:11 +0200141 start_buf = buf;
142 buf_len = check_buf_len;
Valerio Setti5f1c8a72024-10-11 14:55:24 +0200143 /* Intentionally pass a wrong size for the provided output buffer and check
144 * that the writing functions fails as expected. */
Valerio Setti3b9f4be2024-10-14 09:44:06 +0200145 for (size_t i = 1; i < buf_len; i++) {
146 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &i, is_public_key,
147 is_der), expected_result);
148 }
valeriof6853a82023-05-31 12:00:11 +0200149 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
150 is_der), 0);
151
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100152 TEST_MEMORY_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
valeriof6853a82023-05-31 12:00:11 +0200153 }
154#endif /* MBEDTLS_USE_PSA_CRYPTO */
155
Valerio Setti89590952023-04-17 17:34:09 +0200156exit:
valeriof6853a82023-05-31 12:00:11 +0200157#if defined(MBEDTLS_USE_PSA_CRYPTO)
158 psa_destroy_key(opaque_id);
159#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti89590952023-04-17 17:34:09 +0200160 mbedtls_free(buf);
161 mbedtls_free(check_buf);
162 mbedtls_pk_free(&key);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200163 USE_PSA_DONE();
Valerio Setti89590952023-04-17 17:34:09 +0200164}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200165/* END_HEADER */
166
167/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +0200168 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200169 * END_DEPENDENCIES
170 */
171
Valerio Setti15cac172023-04-18 11:25:30 +0200172/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200173void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200174{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200175 pk_write_check_common(key_file, 1, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200176 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200177}
178/* END_CASE */
179
Valerio Setti15cac172023-04-18 11:25:30 +0200180/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200181void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200183 pk_write_check_common(key_file, 0, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200184 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200185}
186/* END_CASE */
Valerio Setti17513412023-04-26 14:48:43 +0200187
188/* BEGIN_CASE */
Valerio Settif5451712023-04-27 10:52:57 +0200189void pk_write_public_from_private(char *priv_key_file, char *pub_key_file)
Valerio Setti17513412023-04-26 14:48:43 +0200190{
Valerio Settif5451712023-04-27 10:52:57 +0200191 mbedtls_pk_context priv_key;
192 uint8_t *derived_key_raw = NULL;
Valerio Setti17513412023-04-26 14:48:43 +0200193 size_t derived_key_len = 0;
Valerio Settif5451712023-04-27 10:52:57 +0200194 uint8_t *pub_key_raw = NULL;
Valerio Setti17513412023-04-26 14:48:43 +0200195 size_t pub_key_len = 0;
196#if defined(MBEDTLS_USE_PSA_CRYPTO)
197 mbedtls_svc_key_id_t opaque_key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100198 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti17513412023-04-26 14:48:43 +0200199#endif /* MBEDTLS_USE_PSA_CRYPTO */
200
201 mbedtls_pk_init(&priv_key);
Valerio Setti17513412023-04-26 14:48:43 +0200202 USE_PSA_INIT();
203
Valerio Setti17513412023-04-26 14:48:43 +0200204 TEST_EQUAL(mbedtls_pk_parse_keyfile(&priv_key, priv_key_file, NULL,
205 mbedtls_test_rnd_std_rand, NULL), 0);
Valerio Settif5451712023-04-27 10:52:57 +0200206 TEST_EQUAL(mbedtls_pk_load_file(pub_key_file, &pub_key_raw,
207 &pub_key_len), 0);
Valerio Setti17513412023-04-26 14:48:43 +0200208
Valerio Settif5451712023-04-27 10:52:57 +0200209 derived_key_len = pub_key_len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100210 TEST_CALLOC(derived_key_raw, derived_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200211
Valerio Setti9a855f22023-04-27 12:07:23 +0200212 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
213 derived_key_len), pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200214
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100215 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100216 pub_key_raw, pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200217
218#if defined(MBEDTLS_USE_PSA_CRYPTO)
Tom Cosgrove351a3912023-09-01 09:54:04 +0100219 mbedtls_platform_zeroize(derived_key_raw, derived_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200220
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100221 /* Turn the priv_key PK context into an opaque one. */
222 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&priv_key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
223 TEST_EQUAL(mbedtls_pk_import_into_psa(&priv_key, &key_attr, &opaque_key_id), 0);
224 mbedtls_pk_free(&priv_key);
225 mbedtls_pk_init(&priv_key);
226 TEST_EQUAL(mbedtls_pk_setup_opaque(&priv_key, opaque_key_id), 0);
Valerio Setti17513412023-04-26 14:48:43 +0200227
Valerio Setti9a855f22023-04-27 12:07:23 +0200228 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
229 derived_key_len), pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200230
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100231 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100232 pub_key_raw, pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200233#endif /* MBEDTLS_USE_PSA_CRYPTO */
234
235exit:
236#if defined(MBEDTLS_USE_PSA_CRYPTO)
237 psa_destroy_key(opaque_key_id);
238#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Settif5451712023-04-27 10:52:57 +0200239 mbedtls_free(derived_key_raw);
240 mbedtls_free(pub_key_raw);
Valerio Setti17513412023-04-26 14:48:43 +0200241 mbedtls_pk_free(&priv_key);
242 USE_PSA_DONE();
243}
244/* END_CASE */