blob: 6a3ca331a963812507da016f56c1acd9457573c1 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/debug.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Przemek Stekiel98b1af42022-10-18 13:16:04 +02004#include "mbedtls/legacy_or_psa.h"
Paul Bakker1f761152010-02-18 18:16:31 +00005
Gilles Peskine449bd832023-01-11 14:50:10 +01006struct buffer_data {
Paul Bakker1f761152010-02-18 18:16:31 +00007 char buf[2000];
8 char *ptr;
9};
10
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020011void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000012{
13 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020014 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000015 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000016
Gilles Peskine449bd832023-01-11 14:50:10 +010017 memcpy(p, file, strlen(file));
18 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020019
20 *p++ = '(';
Gilles Peskine449bd832023-01-11 14:50:10 +010021 *p++ = '0' + (line / 1000) % 10;
22 *p++ = '0' + (line / 100) % 10;
23 *p++ = '0' + (line / 10) % 10;
24 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020025 *p++ = ')';
26 *p++ = ':';
27 *p++ = ' ';
28
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020029#if defined(MBEDTLS_THREADING_C)
30 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine449bd832023-01-11 14:50:10 +010031 while (*str++ != ' ') {
32 ;
33 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020034#endif
35
Gilles Peskine449bd832023-01-11 14:50:10 +010036 memcpy(p, str, strlen(str));
37 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020038
39 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine449bd832023-01-11 14:50:10 +010040 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020041 *p++ = '*';
Gilles Peskine449bd832023-01-11 14:50:10 +010042 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020043
44 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000045}
Paul Bakker33b43f12013-08-20 11:48:36 +020046/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000047
Paul Bakker33b43f12013-08-20 11:48:36 +020048/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020050 * END_DEPENDENCIES
51 */
Paul Bakker5690efc2011-05-26 13:16:06 +000052
Paul Bakker57ffa552014-04-25 14:29:10 +020053/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010054void debug_print_msg_threshold(int threshold, int level, char *file,
55 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020058 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020059 struct buffer_data buffer;
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_ssl_init(&ssl);
62 mbedtls_ssl_config_init(&conf);
63 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020064 buffer.ptr = buffer.buf;
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_ssl_config_defaults(&conf,
67 MBEDTLS_SSL_IS_CLIENT,
68 MBEDTLS_SSL_TRANSPORT_STREAM,
69 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +080070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_debug_print_msg(&ssl, level, file, line,
78 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020081
82exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_ssl_free(&ssl);
84 mbedtls_ssl_config_free(&conf);
Paul Bakkerc73079a2014-04-25 16:34:30 +020085}
86/* END_CASE */
87
88/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010089void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
90 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020093 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020094 struct buffer_data buffer;
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096 mbedtls_ssl_init(&ssl);
97 mbedtls_ssl_config_init(&conf);
98 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +020099 buffer.ptr = buffer.buf;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_ssl_config_defaults(&conf,
102 MBEDTLS_SSL_IS_CLIENT,
103 MBEDTLS_SSL_TRANSPORT_STREAM,
104 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200113
114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_ssl_free(&ssl);
116 mbedtls_ssl_config_free(&conf);
Paul Bakker57ffa552014-04-25 14:29:10 +0200117}
118/* END_CASE */
119
120/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121void mbedtls_debug_print_buf(char *file, int line, char *text,
122 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200125 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200126 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_ssl_init(&ssl);
129 mbedtls_ssl_config_init(&conf);
130 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200131 buffer.ptr = buffer.buf;
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 mbedtls_ssl_config_defaults(&conf,
134 MBEDTLS_SSL_IS_CLIENT,
135 MBEDTLS_SSL_TRANSPORT_STREAM,
136 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200145
146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_ssl_free(&ssl);
148 mbedtls_ssl_config_free(&conf);
Paul Bakker57ffa552014-04-25 14:29:10 +0200149}
150/* END_CASE */
151
Hanno Becker612a2f12020-10-09 09:19:39 +0100152/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
154 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_x509_crt crt;
157 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200158 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000159 struct buffer_data buffer;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_init(&ssl);
162 mbedtls_ssl_config_init(&conf);
163 mbedtls_x509_crt_init(&crt);
164 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200165 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_ssl_config_defaults(&conf,
168 MBEDTLS_SSL_IS_CLIENT,
169 MBEDTLS_SSL_TRANSPORT_STREAM,
170 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
177 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100180
Paul Bakkerbd51b262014-07-10 15:26:12 +0200181exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_x509_crt_free(&crt);
183 mbedtls_ssl_free(&ssl);
184 mbedtls_ssl_config_free(&conf);
Paul Bakker1f761152010-02-18 18:16:31 +0000185}
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189void mbedtls_debug_print_mpi(char *value, char *file, int line,
190 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000191{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200193 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000194 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_ssl_init(&ssl);
198 mbedtls_ssl_config_init(&conf);
199 mbedtls_mpi_init(&val);
200 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200201 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_ssl_config_defaults(&conf,
204 MBEDTLS_SSL_IS_CLIENT,
205 MBEDTLS_SSL_TRANSPORT_STREAM,
206 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +0800207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000217
Paul Bakkerbd51b262014-07-10 15:26:12 +0200218exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_mpi_free(&val);
220 mbedtls_ssl_free(&ssl);
221 mbedtls_ssl_config_free(&conf);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000222}
Paul Bakker33b43f12013-08-20 11:48:36 +0200223/* END_CASE */