blob: a32eba0c2a05ade65248da1a5552089fdf245f5e [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"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Paul Bakker1f761152010-02-18 18:16:31 +00004
5struct buffer_data
6{
7 char buf[2000];
8 char *ptr;
9};
10
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020011void string_debug(void *data, int level, const char *file, int line, const char *str)
Paul Bakker1f761152010-02-18 18:16:31 +000012{
13 struct buffer_data *buffer = (struct buffer_data *) data;
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020014 char *p = buffer->ptr;
Paul Bakker26b41a82011-07-13 14:53:58 +000015 ((void) level);
Paul Bakker1f761152010-02-18 18:16:31 +000016
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020017 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
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020029#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
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020034 memcpy( p, str, strlen( str ) );
35 p += strlen( str );
Paul Bakker92478c32014-04-25 15:18:34 +020036
37 /* Detect if debug messages output partial lines and mark them */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020038 if( p[-1] != '\n' )
39 *p++ = '*';
40
41 buffer->ptr = p;
Paul Bakker1f761152010-02-18 18:16:31 +000042}
Paul Bakker33b43f12013-08-20 11:48:36 +020043/* END_HEADER */
Paul Bakker1f761152010-02-18 18:16:31 +000044
Paul Bakker33b43f12013-08-20 11:48:36 +020045/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 * depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
Paul Bakker33b43f12013-08-20 11:48:36 +020047 * END_DEPENDENCIES
48 */
Paul Bakker5690efc2011-05-26 13:16:06 +000049
Paul Bakker57ffa552014-04-25 14:29:10 +020050/* BEGIN_CASE */
Paul Bakkerc73079a2014-04-25 16:34:30 +020051void debug_print_msg_threshold( int threshold, int level, char *file, int line,
52 char *result_str )
53{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020055 mbedtls_ssl_config conf;
Paul Bakkerc73079a2014-04-25 16:34:30 +020056 struct buffer_data buffer;
57
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020058 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020059 mbedtls_ssl_config_init( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020060 memset( buffer.buf, 0, 2000 );
61 buffer.ptr = buffer.buf;
62
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020063 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_debug_set_threshold( threshold );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020066 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerc73079a2014-04-25 16:34:30 +020067
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020068 mbedtls_debug_print_msg( &ssl, level, file, line,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020069 "Text message, 2 == %d", 2 );
Paul Bakkerc73079a2014-04-25 16:34:30 +020070
71 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020072
73exit:
74 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020075 mbedtls_ssl_config_free( &conf );
Paul Bakkerc73079a2014-04-25 16:34:30 +020076}
77/* END_CASE */
78
79/* BEGIN_CASE */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020080void mbedtls_debug_print_ret( char *file, int line, char *text, int value,
Paul Bakker57ffa552014-04-25 14:29:10 +020081 char *result_str )
82{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020084 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +020085 struct buffer_data buffer;
86
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020087 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020088 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +020089 memset( buffer.buf, 0, 2000 );
90 buffer.ptr = buffer.buf;
91
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020092 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020093
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +020094 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +020095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_debug_print_ret( &ssl, 0, file, line, text, value);
Paul Bakker57ffa552014-04-25 14:29:10 +020097
98 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +020099
100exit:
101 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200102 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200103}
104/* END_CASE */
105
106/* BEGIN_CASE */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200107void mbedtls_debug_print_buf( char *file, int line, char *text,
Paul Bakkereaebbd52014-04-25 15:04:14 +0200108 char *data_string, char *result_str )
Paul Bakker57ffa552014-04-25 14:29:10 +0200109{
110 unsigned char data[10000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200112 mbedtls_ssl_config conf;
Paul Bakker57ffa552014-04-25 14:29:10 +0200113 struct buffer_data buffer;
114 size_t data_len;
115
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200116 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200117 mbedtls_ssl_config_init( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200118 memset( &data, 0, sizeof( data ) );
Paul Bakker57ffa552014-04-25 14:29:10 +0200119 memset( buffer.buf, 0, 2000 );
120 buffer.ptr = buffer.buf;
121
122 data_len = unhexify( data, data_string );
123
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200124 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200125
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200126 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker57ffa552014-04-25 14:29:10 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_debug_print_buf( &ssl, 0, file, line, text, data, data_len );
Paul Bakker57ffa552014-04-25 14:29:10 +0200129
130 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200131
132exit:
133 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200134 mbedtls_ssl_config_free( &conf );
Paul Bakker57ffa552014-04-25 14:29:10 +0200135}
136/* END_CASE */
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200139void mbedtls_debug_print_crt( char *crt_file, char *file, int line,
Paul Bakkereaebbd52014-04-25 15:04:14 +0200140 char *prefix, char *result_str )
Paul Bakker1f761152010-02-18 18:16:31 +0000141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_x509_crt crt;
143 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200144 mbedtls_ssl_config conf;
Paul Bakker1f761152010-02-18 18:16:31 +0000145 struct buffer_data buffer;
146
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200147 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200148 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_x509_crt_init( &crt );
Paul Bakker1f761152010-02-18 18:16:31 +0000150 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200151 buffer.ptr = buffer.buf;
Paul Bakker1f761152010-02-18 18:16:31 +0000152
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200153 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200154
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200155 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakker1f761152010-02-18 18:16:31 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
158 mbedtls_debug_print_crt( &ssl, 0, file, line, prefix, &crt);
Paul Bakker1f761152010-02-18 18:16:31 +0000159
Paul Bakker33b43f12013-08-20 11:48:36 +0200160 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100161
Paul Bakkerbd51b262014-07-10 15:26:12 +0200162exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_x509_crt_free( &crt );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200164 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200165 mbedtls_ssl_config_free( &conf );
Paul Bakker1f761152010-02-18 18:16:31 +0000166}
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* END_CASE */
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200170void mbedtls_debug_print_mpi( int radix, char *value, char *file, int line,
Paul Bakker33b43f12013-08-20 11:48:36 +0200171 char *prefix, char *result_str )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200174 mbedtls_ssl_config conf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000175 struct buffer_data buffer;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_mpi val;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000177
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200178 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200179 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_mpi_init( &val );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000181 memset( buffer.buf, 0, 2000 );
Paul Bakker57ffa552014-04-25 14:29:10 +0200182 buffer.ptr = buffer.buf;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000183
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200184 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 TEST_ASSERT( mbedtls_mpi_read_string( &val, radix, value ) == 0 );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200187
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200188 mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_debug_print_mpi( &ssl, 0, file, line, prefix, &val);
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000191
Paul Bakker33b43f12013-08-20 11:48:36 +0200192 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000193
Paul Bakkerbd51b262014-07-10 15:26:12 +0200194exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_mpi_free( &val );
Manuel Pégourié-Gonnardd5a9e412015-05-04 11:11:42 +0200196 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200197 mbedtls_ssl_config_free( &conf );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000198}
Paul Bakker33b43f12013-08-20 11:48:36 +0200199/* END_CASE */