blob: 377d630d9083d6ffa115be28a0396d69c5089498 [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
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020063 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_debug_set_threshold( threshold );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020066 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020067
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020068 mbedtls_debug_print_msg( &ssl, level, file, line,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020069 "Text message, 2 == %d", 2 );
Paul Bakkerc73079a2014-04-25 16:34:30 +020070
71 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020072
73exit:
74 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020075 mbedtls_ssl_config_free( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020076}
77/* END_CASE */
78
79/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010080void mbedtls_debug_print_ret( char * file, int line, char * text, int value,
81 char * result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +020082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020084 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020085 struct buffer_data buffer;
86
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020087 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020088 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020089 memset( buffer.buf, 0, 2000 );
90 buffer.ptr = buffer.buf;
91
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020092 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020093
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020094 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +020097
98 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020099
100exit:
101 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200102 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200103}
104/* END_CASE */
105
106/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100107void mbedtls_debug_print_buf( char * file, int line, char * text,
Azim Khan5fcca462018-06-29 11:05:32 +0100108 data_t * data, char * result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +0200109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200111 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200112 struct buffer_data buffer;
Paul Bakker57ffa552014-04-25 14:29:10 +0200113
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200114 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200115 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200116 memset( buffer.buf, 0, 2000 );
117 buffer.ptr = buffer.buf;
118
Paul Bakker57ffa552014-04-25 14:29:10 +0200119
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200120 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200121
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200122 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200123
Azim Khand30ca132017-06-09 04:32:58 +0100124 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data->x, data->len );
Paul Bakker57ffa552014-04-25 14:29:10 +0200125
126 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200127
128exit:
129 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200130 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200131}
132/* END_CASE */
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100135void mbedtls_debug_print_crt( char * crt_file, char * file, int line,
136 char * prefix, char * result_str )
Paul Bakker1f761152010-02-18 18:16:31 +0000137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_x509_crt crt;
139 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200140 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000141 struct buffer_data buffer;
142
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200143 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200144 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_crt_init( &crt );
Paul Bakker1f761152010-02-18 18:16:31 +0000146 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200147 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000148
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200149 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200150
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200151 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
154 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100157
Paul Bakkerbd51b262014-07-10 15:26:12 +0200158exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200160 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200161 mbedtls_ssl_config_free( &conf );
Paul Bakker1f761152010-02-18 18:16:31 +0000162}
Paul Bakker33b43f12013-08-20 11:48:36 +0200163/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100166void mbedtls_debug_print_mpi( int radix, char * value, char * file, int line,
167 char * prefix, char * result_str )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200170 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000171 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000173
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200174 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200175 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_mpi_init( &val );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000177 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200178 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000179
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200180 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 TEST_ASSERT( mbedtls_mpi_read_string( &val, radix, value ) == 0 );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200183
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200184 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000187
Paul Bakker33b43f12013-08-20 11:48:36 +0200188 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000189
Paul Bakkerbd51b262014-07-10 15:26:12 +0200190exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_mpi_free( &val );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200192 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200193 mbedtls_ssl_config_free( &conf );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000194}
Paul Bakker33b43f12013-08-20 11:48:36 +0200195/* END_CASE */