blob: 371f3976f400d4e1153daefb28bb8cf3b61d3622 [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útid5102c92025-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
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010064 MD_PSA_INIT();
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_ssl_init(&ssl);
67 mbedtls_ssl_config_init(&conf);
68 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020069 buffer.ptr = buffer.buf;
70
Yanray Wangaad94492023-12-04 10:42:06 +080071 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
72 MBEDTLS_SSL_IS_CLIENT,
73 MBEDTLS_SSL_TRANSPORT_STREAM,
74 MBEDTLS_SSL_PRESET_DEFAULT),
75 0);
Ronald Cronaab4a542024-02-23 18:51:11 +010076 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_debug_print_msg(&ssl, level, file, line,
84 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020087
88exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010089 mbedtls_ssl_free(&ssl);
90 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010091 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020092}
93/* END_CASE */
94
Bence Szépkútid5102c92025-02-28 16:22:33 +010095/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010096void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
97 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200100 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200101 struct buffer_data buffer;
102
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100103 MD_PSA_INIT();
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_ssl_init(&ssl);
106 mbedtls_ssl_config_init(&conf);
107 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200108 buffer.ptr = buffer.buf;
109
Yanray Wangaad94492023-12-04 10:42:06 +0800110 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
111 MBEDTLS_SSL_IS_CLIENT,
112 MBEDTLS_SSL_TRANSPORT_STREAM,
113 MBEDTLS_SSL_PRESET_DEFAULT),
114 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100115 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200123
124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_ssl_free(&ssl);
126 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100127 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200128}
129/* END_CASE */
130
Bence Szépkútid5102c92025-02-28 16:22:33 +0100131/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132void mbedtls_debug_print_buf(char *file, int line, char *text,
133 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200136 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200137 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200138
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100139 MD_PSA_INIT();
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_ssl_init(&ssl);
142 mbedtls_ssl_config_init(&conf);
143 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200144 buffer.ptr = buffer.buf;
145
Yanray Wangaad94492023-12-04 10:42:06 +0800146 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
147 MBEDTLS_SSL_IS_CLIENT,
148 MBEDTLS_SSL_TRANSPORT_STREAM,
149 MBEDTLS_SSL_PRESET_DEFAULT),
150 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100151 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200159
160exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_free(&ssl);
162 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100163 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200164}
165/* END_CASE */
166
Bence Szépkútid5102c92025-02-28 16:22:33 +0100167/* 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 +0100168void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
169 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_x509_crt crt;
172 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200173 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000174 struct buffer_data buffer;
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 mbedtls_ssl_init(&ssl);
177 mbedtls_ssl_config_init(&conf);
178 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200179 MD_OR_USE_PSA_INIT();
180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200182 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000183
Yanray Wangaad94492023-12-04 10:42:06 +0800184 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
185 MBEDTLS_SSL_IS_CLIENT,
186 MBEDTLS_SSL_TRANSPORT_STREAM,
187 MBEDTLS_SSL_PRESET_DEFAULT),
188 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100189 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
195 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100198
Paul Bakkerbd51b262014-07-10 15:26:12 +0200199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_x509_crt_free(&crt);
201 mbedtls_ssl_free(&ssl);
202 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200203 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000204}
Paul Bakker33b43f12013-08-20 11:48:36 +0200205/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000206
Bence Szépkútid5102c92025-02-28 16:22:33 +0100207/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208void mbedtls_debug_print_mpi(char *value, char *file, int line,
209 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200212 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000213 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000215
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100216 MD_PSA_INIT();
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_ssl_init(&ssl);
219 mbedtls_ssl_config_init(&conf);
220 mbedtls_mpi_init(&val);
221 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200222 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000223
Yanray Wangaad94492023-12-04 10:42:06 +0800224 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
225 MBEDTLS_SSL_IS_CLIENT,
226 MBEDTLS_SSL_TRANSPORT_STREAM,
227 MBEDTLS_SSL_PRESET_DEFAULT),
228 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100229 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000239
Paul Bakkerbd51b262014-07-10 15:26:12 +0200240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_mpi_free(&val);
242 mbedtls_ssl_free(&ssl);
243 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100244 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000245}
Paul Bakker33b43f12013-08-20 11:48:36 +0200246/* END_CASE */