blob: de5458ca0047e1151d95c6996cf9d8ef17593ff3 [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úti9cde9d42025-03-02 00:58:11 +01007// Use a macro instead of sizeof(mbedtls_ms_time_t) because the expression store
8// doesn't exclude entries based on depends_on headers, which would cause failures
9// in builds without MBEDTLS_HAVE_TIME
10#if defined(MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO)
11# define MBEDTLS_MS_TIME_SIZE sizeof(MBEDTLS_PLATFORM_MS_TIME_TYPE_MACRO)
12#else
13# define MBEDTLS_MS_TIME_SIZE sizeof(int64_t)
14#endif
15
Gilles Peskine449bd832023-01-11 14:50:10 +010016struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +000017 char buf[2000];
18 char *ptr;
19};
20
Bence Szépkútid5102c92025-02-28 16:22:33 +010021#if defined(MBEDTLS_SSL_TLS_C)
Michael Schusterb1e33fb2024-06-04 02:30:22 +020022static void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000023{
24 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020025 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000026 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000027
Gilles Peskine449bd832023-01-11 14:50:10 +010028 memcpy(p, file, strlen(file));
29 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020030
31 *p++ = '(';
Gilles Peskine449bd832023-01-11 14:50:10 +010032 *p++ = '0' + (line / 1000) % 10;
33 *p++ = '0' + (line / 100) % 10;
34 *p++ = '0' + (line / 10) % 10;
35 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020036 *p++ = ')';
37 *p++ = ':';
38 *p++ = ' ';
39
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020040#if defined(MBEDTLS_THREADING_C)
41 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine449bd832023-01-11 14:50:10 +010042 while (*str++ != ' ') {
43 ;
44 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020045#endif
46
Gilles Peskine449bd832023-01-11 14:50:10 +010047 memcpy(p, str, strlen(str));
48 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020049
50 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine449bd832023-01-11 14:50:10 +010051 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020052 *p++ = '*';
Gilles Peskine449bd832023-01-11 14:50:10 +010053 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020054
55 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000056}
Bence Szépkútid5102c92025-02-28 16:22:33 +010057#endif /* MBEDTLS_SSL_TLS_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020058/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000059
Paul Bakker33b43f12013-08-20 11:48:36 +020060/* BEGIN_DEPENDENCIES
Bence Szépkútid5102c92025-02-28 16:22:33 +010061 * depends_on:MBEDTLS_DEBUG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020062 * END_DEPENDENCIES
63 */
Paul Bakker5690efc2011-05-26 13:16:06 +000064
Bence Szépkúti85d92ec2025-02-28 22:32:15 +010065/* BEGIN_CASE */
66void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
67 intmax_t sizeof_x, intmax_t x, char *result)
68{
69 const char *format = (char *) ((uintptr_t) smuggle_format_expr);
70 char *output = NULL;
71 const size_t n = strlen(result);
72
73 /* Nominal case: buffer just large enough */
74 TEST_CALLOC(output, n + 1);
75 if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
76 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x));
77 } else if (sizeof_x == sizeof(long)) {
78 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x));
79 } else if (sizeof_x == sizeof(long long)) {
80 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x));
81 } else {
82 TEST_FAIL(
83 "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");
84 }
85 TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
86
87exit:
88 mbedtls_free(output);
89 output = NULL;
90}
91/* END_CASE */
92
Bence Szépkútid5102c92025-02-28 16:22:33 +010093/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010094void debug_print_msg_threshold(int threshold, int level, char *file,
95 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020098 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020099 struct buffer_data buffer;
100
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100101 MD_PSA_INIT();
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_ssl_init(&ssl);
104 mbedtls_ssl_config_init(&conf);
105 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200106 buffer.ptr = buffer.buf;
107
Yanray Wangaad94492023-12-04 10:42:06 +0800108 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
109 MBEDTLS_SSL_IS_CLIENT,
110 MBEDTLS_SSL_TRANSPORT_STREAM,
111 MBEDTLS_SSL_PRESET_DEFAULT),
112 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100113 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_debug_print_msg(&ssl, level, file, line,
121 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200124
125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_ssl_free(&ssl);
127 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100128 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200129}
130/* END_CASE */
131
Bence Szépkútid5102c92025-02-28 16:22:33 +0100132/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
134 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200137 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200138 struct buffer_data buffer;
139
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100140 MD_PSA_INIT();
141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_ssl_init(&ssl);
143 mbedtls_ssl_config_init(&conf);
144 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200145 buffer.ptr = buffer.buf;
146
Yanray Wangaad94492023-12-04 10:42:06 +0800147 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
148 MBEDTLS_SSL_IS_CLIENT,
149 MBEDTLS_SSL_TRANSPORT_STREAM,
150 MBEDTLS_SSL_PRESET_DEFAULT),
151 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100152 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +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 Bakker57ffa552014-04-25 14:29:10 +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_buf(char *file, int line, char *text,
170 data_t *data, 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;
Paul Bakker57ffa552014-04-25 14:29:10 +0200175
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_buf(&ssl, 0, file, line, text, data->x, data->len);
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:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
206 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_x509_crt crt;
209 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200210 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000211 struct buffer_data buffer;
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_ssl_init(&ssl);
214 mbedtls_ssl_config_init(&conf);
215 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200216 MD_OR_USE_PSA_INIT();
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200219 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000220
Yanray Wangaad94492023-12-04 10:42:06 +0800221 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
222 MBEDTLS_SSL_IS_CLIENT,
223 MBEDTLS_SSL_TRANSPORT_STREAM,
224 MBEDTLS_SSL_PRESET_DEFAULT),
225 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100226 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
232 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100235
Paul Bakkerbd51b262014-07-10 15:26:12 +0200236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_x509_crt_free(&crt);
238 mbedtls_ssl_free(&ssl);
239 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200240 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000241}
Paul Bakker33b43f12013-08-20 11:48:36 +0200242/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000243
Bence Szépkútid5102c92025-02-28 16:22:33 +0100244/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245void mbedtls_debug_print_mpi(char *value, char *file, int line,
246 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200249 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000250 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000252
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100253 MD_PSA_INIT();
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_ssl_init(&ssl);
256 mbedtls_ssl_config_init(&conf);
257 mbedtls_mpi_init(&val);
258 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200259 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000260
Yanray Wangaad94492023-12-04 10:42:06 +0800261 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
262 MBEDTLS_SSL_IS_CLIENT,
263 MBEDTLS_SSL_TRANSPORT_STREAM,
264 MBEDTLS_SSL_PRESET_DEFAULT),
265 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100266 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000276
Paul Bakkerbd51b262014-07-10 15:26:12 +0200277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_mpi_free(&val);
279 mbedtls_ssl_free(&ssl);
280 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100281 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000282}
Paul Bakker33b43f12013-08-20 11:48:36 +0200283/* END_CASE */