blob: 9adcf07bef8dc2f7e062b8ae64d3a69765fefae1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
33#include <stdarg.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010034#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036
Paul Bakker6edcd412013-10-29 15:22:54 +010037#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
38#if !defined snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000039#define snprintf _snprintf
40#endif
41
Paul Bakker6edcd412013-10-29 15:22:54 +010042#if !defined vsnprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000043#define vsnprintf _vsnprintf
44#endif
Paul Bakker6edcd412013-10-29 15:22:54 +010045#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Rich Evans2387c7d2015-01-30 11:10:20 +000047#if defined(POLARSSL_PLATFORM_C)
48#include "polarssl/platform.h"
49#else
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020050#define polarssl_snprintf snprintf
51#define polarssl_malloc malloc
52#define polarssl_free free
Rich Evans2387c7d2015-01-30 11:10:20 +000053#endif
54
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020055#define DEBUG_BUF_SIZE 512
56
Paul Bakkereaebbd52014-04-25 15:04:14 +020057static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
Manuel Pégourié-Gonnard705de2f2015-08-10 17:36:47 +020058static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020059
60void debug_set_log_mode( int log_mode )
61{
62 debug_log_mode = log_mode;
63}
64
Paul Bakkerc73079a2014-04-25 16:34:30 +020065void debug_set_threshold( int threshold )
66{
67 debug_threshold = threshold;
68}
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070char *debug_fmt( const char *format, ... )
71{
72 va_list argp;
Manuel Pégourié-Gonnard56e245d2015-06-29 19:52:44 +020073#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020074 char *str = polarssl_malloc( DEBUG_BUF_SIZE );
75
76 if( str == NULL )
Manuel Pégourié-Gonnard925a7262015-06-29 19:06:14 +020077 return( NULL );
Manuel Pégourié-Gonnard56e245d2015-06-29 19:52:44 +020078#else
79 static char str[DEBUG_BUF_SIZE];
80#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82 va_start( argp, format );
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020083 vsnprintf( str, DEBUG_BUF_SIZE - 1, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +000084 va_end( argp );
85
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020086 str[DEBUG_BUF_SIZE - 1] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +000087 return( str );
88}
89
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020090void debug_print_msg_free( const ssl_context *ssl, int level,
91 const char *file, int line, char *text )
92{
93 if( text != NULL )
94 debug_print_msg( ssl, level, file, line, text );
95
Manuel Pégourié-Gonnard56e245d2015-06-29 19:52:44 +020096#if defined(POLARSSL_THREADING_C)
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020097 polarssl_free( text );
Manuel Pégourié-Gonnard56e245d2015-06-29 19:52:44 +020098#endif
Manuel Pégourié-Gonnard6c3ccf52015-06-29 14:57:45 +020099}
100
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101void debug_print_msg( const ssl_context *ssl, int level,
102 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
104 char str[512];
105 int maxlen = sizeof( str ) - 1;
106
Paul Bakkerc73079a2014-04-25 16:34:30 +0200107 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 return;
109
Paul Bakkereaebbd52014-04-25 15:04:14 +0200110 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
111 {
Paul Bakker5593f7c2014-05-06 10:29:28 +0200112 ssl->f_dbg( ssl->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200113 return;
114 }
115
Rich Evans2387c7d2015-01-30 11:10:20 +0000116 polarssl_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 str[maxlen] = '\0';
118 ssl->f_dbg( ssl->p_dbg, level, str );
119}
120
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121void debug_print_ret( const ssl_context *ssl, int level,
122 const char *file, int line,
123 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124{
125 char str[512];
126 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200127 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Paul Bakkerc73079a2014-04-25 16:34:30 +0200129 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 return;
131
Paul Bakkereaebbd52014-04-25 15:04:14 +0200132 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Rich Evans2387c7d2015-01-30 11:10:20 +0000133 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200134
Rich Evans2387c7d2015-01-30 11:10:20 +0000135 polarssl_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200136 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138 str[maxlen] = '\0';
139 ssl->f_dbg( ssl->p_dbg, level, str );
140}
141
Paul Bakkerff60ee62010-03-16 21:09:09 +0000142void debug_print_buf( const ssl_context *ssl, int level,
143 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000144 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145{
146 char str[512];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100147 char txt[17];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200148 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Paul Bakkerc73079a2014-04-25 16:34:30 +0200150 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 return;
152
Paul Bakkereaebbd52014-04-25 15:04:14 +0200153 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Rich Evans2387c7d2015-01-30 11:10:20 +0000154 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200155
Rich Evans2387c7d2015-01-30 11:10:20 +0000156 polarssl_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200157 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 str[maxlen] = '\0';
160 ssl->f_dbg( ssl->p_dbg, level, str );
161
Paul Bakker92478c32014-04-25 15:18:34 +0200162 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100163 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 for( i = 0; i < len; i++ )
165 {
166 if( i >= 4096 )
167 break;
168
169 if( i % 16 == 0 )
170 {
171 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200172 {
Rich Evans2387c7d2015-01-30 11:10:20 +0000173 polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200174 ssl->f_dbg( ssl->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100175
Paul Bakker92478c32014-04-25 15:18:34 +0200176 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100177 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200178 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Paul Bakkereaebbd52014-04-25 15:04:14 +0200180 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Rich Evans2387c7d2015-01-30 11:10:20 +0000181 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200182
Rich Evans2387c7d2015-01-30 11:10:20 +0000183 idx += polarssl_snprintf( str + idx, maxlen - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200184 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 }
187
Rich Evans2387c7d2015-01-30 11:10:20 +0000188 idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200189 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100190 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 }
192
193 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200194 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100195 for( /* i = i */; i % 16 != 0; i++ )
Rich Evans2387c7d2015-01-30 11:10:20 +0000196 idx += polarssl_snprintf( str + idx, maxlen - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100197
Rich Evans2387c7d2015-01-30 11:10:20 +0000198 polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200199 ssl->f_dbg( ssl->p_dbg, level, str );
200 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000201}
202
Paul Bakker41c83d32013-03-20 14:39:14 +0100203#if defined(POLARSSL_ECP_C)
204void debug_print_ecp( const ssl_context *ssl, int level,
205 const char *file, int line,
206 const char *text, const ecp_point *X )
207{
208 char str[512];
209 int maxlen = sizeof( str ) - 1;
210
Paul Bakkerc73079a2014-04-25 16:34:30 +0200211 if( ssl->f_dbg == NULL || level > debug_threshold )
212 return;
213
Rich Evans2387c7d2015-01-30 11:10:20 +0000214 polarssl_snprintf( str, maxlen, "%s(X)", text );
Paul Bakker41c83d32013-03-20 14:39:14 +0100215 str[maxlen] = '\0';
216 debug_print_mpi( ssl, level, file, line, str, &X->X );
217
Rich Evans2387c7d2015-01-30 11:10:20 +0000218 polarssl_snprintf( str, maxlen, "%s(Y)", text );
Paul Bakker41c83d32013-03-20 14:39:14 +0100219 str[maxlen] = '\0';
220 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100221}
222#endif /* POLARSSL_ECP_C */
223
Paul Bakkered27a042013-04-18 22:46:23 +0200224#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000225void debug_print_mpi( const ssl_context *ssl, int level,
226 const char *file, int line,
227 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000228{
229 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000230 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200231 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Paul Bakkerc73079a2014-04-25 16:34:30 +0200233 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 return;
235
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000236 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 if( X->p[n] != 0 )
238 break;
239
Paul Bakkera755ca12011-04-24 09:11:17 +0000240 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
242 break;
243
Paul Bakkereaebbd52014-04-25 15:04:14 +0200244 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Rich Evans2387c7d2015-01-30 11:10:20 +0000245 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200246
Rich Evans2387c7d2015-01-30 11:10:20 +0000247 polarssl_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200248 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 str[maxlen] = '\0';
251 ssl->f_dbg( ssl->p_dbg, level, str );
252
Paul Bakker92478c32014-04-25 15:18:34 +0200253 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000254 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 {
Paul Bakker23986e52011-04-24 08:57:21 +0000256 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000257 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkera755ca12011-04-24 09:11:17 +0000259 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200261 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000262 continue;
263 else
264 zeros = 0;
265
266 if( j % 16 == 0 )
267 {
268 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200269 {
Rich Evans2387c7d2015-01-30 11:10:20 +0000270 polarssl_snprintf( str + idx, maxlen - idx, "\n" );
Paul Bakker92478c32014-04-25 15:18:34 +0200271 ssl->f_dbg( ssl->p_dbg, level, str );
272 idx = 0;
273 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000274
Paul Bakkereaebbd52014-04-25 15:04:14 +0200275 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Rich Evans2387c7d2015-01-30 11:10:20 +0000276 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000277 }
278
Rich Evans2387c7d2015-01-30 11:10:20 +0000279 idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200280 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000281
282 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000284
285 }
286
287 if( zeros == 1 )
288 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200289 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
290 {
Rich Evans2387c7d2015-01-30 11:10:20 +0000291 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000292
Paul Bakkereaebbd52014-04-25 15:04:14 +0200293 }
Rich Evans2387c7d2015-01-30 11:10:20 +0000294 idx += polarssl_snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 }
296
Rich Evans2387c7d2015-01-30 11:10:20 +0000297 polarssl_snprintf( str + idx, maxlen - idx, "\n" );
Paul Bakker92478c32014-04-25 15:18:34 +0200298 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299}
Paul Bakkered27a042013-04-18 22:46:23 +0200300#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200303static void debug_print_pk( const ssl_context *ssl, int level,
304 const char *file, int line,
305 const char *text, const pk_context *pk )
306{
307 size_t i;
308 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
309 char name[16];
310
311 memset( items, 0, sizeof( items ) );
312
313 if( pk_debug( pk, items ) != 0 )
314 {
315 debug_print_msg( ssl, level, file, line, "invalid PK context" );
316 return;
317 }
318
Paul Bakker0e4f9112014-04-17 12:39:05 +0200319 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200320 {
321 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
322 return;
323
Rich Evans2387c7d2015-01-30 11:10:20 +0000324 polarssl_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200325 name[sizeof( name ) - 1] = '\0';
326
327 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
328 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200329 else
330#if defined(POLARSSL_ECP_C)
331 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200332 debug_print_ecp( ssl, level, file, line, name, items[i].value );
333 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200334#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200335 debug_print_msg( ssl, level, file, line, "should not happen" );
336 }
337}
338
Paul Bakkerff60ee62010-03-16 21:09:09 +0000339void debug_print_crt( const ssl_context *ssl, int level,
340 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200341 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000343 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200344 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Paul Bakkerc73079a2014-04-25 16:34:30 +0200346 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 return;
348
Paul Bakkereaebbd52014-04-25 15:04:14 +0200349 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
350 {
Rich Evans2387c7d2015-01-30 11:10:20 +0000351 polarssl_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200352 prefix[maxlen] = '\0';
353 }
354 else
355 prefix[0] = '\0';
356
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 maxlen = sizeof( str ) - 1;
358
Paul Bakker29087132010-03-21 21:03:34 +0000359 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200362 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Paul Bakkereaebbd52014-04-25 15:04:14 +0200364 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Rich Evans2387c7d2015-01-30 11:10:20 +0000365 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200366
Rich Evans2387c7d2015-01-30 11:10:20 +0000367 polarssl_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200368 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 str[maxlen] = '\0';
371 ssl->f_dbg( ssl->p_dbg, level, str );
372
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200373 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375 crt = crt->next;
376 }
377}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
Paul Bakker9af723c2014-05-01 13:03:14 +0200380#endif /* POLARSSL_DEBUG_C */