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