blob: b5d077ca66186c8f02f6eceec5b6ba18d9aa49a3 [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 Setti16f02e02023-02-13 11:09:40 +01005#include "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
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_ssl_init(&ssl);
63 mbedtls_ssl_config_init(&conf);
64 memset(buffer.buf, 0, 2000);
Paul Bakkerc73079a2014-04-25 16:34:30 +020065 buffer.ptr = buffer.buf;
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_ssl_config_defaults(&conf,
68 MBEDTLS_SSL_IS_CLIENT,
69 MBEDTLS_SSL_TRANSPORT_STREAM,
70 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +080071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +080073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_debug_set_threshold(threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +020077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_debug_print_msg(&ssl, level, file, line,
79 "Text message, 2 == %d", 2);
Paul Bakkerc73079a2014-04-25 16:34:30 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020082
83exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_ssl_free(&ssl);
85 mbedtls_ssl_config_free(&conf);
Paul Bakkerc73079a2014-04-25 16:34:30 +020086}
87/* END_CASE */
88
89/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010090void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
91 char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +020092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020094 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020095 struct buffer_data buffer;
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_ssl_init(&ssl);
98 mbedtls_ssl_config_init(&conf);
99 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200100 buffer.ptr = buffer.buf;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_ssl_config_defaults(&conf,
103 MBEDTLS_SSL_IS_CLIENT,
104 MBEDTLS_SSL_TRANSPORT_STREAM,
105 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_debug_print_ret(&ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200114
115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ssl_free(&ssl);
117 mbedtls_ssl_config_free(&conf);
Paul Bakker57ffa552014-04-25 14:29:10 +0200118}
119/* END_CASE */
120
121/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122void mbedtls_debug_print_buf(char *file, int line, char *text,
123 data_t *data, char *result_str)
Paul Bakker57ffa552014-04-25 14:29:10 +0200124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200126 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200127 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_ssl_init(&ssl);
130 mbedtls_ssl_config_init(&conf);
131 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200132 buffer.ptr = buffer.buf;
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_ssl_config_defaults(&conf,
135 MBEDTLS_SSL_IS_CLIENT,
136 MBEDTLS_SSL_TRANSPORT_STREAM,
137 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_debug_print_buf(&ssl, 0, file, line, text, data->x, data->len);
Paul Bakker57ffa552014-04-25 14:29:10 +0200144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200146
147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_ssl_free(&ssl);
149 mbedtls_ssl_config_free(&conf);
Paul Bakker57ffa552014-04-25 14:29:10 +0200150}
151/* END_CASE */
152
Hanno Becker612a2f12020-10-09 09:19:39 +0100153/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
155 char *prefix, char *result_str)
Paul Bakker1f761152010-02-18 18:16:31 +0000156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_x509_crt crt;
158 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200159 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000160 struct buffer_data buffer;
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_ssl_init(&ssl);
163 mbedtls_ssl_config_init(&conf);
164 mbedtls_x509_crt_init(&crt);
165 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200166 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 mbedtls_ssl_config_defaults(&conf,
169 MBEDTLS_SSL_IS_CLIENT,
170 MBEDTLS_SSL_TRANSPORT_STREAM,
171 MBEDTLS_SSL_PRESET_DEFAULT);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Jerry Yub19ccc32021-08-09 17:44:56 +0800176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 TEST_ASSERT(mbedtls_x509_crt_parse_file(&crt, crt_file) == 0);
178 mbedtls_debug_print_crt(&ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100181
Paul Bakkerbd51b262014-07-10 15:26:12 +0200182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_x509_crt_free(&crt);
184 mbedtls_ssl_free(&ssl);
185 mbedtls_ssl_config_free(&conf);
Paul Bakker1f761152010-02-18 18:16:31 +0000186}
Paul Bakker33b43f12013-08-20 11:48:36 +0200187/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190void mbedtls_debug_print_mpi(char *value, char *file, int line,
191 char *prefix, char *result_str)
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200194 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000195 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_ssl_init(&ssl);
199 mbedtls_ssl_config_init(&conf);
200 mbedtls_mpi_init(&val);
201 memset(buffer.buf, 0, 2000);
Paul Bakker57ffa552014-04-25 14:29:10 +0200202 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_ssl_config_defaults(&conf,
205 MBEDTLS_SSL_IS_CLIENT,
206 MBEDTLS_SSL_TRANSPORT_STREAM,
207 MBEDTLS_SSL_PRESET_DEFAULT);
Jerry Yub19ccc32021-08-09 17:44:56 +0800208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_ssl_conf_dbg(&conf, string_debug, &buffer);
Jerry Yub19ccc32021-08-09 17:44:56 +0800210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 TEST_ASSERT(mbedtls_test_read_mpi(&val, value) == 0);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_debug_print_mpi(&ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 TEST_ASSERT(strcmp(buffer.buf, result_str) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000218
Paul Bakkerbd51b262014-07-10 15:26:12 +0200219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_mpi_free(&val);
221 mbedtls_ssl_free(&ssl);
222 mbedtls_ssl_config_free(&conf);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000223}
Paul Bakker33b43f12013-08-20 11:48:36 +0200224/* END_CASE */