blob: dad4a535f1e40fe565d00bda3f6c153e3445e1e2 [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"
Valerio Setti1b08d422023-02-13 11:33:26 +01005#include "mbedtls/pk.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
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020012void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000013{
14 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020015 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000016 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000017
Gilles Peskine449bd832023-01-11 14:50:10 +010018 memcpy(p, file, strlen(file));
19 p += strlen(file);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020020
21 *p++ = '(';
Gilles Peskine449bd832023-01-11 14:50:10 +010022 *p++ = '0' + (line / 1000) % 10;
23 *p++ = '0' + (line / 100) % 10;
24 *p++ = '0' + (line / 10) % 10;
25 *p++ = '0' + (line / 1) % 10;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020026 *p++ = ')';
27 *p++ = ':';
28 *p++ = ' ';
29
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020030#if defined(MBEDTLS_THREADING_C)
31 /* Skip "thread ID" (up to the first space) as it is not predictable */
Gilles Peskine449bd832023-01-11 14:50:10 +010032 while (*str++ != ' ') {
33 ;
34 }
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020035#endif
36
Gilles Peskine449bd832023-01-11 14:50:10 +010037 memcpy(p, str, strlen(str));
38 p += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020039
40 /* Detect if debug messages output partial lines and mark them */
Gilles Peskine449bd832023-01-11 14:50:10 +010041 if (p[-1] != '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020042 *p++ = '*';
Gilles Peskine449bd832023-01-11 14:50:10 +010043 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020044
45 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000046}
Paul Bakker33b43f12013-08-20 11:48:36 +020047/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000048
Paul Bakker33b43f12013-08-20 11:48:36 +020049/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020051 * END_DEPENDENCIES
52 */
Paul Bakker5690efc2011-05-26 13:16:06 +000053
Paul Bakker57ffa552014-04-25 14:29:10 +020054/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010055void debug_print_msg_threshold(int threshold, int level, char *file,
56 int line, char *result_str)
Paul Bakkerc73079a2014-04-25 16:34:30 +020057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020059 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020060 struct buffer_data buffer;
61
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010062 MD_PSA_INIT();
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064 mbedtls_ssl_init(&ssl);
65 mbedtls_ssl_config_init(&conf);
66 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020067 buffer.ptr = buffer.buf;
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_ssl_config_defaults(&conf,
70 MBEDTLS_SSL_IS_CLIENT,
71 MBEDTLS_SSL_TRANSPORT_STREAM,
72 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +080073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_debug_print_msg(&ssl, level, file, line,
81 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020084
85exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010086 mbedtls_ssl_free(&ssl);
87 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010088 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020089}
90/* END_CASE */
91
92/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010093void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
94 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020097 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020098 struct buffer_data buffer;
99
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100100 MD_PSA_INIT();
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_ssl_init(&ssl);
103 mbedtls_ssl_config_init(&conf);
104 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200105 buffer.ptr = buffer.buf;
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_ssl_config_defaults(&conf,
108 MBEDTLS_SSL_IS_CLIENT,
109 MBEDTLS_SSL_TRANSPORT_STREAM,
110 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200119
120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_ssl_free(&ssl);
122 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100123 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200124}
125/* END_CASE */
126
127/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128void mbedtls_debug_print_buf(char *file, int line, char *text,
129 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200132 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200133 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200134
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100135 MD_PSA_INIT();
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_ssl_init(&ssl);
138 mbedtls_ssl_config_init(&conf);
139 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200140 buffer.ptr = buffer.buf;
141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_ssl_config_defaults(&conf,
143 MBEDTLS_SSL_IS_CLIENT,
144 MBEDTLS_SSL_TRANSPORT_STREAM,
145 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200154
155exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_ssl_free(&ssl);
157 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100158 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200159}
160/* END_CASE */
161
Hanno Becker612a2f12020-10-09 09:19:39 +0100162/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
164 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_x509_crt crt;
167 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200168 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000169 struct buffer_data buffer;
170
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100171 MD_PSA_INIT();
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);
176 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200177 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_ssl_config_defaults(&conf,
180 MBEDTLS_SSL_IS_CLIENT,
181 MBEDTLS_SSL_TRANSPORT_STREAM,
182 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
189 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100192
Paul Bakkerbd51b262014-07-10 15:26:12 +0200193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 mbedtls_x509_crt_free(&crt);
195 mbedtls_ssl_free(&ssl);
196 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100197 MD_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000198}
Paul Bakker33b43f12013-08-20 11:48:36 +0200199/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202void mbedtls_debug_print_mpi(char *value, char *file, int line,
203 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200206 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000207 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000209
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100210 MD_PSA_INIT();
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 mbedtls_ssl_init(&ssl);
213 mbedtls_ssl_config_init(&conf);
214 mbedtls_mpi_init(&val);
215 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200216 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_ssl_config_defaults(&conf,
219 MBEDTLS_SSL_IS_CLIENT,
220 MBEDTLS_SSL_TRANSPORT_STREAM,
221 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +0800222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000232
Paul Bakkerbd51b262014-07-10 15:26:12 +0200233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_mpi_free(&val);
235 mbedtls_ssl_free(&ssl);
236 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100237 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000238}
Paul Bakker33b43f12013-08-20 11:48:36 +0200239/* END_CASE */