mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/debug.h"
markrad 0:cdf462088d13 3
markrad 0:cdf462088d13 4 struct buffer_data
markrad 0:cdf462088d13 5 {
markrad 0:cdf462088d13 6 char buf[2000];
markrad 0:cdf462088d13 7 char *ptr;
markrad 0:cdf462088d13 8 };
markrad 0:cdf462088d13 9
markrad 0:cdf462088d13 10 void string_debug(void *data, int level, const char *file, int line, const char *str)
markrad 0:cdf462088d13 11 {
markrad 0:cdf462088d13 12 struct buffer_data *buffer = (struct buffer_data *) data;
markrad 0:cdf462088d13 13 char *p = buffer->ptr;
markrad 0:cdf462088d13 14 ((void) level);
markrad 0:cdf462088d13 15
markrad 0:cdf462088d13 16 memcpy( p, file, strlen( file ) );
markrad 0:cdf462088d13 17 p += strlen( file );
markrad 0:cdf462088d13 18
markrad 0:cdf462088d13 19 *p++ = '(';
markrad 0:cdf462088d13 20 *p++ = '0' + ( line / 1000 ) % 10;
markrad 0:cdf462088d13 21 *p++ = '0' + ( line / 100 ) % 10;
markrad 0:cdf462088d13 22 *p++ = '0' + ( line / 10 ) % 10;
markrad 0:cdf462088d13 23 *p++ = '0' + ( line / 1 ) % 10;
markrad 0:cdf462088d13 24 *p++ = ')';
markrad 0:cdf462088d13 25 *p++ = ':';
markrad 0:cdf462088d13 26 *p++ = ' ';
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 29 /* Skip "thread ID" (up to the first space) as it is not predictable */
markrad 0:cdf462088d13 30 while( *str++ != ' ' );
markrad 0:cdf462088d13 31 #endif
markrad 0:cdf462088d13 32
markrad 0:cdf462088d13 33 memcpy( p, str, strlen( str ) );
markrad 0:cdf462088d13 34 p += strlen( str );
markrad 0:cdf462088d13 35
markrad 0:cdf462088d13 36 /* Detect if debug messages output partial lines and mark them */
markrad 0:cdf462088d13 37 if( p[-1] != '\n' )
markrad 0:cdf462088d13 38 *p++ = '*';
markrad 0:cdf462088d13 39
markrad 0:cdf462088d13 40 buffer->ptr = p;
markrad 0:cdf462088d13 41 }
markrad 0:cdf462088d13 42 /* END_HEADER */
markrad 0:cdf462088d13 43
markrad 0:cdf462088d13 44 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 45 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
markrad 0:cdf462088d13 46 * END_DEPENDENCIES
markrad 0:cdf462088d13 47 */
markrad 0:cdf462088d13 48
markrad 0:cdf462088d13 49 /* BEGIN_CASE */
markrad 0:cdf462088d13 50 void debug_print_msg_threshold( int threshold, int level, char *file, int line,
markrad 0:cdf462088d13 51 char *result_str )
markrad 0:cdf462088d13 52 {
markrad 0:cdf462088d13 53 mbedtls_ssl_context ssl;
markrad 0:cdf462088d13 54 mbedtls_ssl_config conf;
markrad 0:cdf462088d13 55 struct buffer_data buffer;
markrad 0:cdf462088d13 56
markrad 0:cdf462088d13 57 mbedtls_ssl_init( &ssl );
markrad 0:cdf462088d13 58 mbedtls_ssl_config_init( &conf );
markrad 0:cdf462088d13 59 memset( buffer.buf, 0, 2000 );
markrad 0:cdf462088d13 60 buffer.ptr = buffer.buf;
markrad 0:cdf462088d13 61
markrad 0:cdf462088d13 62 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
markrad 0:cdf462088d13 63
markrad 0:cdf462088d13 64 mbedtls_debug_set_threshold( threshold );
markrad 0:cdf462088d13 65 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
markrad 0:cdf462088d13 66
markrad 0:cdf462088d13 67 mbedtls_debug_print_msg( &ssl, level, file, line,
markrad 0:cdf462088d13 68 "Text message, 2 == %d", 2 );
markrad 0:cdf462088d13 69
markrad 0:cdf462088d13 70 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
markrad 0:cdf462088d13 71
markrad 0:cdf462088d13 72 exit:
markrad 0:cdf462088d13 73 mbedtls_ssl_free( &ssl );
markrad 0:cdf462088d13 74 mbedtls_ssl_config_free( &conf );
markrad 0:cdf462088d13 75 }
markrad 0:cdf462088d13 76 /* END_CASE */
markrad 0:cdf462088d13 77
markrad 0:cdf462088d13 78 /* BEGIN_CASE */
markrad 0:cdf462088d13 79 void mbedtls_debug_print_ret( char *file, int line, char *text, int value,
markrad 0:cdf462088d13 80 char *result_str )
markrad 0:cdf462088d13 81 {
markrad 0:cdf462088d13 82 mbedtls_ssl_context ssl;
markrad 0:cdf462088d13 83 mbedtls_ssl_config conf;
markrad 0:cdf462088d13 84 struct buffer_data buffer;
markrad 0:cdf462088d13 85
markrad 0:cdf462088d13 86 mbedtls_ssl_init( &ssl );
markrad 0:cdf462088d13 87 mbedtls_ssl_config_init( &conf );
markrad 0:cdf462088d13 88 memset( buffer.buf, 0, 2000 );
markrad 0:cdf462088d13 89 buffer.ptr = buffer.buf;
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
markrad 0:cdf462088d13 92
markrad 0:cdf462088d13 93 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
markrad 0:cdf462088d13 94
markrad 0:cdf462088d13 95 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
markrad 0:cdf462088d13 96
markrad 0:cdf462088d13 97 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
markrad 0:cdf462088d13 98
markrad 0:cdf462088d13 99 exit:
markrad 0:cdf462088d13 100 mbedtls_ssl_free( &ssl );
markrad 0:cdf462088d13 101 mbedtls_ssl_config_free( &conf );
markrad 0:cdf462088d13 102 }
markrad 0:cdf462088d13 103 /* END_CASE */
markrad 0:cdf462088d13 104
markrad 0:cdf462088d13 105 /* BEGIN_CASE */
markrad 0:cdf462088d13 106 void mbedtls_debug_print_buf( char *file, int line, char *text,
markrad 0:cdf462088d13 107 char *data_string, char *result_str )
markrad 0:cdf462088d13 108 {
markrad 0:cdf462088d13 109 unsigned char data[10000];
markrad 0:cdf462088d13 110 mbedtls_ssl_context ssl;
markrad 0:cdf462088d13 111 mbedtls_ssl_config conf;
markrad 0:cdf462088d13 112 struct buffer_data buffer;
markrad 0:cdf462088d13 113 size_t data_len;
markrad 0:cdf462088d13 114
markrad 0:cdf462088d13 115 mbedtls_ssl_init( &ssl );
markrad 0:cdf462088d13 116 mbedtls_ssl_config_init( &conf );
markrad 0:cdf462088d13 117 memset( &data, 0, sizeof( data ) );
markrad 0:cdf462088d13 118 memset( buffer.buf, 0, 2000 );
markrad 0:cdf462088d13 119 buffer.ptr = buffer.buf;
markrad 0:cdf462088d13 120
markrad 0:cdf462088d13 121 data_len = unhexify( data, data_string );
markrad 0:cdf462088d13 122
markrad 0:cdf462088d13 123 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
markrad 0:cdf462088d13 124
markrad 0:cdf462088d13 125 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
markrad 0:cdf462088d13 126
markrad 0:cdf462088d13 127 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data, data_len );
markrad 0:cdf462088d13 128
markrad 0:cdf462088d13 129 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
markrad 0:cdf462088d13 130
markrad 0:cdf462088d13 131 exit:
markrad 0:cdf462088d13 132 mbedtls_ssl_free( &ssl );
markrad 0:cdf462088d13 133 mbedtls_ssl_config_free( &conf );
markrad 0:cdf462088d13 134 }
markrad 0:cdf462088d13 135 /* END_CASE */
markrad 0:cdf462088d13 136
markrad 0:cdf462088d13 137 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
markrad 0:cdf462088d13 138 void mbedtls_debug_print_crt( char *crt_file, char *file, int line,
markrad 0:cdf462088d13 139 char *prefix, char *result_str )
markrad 0:cdf462088d13 140 {
markrad 0:cdf462088d13 141 mbedtls_x509_crt crt;
markrad 0:cdf462088d13 142 mbedtls_ssl_context ssl;
markrad 0:cdf462088d13 143 mbedtls_ssl_config conf;
markrad 0:cdf462088d13 144 struct buffer_data buffer;
markrad 0:cdf462088d13 145
markrad 0:cdf462088d13 146 mbedtls_ssl_init( &ssl );
markrad 0:cdf462088d13 147 mbedtls_ssl_config_init( &conf );
markrad 0:cdf462088d13 148 mbedtls_x509_crt_init( &crt );
markrad 0:cdf462088d13 149 memset( buffer.buf, 0, 2000 );
markrad 0:cdf462088d13 150 buffer.ptr = buffer.buf;
markrad 0:cdf462088d13 151
markrad 0:cdf462088d13 152 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
markrad 0:cdf462088d13 153
markrad 0:cdf462088d13 154 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
markrad 0:cdf462088d13 155
markrad 0:cdf462088d13 156 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
markrad 0:cdf462088d13 157 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
markrad 0:cdf462088d13 158
markrad 0:cdf462088d13 159 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
markrad 0:cdf462088d13 160
markrad 0:cdf462088d13 161 exit:
markrad 0:cdf462088d13 162 mbedtls_x509_crt_free( &crt );
markrad 0:cdf462088d13 163 mbedtls_ssl_free( &ssl );
markrad 0:cdf462088d13 164 mbedtls_ssl_config_free( &conf );
markrad 0:cdf462088d13 165 }
markrad 0:cdf462088d13 166 /* END_CASE */
markrad 0:cdf462088d13 167
markrad 0:cdf462088d13 168 /* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
markrad 0:cdf462088d13 169 void mbedtls_debug_print_mpi( int radix, char *value, char *file, int line,
markrad 0:cdf462088d13 170 char *prefix, char *result_str )
markrad 0:cdf462088d13 171 {
markrad 0:cdf462088d13 172 mbedtls_ssl_context ssl;
markrad 0:cdf462088d13 173 mbedtls_ssl_config conf;
markrad 0:cdf462088d13 174 struct buffer_data buffer;
markrad 0:cdf462088d13 175 mbedtls_mpi val;
markrad 0:cdf462088d13 176
markrad 0:cdf462088d13 177 mbedtls_ssl_init( &ssl );
markrad 0:cdf462088d13 178 mbedtls_ssl_config_init( &conf );
markrad 0:cdf462088d13 179 mbedtls_mpi_init( &val );
markrad 0:cdf462088d13 180 memset( buffer.buf, 0, 2000 );
markrad 0:cdf462088d13 181 buffer.ptr = buffer.buf;
markrad 0:cdf462088d13 182
markrad 0:cdf462088d13 183 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
markrad 0:cdf462088d13 184
markrad 0:cdf462088d13 185 TEST_ASSERT( mbedtls_mpi_read_string( &val, radix, value ) == 0 );
markrad 0:cdf462088d13 186
markrad 0:cdf462088d13 187 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
markrad 0:cdf462088d13 188
markrad 0:cdf462088d13 189 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
markrad 0:cdf462088d13 190
markrad 0:cdf462088d13 191 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
markrad 0:cdf462088d13 192
markrad 0:cdf462088d13 193 exit:
markrad 0:cdf462088d13 194 mbedtls_mpi_free( &val );
markrad 0:cdf462088d13 195 mbedtls_ssl_free( &ssl );
markrad 0:cdf462088d13 196 mbedtls_ssl_config_free( &conf );
markrad 0:cdf462088d13 197 }
markrad 0:cdf462088d13 198 /* END_CASE */