blob: 85544b560cb5be2bd7bb095a71416b3c20e7310e [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"
Paul Bakker1f761152010-02-18 18:16:31 +00005
6struct buffer_data
7{
8 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
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020018 memcpy( p, file, strlen( file ) );
19 p += strlen( file );
20
21 *p++ = '(';
22 *p++ = '0' + ( line / 1000 ) % 10;
23 *p++ = '0' + ( line / 100 ) % 10;
24 *p++ = '0' + ( line / 10 ) % 10;
25 *p++ = '0' + ( line / 1 ) % 10;
26 *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 */
32 while( *str++ != ' ' );
33#endif
34
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020035 memcpy( p, str, strlen( str ) );
36 p += strlen( str );
Paul Bakker92478c32014-04-25 15:18:34 +020037
38 /* Detect if debug messages output partial lines and mark them */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020039 if( p[-1] != '\n' )
40 *p++ = '*';
41
42 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000043}
Paul Bakker33b43f12013-08-20 11:48:36 +020044/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000045
Paul Bakker33b43f12013-08-20 11:48:36 +020046/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020048 * END_DEPENDENCIES
49 */
Paul Bakker5690efc2011-05-26 13:16:06 +000050
Paul Bakker57ffa552014-04-25 14:29:10 +020051/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010052void debug_print_msg_threshold( int threshold, int level, char * file,
53 int line, char * result_str )
Paul Bakkerc73079a2014-04-25 16:34:30 +020054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020056 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020057 struct buffer_data buffer;
58
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020059 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020060 mbedtls_ssl_config_init( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020061 memset( buffer.buf, 0, 2000 );
62 buffer.ptr = buffer.buf;
63
Jerry Yub19ccc32021-08-09 17:44:56 +080064 mbedtls_ssl_config_defaults( &conf,
65 MBEDTLS_SSL_IS_CLIENT,
66 MBEDTLS_SSL_TRANSPORT_STREAM,
67 MBEDTLS_SSL_PRESET_DEFAULT );
68
69 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
70
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020071 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_debug_set_threshold( threshold );
Paul Bakkerc73079a2014-04-25 16:34:30 +020074
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020075 mbedtls_debug_print_msg( &ssl, level, file, line,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020076 "Text message, 2 == %d", 2 );
Paul Bakkerc73079a2014-04-25 16:34:30 +020077
78 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020079
80exit:
81 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020082 mbedtls_ssl_config_free( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020083}
84/* END_CASE */
85
86/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010087void mbedtls_debug_print_ret( char * file, int line, char * text, int value,
88 char * result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +020089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020091 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020092 struct buffer_data buffer;
93
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020094 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020095 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020096 memset( buffer.buf, 0, 2000 );
97 buffer.ptr = buffer.buf;
98
Jerry Yub19ccc32021-08-09 17:44:56 +080099 mbedtls_ssl_config_defaults( &conf,
100 MBEDTLS_SSL_IS_CLIENT,
101 MBEDTLS_SSL_TRANSPORT_STREAM,
102 MBEDTLS_SSL_PRESET_DEFAULT );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200103
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200104 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200105
Jerry Yub19ccc32021-08-09 17:44:56 +0800106 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +0200109
110 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200111
112exit:
113 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200114 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200115}
116/* END_CASE */
117
118/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100119void mbedtls_debug_print_buf( char * file, int line, char * text,
Azim Khan5fcca462018-06-29 11:05:32 +0100120 data_t * data, char * result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200123 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200124 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200125
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200126 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200127 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200128 memset( buffer.buf, 0, 2000 );
129 buffer.ptr = buffer.buf;
130
Jerry Yub19ccc32021-08-09 17:44:56 +0800131 mbedtls_ssl_config_defaults( &conf,
132 MBEDTLS_SSL_IS_CLIENT,
133 MBEDTLS_SSL_TRANSPORT_STREAM,
134 MBEDTLS_SSL_PRESET_DEFAULT );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200136 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200137
Jerry Yub19ccc32021-08-09 17:44:56 +0800138 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
139
Azim Khand30ca132017-06-09 04:32:58 +0100140 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data->x, data->len );
Paul Bakker57ffa552014-04-25 14:29:10 +0200141
142 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200143
144exit:
145 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200146 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200147}
148/* END_CASE */
149
Hanno Becker612a2f12020-10-09 09:19:39 +0100150/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100151void mbedtls_debug_print_crt( char * crt_file, char * file, int line,
152 char * prefix, char * result_str )
Paul Bakker1f761152010-02-18 18:16:31 +0000153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_x509_crt crt;
155 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200156 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000157 struct buffer_data buffer;
158
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200159 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200160 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_x509_crt_init( &crt );
Paul Bakker1f761152010-02-18 18:16:31 +0000162 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200163 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000164
Jerry Yub19ccc32021-08-09 17:44:56 +0800165 mbedtls_ssl_config_defaults( &conf,
166 MBEDTLS_SSL_IS_CLIENT,
167 MBEDTLS_SSL_TRANSPORT_STREAM,
168 MBEDTLS_SSL_PRESET_DEFAULT );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200169
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200170 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000171
Jerry Yub19ccc32021-08-09 17:44:56 +0800172 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
175 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000176
Paul Bakker33b43f12013-08-20 11:48:36 +0200177 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100178
Paul Bakkerbd51b262014-07-10 15:26:12 +0200179exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200181 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200182 mbedtls_ssl_config_free( &conf );
Paul Bakker1f761152010-02-18 18:16:31 +0000183}
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Werner Lewis9802d362022-07-07 11:37:24 +0100187void mbedtls_debug_print_mpi( char * value, char * file, int line,
Azim Khanf1aaec92017-05-30 14:23:15 +0100188 char * prefix, char * result_str )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200191 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000192 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000194
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200195 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200196 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_mpi_init( &val );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000198 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200199 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000200
Jerry Yub19ccc32021-08-09 17:44:56 +0800201 mbedtls_ssl_config_defaults( &conf,
202 MBEDTLS_SSL_IS_CLIENT,
203 MBEDTLS_SSL_TRANSPORT_STREAM,
204 MBEDTLS_SSL_PRESET_DEFAULT );
205
206 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
207
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200208 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200209
Werner Lewis19b4cd82022-07-07 11:02:27 +0100210 TEST_ASSERT( mbedtls_test_read_mpi( &val, value ) == 0 );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000213
Paul Bakker33b43f12013-08-20 11:48:36 +0200214 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000215
Paul Bakkerbd51b262014-07-10 15:26:12 +0200216exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_mpi_free( &val );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200218 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200219 mbedtls_ssl_config_free( &conf );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000220}
Paul Bakker33b43f12013-08-20 11:48:36 +0200221/* END_CASE */