blob: a8a8c68fa354930dba6c7830155828fcdd6ee280 [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útic6a8bf02025-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úti12210522025-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
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_ssl_init(&ssl);
93 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +020094 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +010095 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020096 buffer.ptr = buffer.buf;
97
Yanray Wangaad94492023-12-04 10:42:06 +080098 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
99 MBEDTLS_SSL_IS_CLIENT,
100 MBEDTLS_SSL_TRANSPORT_STREAM,
101 MBEDTLS_SSL_PRESET_DEFAULT),
102 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100103 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_debug_print_msg(&ssl, level, file, line,
111 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200114
115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ssl_free(&ssl);
117 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200118 MD_OR_USE_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +0200119}
120/* END_CASE */
121
Bence Szépkúti12210522025-02-28 16:22:33 +0100122/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
124 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200127 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200128 struct buffer_data buffer;
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_ssl_init(&ssl);
131 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200132 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200134 buffer.ptr = buffer.buf;
135
Yanray Wangaad94492023-12-04 10:42:06 +0800136 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
137 MBEDTLS_SSL_IS_CLIENT,
138 MBEDTLS_SSL_TRANSPORT_STREAM,
139 MBEDTLS_SSL_PRESET_DEFAULT),
140 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100141 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200149
150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_ssl_free(&ssl);
152 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200153 MD_OR_USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200154}
155/* END_CASE */
156
Bence Szépkúti12210522025-02-28 16:22:33 +0100157/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158void mbedtls_debug_print_buf(char *file, int line, char *text,
159 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200162 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200163 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_ssl_init(&ssl);
166 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200167 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200169 buffer.ptr = buffer.buf;
170
Yanray Wangaad94492023-12-04 10:42:06 +0800171 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
172 MBEDTLS_SSL_IS_CLIENT,
173 MBEDTLS_SSL_TRANSPORT_STREAM,
174 MBEDTLS_SSL_PRESET_DEFAULT),
175 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100176 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200184
185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_ssl_free(&ssl);
187 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200188 MD_OR_USE_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200189}
190/* END_CASE */
191
Bence Szépkúti12210522025-02-28 16:22:33 +0100192/* 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 +0100193void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
194 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000195{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_x509_crt crt;
197 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200198 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000199 struct buffer_data buffer;
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 mbedtls_ssl_init(&ssl);
202 mbedtls_ssl_config_init(&conf);
203 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200204 MD_OR_USE_PSA_INIT();
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200207 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000208
Yanray Wangaad94492023-12-04 10:42:06 +0800209 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
210 MBEDTLS_SSL_IS_CLIENT,
211 MBEDTLS_SSL_TRANSPORT_STREAM,
212 MBEDTLS_SSL_PRESET_DEFAULT),
213 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100214 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
220 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100223
Paul Bakkerbd51b262014-07-10 15:26:12 +0200224exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 mbedtls_x509_crt_free(&crt);
226 mbedtls_ssl_free(&ssl);
227 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200228 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000229}
Paul Bakker33b43f12013-08-20 11:48:36 +0200230/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000231
Bence Szépkúti12210522025-02-28 16:22:33 +0100232/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233void mbedtls_debug_print_mpi(char *value, char *file, int line,
234 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200237 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000238 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_ssl_init(&ssl);
242 mbedtls_ssl_config_init(&conf);
243 mbedtls_mpi_init(&val);
Valerio Setti3a994b72024-07-03 16:58:10 +0200244 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200246 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247
Yanray Wangaad94492023-12-04 10:42:06 +0800248 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
249 MBEDTLS_SSL_IS_CLIENT,
250 MBEDTLS_SSL_TRANSPORT_STREAM,
251 MBEDTLS_SSL_PRESET_DEFAULT),
252 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100253 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000263
Paul Bakkerbd51b262014-07-10 15:26:12 +0200264exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_mpi_free(&val);
266 mbedtls_ssl_free(&ssl);
267 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200268 MD_OR_USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000269}
Paul Bakker33b43f12013-08-20 11:48:36 +0200270/* END_CASE */