blob: c0c5ad0b65c3dddd06341ef8fce57097507e9bbb [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 Setti89590952023-04-17 17:34:09 +02005
Valerio Settic9cb5322023-04-18 11:20:36 +02006typedef enum {
7 TEST_PEM,
8 TEST_DER
9} pkwrite_file_format_t;
10
Valerio Setti547b3a42023-04-24 10:24:37 +020011/* Helper function for removing "\r" chars from a buffer. */
Valerio Setti7bacaf82023-04-24 08:52:16 +020012static void fix_new_lines(unsigned char *in_str, size_t *len)
13{
14 size_t chars_left;
15 unsigned int i;
16
17 for (i = 0; (i < *len) && (*len > 0); i++) {
18 if (in_str[i] == '\r') {
19 if (i < (*len - 1)) {
20 chars_left = *len - i - 1;
Valerio Settibf974b92023-04-24 10:26:24 +020021 memmove(&in_str[i], &in_str[i+1], chars_left);
Valerio Setti7bacaf82023-04-24 08:52:16 +020022 } else {
23 in_str[i] = '\0';
24 }
25 *len = *len - 1;
26 }
27 }
28}
29
Valerio Settic60bc5e2023-04-17 18:43:06 +020030static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
Valerio Setti89590952023-04-17 17:34:09 +020031{
32 mbedtls_pk_context key;
33 unsigned char *buf = NULL;
34 unsigned char *check_buf = NULL;
Valerio Settic60bc5e2023-04-17 18:43:06 +020035 unsigned char *start_buf;
Valerio Setti3401b302023-04-18 10:42:53 +020036 size_t buf_len, check_buf_len;
Valerio Setti89590952023-04-17 17:34:09 +020037 int ret;
38
39 /* Note: if mbedtls_pk_load_file() successfully reads the file, then
40 it also allocates check_buf, which should be freed on exit */
Valerio Setti3401b302023-04-18 10:42:53 +020041 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
Valerio Setti89590952023-04-17 17:34:09 +020042 TEST_ASSERT(check_buf_len > 0);
43
Valerio Setti7bacaf82023-04-24 08:52:16 +020044 /* Windows' line ending is different from the Linux's one ("\r\n" vs "\n").
45 * Git treats PEM files as text, so when on Windows, it replaces new lines
46 * with "\r\n" on checkout.
47 * Unfortunately mbedtls_pk_load_file() loads files in binary format,
48 * while mbedtls_pk_write_pubkey_pem() goes through the I/O layer which
49 * uses "\n" for newlines in both Windows and Linux.
50 * Here we remove the extra "\r" so that "buf" and "check_buf" can be
51 * easily compared later. */
52 if (!is_der) {
53 fix_new_lines(check_buf, &check_buf_len);
54 }
55 TEST_ASSERT(check_buf_len > 0);
56
Valerio Setti89590952023-04-17 17:34:09 +020057 ASSERT_ALLOC(buf, check_buf_len);
58
59 mbedtls_pk_init(&key);
60 if (is_public_key) {
Valerio Setti3401b302023-04-18 10:42:53 +020061 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
Valerio Settic60bc5e2023-04-17 18:43:06 +020062 if (is_der) {
63 ret = mbedtls_pk_write_pubkey_der(&key, buf, check_buf_len);
64 } else {
Valerio Setti15cac172023-04-18 11:25:30 +020065#if defined(MBEDTLS_PEM_WRITE_C)
Valerio Settic60bc5e2023-04-17 18:43:06 +020066 ret = mbedtls_pk_write_pubkey_pem(&key, buf, check_buf_len);
Valerio Setti15cac172023-04-18 11:25:30 +020067#else
68 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
69#endif
Valerio Settic60bc5e2023-04-17 18:43:06 +020070 }
Valerio Setti89590952023-04-17 17:34:09 +020071 } else {
Valerio Setti3401b302023-04-18 10:42:53 +020072 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
Valerio Setti22808952023-04-18 12:57:52 +020073 mbedtls_test_rnd_std_rand, NULL), 0);
Valerio Settic60bc5e2023-04-17 18:43:06 +020074 if (is_der) {
75 ret = mbedtls_pk_write_key_der(&key, buf, check_buf_len);
76 } else {
Valerio Setti15cac172023-04-18 11:25:30 +020077#if defined(MBEDTLS_PEM_WRITE_C)
Valerio Settic60bc5e2023-04-17 18:43:06 +020078 ret = mbedtls_pk_write_key_pem(&key, buf, check_buf_len);
Valerio Setti15cac172023-04-18 11:25:30 +020079#else
80 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
81#endif
Valerio Settic60bc5e2023-04-17 18:43:06 +020082 }
Valerio Setti89590952023-04-17 17:34:09 +020083 }
Valerio Setti89590952023-04-17 17:34:09 +020084
Valerio Settic60bc5e2023-04-17 18:43:06 +020085 if (is_der) {
Valerio Setti3401b302023-04-18 10:42:53 +020086 TEST_LE_U(1, ret);
87 buf_len = ret;
88 start_buf = buf + check_buf_len - buf_len;
Valerio Settic60bc5e2023-04-17 18:43:06 +020089 } else {
Valerio Setti3401b302023-04-18 10:42:53 +020090 TEST_EQUAL(ret, 0);
Valerio Setti22808952023-04-18 12:57:52 +020091 buf_len = strlen((char *) buf) + 1; /* +1 takes the string terminator into account */
Valerio Settic60bc5e2023-04-17 18:43:06 +020092 start_buf = buf;
93 }
94
Valerio Setti3401b302023-04-18 10:42:53 +020095 ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
Valerio Setti89590952023-04-17 17:34:09 +020096
97exit:
98 mbedtls_free(buf);
99 mbedtls_free(check_buf);
100 mbedtls_pk_free(&key);
101}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200102/* END_HEADER */
103
104/* BEGIN_DEPENDENCIES
Hanno Becker19d858e2018-10-16 13:46:25 +0100105 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200106 * END_DEPENDENCIES
107 */
108
Valerio Setti15cac172023-04-18 11:25:30 +0200109/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200110void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200111{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200112 pk_write_check_common(key_file, 1, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200113 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200114}
115/* END_CASE */
116
Valerio Setti15cac172023-04-18 11:25:30 +0200117/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200118void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200119{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200120 pk_write_check_common(key_file, 0, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200121 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200122}
123/* END_CASE */