26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_DEBUG_C)
39 #if defined(EFIX64) || defined(EFI32)
43 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
45 #define snprintf _snprintf
48 #if !defined vsnprintf
49 #define vsnprintf _vsnprintf
54 static int debug_threshold = 0;
58 debug_log_mode = log_mode;
63 debug_threshold = threshold;
66 char *
debug_fmt(
const char *format, ... )
70 int maxlen =
sizeof( str ) - 1;
72 va_start( argp, format );
73 vsnprintf( str, maxlen, format, argp );
81 const char *file,
int line,
const char *text )
84 int maxlen =
sizeof( str ) - 1;
86 if( ssl->
f_dbg == NULL || level > debug_threshold )
95 snprintf( str, maxlen,
"%s(%04d): %s\n", file, line, text );
101 const char *file,
int line,
102 const char *text,
int ret )
105 int maxlen =
sizeof( str ) - 1;
108 if( ssl->
f_dbg == NULL || level > debug_threshold )
112 idx = snprintf( str, maxlen,
"%s(%04d): ", file, line );
114 snprintf( str + idx, maxlen - idx,
"%s() returned %d (-0x%04x)\n",
122 const char *file,
int line,
const char *text,
123 unsigned char *buf,
size_t len )
126 size_t i, maxlen =
sizeof( str ) - 1, idx = 0;
128 if( ssl->
f_dbg == NULL || level > debug_threshold )
132 idx = snprintf( str, maxlen,
"%s(%04d): ", file, line );
134 snprintf( str + idx, maxlen - idx,
"dumping '%s' (%u bytes)\n",
135 text, (
unsigned int) len );
141 for( i = 0; i < len; i++ )
150 snprintf( str + idx, maxlen - idx,
"\n" );
156 idx = snprintf( str, maxlen,
"%s(%04d): ", file, line );
158 idx += snprintf( str + idx, maxlen - idx,
"%04x: ",
163 idx += snprintf( str + idx, maxlen - idx,
" %02x",
164 (
unsigned int) buf[i] );
169 snprintf( str + idx, maxlen - idx,
"\n" );
174 #if defined(POLARSSL_ECP_C)
176 const char *file,
int line,
180 int maxlen =
sizeof( str ) - 1;
182 if( ssl->
f_dbg == NULL || level > debug_threshold )
185 snprintf( str, maxlen,
"%s(X)", text );
189 snprintf( str, maxlen,
"%s(Y)", text );
195 #if defined(POLARSSL_BIGNUM_C)
197 const char *file,
int line,
198 const char *text,
const mpi *X )
201 int j, k, maxlen =
sizeof( str ) - 1, zeros = 1;
202 size_t i, n, idx = 0;
204 if( ssl->
f_dbg == NULL || X == NULL || level > debug_threshold )
207 for( n = X->
n - 1; n > 0; n-- )
211 for( j = (
sizeof(
t_uint) << 3 ) - 1; j >= 0; j-- )
212 if( ( ( X->
p[n] >> j ) & 1 ) != 0 )
216 idx = snprintf( str, maxlen,
"%s(%04d): ", file, line );
218 snprintf( str + idx, maxlen - idx,
"value of '%s' (%d bits) is:\n",
219 text, (
int) ( ( n * (
sizeof(
t_uint) << 3 ) ) + j + 1 ) );
225 for( i = n + 1, j = 0; i > 0; i-- )
227 if( zeros && X->
p[i - 1] == 0 )
230 for( k =
sizeof(
t_uint ) - 1; k >= 0; k-- )
232 if( zeros && ( ( X->
p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
241 snprintf( str + idx, maxlen - idx,
"\n" );
247 idx = snprintf( str, maxlen,
"%s(%04d): ", file, line );
250 idx += snprintf( str + idx, maxlen - idx,
" %02x", (
unsigned int)
251 ( X->
p[i - 1] >> ( k << 3 ) ) & 0xFF );
262 idx = snprintf( str, maxlen,
"%s(%04d): ", file, line );
265 idx += snprintf( str + idx, maxlen - idx,
" 00" );
268 snprintf( str + idx, maxlen - idx,
"\n" );
273 #if defined(POLARSSL_X509_CRT_PARSE_C)
274 static void debug_print_pk(
const ssl_context *ssl,
int level,
275 const char *file,
int line,
282 memset( items, 0,
sizeof( items ) );
295 snprintf( name,
sizeof( name ),
"%s%s", text, items[i].name );
296 name[
sizeof( name ) - 1] =
'\0';
301 #if defined(POLARSSL_ECP_C)
311 const char *file,
int line,
312 const char *text,
const x509_crt *crt )
314 char str[1024], prefix[64];
315 int i = 0, maxlen =
sizeof( prefix ) - 1, idx = 0;
317 if( ssl->
f_dbg == NULL || crt == NULL || level > debug_threshold )
322 snprintf( prefix, maxlen,
"%s(%04d): ", file, line );
323 prefix[maxlen] =
'\0';
328 maxlen =
sizeof( str ) - 1;
336 idx = snprintf( str, maxlen,
"%s(%04d): ", file, line );
338 snprintf( str + idx, maxlen - idx,
"%s #%d:\n%s",
344 debug_print_pk( ssl, level, file, line,
"crt->", &crt->
pk );