blob: 97fd92a5bd0b1243ae275a01d3cdd58fba2721ef [file] [log] [blame]
Paul Bakkerc7bb02b2013-09-15 14:54:56 +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 Setti7406e962023-04-26 14:48:43 +02005#include "psa/crypto_sizes.h"
Valerio Settif1477da2023-04-18 16:37:30 +02006
7typedef enum {
8 TEST_PEM,
9 TEST_DER
10} pkwrite_file_format_t;
11
Valerio Setti755582b2023-04-24 10:24:37 +020012/* Helper function for removing "\r" chars from a buffer. */
Valerio Setti3b608de2023-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 Settia4d460c2023-04-24 10:26:24 +020022 memmove(&in_str[i], &in_str[i+1], chars_left);
Valerio Setti3b608de2023-04-24 08:52:16 +020023 } else {
24 in_str[i] = '\0';
25 }
26 *len = *len - 1;
27 }
28 }
29}
30
Valerio Settif1477da2023-04-18 16:37:30 +020031static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
32{
33 mbedtls_pk_context key;
Gilles Peskine11f41792023-10-17 16:35:20 +020034 mbedtls_pk_init(&key);
Valerio Settif1477da2023-04-18 16:37:30 +020035 unsigned char *buf = NULL;
36 unsigned char *check_buf = NULL;
37 unsigned char *start_buf;
38 size_t buf_len, check_buf_len;
39 int ret;
40
Valerio Settid64e2492023-04-24 13:53:21 +020041 USE_PSA_INIT();
42
Valerio Settif1477da2023-04-18 16:37:30 +020043 /* Note: if mbedtls_pk_load_file() successfully reads the file, then
44 it also allocates check_buf, which should be freed on exit */
45 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
46 TEST_ASSERT(check_buf_len > 0);
47
Valerio Setti3b608de2023-04-24 08:52:16 +020048 /* Windows' line ending is different from the Linux's one ("\r\n" vs "\n").
49 * Git treats PEM files as text, so when on Windows, it replaces new lines
50 * with "\r\n" on checkout.
51 * Unfortunately mbedtls_pk_load_file() loads files in binary format,
52 * while mbedtls_pk_write_pubkey_pem() goes through the I/O layer which
53 * uses "\n" for newlines in both Windows and Linux.
54 * Here we remove the extra "\r" so that "buf" and "check_buf" can be
55 * easily compared later. */
56 if (!is_der) {
57 fix_new_lines(check_buf, &check_buf_len);
58 }
59 TEST_ASSERT(check_buf_len > 0);
60
Tom Cosgrove30ceb232023-09-04 11:20:19 +010061 TEST_CALLOC(buf, check_buf_len);
Valerio Settif1477da2023-04-18 16:37:30 +020062
Valerio Settif1477da2023-04-18 16:37:30 +020063 if (is_public_key) {
64 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
65 if (is_der) {
66 ret = mbedtls_pk_write_pubkey_der(&key, buf, check_buf_len);
67 } else {
68#if defined(MBEDTLS_PEM_WRITE_C)
69 ret = mbedtls_pk_write_pubkey_pem(&key, buf, check_buf_len);
70#else
71 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
72#endif
73 }
74 } else {
75 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL), 0);
76 if (is_der) {
77 ret = mbedtls_pk_write_key_der(&key, buf, check_buf_len);
78 } else {
79#if defined(MBEDTLS_PEM_WRITE_C)
80 ret = mbedtls_pk_write_key_pem(&key, buf, check_buf_len);
81#else
82 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
83#endif
84 }
85 }
86
87 if (is_der) {
88 TEST_LE_U(1, ret);
89 buf_len = ret;
90 start_buf = buf + check_buf_len - buf_len;
91 } else {
92 TEST_EQUAL(ret, 0);
93 buf_len = strlen((char *) buf) + 1; /* +1 takes the string terminator into account */
94 start_buf = buf;
95 }
96
Tom Cosgroveba3b14d2023-09-04 11:23:02 +010097 TEST_MEMORY_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
Valerio Settif1477da2023-04-18 16:37:30 +020098
99exit:
100 mbedtls_free(buf);
101 mbedtls_free(check_buf);
102 mbedtls_pk_free(&key);
Valerio Settid64e2492023-04-24 13:53:21 +0200103 USE_PSA_DONE();
Valerio Settif1477da2023-04-18 16:37:30 +0200104}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105/* END_HEADER */
106
107/* BEGIN_DEPENDENCIES
Hanno Becker19d858e2018-10-16 13:46:25 +0100108 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200109 * END_DEPENDENCIES
110 */
111
Valerio Settif1477da2023-04-18 16:37:30 +0200112/* BEGIN_CASE */
113void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200114{
Valerio Settif1477da2023-04-18 16:37:30 +0200115 pk_write_check_common(key_file, 1, is_der);
116 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200117}
118/* END_CASE */
119
Valerio Settif1477da2023-04-18 16:37:30 +0200120/* BEGIN_CASE */
121void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200122{
Valerio Settif1477da2023-04-18 16:37:30 +0200123 pk_write_check_common(key_file, 0, is_der);
124 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200125}
126/* END_CASE */
Valerio Setti7406e962023-04-26 14:48:43 +0200127
128/* BEGIN_CASE */
Valerio Setti3f8bf062023-04-27 10:52:57 +0200129void pk_write_public_from_private(char *priv_key_file, char *pub_key_file)
Valerio Setti7406e962023-04-26 14:48:43 +0200130{
Valerio Setti3f8bf062023-04-27 10:52:57 +0200131 mbedtls_pk_context priv_key;
132 uint8_t *derived_key_raw = NULL;
Valerio Setti7406e962023-04-26 14:48:43 +0200133 size_t derived_key_len = 0;
Valerio Setti3f8bf062023-04-27 10:52:57 +0200134 uint8_t *pub_key_raw = NULL;
Valerio Setti7406e962023-04-26 14:48:43 +0200135 size_t pub_key_len = 0;
136#if defined(MBEDTLS_USE_PSA_CRYPTO)
137 mbedtls_svc_key_id_t opaque_key_id = MBEDTLS_SVC_KEY_ID_INIT;
138#endif /* MBEDTLS_USE_PSA_CRYPTO */
139
140 mbedtls_pk_init(&priv_key);
Valerio Setti7406e962023-04-26 14:48:43 +0200141 USE_PSA_INIT();
142
Valerio Setti0eace412023-05-02 16:38:57 +0200143 TEST_EQUAL(mbedtls_pk_parse_keyfile(&priv_key, priv_key_file, NULL), 0);
Valerio Setti3f8bf062023-04-27 10:52:57 +0200144 TEST_EQUAL(mbedtls_pk_load_file(pub_key_file, &pub_key_raw,
145 &pub_key_len), 0);
Valerio Setti7406e962023-04-26 14:48:43 +0200146
Valerio Setti3f8bf062023-04-27 10:52:57 +0200147 derived_key_len = pub_key_len;
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100148 TEST_CALLOC(derived_key_raw, derived_key_len);
Valerio Setti7406e962023-04-26 14:48:43 +0200149
Valerio Setti974b8162023-04-27 12:07:23 +0200150 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
151 derived_key_len), pub_key_len);
Valerio Setti7406e962023-04-26 14:48:43 +0200152
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100153 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +0100154 pub_key_raw, pub_key_len);
Valerio Setti7406e962023-04-26 14:48:43 +0200155
156#if defined(MBEDTLS_USE_PSA_CRYPTO)
Tom Cosgrovef7829b02023-09-01 09:54:04 +0100157 mbedtls_platform_zeroize(derived_key_raw, derived_key_len);
Valerio Setti7406e962023-04-26 14:48:43 +0200158
159 TEST_EQUAL(mbedtls_pk_wrap_as_opaque(&priv_key, &opaque_key_id,
Valerio Setti7406e962023-04-26 14:48:43 +0200160 PSA_ALG_NONE), 0);
161
Valerio Setti974b8162023-04-27 12:07:23 +0200162 TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
163 derived_key_len), pub_key_len);
Valerio Setti7406e962023-04-26 14:48:43 +0200164
Tom Cosgroveba3b14d2023-09-04 11:23:02 +0100165 TEST_MEMORY_COMPARE(derived_key_raw, derived_key_len,
Tom Cosgrovea240fe32023-09-04 11:29:39 +0100166 pub_key_raw, pub_key_len);
Valerio Setti7406e962023-04-26 14:48:43 +0200167#endif /* MBEDTLS_USE_PSA_CRYPTO */
168
169exit:
170#if defined(MBEDTLS_USE_PSA_CRYPTO)
171 psa_destroy_key(opaque_key_id);
172#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti3f8bf062023-04-27 10:52:57 +0200173 mbedtls_free(derived_key_raw);
174 mbedtls_free(pub_key_raw);
Valerio Setti7406e962023-04-26 14:48:43 +0200175 mbedtls_pk_free(&priv_key);
176 USE_PSA_DONE();
177}
178/* END_CASE */