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