blob: 5e1856a9bac058d28c21aa58c44a03dc91370740 [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 Settif1477da2023-04-18 16:37:30 +02005
6typedef enum {
7 TEST_PEM,
8 TEST_DER
9} pkwrite_file_format_t;
10
Valerio Setti755582b2023-04-24 10:24:37 +020011/* Helper function for removing "\r" chars from a buffer. */
Valerio Setti3b608de2023-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;
21 memcpy(&in_str[i], &in_str[i+1], chars_left);
22 } else {
23 in_str[i] = '\0';
24 }
25 *len = *len - 1;
26 }
27 }
28}
29
Valerio Settif1477da2023-04-18 16:37:30 +020030static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
31{
32 mbedtls_pk_context key;
33 unsigned char *buf = NULL;
34 unsigned char *check_buf = NULL;
35 unsigned char *start_buf;
36 size_t buf_len, check_buf_len;
37 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 */
41 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
42 TEST_ASSERT(check_buf_len > 0);
43
Valerio Setti3b608de2023-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 Settif1477da2023-04-18 16:37:30 +020057 ASSERT_ALLOC(buf, check_buf_len);
58
59 mbedtls_pk_init(&key);
60 if (is_public_key) {
61 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
62 if (is_der) {
63 ret = mbedtls_pk_write_pubkey_der(&key, buf, check_buf_len);
64 } else {
65#if defined(MBEDTLS_PEM_WRITE_C)
66 ret = mbedtls_pk_write_pubkey_pem(&key, buf, check_buf_len);
67#else
68 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
69#endif
70 }
71 } else {
72 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL), 0);
73 if (is_der) {
74 ret = mbedtls_pk_write_key_der(&key, buf, check_buf_len);
75 } else {
76#if defined(MBEDTLS_PEM_WRITE_C)
77 ret = mbedtls_pk_write_key_pem(&key, buf, check_buf_len);
78#else
79 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
80#endif
81 }
82 }
83
84 if (is_der) {
85 TEST_LE_U(1, ret);
86 buf_len = ret;
87 start_buf = buf + check_buf_len - buf_len;
88 } else {
89 TEST_EQUAL(ret, 0);
90 buf_len = strlen((char *) buf) + 1; /* +1 takes the string terminator into account */
91 start_buf = buf;
92 }
93
94 ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
95
96exit:
97 mbedtls_free(buf);
98 mbedtls_free(check_buf);
99 mbedtls_pk_free(&key);
100}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200101/* END_HEADER */
102
103/* BEGIN_DEPENDENCIES
Hanno Becker19d858e2018-10-16 13:46:25 +0100104 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105 * END_DEPENDENCIES
106 */
107
Valerio Settif1477da2023-04-18 16:37:30 +0200108/* BEGIN_CASE */
109void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200110{
Valerio Settif1477da2023-04-18 16:37:30 +0200111 pk_write_check_common(key_file, 1, is_der);
112 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200113}
114/* END_CASE */
115
Valerio Settif1477da2023-04-18 16:37:30 +0200116/* BEGIN_CASE */
117void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200118{
Valerio Settif1477da2023-04-18 16:37:30 +0200119 pk_write_check_common(key_file, 0, is_der);
120 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200121}
122/* END_CASE */