blob: e17ffbb921af4c84038a2e8f81547787c49ad94c [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
Gilles Peskine449bd832023-01-11 14:50:10 +01007struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +00008 char buf[2000];
9 char *ptr;
10};
11
Bence Szépkútid5102c92025-02-28 16:22:33 +010012#if defined(MBEDTLS_SSL_TLS_C)
Michael Schusterb1e33fb2024-06-04 02:30:22 +020013static void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000014{
15 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020016 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000017 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000018
Gilles Peskine449bd832023-01-11 14:50:10 +010019 memcpy(p, file, strlen(file));
20 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020021
22 *p++ = '(';
Gilles Peskine449bd832023-01-11 14:50:10 +010023 *p++ = '0' + (line / 1000) % 10;
24 *p++ = '0' + (line / 100) % 10;
25 *p++ = '0' + (line / 10) % 10;
26 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020027 *p++ = ')';
28 *p++ = ':';
29 *p++ = ' ';
30
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020031#if defined(MBEDTLS_THREADING_C)
32 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine449bd832023-01-11 14:50:10 +010033 while (*str++ != ' ') {
34 ;
35 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020036#endif
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038 memcpy(p, str, strlen(str));
39 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020040
41 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine449bd832023-01-11 14:50:10 +010042 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020043 *p++ = '*';
Gilles Peskine449bd832023-01-11 14:50:10 +010044 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020045
46 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000047}
Bence Szépkútid5102c92025-02-28 16:22:33 +010048#endif /* MBEDTLS_SSL_TLS_C */
Paul Bakker33b43f12013-08-20 11:48:36 +020049/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000050
Paul Bakker33b43f12013-08-20 11:48:36 +020051/* BEGIN_DEPENDENCIES
Bence Szépkútid5102c92025-02-28 16:22:33 +010052 * depends_on:MBEDTLS_DEBUG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020053 * END_DEPENDENCIES
54 */
Paul Bakker5690efc2011-05-26 13:16:06 +000055
Bence Szépkúti85d92ec2025-02-28 22:32:15 +010056/* BEGIN_CASE */
57void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
58 intmax_t sizeof_x, intmax_t x, char *result)
59{
60 const char *format = (char *) ((uintptr_t) smuggle_format_expr);
61 char *output = NULL;
62 const size_t n = strlen(result);
63
64 /* Nominal case: buffer just large enough */
65 TEST_CALLOC(output, n + 1);
66 if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
67 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x));
68 } else if (sizeof_x == sizeof(long)) {
69 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x));
70 } else if (sizeof_x == sizeof(long long)) {
71 TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x));
72 } else {
73 TEST_FAIL(
74 "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");
75 }
76 TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
77
78exit:
79 mbedtls_free(output);
80 output = NULL;
81}
82/* END_CASE */
83
Bence Szépkútid5102c92025-02-28 16:22:33 +010084/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010085void debug_print_msg_threshold(int threshold, int level, char *file,
86 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020089 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020090 struct buffer_data buffer;
91
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010092 MD_PSA_INIT();
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_ssl_init(&ssl);
95 mbedtls_ssl_config_init(&conf);
96 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020097 buffer.ptr = buffer.buf;
98
Yanray Wangaad94492023-12-04 10:42:06 +080099 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
100 MBEDTLS_SSL_IS_CLIENT,
101 MBEDTLS_SSL_TRANSPORT_STREAM,
102 MBEDTLS_SSL_PRESET_DEFAULT),
103 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100104 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_debug_print_msg(&ssl, level, file, line,
112 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200115
116exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_ssl_free(&ssl);
118 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100119 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200120}
121/* END_CASE */
122
Bence Szépkútid5102c92025-02-28 16:22:33 +0100123/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
125 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200128 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200129 struct buffer_data buffer;
130
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100131 MD_PSA_INIT();
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 mbedtls_ssl_init(&ssl);
134 mbedtls_ssl_config_init(&conf);
135 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200136 buffer.ptr = buffer.buf;
137
Yanray Wangaad94492023-12-04 10:42:06 +0800138 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
139 MBEDTLS_SSL_IS_CLIENT,
140 MBEDTLS_SSL_TRANSPORT_STREAM,
141 MBEDTLS_SSL_PRESET_DEFAULT),
142 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100143 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200151
152exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_ssl_free(&ssl);
154 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100155 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200156}
157/* END_CASE */
158
Bence Szépkútid5102c92025-02-28 16:22:33 +0100159/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160void mbedtls_debug_print_buf(char *file, int line, char *text,
161 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200164 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200165 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200166
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100167 MD_PSA_INIT();
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 mbedtls_ssl_init(&ssl);
170 mbedtls_ssl_config_init(&conf);
171 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200172 buffer.ptr = buffer.buf;
173
Yanray Wangaad94492023-12-04 10:42:06 +0800174 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
175 MBEDTLS_SSL_IS_CLIENT,
176 MBEDTLS_SSL_TRANSPORT_STREAM,
177 MBEDTLS_SSL_PRESET_DEFAULT),
178 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100179 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200187
188exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_ssl_free(&ssl);
190 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100191 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200192}
193/* END_CASE */
194
Bence Szépkútid5102c92025-02-28 16:22:33 +0100195/* 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 +0100196void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
197 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_x509_crt crt;
200 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200201 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000202 struct buffer_data buffer;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_ssl_init(&ssl);
205 mbedtls_ssl_config_init(&conf);
206 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200207 MD_OR_USE_PSA_INIT();
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200210 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000211
Yanray Wangaad94492023-12-04 10:42:06 +0800212 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
213 MBEDTLS_SSL_IS_CLIENT,
214 MBEDTLS_SSL_TRANSPORT_STREAM,
215 MBEDTLS_SSL_PRESET_DEFAULT),
216 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100217 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
223 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100226
Paul Bakkerbd51b262014-07-10 15:26:12 +0200227exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_x509_crt_free(&crt);
229 mbedtls_ssl_free(&ssl);
230 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200231 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000232}
Paul Bakker33b43f12013-08-20 11:48:36 +0200233/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000234
Bence Szépkútid5102c92025-02-28 16:22:33 +0100235/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236void mbedtls_debug_print_mpi(char *value, char *file, int line,
237 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200240 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000243
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100244 MD_PSA_INIT();
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_ssl_init(&ssl);
247 mbedtls_ssl_config_init(&conf);
248 mbedtls_mpi_init(&val);
249 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200250 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251
Yanray Wangaad94492023-12-04 10:42:06 +0800252 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
253 MBEDTLS_SSL_IS_CLIENT,
254 MBEDTLS_SSL_TRANSPORT_STREAM,
255 MBEDTLS_SSL_PRESET_DEFAULT),
256 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100257 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000267
Paul Bakkerbd51b262014-07-10 15:26:12 +0200268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 mbedtls_mpi_free(&val);
270 mbedtls_ssl_free(&ssl);
271 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100272 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000273}
Paul Bakker33b43f12013-08-20 11:48:36 +0200274/* END_CASE */