• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/debug.h"
3#include "string.h"
4
5struct buffer_data
6{
7    char buf[2000];
8    char *ptr;
9};
10
11void 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 */
51void 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    mbedtls_ssl_config_defaults( &conf,
64                                 MBEDTLS_SSL_IS_CLIENT,
65                                 MBEDTLS_SSL_TRANSPORT_STREAM,
66                                 MBEDTLS_SSL_PRESET_DEFAULT );
67
68    mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
69
70    TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
71
72    mbedtls_debug_set_threshold( threshold );
73
74    mbedtls_debug_print_msg( &ssl, level, file, line,
75                             "Text message, 2 == %d", 2 );
76
77    TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
78
79exit:
80    mbedtls_ssl_free( &ssl );
81    mbedtls_ssl_config_free( &conf );
82}
83/* END_CASE */
84
85/* BEGIN_CASE */
86void mbedtls_debug_print_ret( char * file, int line, char * text, int value,
87                              char * result_str )
88{
89    mbedtls_ssl_context ssl;
90    mbedtls_ssl_config conf;
91    struct buffer_data buffer;
92
93    mbedtls_ssl_init( &ssl );
94    mbedtls_ssl_config_init( &conf );
95    memset( buffer.buf, 0, 2000 );
96    buffer.ptr = buffer.buf;
97
98    mbedtls_ssl_config_defaults( &conf,
99                                 MBEDTLS_SSL_IS_CLIENT,
100                                 MBEDTLS_SSL_TRANSPORT_STREAM,
101                                 MBEDTLS_SSL_PRESET_DEFAULT );
102
103    mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
104
105    TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
106
107    mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
108
109    TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
110
111exit:
112    mbedtls_ssl_free( &ssl );
113    mbedtls_ssl_config_free( &conf );
114}
115/* END_CASE */
116
117/* BEGIN_CASE */
118void mbedtls_debug_print_buf( char * file, int line, char * text,
119                              data_t * data, char * result_str )
120{
121    mbedtls_ssl_context ssl;
122    mbedtls_ssl_config conf;
123    struct buffer_data buffer;
124
125    mbedtls_ssl_init( &ssl );
126    mbedtls_ssl_config_init( &conf );
127    memset( buffer.buf, 0, 2000 );
128    buffer.ptr = buffer.buf;
129
130    mbedtls_ssl_config_defaults( &conf,
131                                 MBEDTLS_SSL_IS_CLIENT,
132                                 MBEDTLS_SSL_TRANSPORT_STREAM,
133                                 MBEDTLS_SSL_PRESET_DEFAULT );
134
135    mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
136
137    TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
138
139    mbedtls_debug_print_buf( &ssl, 0, file, line, text, data->x, data->len );
140
141    TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
142
143exit:
144    mbedtls_ssl_free( &ssl );
145    mbedtls_ssl_config_free( &conf );
146}
147/* END_CASE */
148
149/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
150void mbedtls_debug_print_crt( char * crt_file, char * file, int line,
151                              char * prefix, char * result_str )
152{
153    mbedtls_x509_crt   crt;
154    mbedtls_ssl_context ssl;
155    mbedtls_ssl_config conf;
156    struct buffer_data buffer;
157
158    mbedtls_ssl_init( &ssl );
159    mbedtls_ssl_config_init( &conf );
160    mbedtls_x509_crt_init( &crt );
161    memset( buffer.buf, 0, 2000 );
162    buffer.ptr = buffer.buf;
163
164    mbedtls_ssl_config_defaults( &conf,
165                                 MBEDTLS_SSL_IS_CLIENT,
166                                 MBEDTLS_SSL_TRANSPORT_STREAM,
167                                 MBEDTLS_SSL_PRESET_DEFAULT );
168
169    mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
170
171    TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
172
173    TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
174    mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
175
176    TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
177
178exit:
179    mbedtls_x509_crt_free( &crt );
180    mbedtls_ssl_free( &ssl );
181    mbedtls_ssl_config_free( &conf );
182}
183/* END_CASE */
184
185/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
186void mbedtls_debug_print_mpi( int radix, char * value, char * file, int line,
187                              char * prefix, char * result_str )
188{
189    mbedtls_ssl_context ssl;
190    mbedtls_ssl_config conf;
191    struct buffer_data buffer;
192    mbedtls_mpi val;
193
194    mbedtls_ssl_init( &ssl );
195    mbedtls_ssl_config_init( &conf );
196    mbedtls_mpi_init( &val );
197    memset( buffer.buf, 0, 2000 );
198    buffer.ptr = buffer.buf;
199
200    mbedtls_ssl_config_defaults( &conf,
201                                 MBEDTLS_SSL_IS_CLIENT,
202                                 MBEDTLS_SSL_TRANSPORT_STREAM,
203                                 MBEDTLS_SSL_PRESET_DEFAULT );
204
205    mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
206
207    TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
208
209    TEST_ASSERT( mbedtls_test_read_mpi( &val, radix, value ) == 0 );
210
211    mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
212
213    TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
214
215exit:
216    mbedtls_mpi_free( &val );
217    mbedtls_ssl_free( &ssl );
218    mbedtls_ssl_config_free( &conf );
219}
220/* END_CASE */
221