blob: f90281d9966d5161871afc83241c18a7995e941a [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;
Valerio Settia4d460c2023-04-24 10:26:24 +020021 memmove(&in_str[i], &in_str[i+1], chars_left);
Valerio Setti3b608de2023-04-24 08:52:16 +020022 } 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
Valerio Settid64e2492023-04-24 13:53:21 +020039 mbedtls_pk_init(&key);
40 USE_PSA_INIT();
41
Valerio Settif1477da2023-04-18 16:37:30 +020042 /* Note: if mbedtls_pk_load_file() successfully reads the file, then
43 it also allocates check_buf, which should be freed on exit */
44 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
45 TEST_ASSERT(check_buf_len > 0);
46
Valerio Setti3b608de2023-04-24 08:52:16 +020047 /* Windows' line ending is different from the Linux's one ("\r\n" vs "\n").
48 * Git treats PEM files as text, so when on Windows, it replaces new lines
49 * with "\r\n" on checkout.
50 * Unfortunately mbedtls_pk_load_file() loads files in binary format,
51 * while mbedtls_pk_write_pubkey_pem() goes through the I/O layer which
52 * uses "\n" for newlines in both Windows and Linux.
53 * Here we remove the extra "\r" so that "buf" and "check_buf" can be
54 * easily compared later. */
55 if (!is_der) {
56 fix_new_lines(check_buf, &check_buf_len);
57 }
58 TEST_ASSERT(check_buf_len > 0);
59
Valerio Settif1477da2023-04-18 16:37:30 +020060 ASSERT_ALLOC(buf, check_buf_len);
61
Valerio Settif1477da2023-04-18 16:37:30 +020062 if (is_public_key) {
63 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
64 if (is_der) {
65 ret = mbedtls_pk_write_pubkey_der(&key, buf, check_buf_len);
66 } else {
67#if defined(MBEDTLS_PEM_WRITE_C)
68 ret = mbedtls_pk_write_pubkey_pem(&key, buf, check_buf_len);
69#else
70 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
71#endif
72 }
73 } else {
74 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL), 0);
75 if (is_der) {
76 ret = mbedtls_pk_write_key_der(&key, buf, check_buf_len);
77 } else {
78#if defined(MBEDTLS_PEM_WRITE_C)
79 ret = mbedtls_pk_write_key_pem(&key, buf, check_buf_len);
80#else
81 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
82#endif
83 }
84 }
85
86 if (is_der) {
87 TEST_LE_U(1, ret);
88 buf_len = ret;
89 start_buf = buf + check_buf_len - buf_len;
90 } else {
91 TEST_EQUAL(ret, 0);
92 buf_len = strlen((char *) buf) + 1; /* +1 takes the string terminator into account */
93 start_buf = buf;
94 }
95
96 ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
97
98exit:
99 mbedtls_free(buf);
100 mbedtls_free(check_buf);
101 mbedtls_pk_free(&key);
Valerio Settid64e2492023-04-24 13:53:21 +0200102 USE_PSA_DONE();
Valerio Settif1477da2023-04-18 16:37:30 +0200103}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200104/* END_HEADER */
105
106/* BEGIN_DEPENDENCIES
Hanno Becker19d858e2018-10-16 13:46:25 +0100107 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200108 * END_DEPENDENCIES
109 */
110
Valerio Settif1477da2023-04-18 16:37:30 +0200111/* BEGIN_CASE */
112void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200113{
Valerio Settif1477da2023-04-18 16:37:30 +0200114 pk_write_check_common(key_file, 1, is_der);
115 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200116}
117/* END_CASE */
118
Valerio Settif1477da2023-04-18 16:37:30 +0200119/* BEGIN_CASE */
120void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200121{
Valerio Settif1477da2023-04-18 16:37:30 +0200122 pk_write_check_common(key_file, 0, is_der);
123 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200124}
125/* END_CASE */