blob: fd5d1f52693e3f3bbd7fa7d7d9ebaea441b63f54 [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 Wangaad94492023-12-04 10:42:06 +080069 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
70 MBEDTLS_SSL_IS_CLIENT,
71 MBEDTLS_SSL_TRANSPORT_STREAM,
72 MBEDTLS_SSL_PRESET_DEFAULT),
73 0);
Ronald Cronaab4a542024-02-23 18:51:11 +010074 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_debug_print_msg(&ssl, level, file, line,
82 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020085
86exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_ssl_free(&ssl);
88 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010089 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020090}
91/* END_CASE */
92
93/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010094void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
95 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020098 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020099 struct buffer_data buffer;
100
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100101 MD_PSA_INIT();
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_ssl_init(&ssl);
104 mbedtls_ssl_config_init(&conf);
105 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200106 buffer.ptr = buffer.buf;
107
Yanray Wangaad94492023-12-04 10:42:06 +0800108 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
109 MBEDTLS_SSL_IS_CLIENT,
110 MBEDTLS_SSL_TRANSPORT_STREAM,
111 MBEDTLS_SSL_PRESET_DEFAULT),
112 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100113 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200121
122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_ssl_free(&ssl);
124 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100125 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200126}
127/* END_CASE */
128
129/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130void mbedtls_debug_print_buf(char *file, int line, char *text,
131 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200134 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200135 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200136
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100137 MD_PSA_INIT();
138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 mbedtls_ssl_init(&ssl);
140 mbedtls_ssl_config_init(&conf);
141 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200142 buffer.ptr = buffer.buf;
143
Yanray Wangaad94492023-12-04 10:42:06 +0800144 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
145 MBEDTLS_SSL_IS_CLIENT,
146 MBEDTLS_SSL_TRANSPORT_STREAM,
147 MBEDTLS_SSL_PRESET_DEFAULT),
148 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100149 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200157
158exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_ssl_free(&ssl);
160 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100161 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200162}
163/* END_CASE */
164
Hanno Becker612a2f12020-10-09 09:19:39 +0100165/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
167 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_x509_crt crt;
170 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200171 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000172 struct buffer_data buffer;
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_ssl_init(&ssl);
175 mbedtls_ssl_config_init(&conf);
176 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200177 MD_OR_USE_PSA_INIT();
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200180 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000181
Yanray Wangaad94492023-12-04 10:42:06 +0800182 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
183 MBEDTLS_SSL_IS_CLIENT,
184 MBEDTLS_SSL_TRANSPORT_STREAM,
185 MBEDTLS_SSL_PRESET_DEFAULT),
186 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100187 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
193 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100196
Paul Bakkerbd51b262014-07-10 15:26:12 +0200197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_x509_crt_free(&crt);
199 mbedtls_ssl_free(&ssl);
200 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200201 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000202}
Paul Bakker33b43f12013-08-20 11:48:36 +0200203/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206void mbedtls_debug_print_mpi(char *value, char *file, int line,
207 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200210 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000211 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000213
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100214 MD_PSA_INIT();
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_ssl_init(&ssl);
217 mbedtls_ssl_config_init(&conf);
218 mbedtls_mpi_init(&val);
219 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200220 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000221
Yanray Wangaad94492023-12-04 10:42:06 +0800222 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
223 MBEDTLS_SSL_IS_CLIENT,
224 MBEDTLS_SSL_TRANSPORT_STREAM,
225 MBEDTLS_SSL_PRESET_DEFAULT),
226 0);
Ronald Cronaab4a542024-02-23 18:51:11 +0100227 mbedtls_ssl_conf_rng(&conf, mbedtls_test_random, NULL);
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000237
Paul Bakkerbd51b262014-07-10 15:26:12 +0200238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_mpi_free(&val);
240 mbedtls_ssl_free(&ssl);
241 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100242 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000243}
Paul Bakker33b43f12013-08-20 11:48:36 +0200244/* END_CASE */