blob: c96e305d4f26ef310720c818004340f97a8b0c0b [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
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_ssl_config_defaults(&conf,
72 MBEDTLS_SSL_IS_CLIENT,
73 MBEDTLS_SSL_TRANSPORT_STREAM,
74 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +080075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_debug_print_msg(&ssl, level, file, line,
83 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020086
87exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_ssl_free(&ssl);
89 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010090 MD_PSA_DONE();
Paul Bakkerc73079a2014-04-25 16:34:30 +020091}
92/* END_CASE */
93
94/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
96 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020099 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200100 struct buffer_data buffer;
101
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100102 MD_PSA_INIT();
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_ssl_init(&ssl);
105 mbedtls_ssl_config_init(&conf);
106 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200107 buffer.ptr = buffer.buf;
108
Yanray Wang5b60b422023-12-01 17:20:22 +0800109 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_ssl_config_defaults(&conf,
112 MBEDTLS_SSL_IS_CLIENT,
113 MBEDTLS_SSL_TRANSPORT_STREAM,
114 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200123
124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_ssl_free(&ssl);
126 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100127 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200128}
129/* END_CASE */
130
131/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132void mbedtls_debug_print_buf(char *file, int line, char *text,
133 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200136 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200137 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200138
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100139 MD_PSA_INIT();
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_ssl_init(&ssl);
142 mbedtls_ssl_config_init(&conf);
143 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200144 buffer.ptr = buffer.buf;
145
Yanray Wang5b60b422023-12-01 17:20:22 +0800146 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_ssl_config_defaults(&conf,
149 MBEDTLS_SSL_IS_CLIENT,
150 MBEDTLS_SSL_TRANSPORT_STREAM,
151 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200160
161exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_ssl_free(&ssl);
163 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100164 MD_PSA_DONE();
Paul Bakker57ffa552014-04-25 14:29:10 +0200165}
166/* END_CASE */
167
Hanno Becker612a2f12020-10-09 09:19:39 +0100168/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
170 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_x509_crt crt;
173 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200174 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000175 struct buffer_data buffer;
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_ssl_init(&ssl);
178 mbedtls_ssl_config_init(&conf);
179 mbedtls_x509_crt_init(&crt);
Valerio Setti92c3f362023-05-17 15:36:44 +0200180 MD_OR_USE_PSA_INIT();
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200183 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000184
Yanray Wang5b60b422023-12-01 17:20:22 +0800185 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_ssl_config_defaults(&conf,
188 MBEDTLS_SSL_IS_CLIENT,
189 MBEDTLS_SSL_TRANSPORT_STREAM,
190 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
197 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100200
Paul Bakkerbd51b262014-07-10 15:26:12 +0200201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_x509_crt_free(&crt);
203 mbedtls_ssl_free(&ssl);
204 mbedtls_ssl_config_free(&conf);
Valerio Setti92c3f362023-05-17 15:36:44 +0200205 MD_OR_USE_PSA_DONE();
Paul Bakker1f761152010-02-18 18:16:31 +0000206}
Paul Bakker33b43f12013-08-20 11:48:36 +0200207/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210void mbedtls_debug_print_mpi(char *value, char *file, int line,
211 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200214 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000215 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000217
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100218 MD_PSA_INIT();
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_ssl_init(&ssl);
221 mbedtls_ssl_config_init(&conf);
222 mbedtls_mpi_init(&val);
223 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200224 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000225
Yanray Wang5b60b422023-12-01 17:20:22 +0800226 mbedtls_ssl_conf_rng(&conf, rng_get, NULL);
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_ssl_config_defaults(&conf,
229 MBEDTLS_SSL_IS_CLIENT,
230 MBEDTLS_SSL_TRANSPORT_STREAM,
231 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +0800232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000242
Paul Bakkerbd51b262014-07-10 15:26:12 +0200243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_mpi_free(&val);
245 mbedtls_ssl_free(&ssl);
246 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +0100247 MD_PSA_DONE();
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000248}
Paul Bakker33b43f12013-08-20 11:48:36 +0200249/* END_CASE */