blob: 15605c766ddc17ee6fc03dffc1059191fdf7a8b4 [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
Bence Szépkúticb094f92025-03-12 17:08:46 +010010typedef enum {
11 PRINTF_SIZET,
12 PRINTF_LONGLONG,
13} printf_format_indicator_t;
14
15const char *const printf_formats[] = {
16 [PRINTF_SIZET] = "%" MBEDTLS_PRINTF_SIZET,
17 [PRINTF_LONGLONG] = "%" MBEDTLS_PRINTF_LONGLONG,
18};
19
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010020struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +000021 char buf[2000];
22 char *ptr;
23};
24
Bence Szépkúti27da54d2025-02-28 16:22:33 +010025#if defined(MBEDTLS_SSL_TLS_C)
26static void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000027{
28 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020029 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000030 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032 memcpy(p, file, strlen(file));
33 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020034
35 *p++ = '(';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036 *p++ = '0' + (line / 1000) % 10;
37 *p++ = '0' + (line / 100) % 10;
38 *p++ = '0' + (line / 10) % 10;
39 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020040 *p++ = ')';
41 *p++ = ':';
42 *p++ = ' ';
43
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020044#if defined(MBEDTLS_THREADING_C)
45 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046 while (*str++ != ' ') {
47 ;
48 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020049#endif
50
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 memcpy(p, str, strlen(str));
52 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020053
54 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020056 *p++ = '*';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020058
59 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000060}
Bence Szépkúti27da54d2025-02-28 16:22:33 +010061#endif /* MBEDTLS_SSL_TLS_C */
Bence Szépkúti5d554662025-03-02 01:17:02 +010062
63#if defined(_WIN32)
64static void noop_invalid_parameter_handler(
65 const wchar_t *expression,
66 const wchar_t *function,
67 const wchar_t *file,
68 unsigned int line,
69 uintptr_t pReserved)
70{
71 (void) expression;
72 (void) function;
73 (void) file;
74 (void) line;
75 (void) pReserved;
76}
77#endif /* _WIN32 */
78
Paul Bakker33b43f12013-08-20 11:48:36 +020079/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000080
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* BEGIN_DEPENDENCIES
Bence Szépkúti27da54d2025-02-28 16:22:33 +010082 * depends_on:MBEDTLS_DEBUG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020083 * END_DEPENDENCIES
84 */
Paul Bakker5690efc2011-05-26 13:16:06 +000085
Bence Szépkúti94b0eea2025-02-28 22:32:15 +010086/* BEGIN_CASE */
Bence Szépkúticb094f92025-03-12 17:08:46 +010087void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *result)
Bence Szépkúti94b0eea2025-02-28 22:32:15 +010088{
Bence Szépkúti5d554662025-03-02 01:17:02 +010089#if defined(_WIN32)
90 /* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
91 Disable this behaviour temporarily, so the rest of the test cases can complete. */
92 _invalid_parameter_handler saved_handler =
93 _set_invalid_parameter_handler(noop_invalid_parameter_handler);
94
95 // Disable assertion pop-up window in Debug builds
96 int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE);
97 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
98#endif
99
Bence Szépkúticb094f92025-03-12 17:08:46 +0100100 const char *format = printf_formats[format_indicator];
Bence Szépkúti94b0eea2025-02-28 22:32:15 +0100101 char *output = NULL;
102 const size_t n = strlen(result);
103
104 /* Nominal case: buffer just large enough */
105 TEST_CALLOC(output, n + 1);
106 if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
107 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x));
108 } else if (sizeof_x == sizeof(long)) {
109 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x));
110 } else if (sizeof_x == sizeof(long long)) {
111 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x));
112 } else {
113 TEST_FAIL(
114 "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");
115 }
116 TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
117
118exit:
119 mbedtls_free(output);
120 output = NULL;
Bence Szépkúti5d554662025-03-02 01:17:02 +0100121
122#if defined(_WIN32)
123 // Restore default Windows behaviour
124 _set_invalid_parameter_handler(saved_handler);
125 _CrtSetReportMode(_CRT_ASSERT, saved_report_mode);
126 (void) saved_report_mode;
127#endif
Bence Szépkúti94b0eea2025-02-28 22:32:15 +0100128}
129/* END_CASE */
130
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100131/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132void debug_print_msg_threshold(int threshold, int level, char *file,
133 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +0200134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200136 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +0200137 struct buffer_data buffer;
138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 mbedtls_ssl_init(&ssl);
140 mbedtls_ssl_config_init(&conf);
141 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200142 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200143 USE_PSA_INIT();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_debug_set_threshold(threshold);
148 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_debug_print_msg(&ssl, level, file, line,
151 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200154
155exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 mbedtls_ssl_free(&ssl);
157 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200158 USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200159}
160/* END_CASE */
161
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100162/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
164 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200167 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200168 struct buffer_data buffer;
169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_ssl_init(&ssl);
171 mbedtls_ssl_config_init(&conf);
172 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200173 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200174 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200183
184exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 mbedtls_ssl_free(&ssl);
186 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200187 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200188}
189/* END_CASE */
190
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100191/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192void mbedtls_debug_print_buf(char *file, int line, char *text,
193 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200196 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200197 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 mbedtls_ssl_init(&ssl);
200 mbedtls_ssl_config_init(&conf);
201 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200202 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200203 USE_PSA_INIT();
Paul Bakker57ffa552014-04-25 14:29:10 +0200204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200212
213exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 mbedtls_ssl_free(&ssl);
215 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200216 USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200217}
218/* END_CASE */
219
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100220/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
222 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_x509_crt crt;
225 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200226 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000227 struct buffer_data buffer;
228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 mbedtls_ssl_init(&ssl);
230 mbedtls_ssl_config_init(&conf);
231 mbedtls_x509_crt_init(&crt);
Valerio Settic6240f72023-05-23 10:44:08 +0200232 USE_PSA_INIT();
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200235 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
242 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100245
Paul Bakkerbd51b262014-07-10 15:26:12 +0200246exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 mbedtls_x509_crt_free(&crt);
248 mbedtls_ssl_free(&ssl);
249 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200250 USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000251}
Paul Bakker33b43f12013-08-20 11:48:36 +0200252/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000253
Bence Szépkúti27da54d2025-02-28 16:22:33 +0100254/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255void mbedtls_debug_print_mpi(char *value, char *file, int line,
256 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200259 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000260 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 mbedtls_ssl_init(&ssl);
264 mbedtls_ssl_config_init(&conf);
265 mbedtls_mpi_init(&val);
266 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200267 buffer.ptr = buffer.buf;
Valerio Settic6240f72023-05-23 10:44:08 +0200268 USE_PSA_INIT();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000279
Paul Bakkerbd51b262014-07-10 15:26:12 +0200280exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 mbedtls_mpi_free(&val);
282 mbedtls_ssl_free(&ssl);
283 mbedtls_ssl_config_free(&conf);
Valerio Settic6240f72023-05-23 10:44:08 +0200284 USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000285}
Paul Bakker33b43f12013-08-20 11:48:36 +0200286/* END_CASE */
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400287
288/* BEGIN_CASE */
289void check_mbedtls_calloc_overallocation(int num, int size)
290{
291 unsigned char *buf;
Andrzej Kurek0841b5a2023-07-14 15:16:35 -0400292 buf = mbedtls_calloc((size_t) num * SIZE_MAX/2, (size_t) size * SIZE_MAX/2);
Andrzej Kurekf1e61fc2023-07-14 10:16:00 -0400293 /* Dummy usage of the pointer to prevent optimizing it */
294 mbedtls_printf("calloc pointer : %p\n", buf);
Andrzej Kurekf35490e2023-07-14 10:12:11 -0400295 TEST_ASSERT(buf == NULL);
296
297exit:
298 mbedtls_free(buf);
299}
300/* END_CASE */