blob: 57b8f4e175ae7d632abf0f909aa74d63b1e15b37 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Valerio Settib4f50762024-01-17 10:24:52 +01002#include "debug_internal.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Valerio Setti1b08d422023-02-13 11:33:26 +01004#include "mbedtls/pk.h"
Yanray Wang5b60b422023-12-01 17:20:22 +08005#include <test/ssl_helpers.h>
Paul Bakker1f761152010-02-18 18:16:31 +00006
Bence Szépkúti58bb7ec2025-03-02 01:17:02 +01007#if defined(_WIN32)
8# include <stdlib.h>
9# include <crtdbg.h>
10#endif
11
Bence Szépkúti46e0b1c2025-03-12 16:43:38 +010012// Dummy type for builds without MBEDTLS_HAVE_TIME
13#if !defined(MBEDTLS_HAVE_TIME)
14typedef int64_t mbedtls_ms_time_t;
Bence Szépkúti154066d2025-03-02 00:58:11 +010015#endif
16
Bence Szépkúti24f11a32025-03-12 17:08:46 +010017typedef enum {
18 PRINTF_SIZET,
19 PRINTF_LONGLONG,
20 PRINTF_MS_TIME,
21} printf_format_indicator_t;
22
23const 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 Peskine449bd832023-01-11 14:50:10 +010029struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +000030 char buf[2000];
31 char *ptr;
32};
33
Bence Szépkúti12210522025-02-28 16:22:33 +010034#if defined(MBEDTLS_SSL_TLS_C)
Michael Schuster54300d42024-06-04 02:30:22 +020035static void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000036{
37 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020038 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000039 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000040
Gilles Peskine449bd832023-01-11 14:50:10 +010041 memcpy(p, file, strlen(file));
42 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020043
44 *p++ = '(';
Gilles Peskine449bd832023-01-11 14:50:10 +010045 *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é-Gonnardfd474232015-06-23 16:34:24 +020049 *p++ = ')';
50 *p++ = ':';
51 *p++ = ' ';
52
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020053#if defined(MBEDTLS_THREADING_C)
54 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine449bd832023-01-11 14:50:10 +010055 while (*str++ != ' ') {
56 ;
57 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020058#endif
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060 memcpy(p, str, strlen(str));
61 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020062
63 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020065 *p++ = '*';
Gilles Peskine449bd832023-01-11 14:50:10 +010066 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020067
68 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000069}
Bence Szépkúti12210522025-02-28 16:22:33 +010070#endif /* MBEDTLS_SSL_TLS_C */
Bence Szépkúti58bb7ec2025-03-02 01:17:02 +010071
72#if defined(_WIN32)
73static 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 Bakker33b43f12013-08-20 11:48:36 +020088/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000089
Paul Bakker33b43f12013-08-20 11:48:36 +020090/* BEGIN_DEPENDENCIES
Bence Szépkúti12210522025-02-28 16:22:33 +010091 * depends_on:MBEDTLS_DEBUG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020092 * END_DEPENDENCIES
93 */
Paul Bakker5690efc2011-05-26 13:16:06 +000094
Bence Szépkútic6a8bf02025-02-28 22:32:15 +010095/* BEGIN_CASE */
Bence Szépkúti24f11a32025-03-12 17:08:46 +010096void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *result)
Bence Szépkútic6a8bf02025-02-28 22:32:15 +010097{
Bence Szépkúti58bb7ec2025-03-02 01:17:02 +010098#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úti24f11a32025-03-12 17:08:46 +0100109 const char *format = printf_formats[format_indicator];
Bence Szépkútic6a8bf02025-02-28 22:32:15 +0100110 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
127exit:
128 mbedtls_free(output);
129 output = NULL;
Bence Szépkúti58bb7ec2025-03-02 01:17:02 +0100130
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útic6a8bf02025-02-28 22:32:15 +0100137}
138/* END_CASE */
139
Bence Szépkúti12210522025-02-28 16:22:33 +0100140/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141void debug_print_msg_threshold(int threshold, int level, char *file,
142 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +0200143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200145 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +0200146 struct buffer_data buffer;
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_ssl_init(&ssl);
149 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200150 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200152 buffer.ptr = buffer.buf;
153
Yanray Wangaad94492023-12-04 10:42:06 +0800154 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
155 MBEDTLS_SSL_IS_CLIENT,
156 MBEDTLS_SSL_TRANSPORT_STREAM,
157 MBEDTLS_SSL_PRESET_DEFAULT),
158 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_debug_print_msg(&ssl, level, file, line,
166 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200169
170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_ssl_free(&ssl);
172 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200173 MD_OR_USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200174}
175/* END_CASE */
176
Bence Szépkúti12210522025-02-28 16:22:33 +0100177/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
179 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200182 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200183 struct buffer_data buffer;
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_ssl_init(&ssl);
186 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200187 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200189 buffer.ptr = buffer.buf;
190
Yanray Wangaad94492023-12-04 10:42:06 +0800191 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
192 MBEDTLS_SSL_IS_CLIENT,
193 MBEDTLS_SSL_TRANSPORT_STREAM,
194 MBEDTLS_SSL_PRESET_DEFAULT),
195 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200203
204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_ssl_free(&ssl);
206 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200207 MD_OR_USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200208}
209/* END_CASE */
210
Bence Szépkúti12210522025-02-28 16:22:33 +0100211/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212void mbedtls_debug_print_buf(char *file, int line, char *text,
213 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200214{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200216 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200217 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_ssl_init(&ssl);
220 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200221 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200223 buffer.ptr = buffer.buf;
224
Yanray Wangaad94492023-12-04 10:42:06 +0800225 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
226 MBEDTLS_SSL_IS_CLIENT,
227 MBEDTLS_SSL_TRANSPORT_STREAM,
228 MBEDTLS_SSL_PRESET_DEFAULT),
229 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200237
238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_ssl_free(&ssl);
240 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200241 MD_OR_USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200242}
243/* END_CASE */
244
Bence Szépkúti12210522025-02-28 16:22:33 +0100245/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
247 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_x509_crt crt;
250 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200251 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000252 struct buffer_data buffer;
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_ssl_init(&ssl);
255 mbedtls_ssl_config_init(&conf);
256 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200257 MD_OR_USE_PSA_INIT();
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200260 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000261
Yanray Wangaad94492023-12-04 10:42:06 +0800262 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
263 MBEDTLS_SSL_IS_CLIENT,
264 MBEDTLS_SSL_TRANSPORT_STREAM,
265 MBEDTLS_SSL_PRESET_DEFAULT),
266 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
272 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100275
Paul Bakkerbd51b262014-07-10 15:26:12 +0200276exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_x509_crt_free(&crt);
278 mbedtls_ssl_free(&ssl);
279 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200280 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000281}
Paul Bakker33b43f12013-08-20 11:48:36 +0200282/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000283
Bence Szépkúti12210522025-02-28 16:22:33 +0100284/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285void mbedtls_debug_print_mpi(char *value, char *file, int line,
286 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200289 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000290 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_ssl_init(&ssl);
294 mbedtls_ssl_config_init(&conf);
295 mbedtls_mpi_init(&val);
Valerio Setti3a994b72024-07-03 16:58:10 +0200296 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200298 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000299
Yanray Wangaad94492023-12-04 10:42:06 +0800300 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
301 MBEDTLS_SSL_IS_CLIENT,
302 MBEDTLS_SSL_TRANSPORT_STREAM,
303 MBEDTLS_SSL_PRESET_DEFAULT),
304 0);
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000314
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 mbedtls_mpi_free(&val);
317 mbedtls_ssl_free(&ssl);
318 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200319 MD_OR_USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000320}
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* END_CASE */