blob: 804e9a77eb4c4371f73efe2e7b4644a81cc78aa9 [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
Valerio Setti14bfdbf2023-04-24 13:53:21 +020039 mbedtls_pk_init(&key);
40 USE_PSA_INIT();
41
Valerio Setti89590952023-04-17 17:34:09 +020042 /* Note: if mbedtls_pk_load_file() successfully reads the file, then
43 it also allocates check_buf, which should be freed on exit */
Valerio Setti3401b302023-04-18 10:42:53 +020044 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
Valerio Setti89590952023-04-17 17:34:09 +020045 TEST_ASSERT(check_buf_len > 0);
46
Valerio Setti7bacaf82023-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 Setti89590952023-04-17 17:34:09 +020060 ASSERT_ALLOC(buf, check_buf_len);
61
Valerio Setti89590952023-04-17 17:34:09 +020062 if (is_public_key) {
Valerio Setti3401b302023-04-18 10:42:53 +020063 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
Valerio Settic60bc5e2023-04-17 18:43:06 +020064 if (is_der) {
65 ret = mbedtls_pk_write_pubkey_der(&key, buf, check_buf_len);
66 } else {
Valerio Setti15cac172023-04-18 11:25:30 +020067#if defined(MBEDTLS_PEM_WRITE_C)
Valerio Settic60bc5e2023-04-17 18:43:06 +020068 ret = mbedtls_pk_write_pubkey_pem(&key, buf, check_buf_len);
Valerio Setti15cac172023-04-18 11:25:30 +020069#else
70 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
71#endif
Valerio Settic60bc5e2023-04-17 18:43:06 +020072 }
Valerio Setti89590952023-04-17 17:34:09 +020073 } else {
Valerio Setti3401b302023-04-18 10:42:53 +020074 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
Valerio Setti22808952023-04-18 12:57:52 +020075 mbedtls_test_rnd_std_rand, NULL), 0);
Valerio Settic60bc5e2023-04-17 18:43:06 +020076 if (is_der) {
77 ret = mbedtls_pk_write_key_der(&key, buf, check_buf_len);
78 } else {
Valerio Setti15cac172023-04-18 11:25:30 +020079#if defined(MBEDTLS_PEM_WRITE_C)
Valerio Settic60bc5e2023-04-17 18:43:06 +020080 ret = mbedtls_pk_write_key_pem(&key, buf, check_buf_len);
Valerio Setti15cac172023-04-18 11:25:30 +020081#else
82 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
83#endif
Valerio Settic60bc5e2023-04-17 18:43:06 +020084 }
Valerio Setti89590952023-04-17 17:34:09 +020085 }
Valerio Setti89590952023-04-17 17:34:09 +020086
Valerio Settic60bc5e2023-04-17 18:43:06 +020087 if (is_der) {
Valerio Setti3401b302023-04-18 10:42:53 +020088 TEST_LE_U(1, ret);
89 buf_len = ret;
90 start_buf = buf + check_buf_len - buf_len;
Valerio Settic60bc5e2023-04-17 18:43:06 +020091 } else {
Valerio Setti3401b302023-04-18 10:42:53 +020092 TEST_EQUAL(ret, 0);
Valerio Setti22808952023-04-18 12:57:52 +020093 buf_len = strlen((char *) buf) + 1; /* +1 takes the string terminator into account */
Valerio Settic60bc5e2023-04-17 18:43:06 +020094 start_buf = buf;
95 }
96
Valerio Setti3401b302023-04-18 10:42:53 +020097 ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
Valerio Setti89590952023-04-17 17:34:09 +020098
99exit:
100 mbedtls_free(buf);
101 mbedtls_free(check_buf);
102 mbedtls_pk_free(&key);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200103 USE_PSA_DONE();
Valerio Setti89590952023-04-17 17:34:09 +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 Setti15cac172023-04-18 11:25:30 +0200112/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200113void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200114{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200115 pk_write_check_common(key_file, 1, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200116 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200117}
118/* END_CASE */
119
Valerio Setti15cac172023-04-18 11:25:30 +0200120/* BEGIN_CASE */
Valerio Settic60bc5e2023-04-17 18:43:06 +0200121void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200122{
Valerio Settic60bc5e2023-04-17 18:43:06 +0200123 pk_write_check_common(key_file, 0, is_der);
Valerio Setti89590952023-04-17 17:34:09 +0200124 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200125}
126/* END_CASE */