blob: ed3505a9fc80ec0fc36203c559515a00d76b7cbb [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/debug.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Paul Bakker1f761152010-02-18 18:16:31 +00004
Bence Szépkúti5d554662025-03-02 01:17:02 +01005#if defined(_WIN32)
6# include <stdlib.h>
7# include <crtdbg.h>
8#endif
9
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010010struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +000011 char buf[2000];
12 char *ptr;
13};
14
Bence Szépkúti27da54d2025-02-28 16:22:33 +010015#if defined(MBEDTLS_SSL_TLS_C)
16static void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000017{
18 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020019 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000020 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010022 memcpy(p, file, strlen(file));
23 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020024
25 *p++ = '(';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026 *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é-Gonnardfd474232015-06-23 16:34:24 +020030 *p++ = ')';
31 *p++ = ':';
32 *p++ = ' ';
33
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020034#if defined(MBEDTLS_THREADING_C)
35 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036 while (*str++ != ' ') {
37 ;
38 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020039#endif
40
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 memcpy(p, str, strlen(str));
42 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020043
44 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020046 *p++ = '*';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020048
49 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000050}
Bence Szépkúti27da54d2025-02-28 16:22:33 +010051#endif /* MBEDTLS_SSL_TLS_C */
Bence Szépkúti5d554662025-03-02 01:17:02 +010052
53#if defined(_WIN32)
54static 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 Bakker33b43f12013-08-20 11:48:36 +020069/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000070
Paul Bakker33b43f12013-08-20 11:48:36 +020071/* BEGIN_DEPENDENCIES
Bence Szépkúti27da54d2025-02-28 16:22:33 +010072 * depends_on:MBEDTLS_DEBUG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020073 * END_DEPENDENCIES
74 */
Paul Bakker5690efc2011-05-26 13:16:06 +000075
Bence Szépkúti94b0eea2025-02-28 22:32:15 +010076/* BEGIN_CASE */
77void 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úti5d554662025-03-02 01:17:02 +010080#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úti94b0eea2025-02-28 22:32:15 +010091 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
109exit:
110 mbedtls_free(output);
111 output = NULL;
Bence Szépkúti5d554662025-03-02 01:17:02 +0100112
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úti94b0eea2025-02-28 22:32:15 +0100119}
120/* END_CASE */
121
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100122/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123void debug_print_msg_threshold(int threshold, int level, char *file,
124 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +0200125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200127 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +0200128 struct buffer_data buffer;
129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mbedtls_ssl_init(&ssl);
131 mbedtls_ssl_config_init(&conf);
132 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200133 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200134 USE_PSA_INIT();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 mbedtls_debug_set_threshold(threshold);
139 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mbedtls_debug_print_msg(&ssl, level, file, line,
142 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200145
146exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_ssl_free(&ssl);
148 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200149 USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200150}
151/* END_CASE */
152
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100153/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
155 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200158 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200159 struct buffer_data buffer;
160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 mbedtls_ssl_init(&ssl);
162 mbedtls_ssl_config_init(&conf);
163 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200164 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200165 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200174
175exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 mbedtls_ssl_free(&ssl);
177 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200178 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200179}
180/* END_CASE */
181
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100182/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183void mbedtls_debug_print_buf(char *file, int line, char *text,
184 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200187 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200188 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 mbedtls_ssl_init(&ssl);
191 mbedtls_ssl_config_init(&conf);
192 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200193 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200194 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200203
204exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 mbedtls_ssl_free(&ssl);
206 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200207 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200208}
209/* END_CASE */
210
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100211/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
213 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000214{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_x509_crt crt;
216 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200217 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000218 struct buffer_data buffer;
219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_ssl_init(&ssl);
221 mbedtls_ssl_config_init(&conf);
222 mbedtls_x509_crt_init(&crt);
Valerio Settic6240f72023-05-23 10:44:08 +0200223 USE_PSA_INIT();
224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200226 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
233 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100236
Paul Bakkerbd51b262014-07-10 15:26:12 +0200237exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 mbedtls_x509_crt_free(&crt);
239 mbedtls_ssl_free(&ssl);
240 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200241 USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000242}
Paul Bakker33b43f12013-08-20 11:48:36 +0200243/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000244
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100245/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246void mbedtls_debug_print_mpi(char *value, char *file, int line,
247 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200250 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 mbedtls_ssl_init(&ssl);
255 mbedtls_ssl_config_init(&conf);
256 mbedtls_mpi_init(&val);
257 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200258 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200259 USE_PSA_INIT();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000270
Paul Bakkerbd51b262014-07-10 15:26:12 +0200271exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 mbedtls_mpi_free(&val);
273 mbedtls_ssl_free(&ssl);
274 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200275 USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000276}
Paul Bakker33b43f12013-08-20 11:48:36 +0200277/* END_CASE */
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400278
279/* BEGIN_CASE */
280void check_mbedtls_calloc_overallocation(int num, int size)
281{
282 unsigned char *buf;
Andrzej Kurek0841b5a2023-07-14 15:16:35 -0400283 buf = mbedtls_calloc((size_t) num * SIZE_MAX/2, (size_t) size * SIZE_MAX/2);
Andrzej Kurekf1e61fc2023-07-14 10:16:00 -0400284 /* Dummy usage of the pointer to prevent optimizing it */
285 mbedtls_printf("calloc pointer : %p\n", buf);
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400286 TEST_ASSERT(buf == NULL);
287
288exit:
289 mbedtls_free(buf);
290}
291/* END_CASE */