Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/pk.h" |
| 3 | #include "mbedtls/pem.h" |
| 4 | #include "mbedtls/oid.h" |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 5 | |
| 6 | typedef enum { |
| 7 | TEST_PEM, |
| 8 | TEST_DER |
| 9 | } pkwrite_file_format_t; |
| 10 | |
Valerio Setti | 755582b | 2023-04-24 10:24:37 +0200 | [diff] [blame] | 11 | /* Helper function for removing "\r" chars from a buffer. */ |
Valerio Setti | 3b608de | 2023-04-24 08:52:16 +0200 | [diff] [blame] | 12 | static 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 Setti | a4d460c | 2023-04-24 10:26:24 +0200 | [diff] [blame] | 21 | memmove(&in_str[i], &in_str[i+1], chars_left); |
Valerio Setti | 3b608de | 2023-04-24 08:52:16 +0200 | [diff] [blame] | 22 | } else { |
| 23 | in_str[i] = '\0'; |
| 24 | } |
| 25 | *len = *len - 1; |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 30 | static 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 Setti | d64e249 | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 39 | mbedtls_pk_init(&key); |
| 40 | USE_PSA_INIT(); |
| 41 | |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 42 | /* 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 Setti | 3b608de | 2023-04-24 08:52:16 +0200 | [diff] [blame] | 47 | /* 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 Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 60 | ASSERT_ALLOC(buf, check_buf_len); |
| 61 | |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 62 | 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 | |
| 98 | exit: |
| 99 | mbedtls_free(buf); |
| 100 | mbedtls_free(check_buf); |
| 101 | mbedtls_pk_free(&key); |
Valerio Setti | d64e249 | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 102 | USE_PSA_DONE(); |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 103 | } |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 104 | /* END_HEADER */ |
| 105 | |
| 106 | /* BEGIN_DEPENDENCIES |
Hanno Becker | 19d858e | 2018-10-16 13:46:25 +0100 | [diff] [blame] | 107 | * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 108 | * END_DEPENDENCIES |
| 109 | */ |
| 110 | |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 111 | /* BEGIN_CASE */ |
| 112 | void pk_write_pubkey_check(char *key_file, int is_der) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 113 | { |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 114 | pk_write_check_common(key_file, 1, is_der); |
| 115 | goto exit; /* make the compiler happy */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 116 | } |
| 117 | /* END_CASE */ |
| 118 | |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 119 | /* BEGIN_CASE */ |
| 120 | void pk_write_key_check(char *key_file, int is_der) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 121 | { |
Valerio Setti | f1477da | 2023-04-18 16:37:30 +0200 | [diff] [blame] | 122 | pk_write_check_common(key_file, 0, is_der); |
| 123 | goto exit; /* make the compiler happy */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 124 | } |
| 125 | /* END_CASE */ |