blob: b99e27d7382cceb451eac35b7ce4612aa72a4e38 [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úti1e62c952025-03-02 01:17:02 +01007#if defined(_WIN32)
8# include <stdlib.h>
9# include <crtdbg.h>
10#endif
11
Bence Szépkútia0293872025-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úti9cde9d42025-03-02 00:58:11 +010015#endif
16
Gilles Peskine449bd832023-01-11 14:50:10 +010017struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +000018 char buf[2000];
19 char *ptr;
20};
21
Bence Szépkútid5102c92025-02-28 16:22:33 +010022#if defined(MBEDTLS_SSL_TLS_C)
Michael Schusterb1e33fb2024-06-04 02:30:22 +020023static void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000024{
25 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020026 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000027 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000028
Gilles Peskine449bd832023-01-11 14:50:10 +010029 memcpy(p, file, strlen(file));
30 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020031
32 *p++ = '(';
Gilles Peskine449bd832023-01-11 14:50:10 +010033 *p++ = '0' + (line / 1000) % 10;
34 *p++ = '0' + (line / 100) % 10;
35 *p++ = '0' + (line / 10) % 10;
36 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020037 *p++ = ')';
38 *p++ = ':';
39 *p++ = ' ';
40
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020041#if defined(MBEDTLS_THREADING_C)
42 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine449bd832023-01-11 14:50:10 +010043 while (*str++ != ' ') {
44 ;
45 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020046#endif
47
Gilles Peskine449bd832023-01-11 14:50:10 +010048 memcpy(p, str, strlen(str));
49 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020050
51 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020053 *p++ = '*';
Gilles Peskine449bd832023-01-11 14:50:10 +010054 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020055
56 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000057}
Bence Szépkútid5102c92025-02-28 16:22:33 +010058#endif /* MBEDTLS_SSL_TLS_C */
Bence Szépkúti1e62c952025-03-02 01:17:02 +010059
60#if defined(_WIN32)
61static void noop_invalid_parameter_handler(
62 const wchar_t *expression,
63 const wchar_t *function,
64 const wchar_t *file,
65 unsigned int line,
66 uintptr_t pReserved)
67{
68 (void) expression;
69 (void) function;
70 (void) file;
71 (void) line;
72 (void) pReserved;
73}
74#endif /* _WIN32 */
75
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000077
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* BEGIN_DEPENDENCIES
Bence Szépkútid5102c92025-02-28 16:22:33 +010079 * depends_on:MBEDTLS_DEBUG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020080 * END_DEPENDENCIES
81 */
Paul Bakker5690efc2011-05-26 13:16:06 +000082
Bence Szépkúti85d92ec2025-02-28 22:32:15 +010083/* BEGIN_CASE */
84void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
85 intmax_t sizeof_x, intmax_t x, char *result)
86{
Bence Szépkúti1e62c952025-03-02 01:17:02 +010087#if defined(_WIN32)
88 /* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
89 Disable this behaviour temporarily, so the rest of the test cases can complete. */
90 _invalid_parameter_handler saved_handler =
91 _set_invalid_parameter_handler(noop_invalid_parameter_handler);
92
93 // Disable assertion pop-up window in Debug builds
94 int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE);
95 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
96#endif
97
Bence Szépkúti85d92ec2025-02-28 22:32:15 +010098 const char *format = (char *) ((uintptr_t) smuggle_format_expr);
99 char *output = NULL;
100 const size_t n = strlen(result);
101
102 /* Nominal case: buffer just large enough */
103 TEST_CALLOC(output, n + 1);
104 if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
105 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x));
106 } else if (sizeof_x == sizeof(long)) {
107 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x));
108 } else if (sizeof_x == sizeof(long long)) {
109 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x));
110 } else {
111 TEST_FAIL(
112 "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");
113 }
114 TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
115
116exit:
117 mbedtls_free(output);
118 output = NULL;
Bence Szépkúti1e62c952025-03-02 01:17:02 +0100119
120#if defined(_WIN32)
121 // Restore default Windows behaviour
122 _set_invalid_parameter_handler(saved_handler);
123 _CrtSetReportMode(_CRT_ASSERT, saved_report_mode);
124 (void) saved_report_mode;
125#endif
Bence Szépkúti85d92ec2025-02-28 22:32:15 +0100126}
127/* END_CASE */
128
Bence Szépkútid5102c92025-02-28 16:22:33 +0100129/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130void debug_print_msg_threshold(int threshold, int level, char *file,
131 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200134 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +0200135 struct buffer_data buffer;
136
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100137 MD_PSA_INIT();
138
Gilles Peskine449bd832023-01-11 14:50:10 +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;
143
Yanray Wangaad94492023-12-04 10:42:06 +0800144 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
145 MBEDTLS_SSL_IS_CLIENT,
146 MBEDTLS_SSL_TRANSPORT_STREAM,
147 MBEDTLS_SSL_PRESET_DEFAULT),
148 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100149 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_debug_print_msg(&ssl, level, file, line,
157 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200160
161exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_ssl_free(&ssl);
163 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100164 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200165}
166/* END_CASE */
167
Bence Szépkútid5102c92025-02-28 16:22:33 +0100168/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
170 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200173 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200174 struct buffer_data buffer;
175
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100176 MD_PSA_INIT();
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_ssl_init(&ssl);
179 mbedtls_ssl_config_init(&conf);
180 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200181 buffer.ptr = buffer.buf;
182
Yanray Wangaad94492023-12-04 10:42:06 +0800183 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
184 MBEDTLS_SSL_IS_CLIENT,
185 MBEDTLS_SSL_TRANSPORT_STREAM,
186 MBEDTLS_SSL_PRESET_DEFAULT),
187 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100188 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200196
197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_ssl_free(&ssl);
199 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100200 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200201}
202/* END_CASE */
203
Bence Szépkútid5102c92025-02-28 16:22:33 +0100204/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205void mbedtls_debug_print_buf(char *file, int line, char *text,
206 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200209 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200210 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200211
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100212 MD_PSA_INIT();
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_ssl_init(&ssl);
215 mbedtls_ssl_config_init(&conf);
216 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200217 buffer.ptr = buffer.buf;
218
Yanray Wangaad94492023-12-04 10:42:06 +0800219 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
220 MBEDTLS_SSL_IS_CLIENT,
221 MBEDTLS_SSL_TRANSPORT_STREAM,
222 MBEDTLS_SSL_PRESET_DEFAULT),
223 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100224 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200232
233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_ssl_free(&ssl);
235 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100236 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200237}
238/* END_CASE */
239
Bence Szépkútid5102c92025-02-28 16:22:33 +0100240/* 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 +0100241void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
242 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_x509_crt crt;
245 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200246 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000247 struct buffer_data buffer;
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_ssl_init(&ssl);
250 mbedtls_ssl_config_init(&conf);
251 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200252 MD_OR_USE_PSA_INIT();
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200255 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000256
Yanray Wangaad94492023-12-04 10:42:06 +0800257 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
258 MBEDTLS_SSL_IS_CLIENT,
259 MBEDTLS_SSL_TRANSPORT_STREAM,
260 MBEDTLS_SSL_PRESET_DEFAULT),
261 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100262 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
268 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100271
Paul Bakkerbd51b262014-07-10 15:26:12 +0200272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 mbedtls_x509_crt_free(&crt);
274 mbedtls_ssl_free(&ssl);
275 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200276 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000277}
Paul Bakker33b43f12013-08-20 11:48:36 +0200278/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000279
Bence Szépkútid5102c92025-02-28 16:22:33 +0100280/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281void mbedtls_debug_print_mpi(char *value, char *file, int line,
282 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200285 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000286 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000288
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100289 MD_PSA_INIT();
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_ssl_init(&ssl);
292 mbedtls_ssl_config_init(&conf);
293 mbedtls_mpi_init(&val);
294 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200295 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000296
Yanray Wangaad94492023-12-04 10:42:06 +0800297 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
298 MBEDTLS_SSL_IS_CLIENT,
299 MBEDTLS_SSL_TRANSPORT_STREAM,
300 MBEDTLS_SSL_PRESET_DEFAULT),
301 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100302 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000312
Paul Bakkerbd51b262014-07-10 15:26:12 +0200313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_mpi_free(&val);
315 mbedtls_ssl_free(&ssl);
316 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100317 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000318}
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* END_CASE */