blob: 5cd02b95a2a92de7c7a4e00b5bd46c9a9135c444 [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
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
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_ssl_init(&ssl);
63 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +020064 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +010065 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020066 buffer.ptr = buffer.buf;
67
Yanray Wangaad94492023-12-04 10:42:06 +080068 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
69 MBEDTLS_SSL_IS_CLIENT,
70 MBEDTLS_SSL_TRANSPORT_STREAM,
71 MBEDTLS_SSL_PRESET_DEFAULT),
72 0);
Ronald Cronaab4a542024-02-23 18:51:11 +010073 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
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);
Valerio Setti84733902024-06-27 08:05:09 +020088 MD_OR_USE_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
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_ssl_init(&ssl);
101 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200102 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200104 buffer.ptr = buffer.buf;
105
Yanray Wangaad94492023-12-04 10:42:06 +0800106 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
107 MBEDTLS_SSL_IS_CLIENT,
108 MBEDTLS_SSL_TRANSPORT_STREAM,
109 MBEDTLS_SSL_PRESET_DEFAULT),
110 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100111 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
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);
Valerio Setti84733902024-06-27 08:05:09 +0200123 MD_OR_USE_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
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_ssl_init(&ssl);
136 mbedtls_ssl_config_init(&conf);
Valerio Setti3a994b72024-07-03 16:58:10 +0200137 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200139 buffer.ptr = buffer.buf;
140
Yanray Wangaad94492023-12-04 10:42:06 +0800141 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
142 MBEDTLS_SSL_IS_CLIENT,
143 MBEDTLS_SSL_TRANSPORT_STREAM,
144 MBEDTLS_SSL_PRESET_DEFAULT),
145 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100146 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
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);
Valerio Setti84733902024-06-27 08:05:09 +0200158 MD_OR_USE_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
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_ssl_init(&ssl);
172 mbedtls_ssl_config_init(&conf);
173 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200174 MD_OR_USE_PSA_INIT();
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 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
Yanray Wangaad94492023-12-04 10:42:06 +0800179 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
180 MBEDTLS_SSL_IS_CLIENT,
181 MBEDTLS_SSL_TRANSPORT_STREAM,
182 MBEDTLS_SSL_PRESET_DEFAULT),
183 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100184 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
190 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100193
Paul Bakkerbd51b262014-07-10 15:26:12 +0200194exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_x509_crt_free(&crt);
196 mbedtls_ssl_free(&ssl);
197 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200198 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000199}
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203void mbedtls_debug_print_mpi(char *value, char *file, int line,
204 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200207 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 mbedtls_ssl_init(&ssl);
212 mbedtls_ssl_config_init(&conf);
213 mbedtls_mpi_init(&val);
Valerio Setti3a994b72024-07-03 16:58:10 +0200214 MD_OR_USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 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
Yanray Wangaad94492023-12-04 10:42:06 +0800218 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
219 MBEDTLS_SSL_IS_CLIENT,
220 MBEDTLS_SSL_TRANSPORT_STREAM,
221 MBEDTLS_SSL_PRESET_DEFAULT),
222 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100223 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000233
Paul Bakkerbd51b262014-07-10 15:26:12 +0200234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_mpi_free(&val);
236 mbedtls_ssl_free(&ssl);
237 mbedtls_ssl_config_free(&conf);
Valerio Setti84733902024-06-27 08:05:09 +0200238 MD_OR_USE_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000239}
Paul Bakker33b43f12013-08-20 11:48:36 +0200240/* END_CASE */