blob: 21eefc59db7a9e2eb7ae8486486949f3ea554921 [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"
Paul Bakker1f761152010-02-18 18:16:31 +00004
5struct buffer_data
6{
7 char buf[2000];
8 char *ptr;
9};
10
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020011void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000012{
13 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020014 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000015 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000016
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020017 memcpy( p, file, strlen( file ) );
18 p += strlen( file );
19
20 *p++ = '(';
21 *p++ = '0' + ( line / 1000 ) % 10;
22 *p++ = '0' + ( line / 100 ) % 10;
23 *p++ = '0' + ( line / 10 ) % 10;
24 *p++ = '0' + ( line / 1 ) % 10;
25 *p++ = ')';
26 *p++ = ':';
27 *p++ = ' ';
28
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020029#if defined(MBEDTLS_THREADING_C)
30 /* Skip "thread ID" (up to the first space) as it is not predictable */
31 while( *str++ != ' ' );
32#endif
33
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020034 memcpy( p, str, strlen( str ) );
35 p += strlen( str );
Paul Bakker92478c32014-04-25 15:18:34 +020036
37 /* Detect if debug messages output partial lines and mark them */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020038 if( p[-1] != '\n' )
39 *p++ = '*';
40
41 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000042}
Paul Bakker33b43f12013-08-20 11:48:36 +020043/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000044
Paul Bakker33b43f12013-08-20 11:48:36 +020045/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020047 * END_DEPENDENCIES
48 */
Paul Bakker5690efc2011-05-26 13:16:06 +000049
Paul Bakker57ffa552014-04-25 14:29:10 +020050/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010051void debug_print_msg_threshold( int threshold, int level, char * file,
52 int line, char * result_str )
Paul Bakkerc73079a2014-04-25 16:34:30 +020053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020055 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020056 struct buffer_data buffer;
57
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020058 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020059 mbedtls_ssl_config_init( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020060 memset( buffer.buf, 0, 2000 );
61 buffer.ptr = buffer.buf;
62
Jerry Yub19ccc32021-08-09 17:44:56 +080063 mbedtls_ssl_config_defaults( &conf,
64 MBEDTLS_SSL_IS_CLIENT,
65 MBEDTLS_SSL_TRANSPORT_STREAM,
66 MBEDTLS_SSL_PRESET_DEFAULT );
67
68 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
69
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020070 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_debug_set_threshold( threshold );
Paul Bakkerc73079a2014-04-25 16:34:30 +020073
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020074 mbedtls_debug_print_msg( &ssl, level, file, line,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020075 "Text message, 2 == %d", 2 );
Paul Bakkerc73079a2014-04-25 16:34:30 +020076
77 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020078
79exit:
80 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020081 mbedtls_ssl_config_free( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020082}
83/* END_CASE */
84
85/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010086void mbedtls_debug_print_ret( char * file, int line, char * text, int value,
87 char * result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +020088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020090 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020091 struct buffer_data buffer;
92
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020093 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020094 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020095 memset( buffer.buf, 0, 2000 );
96 buffer.ptr = buffer.buf;
97
Jerry Yub19ccc32021-08-09 17:44:56 +080098 mbedtls_ssl_config_defaults( &conf,
99 MBEDTLS_SSL_IS_CLIENT,
100 MBEDTLS_SSL_TRANSPORT_STREAM,
101 MBEDTLS_SSL_PRESET_DEFAULT );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200102
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200103 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200104
Jerry Yub19ccc32021-08-09 17:44:56 +0800105 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200108
109 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200110
111exit:
112 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200113 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200114}
115/* END_CASE */
116
117/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100118void mbedtls_debug_print_buf( char * file, int line, char * text,
Azim Khan5fcca462018-06-29 11:05:32 +0100119 data_t * data, char * result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +0200120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200122 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200123 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200124
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200125 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200126 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200127 memset( buffer.buf, 0, 2000 );
128 buffer.ptr = buffer.buf;
129
Jerry Yub19ccc32021-08-09 17:44:56 +0800130 mbedtls_ssl_config_defaults( &conf,
131 MBEDTLS_SSL_IS_CLIENT,
132 MBEDTLS_SSL_TRANSPORT_STREAM,
133 MBEDTLS_SSL_PRESET_DEFAULT );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200134
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200135 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200136
Jerry Yub19ccc32021-08-09 17:44:56 +0800137 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
138
Azim Khand30ca132017-06-09 04:32:58 +0100139 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data->x, data->len );
Paul Bakker57ffa552014-04-25 14:29:10 +0200140
141 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200142
143exit:
144 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200145 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200146}
147/* END_CASE */
148
Hanno Becker612a2f12020-10-09 09:19:39 +0100149/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100150void mbedtls_debug_print_crt( char * crt_file, char * file, int line,
151 char * prefix, char * result_str )
Paul Bakker1f761152010-02-18 18:16:31 +0000152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_x509_crt crt;
154 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200155 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000156 struct buffer_data buffer;
157
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200158 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200159 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_x509_crt_init( &crt );
Paul Bakker1f761152010-02-18 18:16:31 +0000161 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200162 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000163
Jerry Yub19ccc32021-08-09 17:44:56 +0800164 mbedtls_ssl_config_defaults( &conf,
165 MBEDTLS_SSL_IS_CLIENT,
166 MBEDTLS_SSL_TRANSPORT_STREAM,
167 MBEDTLS_SSL_PRESET_DEFAULT );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200169 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000170
Jerry Yub19ccc32021-08-09 17:44:56 +0800171 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
174 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000175
Paul Bakker33b43f12013-08-20 11:48:36 +0200176 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100177
Paul Bakkerbd51b262014-07-10 15:26:12 +0200178exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200180 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200181 mbedtls_ssl_config_free( &conf );
Paul Bakker1f761152010-02-18 18:16:31 +0000182}
Paul Bakker33b43f12013-08-20 11:48:36 +0200183/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100186void mbedtls_debug_print_mpi( int radix, char * value, char * file, int line,
187 char * prefix, char * result_str )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200190 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000191 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000193
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200194 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200195 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_mpi_init( &val );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000197 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200198 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000199
Jerry Yub19ccc32021-08-09 17:44:56 +0800200 mbedtls_ssl_config_defaults( &conf,
201 MBEDTLS_SSL_IS_CLIENT,
202 MBEDTLS_SSL_TRANSPORT_STREAM,
203 MBEDTLS_SSL_PRESET_DEFAULT );
204
205 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
206
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200207 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200208
Werner Lewis19b4cd82022-07-07 11:02:27 +0100209 TEST_ASSERT( mbedtls_test_read_mpi( &val, value ) == 0 );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212
Paul Bakker33b43f12013-08-20 11:48:36 +0200213 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000214
Paul Bakkerbd51b262014-07-10 15:26:12 +0200215exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_mpi_free( &val );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200217 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200218 mbedtls_ssl_config_free( &conf );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000219}
Paul Bakker33b43f12013-08-20 11:48:36 +0200220/* END_CASE */