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