blob: b4692ca1f37ab359419e2994752e0990cd28932d [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úti12210522025-02-28 16:22:33 +010012#if defined(MBEDTLS_SSL_TLS_C)
Michael Schuster54300d42024-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úti12210522025-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úti12210522025-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úti12210522025-02-28 16:22:33 +010056/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010057void debug_print_msg_threshold(int threshold, int level, char *file,
58 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020061 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020062 struct buffer_data buffer;
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064 mbedtls_ssl_init(&ssl);
65 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +020066 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +010067 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020068 buffer.ptr = buffer.buf;
69
Yanray Wangaad94492023-12-04 10:42:06 +080070 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
71 MBEDTLS_SSL_IS_CLIENT,
72 MBEDTLS_SSL_TRANSPORT_STREAM,
73 MBEDTLS_SSL_PRESET_DEFAULT),
74 0);
Ronald Cronaab4a542024-02-23 18:51:11 +010075 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_debug_print_msg(&ssl, level, file, line,
83 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020086
87exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_ssl_free(&ssl);
89 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +020090 MD_OR_USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020091}
92/* END_CASE */
93
Bence Szépkúti12210522025-02-28 16:22:33 +010094/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
96 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020099 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200100 struct buffer_data buffer;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_ssl_init(&ssl);
103 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200104 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +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);
Paul Bakker57ffa552014-04-25 14:29:10 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200121
122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_ssl_free(&ssl);
124 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200125 MD_OR_USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200126}
127/* END_CASE */
128
Bence Szépkúti12210522025-02-28 16:22:33 +0100129/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130void mbedtls_debug_print_buf(char *file, int line, char *text,
131 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +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 Bakker57ffa552014-04-25 14:29:10 +0200135 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_ssl_init(&ssl);
138 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200139 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200141 buffer.ptr = buffer.buf;
142
Yanray Wangaad94492023-12-04 10:42:06 +0800143 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
144 MBEDTLS_SSL_IS_CLIENT,
145 MBEDTLS_SSL_TRANSPORT_STREAM,
146 MBEDTLS_SSL_PRESET_DEFAULT),
147 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100148 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200156
157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_ssl_free(&ssl);
159 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200160 MD_OR_USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200161}
162/* END_CASE */
163
Bence Szépkúti12210522025-02-28 16:22:33 +0100164/* 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 +0100165void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
166 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_x509_crt crt;
169 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200170 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000171 struct buffer_data buffer;
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_ssl_init(&ssl);
174 mbedtls_ssl_config_init(&conf);
175 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200176 MD_OR_USE_PSA_INIT();
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200179 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000180
Yanray Wangaad94492023-12-04 10:42:06 +0800181 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
182 MBEDTLS_SSL_IS_CLIENT,
183 MBEDTLS_SSL_TRANSPORT_STREAM,
184 MBEDTLS_SSL_PRESET_DEFAULT),
185 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100186 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
192 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100195
Paul Bakkerbd51b262014-07-10 15:26:12 +0200196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_x509_crt_free(&crt);
198 mbedtls_ssl_free(&ssl);
199 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200200 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000201}
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000203
Bence Szépkúti12210522025-02-28 16:22:33 +0100204/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205void mbedtls_debug_print_mpi(char *value, char *file, int line,
206 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000207{
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 Bakkerbe4e7dc2011-03-14 20:41:31 +0000210 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_ssl_init(&ssl);
214 mbedtls_ssl_config_init(&conf);
215 mbedtls_mpi_init(&val);
Valerio Setti3a994b72024-07-03 16:58:10 +0200216 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200218 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000219
Yanray Wangaad94492023-12-04 10:42:06 +0800220 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
221 MBEDTLS_SSL_IS_CLIENT,
222 MBEDTLS_SSL_TRANSPORT_STREAM,
223 MBEDTLS_SSL_PRESET_DEFAULT),
224 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100225 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000235
Paul Bakkerbd51b262014-07-10 15:26:12 +0200236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_mpi_free(&val);
238 mbedtls_ssl_free(&ssl);
239 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200240 MD_OR_USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241}
Paul Bakker33b43f12013-08-20 11:48:36 +0200242/* END_CASE */