blob: 339252811533734d9bcdab91189c7987cf1ce5fd [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 Setti5f1c8a72024-10-11 14:55:24 +020076 size_t buf_len, check_buf_len, wrong_buf_len = 1;
77 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. */
121 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &wrong_buf_len, is_public_key,
122 is_der), expected_result);
123 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
124 is_der), 0);
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. */
145 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &wrong_buf_len, is_public_key,
146 is_der), expected_result);
valeriof6853a82023-05-31 12:00:11 +0200147 TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
148 is_der), 0);
149
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100150 TEST_MEMORY_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
valeriof6853a82023-05-31 12:00:11 +0200151 }
152#endif /* MBEDTLS_USE_PSA_CRYPTO */
153
Valerio Setti89590952023-04-17 17:34:09 +0200154exit:
valeriof6853a82023-05-31 12:00:11 +0200155#if defined(MBEDTLS_USE_PSA_CRYPTO)
156 psa_destroy_key(opaque_id);
157#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti89590952023-04-17 17:34:09 +0200158 mbedtls_free(buf);
159 mbedtls_free(check_buf);
160 mbedtls_pk_free(&key);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200161 USE_PSA_DONE();
Valerio Setti89590952023-04-17 17:34:09 +0200162}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200163/* END_HEADER */
164
165/* BEGIN_DEPENDENCIES
Valerio Settic5d85e52023-07-26 18:12:23 +0200166 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200167 * END_DEPENDENCIES
168 */
169
Valerio Setti15cac172023-04-18 11:25:30 +0200170/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200171void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200172{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200173 pk_write_check_common(key_file, 1, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200174 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200175}
176/* END_CASE */
177
Valerio Setti15cac172023-04-18 11:25:30 +0200178/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200179void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200180{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200181 pk_write_check_common(key_file, 0, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200182 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200183}
184/* END_CASE */
Valerio Setti17513412023-04-26 14:48:43 +0200185
186/* BEGIN_CASE */
Valerio Settif5451712023-04-27 10:52:57 +0200187void pk_write_public_from_private(char *priv_key_file, char *pub_key_file)
Valerio Setti17513412023-04-26 14:48:43 +0200188{
Valerio Settif5451712023-04-27 10:52:57 +0200189 mbedtls_pk_context priv_key;
190 uint8_t *derived_key_raw = NULL;
Valerio Setti17513412023-04-26 14:48:43 +0200191 size_t derived_key_len = 0;
Valerio Settif5451712023-04-27 10:52:57 +0200192 uint8_t *pub_key_raw = NULL;
Valerio Setti17513412023-04-26 14:48:43 +0200193 size_t pub_key_len = 0;
194#if defined(MBEDTLS_USE_PSA_CRYPTO)
195 mbedtls_svc_key_id_t opaque_key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100196 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti17513412023-04-26 14:48:43 +0200197#endif /* MBEDTLS_USE_PSA_CRYPTO */
198
199 mbedtls_pk_init(&priv_key);
Valerio Setti17513412023-04-26 14:48:43 +0200200 USE_PSA_INIT();
201
Valerio Setti17513412023-04-26 14:48:43 +0200202 TEST_EQUAL(mbedtls_pk_parse_keyfile(&priv_key, priv_key_file, NULL,
203 mbedtls_test_rnd_std_rand, NULL), 0);
Valerio Settif5451712023-04-27 10:52:57 +0200204 TEST_EQUAL(mbedtls_pk_load_file(pub_key_file, &pub_key_raw,
205 &pub_key_len), 0);
Valerio Setti17513412023-04-26 14:48:43 +0200206
Valerio Settif5451712023-04-27 10:52:57 +0200207 derived_key_len = pub_key_len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100208 TEST_CALLOC(derived_key_raw, derived_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200209
Valerio Setti9a855f22023-04-27 12:07:23 +0200210 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
211 derived_key_len), pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200212
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100213 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100214 pub_key_raw, pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200215
216#if defined(MBEDTLS_USE_PSA_CRYPTO)
Tom Cosgrove351a3912023-09-01 09:54:04 +0100217 mbedtls_platform_zeroize(derived_key_raw, derived_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200218
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100219 /* Turn the priv_key PK context into an opaque one. */
220 TEST_EQUAL(mbedtls_pk_get_psa_attributes(&priv_key, PSA_KEY_USAGE_SIGN_HASH, &key_attr), 0);
221 TEST_EQUAL(mbedtls_pk_import_into_psa(&priv_key, &key_attr, &opaque_key_id), 0);
222 mbedtls_pk_free(&priv_key);
223 mbedtls_pk_init(&priv_key);
224 TEST_EQUAL(mbedtls_pk_setup_opaque(&priv_key, opaque_key_id), 0);
Valerio Setti17513412023-04-26 14:48:43 +0200225
Valerio Setti9a855f22023-04-27 12:07:23 +0200226 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
227 derived_key_len), pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200228
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100229 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100230 pub_key_raw, pub_key_len);
Valerio Setti17513412023-04-26 14:48:43 +0200231#endif /* MBEDTLS_USE_PSA_CRYPTO */
232
233exit:
234#if defined(MBEDTLS_USE_PSA_CRYPTO)
235 psa_destroy_key(opaque_key_id);
236#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Settif5451712023-04-27 10:52:57 +0200237 mbedtls_free(derived_key_raw);
238 mbedtls_free(pub_key_raw);
Valerio Setti17513412023-04-26 14:48:43 +0200239 mbedtls_pk_free(&priv_key);
240 USE_PSA_DONE();
241}
242/* END_CASE */