blob: eeefc9597475c1158cd296e0ea4a7ed15a3eb35c [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"
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
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010061 MD_PSA_INIT();
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 mbedtls_ssl_init(&ssl);
64 mbedtls_ssl_config_init(&conf);
65 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020066 buffer.ptr = buffer.buf;
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_ssl_config_defaults(&conf,
69 MBEDTLS_SSL_IS_CLIENT,
70 MBEDTLS_SSL_TRANSPORT_STREAM,
71 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +080072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_debug_print_msg(&ssl, level, file, line,
80 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020083
84exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010085 mbedtls_ssl_free(&ssl);
86 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010087 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020088}
89/* END_CASE */
90
91/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010092void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
93 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020096 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020097 struct buffer_data buffer;
98
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010099 MD_PSA_INIT();
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_ssl_init(&ssl);
102 mbedtls_ssl_config_init(&conf);
103 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200104 buffer.ptr = buffer.buf;
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_ssl_config_defaults(&conf,
107 MBEDTLS_SSL_IS_CLIENT,
108 MBEDTLS_SSL_TRANSPORT_STREAM,
109 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200118
119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_ssl_free(&ssl);
121 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100122 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200123}
124/* END_CASE */
125
126/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void mbedtls_debug_print_buf(char *file, int line, char *text,
128 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200131 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200132 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200133
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100134 MD_PSA_INIT();
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_ssl_init(&ssl);
137 mbedtls_ssl_config_init(&conf);
138 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200139 buffer.ptr = buffer.buf;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_ssl_config_defaults(&conf,
142 MBEDTLS_SSL_IS_CLIENT,
143 MBEDTLS_SSL_TRANSPORT_STREAM,
144 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200153
154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 mbedtls_ssl_free(&ssl);
156 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100157 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200158}
159/* END_CASE */
160
Hanno Becker612a2f12020-10-09 09:19:39 +0100161/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
163 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_x509_crt crt;
166 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200167 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000168 struct buffer_data buffer;
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_ssl_init(&ssl);
171 mbedtls_ssl_config_init(&conf);
172 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200173 MD_OR_USE_PSA_INIT();
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200176 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_ssl_config_defaults(&conf,
179 MBEDTLS_SSL_IS_CLIENT,
180 MBEDTLS_SSL_TRANSPORT_STREAM,
181 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
188 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_x509_crt_free(&crt);
194 mbedtls_ssl_free(&ssl);
195 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200196 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000197}
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201void mbedtls_debug_print_mpi(char *value, char *file, int line,
202 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200205 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000206 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100209 MD_PSA_INIT();
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 mbedtls_ssl_init(&ssl);
212 mbedtls_ssl_config_init(&conf);
213 mbedtls_mpi_init(&val);
214 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200215 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_ssl_config_defaults(&conf,
218 MBEDTLS_SSL_IS_CLIENT,
219 MBEDTLS_SSL_TRANSPORT_STREAM,
220 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +0800221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000231
Paul Bakkerbd51b262014-07-10 15:26:12 +0200232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_mpi_free(&val);
234 mbedtls_ssl_free(&ssl);
235 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100236 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000237}
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* END_CASE */