blob: 3cf6c9f07bccd2af73193c171d850aa2c3922ecb [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"
Paul Bakker1f761152010-02-18 18:16:31 +00003
4struct buffer_data
5{
6 char buf[2000];
7 char *ptr;
8};
9
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020010void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000011{
12 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020013 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000014 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000015
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020016 memcpy( p, file, strlen( file ) );
17 p += strlen( file );
18
19 *p++ = '(';
20 *p++ = '0' + ( line / 1000 ) % 10;
21 *p++ = '0' + ( line / 100 ) % 10;
22 *p++ = '0' + ( line / 10 ) % 10;
23 *p++ = '0' + ( line / 1 ) % 10;
24 *p++ = ')';
25 *p++ = ':';
26 *p++ = ' ';
27
28 memcpy( p, str, strlen( str ) );
29 p += strlen( str );
Paul Bakker92478c32014-04-25 15:18:34 +020030
31 /* Detect if debug messages output partial lines and mark them */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020032 if( p[-1] != '\n' )
33 *p++ = '*';
34
35 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000036}
Paul Bakker33b43f12013-08-20 11:48:36 +020037/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000038
Paul Bakker33b43f12013-08-20 11:48:36 +020039/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020041 * END_DEPENDENCIES
42 */
Paul Bakker5690efc2011-05-26 13:16:06 +000043
Paul Bakker57ffa552014-04-25 14:29:10 +020044/* BEGIN_CASE */
Paul Bakkerc73079a2014-04-25 16:34:30 +020045void debug_print_msg_threshold( int threshold, int level, char *file, int line,
46 char *result_str )
47{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020049 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020050 struct buffer_data buffer;
51
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020052 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020053 mbedtls_ssl_config_init( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020054 memset( buffer.buf, 0, 2000 );
55 buffer.ptr = buffer.buf;
56
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020057 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_debug_set_threshold( threshold );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020060 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020061
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020062 mbedtls_debug_print_msg_free( &ssl, level, file, line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_debug_fmt("Text message, 2 == %d", 2 ) );
Paul Bakkerc73079a2014-04-25 16:34:30 +020064
65 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020066
67exit:
68 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020069 mbedtls_ssl_config_free( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020070}
71/* END_CASE */
72
73/* BEGIN_CASE */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020074void mbedtls_debug_print_ret( char *file, int line, char *text, int value,
Paul Bakker57ffa552014-04-25 14:29:10 +020075 char *result_str )
76{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020078 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020079 struct buffer_data buffer;
80
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020081 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020082 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020083 memset( buffer.buf, 0, 2000 );
84 buffer.ptr = buffer.buf;
85
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020086 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020087
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020088 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +020091
92 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020093
94exit:
95 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020096 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020097}
98/* END_CASE */
99
100/* BEGIN_CASE */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200101void mbedtls_debug_print_buf( char *file, int line, char *text,
Paul Bakkereaebbd52014-04-25 15:04:14 +0200102 char *data_string, char *result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +0200103{
104 unsigned char data[10000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200106 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200107 struct buffer_data buffer;
108 size_t data_len;
109
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200110 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200111 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200112 memset( &data, 0, sizeof( data ) );
Paul Bakker57ffa552014-04-25 14:29:10 +0200113 memset( buffer.buf, 0, 2000 );
114 buffer.ptr = buffer.buf;
115
116 data_len = unhexify( data, data_string );
117
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200118 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200120 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data, data_len );
Paul Bakker57ffa552014-04-25 14:29:10 +0200123
124 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200125
126exit:
127 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200128 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200129}
130/* END_CASE */
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200133void mbedtls_debug_print_crt( char *crt_file, char *file, int line,
Paul Bakkereaebbd52014-04-25 15:04:14 +0200134 char *prefix, char *result_str )
Paul Bakker1f761152010-02-18 18:16:31 +0000135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_x509_crt crt;
137 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200138 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000139 struct buffer_data buffer;
140
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200141 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200142 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_x509_crt_init( &crt );
Paul Bakker1f761152010-02-18 18:16:31 +0000144 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200145 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000146
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200147 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200148
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200149 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
152 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000153
Paul Bakker33b43f12013-08-20 11:48:36 +0200154 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100155
Paul Bakkerbd51b262014-07-10 15:26:12 +0200156exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200158 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200159 mbedtls_ssl_config_free( &conf );
Paul Bakker1f761152010-02-18 18:16:31 +0000160}
Paul Bakker33b43f12013-08-20 11:48:36 +0200161/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200164void mbedtls_debug_print_mpi( int radix, char *value, char *file, int line,
Paul Bakker33b43f12013-08-20 11:48:36 +0200165 char *prefix, char *result_str )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200168 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000169 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000171
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200172 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200173 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_mpi_init( &val );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000175 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200176 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000177
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200178 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 TEST_ASSERT( mbedtls_mpi_read_string( &val, radix, value ) == 0 );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200181
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200182 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000187
Paul Bakkerbd51b262014-07-10 15:26:12 +0200188exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_mpi_free( &val );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200190 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200191 mbedtls_ssl_config_free( &conf );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000192}
Paul Bakker33b43f12013-08-20 11:48:36 +0200193/* END_CASE */