blob: 98f98b061b7f4cc77d5ea27e7774593534ff30a9 [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
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020028#if defined(MBEDTLS_THREADING_C)
29 /* Skip "thread ID" (up to the first space) as it is not predictable */
30 while( *str++ != ' ' );
31#endif
32
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020033 memcpy( p, str, strlen( str ) );
34 p += strlen( str );
Paul Bakker92478c32014-04-25 15:18:34 +020035
36 /* Detect if debug messages output partial lines and mark them */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020037 if( p[-1] != '\n' )
38 *p++ = '*';
39
40 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000041}
Paul Bakker33b43f12013-08-20 11:48:36 +020042/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000043
Paul Bakker33b43f12013-08-20 11:48:36 +020044/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020046 * END_DEPENDENCIES
47 */
Paul Bakker5690efc2011-05-26 13:16:06 +000048
Paul Bakker57ffa552014-04-25 14:29:10 +020049/* BEGIN_CASE */
Paul Bakkerc73079a2014-04-25 16:34:30 +020050void debug_print_msg_threshold( int threshold, int level, char *file, int line,
51 char *result_str )
52{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020054 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020055 struct buffer_data buffer;
56
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020057 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020058 mbedtls_ssl_config_init( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020059 memset( buffer.buf, 0, 2000 );
60 buffer.ptr = buffer.buf;
61
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020062 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_debug_set_threshold( threshold );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020065 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020066
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020067 mbedtls_debug_print_msg( &ssl, level, file, line,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020068 "Text message, 2 == %d", 2 );
Paul Bakkerc73079a2014-04-25 16:34:30 +020069
70 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020071
72exit:
73 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020074 mbedtls_ssl_config_free( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020075}
76/* END_CASE */
77
78/* BEGIN_CASE */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020079void mbedtls_debug_print_ret( char *file, int line, char *text, int value,
Paul Bakker57ffa552014-04-25 14:29:10 +020080 char *result_str )
81{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020083 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020084 struct buffer_data buffer;
85
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020086 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020087 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020088 memset( buffer.buf, 0, 2000 );
89 buffer.ptr = buffer.buf;
90
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020091 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020092
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020093 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +020094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +020096
97 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020098
99exit:
100 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200101 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200102}
103/* END_CASE */
104
105/* BEGIN_CASE */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200106void mbedtls_debug_print_buf( char *file, int line, char *text,
Paul Bakkereaebbd52014-04-25 15:04:14 +0200107 char *data_string, char *result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +0200108{
109 unsigned char data[10000];
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;
113 size_t data_len;
114
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200115 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200116 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200117 memset( &data, 0, sizeof( data ) );
Paul Bakker57ffa552014-04-25 14:29:10 +0200118 memset( buffer.buf, 0, 2000 );
119 buffer.ptr = buffer.buf;
120
121 data_len = unhexify( data, data_string );
122
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200123 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200124
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200125 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data, data_len );
Paul Bakker57ffa552014-04-25 14:29:10 +0200128
129 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200130
131exit:
132 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200133 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200134}
135/* END_CASE */
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200138void mbedtls_debug_print_crt( char *crt_file, char *file, int line,
Paul Bakkereaebbd52014-04-25 15:04:14 +0200139 char *prefix, char *result_str )
Paul Bakker1f761152010-02-18 18:16:31 +0000140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_x509_crt crt;
142 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200143 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000144 struct buffer_data buffer;
145
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200146 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200147 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509_crt_init( &crt );
Paul Bakker1f761152010-02-18 18:16:31 +0000149 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200150 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000151
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200152 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200153
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200154 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
157 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000158
Paul Bakker33b43f12013-08-20 11:48:36 +0200159 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100160
Paul Bakkerbd51b262014-07-10 15:26:12 +0200161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200163 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200164 mbedtls_ssl_config_free( &conf );
Paul Bakker1f761152010-02-18 18:16:31 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200169void mbedtls_debug_print_mpi( int radix, char *value, char *file, int line,
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 char *prefix, char *result_str )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200173 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000174 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000176
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200177 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200178 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_mpi_init( &val );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000180 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200181 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000182
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200183 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 TEST_ASSERT( mbedtls_mpi_read_string( &val, radix, value ) == 0 );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200186
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200187 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000190
Paul Bakker33b43f12013-08-20 11:48:36 +0200191 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000192
Paul Bakkerbd51b262014-07-10 15:26:12 +0200193exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_mpi_free( &val );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200195 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200196 mbedtls_ssl_config_free( &conf );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000197}
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* END_CASE */