blob: f170332c4cd74d817f183a9950a19c98f81ed726 [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
Paul Bakkerff60ee62010-03-16 21:09:09 +000010void string_debug(void *data, int level, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000011{
12 struct buffer_data *buffer = (struct buffer_data *) data;
Paul Bakker26b41a82011-07-13 14:53:58 +000013 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000014
15 memcpy(buffer->ptr, str, strlen(str));
16 buffer->ptr += strlen(str);
Paul Bakker92478c32014-04-25 15:18:34 +020017
18 /* Detect if debug messages output partial lines and mark them */
19 if( *(buffer->ptr - 1) != '\n' )
20 {
21 *buffer->ptr = '*';
22 buffer->ptr++;
23 }
Paul Bakker1f761152010-02-18 18:16:31 +000024}
Paul Bakker33b43f12013-08-20 11:48:36 +020025/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000026
Paul Bakker33b43f12013-08-20 11:48:36 +020027/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020029 * END_DEPENDENCIES
30 */
Paul Bakker5690efc2011-05-26 13:16:06 +000031
Paul Bakker57ffa552014-04-25 14:29:10 +020032/* BEGIN_CASE */
Paul Bakkerc73079a2014-04-25 16:34:30 +020033void debug_print_msg_threshold( int threshold, int level, char *file, int line,
34 char *result_str )
35{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020037 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020038 struct buffer_data buffer;
39
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020040 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020041 mbedtls_ssl_config_init( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020042 memset( buffer.buf, 0, 2000 );
43 buffer.ptr = buffer.buf;
44
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020045 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_debug_set_log_mode( MBEDTLS_DEBUG_LOG_FULL );
48 mbedtls_debug_set_threshold( threshold );
49 mbedtls_ssl_set_dbg(&ssl, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_debug_print_msg( &ssl, level, file, line,
52 mbedtls_debug_fmt("Text message, 2 == %d", 2 ) );
Paul Bakkerc73079a2014-04-25 16:34:30 +020053
54 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020055
56exit:
57 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020058 mbedtls_ssl_config_free( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020059}
60/* END_CASE */
61
62/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063void mbedtls_debug_print_ret( int mode, char *file, int line, char *text, int value,
Paul Bakker57ffa552014-04-25 14:29:10 +020064 char *result_str )
65{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020067 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020068 struct buffer_data buffer;
69
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020070 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020071 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020072 memset( buffer.buf, 0, 2000 );
73 buffer.ptr = buffer.buf;
74
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020075 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_debug_set_log_mode( mode );
78 mbedtls_ssl_set_dbg(&ssl, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +020081
82 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020083
84exit:
85 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020086 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020087}
88/* END_CASE */
89
90/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_debug_print_buf( int mode, char *file, int line, char *text,
Paul Bakkereaebbd52014-04-25 15:04:14 +020092 char *data_string, char *result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +020093{
94 unsigned char data[10000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020096 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020097 struct buffer_data buffer;
98 size_t data_len;
99
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200100 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200101 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200102 memset( &data, 0, sizeof( data ) );
Paul Bakker57ffa552014-04-25 14:29:10 +0200103 memset( buffer.buf, 0, 2000 );
104 buffer.ptr = buffer.buf;
105
106 data_len = unhexify( data, data_string );
107
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200108 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_debug_set_log_mode( mode );
111 mbedtls_ssl_set_dbg(&ssl, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data, data_len );
Paul Bakker57ffa552014-04-25 14:29:10 +0200114
115 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200116
117exit:
118 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200119 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200120}
121/* END_CASE */
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
124void mbedtls_debug_print_crt( int mode, char *crt_file, char *file, int line,
Paul Bakkereaebbd52014-04-25 15:04:14 +0200125 char *prefix, char *result_str )
Paul Bakker1f761152010-02-18 18:16:31 +0000126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_x509_crt crt;
128 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200129 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000130 struct buffer_data buffer;
131
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200132 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200133 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_x509_crt_init( &crt );
Paul Bakker1f761152010-02-18 18:16:31 +0000135 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200136 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000137
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200138 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_debug_set_log_mode( mode );
141 mbedtls_ssl_set_dbg(&ssl, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
144 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100147
Paul Bakkerbd51b262014-07-10 15:26:12 +0200148exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200150 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200151 mbedtls_ssl_config_free( &conf );
Paul Bakker1f761152010-02-18 18:16:31 +0000152}
Paul Bakker33b43f12013-08-20 11:48:36 +0200153/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
156void mbedtls_debug_print_mpi( int mode, int radix, char *value, char *file, int line,
Paul Bakker33b43f12013-08-20 11:48:36 +0200157 char *prefix, char *result_str )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200160 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000161 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000163
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200164 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200165 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_mpi_init( &val );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000167 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200168 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000169
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200170 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 TEST_ASSERT( mbedtls_mpi_read_string( &val, radix, value ) == 0 );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_debug_set_log_mode( mode );
175 mbedtls_ssl_set_dbg(&ssl, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000178
Paul Bakker33b43f12013-08-20 11:48:36 +0200179 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000180
Paul Bakkerbd51b262014-07-10 15:26:12 +0200181exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_mpi_free( &val );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200183 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200184 mbedtls_ssl_config_free( &conf );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000185}
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* END_CASE */