blob: fca7ea0615fedcfa2a7c291df5b0cbc2d99a4295 [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"
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
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
Yanray Wang5b60b422023-12-01 17:20:22 +080069 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
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);
Jerry Yub19ccc32021-08-09 17:44:56 +080076
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
95/* BEGIN_CASE */
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 Wang5b60b422023-12-01 17:20:22 +0800110 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
111
Yanray Wangaad94492023-12-04 10:42:06 +0800112 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
113 MBEDTLS_SSL_IS_CLIENT,
114 MBEDTLS_SSL_TRANSPORT_STREAM,
115 MBEDTLS_SSL_PRESET_DEFAULT),
116 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200125
126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_ssl_free(&ssl);
128 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100129 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200130}
131/* END_CASE */
132
133/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134void mbedtls_debug_print_buf(char *file, int line, char *text,
135 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200138 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200139 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200140
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100141 MD_PSA_INIT();
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_ssl_init(&ssl);
144 mbedtls_ssl_config_init(&conf);
145 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200146 buffer.ptr = buffer.buf;
147
Yanray Wang5b60b422023-12-01 17:20:22 +0800148 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
149
Yanray Wangaad94492023-12-04 10:42:06 +0800150 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
151 MBEDTLS_SSL_IS_CLIENT,
152 MBEDTLS_SSL_TRANSPORT_STREAM,
153 MBEDTLS_SSL_PRESET_DEFAULT),
154 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200163
164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_ssl_free(&ssl);
166 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100167 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200168}
169/* END_CASE */
170
Hanno Becker612a2f12020-10-09 09:19:39 +0100171/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
173 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_x509_crt crt;
176 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200177 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000178 struct buffer_data buffer;
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_ssl_init(&ssl);
181 mbedtls_ssl_config_init(&conf);
182 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200183 MD_OR_USE_PSA_INIT();
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200186 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000187
Yanray Wang5b60b422023-12-01 17:20:22 +0800188 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
189
Yanray Wangaad94492023-12-04 10:42:06 +0800190 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
191 MBEDTLS_SSL_IS_CLIENT,
192 MBEDTLS_SSL_TRANSPORT_STREAM,
193 MBEDTLS_SSL_PRESET_DEFAULT),
194 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
201 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100204
Paul Bakkerbd51b262014-07-10 15:26:12 +0200205exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_x509_crt_free(&crt);
207 mbedtls_ssl_free(&ssl);
208 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200209 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000210}
Paul Bakker33b43f12013-08-20 11:48:36 +0200211/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214void mbedtls_debug_print_mpi(char *value, char *file, int line,
215 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200218 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000219 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000221
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100222 MD_PSA_INIT();
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_ssl_init(&ssl);
225 mbedtls_ssl_config_init(&conf);
226 mbedtls_mpi_init(&val);
227 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200228 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000229
Yanray Wang5b60b422023-12-01 17:20:22 +0800230 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
231
Yanray Wangaad94492023-12-04 10:42:06 +0800232 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
233 MBEDTLS_SSL_IS_CLIENT,
234 MBEDTLS_SSL_TRANSPORT_STREAM,
235 MBEDTLS_SSL_PRESET_DEFAULT),
236 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000247
Paul Bakkerbd51b262014-07-10 15:26:12 +0200248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_mpi_free(&val);
250 mbedtls_ssl_free(&ssl);
251 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100252 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000253}
Paul Bakker33b43f12013-08-20 11:48:36 +0200254/* END_CASE */