Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Valerio Setti | b4f5076 | 2024-01-17 10:24:52 +0100 | [diff] [blame] | 2 | #include "debug_internal.h" |
Mohammad Azim Khan | 67735d5 | 2017-04-06 11:55:43 +0100 | [diff] [blame] | 3 | #include "string.h" |
Valerio Setti | 1b08d42 | 2023-02-13 11:33:26 +0100 | [diff] [blame] | 4 | #include "mbedtls/pk.h" |
Yanray Wang | 5b60b42 | 2023-12-01 17:20:22 +0800 | [diff] [blame] | 5 | #include <test/ssl_helpers.h> |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 6 | |
Bence Szépkúti | 1e62c95 | 2025-03-02 01:17:02 +0100 | [diff] [blame] | 7 | #if defined(_WIN32) |
| 8 | # include <stdlib.h> |
| 9 | # include <crtdbg.h> |
| 10 | #endif |
| 11 | |
Bence Szépkúti | a029387 | 2025-03-12 16:43:38 +0100 | [diff] [blame] | 12 | // Dummy type for builds without MBEDTLS_HAVE_TIME |
| 13 | #if !defined(MBEDTLS_HAVE_TIME) |
| 14 | typedef int64_t mbedtls_ms_time_t; |
Bence Szépkúti | 9cde9d4 | 2025-03-02 00:58:11 +0100 | [diff] [blame] | 15 | #endif |
| 16 | |
Bence Szépkúti | c64b7bc | 2025-03-12 17:08:46 +0100 | [diff] [blame^] | 17 | typedef enum { |
| 18 | PRINTF_SIZET, |
| 19 | PRINTF_LONGLONG, |
| 20 | PRINTF_MS_TIME, |
| 21 | } printf_format_indicator_t; |
| 22 | |
| 23 | const char *const printf_formats[] = { |
| 24 | [PRINTF_SIZET] = "%" MBEDTLS_PRINTF_SIZET, |
| 25 | [PRINTF_LONGLONG] = "%" MBEDTLS_PRINTF_LONGLONG, |
| 26 | [PRINTF_MS_TIME] = "%" MBEDTLS_PRINTF_MS_TIME, |
| 27 | }; |
| 28 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 29 | struct buffer_data { |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 30 | char buf[2000]; |
| 31 | char *ptr; |
| 32 | }; |
| 33 | |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 34 | #if defined(MBEDTLS_SSL_TLS_C) |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 35 | static void string_debug(void *data, int level, const char *file, int line, const char *str) |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 36 | { |
| 37 | struct buffer_data *buffer = (struct buffer_data *) data; |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 38 | char *p = buffer->ptr; |
Paul Bakker | 26b41a8 | 2011-07-13 14:53:58 +0000 | [diff] [blame] | 39 | ((void) level); |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 40 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 41 | memcpy(p, file, strlen(file)); |
| 42 | p += strlen(file); |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 43 | |
| 44 | *p++ = '('; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | *p++ = '0' + (line / 1000) % 10; |
| 46 | *p++ = '0' + (line / 100) % 10; |
| 47 | *p++ = '0' + (line / 10) % 10; |
| 48 | *p++ = '0' + (line / 1) % 10; |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 49 | *p++ = ')'; |
| 50 | *p++ = ':'; |
| 51 | *p++ = ' '; |
| 52 | |
Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_THREADING_C) |
| 54 | /* Skip "thread ID" (up to the first space) as it is not predictable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | while (*str++ != ' ') { |
| 56 | ; |
| 57 | } |
Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 58 | #endif |
| 59 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | memcpy(p, str, strlen(str)); |
| 61 | p += strlen(str); |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 62 | |
| 63 | /* Detect if debug messages output partial lines and mark them */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | if (p[-1] != '\n') { |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 65 | *p++ = '*'; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | } |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 67 | |
| 68 | buffer->ptr = p; |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 69 | } |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 70 | #endif /* MBEDTLS_SSL_TLS_C */ |
Bence Szépkúti | 1e62c95 | 2025-03-02 01:17:02 +0100 | [diff] [blame] | 71 | |
| 72 | #if defined(_WIN32) |
| 73 | static void noop_invalid_parameter_handler( |
| 74 | const wchar_t *expression, |
| 75 | const wchar_t *function, |
| 76 | const wchar_t *file, |
| 77 | unsigned int line, |
| 78 | uintptr_t pReserved) |
| 79 | { |
| 80 | (void) expression; |
| 81 | (void) function; |
| 82 | (void) file; |
| 83 | (void) line; |
| 84 | (void) pReserved; |
| 85 | } |
| 86 | #endif /* _WIN32 */ |
| 87 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 88 | /* END_HEADER */ |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 89 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 90 | /* BEGIN_DEPENDENCIES |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 91 | * depends_on:MBEDTLS_DEBUG_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 92 | * END_DEPENDENCIES |
| 93 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 94 | |
Bence Szépkúti | 85d92ec | 2025-02-28 22:32:15 +0100 | [diff] [blame] | 95 | /* BEGIN_CASE */ |
Bence Szépkúti | c64b7bc | 2025-03-12 17:08:46 +0100 | [diff] [blame^] | 96 | void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *result) |
Bence Szépkúti | 85d92ec | 2025-02-28 22:32:15 +0100 | [diff] [blame] | 97 | { |
Bence Szépkúti | 1e62c95 | 2025-03-02 01:17:02 +0100 | [diff] [blame] | 98 | #if defined(_WIN32) |
| 99 | /* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures. |
| 100 | Disable this behaviour temporarily, so the rest of the test cases can complete. */ |
| 101 | _invalid_parameter_handler saved_handler = |
| 102 | _set_invalid_parameter_handler(noop_invalid_parameter_handler); |
| 103 | |
| 104 | // Disable assertion pop-up window in Debug builds |
| 105 | int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE); |
| 106 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 107 | #endif |
| 108 | |
Bence Szépkúti | c64b7bc | 2025-03-12 17:08:46 +0100 | [diff] [blame^] | 109 | const char *format = printf_formats[format_indicator]; |
Bence Szépkúti | 85d92ec | 2025-02-28 22:32:15 +0100 | [diff] [blame] | 110 | char *output = NULL; |
| 111 | const size_t n = strlen(result); |
| 112 | |
| 113 | /* Nominal case: buffer just large enough */ |
| 114 | TEST_CALLOC(output, n + 1); |
| 115 | if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function |
| 116 | TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x)); |
| 117 | } else if (sizeof_x == sizeof(long)) { |
| 118 | TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x)); |
| 119 | } else if (sizeof_x == sizeof(long long)) { |
| 120 | TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x)); |
| 121 | } else { |
| 122 | TEST_FAIL( |
| 123 | "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)"); |
| 124 | } |
| 125 | TEST_MEMORY_COMPARE(result, n + 1, output, n + 1); |
| 126 | |
| 127 | exit: |
| 128 | mbedtls_free(output); |
| 129 | output = NULL; |
Bence Szépkúti | 1e62c95 | 2025-03-02 01:17:02 +0100 | [diff] [blame] | 130 | |
| 131 | #if defined(_WIN32) |
| 132 | // Restore default Windows behaviour |
| 133 | _set_invalid_parameter_handler(saved_handler); |
| 134 | _CrtSetReportMode(_CRT_ASSERT, saved_report_mode); |
| 135 | (void) saved_report_mode; |
| 136 | #endif |
Bence Szépkúti | 85d92ec | 2025-02-28 22:32:15 +0100 | [diff] [blame] | 137 | } |
| 138 | /* END_CASE */ |
| 139 | |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 140 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | void debug_print_msg_threshold(int threshold, int level, char *file, |
| 142 | int line, char *result_str) |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 143 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 145 | mbedtls_ssl_config conf; |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 146 | struct buffer_data buffer; |
| 147 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 148 | MD_PSA_INIT(); |
| 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | mbedtls_ssl_init(&ssl); |
| 151 | mbedtls_ssl_config_init(&conf); |
| 152 | memset(buffer.buf, 0, 2000); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 153 | buffer.ptr = buffer.buf; |
| 154 | |
Yanray Wang | aad9449 | 2023-12-04 10:42:06 +0800 | [diff] [blame] | 155 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 156 | MBEDTLS_SSL_IS_CLIENT, |
| 157 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 158 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 159 | 0); |
Ronald Cron | aab4a54 | 2024-02-23 18:51:11 +0100 | [diff] [blame] | 160 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
Jerry Yu | b19ccc3 | 2021-08-09 17:44:56 +0800 | [diff] [blame] | 162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | d5a9e41 | 2015-05-04 11:11:42 +0200 | [diff] [blame] | 164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | mbedtls_debug_set_threshold(threshold); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | mbedtls_debug_print_msg(&ssl, level, file, line, |
| 168 | "Text message, 2 == %d", 2); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
Manuel Pégourié-Gonnard | d5a9e41 | 2015-05-04 11:11:42 +0200 | [diff] [blame] | 171 | |
| 172 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | mbedtls_ssl_free(&ssl); |
| 174 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 175 | MD_PSA_DONE(); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 176 | } |
| 177 | /* END_CASE */ |
| 178 | |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 179 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | void mbedtls_debug_print_ret(char *file, int line, char *text, int value, |
| 181 | char *result_str) |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 184 | mbedtls_ssl_config conf; |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 185 | struct buffer_data buffer; |
| 186 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 187 | MD_PSA_INIT(); |
| 188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | mbedtls_ssl_init(&ssl); |
| 190 | mbedtls_ssl_config_init(&conf); |
| 191 | memset(buffer.buf, 0, 2000); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 192 | buffer.ptr = buffer.buf; |
| 193 | |
Yanray Wang | aad9449 | 2023-12-04 10:42:06 +0800 | [diff] [blame] | 194 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 195 | MBEDTLS_SSL_IS_CLIENT, |
| 196 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 197 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 198 | 0); |
Ronald Cron | aab4a54 | 2024-02-23 18:51:11 +0100 | [diff] [blame] | 199 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Jerry Yu | b19ccc3 | 2021-08-09 17:44:56 +0800 | [diff] [blame] | 203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | mbedtls_debug_print_ret(&ssl, 0, file, line, text, value); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
Manuel Pégourié-Gonnard | d5a9e41 | 2015-05-04 11:11:42 +0200 | [diff] [blame] | 207 | |
| 208 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | mbedtls_ssl_free(&ssl); |
| 210 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 211 | MD_PSA_DONE(); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 212 | } |
| 213 | /* END_CASE */ |
| 214 | |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 215 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | void mbedtls_debug_print_buf(char *file, int line, char *text, |
| 217 | data_t *data, char *result_str) |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 218 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 220 | mbedtls_ssl_config conf; |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 221 | struct buffer_data buffer; |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 222 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 223 | MD_PSA_INIT(); |
| 224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | mbedtls_ssl_init(&ssl); |
| 226 | mbedtls_ssl_config_init(&conf); |
| 227 | memset(buffer.buf, 0, 2000); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 228 | buffer.ptr = buffer.buf; |
| 229 | |
Yanray Wang | aad9449 | 2023-12-04 10:42:06 +0800 | [diff] [blame] | 230 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 231 | MBEDTLS_SSL_IS_CLIENT, |
| 232 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 233 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 234 | 0); |
Ronald Cron | aab4a54 | 2024-02-23 18:51:11 +0100 | [diff] [blame] | 235 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Jerry Yu | b19ccc3 | 2021-08-09 17:44:56 +0800 | [diff] [blame] | 239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
Manuel Pégourié-Gonnard | d5a9e41 | 2015-05-04 11:11:42 +0200 | [diff] [blame] | 243 | |
| 244 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | mbedtls_ssl_free(&ssl); |
| 246 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 247 | MD_PSA_DONE(); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 248 | } |
| 249 | /* END_CASE */ |
| 250 | |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 251 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | void mbedtls_debug_print_crt(char *crt_file, char *file, int line, |
| 253 | char *prefix, char *result_str) |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 254 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | mbedtls_x509_crt crt; |
| 256 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 257 | mbedtls_ssl_config conf; |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 258 | struct buffer_data buffer; |
| 259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | mbedtls_ssl_init(&ssl); |
| 261 | mbedtls_ssl_config_init(&conf); |
| 262 | mbedtls_x509_crt_init(&crt); |
Valerio Setti | 92c3f36 | 2023-05-17 15:36:44 +0200 | [diff] [blame] | 263 | MD_OR_USE_PSA_INIT(); |
| 264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | memset(buffer.buf, 0, 2000); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 266 | buffer.ptr = buffer.buf; |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 267 | |
Yanray Wang | aad9449 | 2023-12-04 10:42:06 +0800 | [diff] [blame] | 268 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 269 | MBEDTLS_SSL_IS_CLIENT, |
| 270 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 271 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 272 | 0); |
Ronald Cron | aab4a54 | 2024-02-23 18:51:11 +0100 | [diff] [blame] | 273 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Jerry Yu | b19ccc3 | 2021-08-09 17:44:56 +0800 | [diff] [blame] | 277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0); |
| 279 | mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt); |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 282 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 283 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | mbedtls_x509_crt_free(&crt); |
| 285 | mbedtls_ssl_free(&ssl); |
| 286 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 92c3f36 | 2023-05-17 15:36:44 +0200 | [diff] [blame] | 287 | MD_OR_USE_PSA_DONE(); |
Paul Bakker | 1f76115 | 2010-02-18 18:16:31 +0000 | [diff] [blame] | 288 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 289 | /* END_CASE */ |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 290 | |
Bence Szépkúti | d5102c9 | 2025-02-28 16:22:33 +0100 | [diff] [blame] | 291 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | void mbedtls_debug_print_mpi(char *value, char *file, int line, |
| 293 | char *prefix, char *result_str) |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 294 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 296 | mbedtls_ssl_config conf; |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 297 | struct buffer_data buffer; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | mbedtls_mpi val; |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 299 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 300 | MD_PSA_INIT(); |
| 301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | mbedtls_ssl_init(&ssl); |
| 303 | mbedtls_ssl_config_init(&conf); |
| 304 | mbedtls_mpi_init(&val); |
| 305 | memset(buffer.buf, 0, 2000); |
Paul Bakker | 57ffa55 | 2014-04-25 14:29:10 +0200 | [diff] [blame] | 306 | buffer.ptr = buffer.buf; |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 307 | |
Yanray Wang | aad9449 | 2023-12-04 10:42:06 +0800 | [diff] [blame] | 308 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 309 | MBEDTLS_SSL_IS_CLIENT, |
| 310 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 311 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 312 | 0); |
Ronald Cron | aab4a54 | 2024-02-23 18:51:11 +0100 | [diff] [blame] | 313 | mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer); |
Jerry Yu | b19ccc3 | 2021-08-09 17:44:56 +0800 | [diff] [blame] | 315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | d5a9e41 | 2015-05-04 11:11:42 +0200 | [diff] [blame] | 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0); |
Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val); |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | TEST_ASSERT(strcmp(buffer.buf, result_str) == 0); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 323 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 324 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | mbedtls_mpi_free(&val); |
| 326 | mbedtls_ssl_free(&ssl); |
| 327 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 328 | MD_PSA_DONE(); |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 329 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 330 | /* END_CASE */ |