blob: 8dae2ed0a6bf2dc87cfa2489442db8e796f29d00 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define mbedtls_snprintf snprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053static int debug_log_mode = MBEDTLS_DEBUG_DFL_MODE;
Paul Bakkerc73079a2014-04-25 16:34:30 +020054static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_debug_set_log_mode( int log_mode )
Paul Bakkereaebbd52014-04-25 15:04:14 +020057{
58 debug_log_mode = log_mode;
59}
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020062{
63 debug_threshold = threshold;
64}
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066char *mbedtls_debug_fmt( const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
68 va_list argp;
69 static char str[512];
70 int maxlen = sizeof( str ) - 1;
71
72 va_start( argp, format );
73 vsnprintf( str, maxlen, format, argp );
74 va_end( argp );
75
76 str[maxlen] = '\0';
77 return( str );
78}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000082{
83 char str[512];
84 int maxlen = sizeof( str ) - 1;
85
Paul Bakkerc73079a2014-04-25 16:34:30 +020086 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000087 return;
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 if( debug_log_mode == MBEDTLS_DEBUG_LOG_RAW )
Paul Bakkereaebbd52014-04-25 15:04:14 +020090 {
Paul Bakker5593f7c2014-05-06 10:29:28 +020091 ssl->f_dbg( ssl->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +020092 return;
93 }
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
Paul Bakker5121ce52009-01-03 21:22:43 +000096 str[maxlen] = '\0';
97 ssl->f_dbg( ssl->p_dbg, level, str );
98}
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101 const char *file, int line,
102 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
104 char str[512];
105 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200106 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Paul Bakkerc73079a2014-04-25 16:34:30 +0200108 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 return;
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
112 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200115 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117 str[maxlen] = '\0';
118 ssl->f_dbg( ssl->p_dbg, level, str );
119}
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000122 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000123 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124{
125 char str[512];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100126 char txt[17];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200127 size_t i, maxlen = sizeof( str ) - 1, 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
133 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200136 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138 str[maxlen] = '\0';
139 ssl->f_dbg( ssl->p_dbg, level, str );
140
Paul Bakker92478c32014-04-25 15:18:34 +0200141 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100142 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 for( i = 0; i < len; i++ )
144 {
145 if( i >= 4096 )
146 break;
147
148 if( i % 16 == 0 )
149 {
150 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200153 ssl->f_dbg( ssl->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100154
Paul Bakker92478c32014-04-25 15:18:34 +0200155 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100156 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200157 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
160 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 idx += mbedtls_snprintf( str + idx, maxlen - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200163 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 }
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200168 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100169 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 }
171
172 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200173 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100174 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 idx += mbedtls_snprintf( str + idx, maxlen - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200178 ssl->f_dbg( ssl->p_dbg, level, str );
179 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_ECP_C)
183void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100184 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100186{
187 char str[512];
188 int maxlen = sizeof( str ) - 1;
189
Paul Bakkerc73079a2014-04-25 16:34:30 +0200190 if( ssl->f_dbg == NULL || level > debug_threshold )
191 return;
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_snprintf( str, maxlen, "%s(X)", text );
Paul Bakker41c83d32013-03-20 14:39:14 +0100194 str[maxlen] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_snprintf( str, maxlen, "%s(Y)", text );
Paul Bakker41c83d32013-03-20 14:39:14 +0100198 str[maxlen] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_BIGNUM_C)
204void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000205 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
208 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000209 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200210 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Paul Bakkerc73079a2014-04-25 16:34:30 +0200212 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 return;
214
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000215 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 if( X->p[n] != 0 )
217 break;
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000220 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
221 break;
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
224 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
227 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 str[maxlen] = '\0';
230 ssl->f_dbg( ssl->p_dbg, level, str );
231
Paul Bakker92478c32014-04-25 15:18:34 +0200232 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000233 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 {
Paul Bakker23986e52011-04-24 08:57:21 +0000235 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000236 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200240 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241 continue;
242 else
243 zeros = 0;
244
245 if( j % 16 == 0 )
246 {
247 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
Paul Bakker92478c32014-04-25 15:18:34 +0200250 ssl->f_dbg( ssl->p_dbg, level, str );
251 idx = 0;
252 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
255 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000256 }
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200259 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000260
261 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000263
264 }
265
266 if( zeros == 1 )
267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Paul Bakkereaebbd52014-04-25 15:04:14 +0200269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000271
Paul Bakkereaebbd52014-04-25 15:04:14 +0200272 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 idx += mbedtls_snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 }
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
Paul Bakker92478c32014-04-25 15:18:34 +0200277 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#if defined(MBEDTLS_X509_CRT_PARSE_C)
282static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200283 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200285{
286 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200288 char name[16];
289
290 memset( items, 0, sizeof( items ) );
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_debug_print_msg( ssl, level, file, line, "invalid PK context" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200295 return;
296 }
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200301 return;
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200304 name[sizeof( name ) - 1] = '\0';
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
307 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200308 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#if defined(MBEDTLS_ECP_C)
310 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
311 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200312 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_debug_print_msg( ssl, level, file, line, "should not happen" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200315 }
316}
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000319 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000321{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000322 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200323 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Paul Bakkerc73079a2014-04-25 16:34:30 +0200325 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 return;
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Paul Bakkereaebbd52014-04-25 15:04:14 +0200329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200331 prefix[maxlen] = '\0';
332 }
333 else
334 prefix[0] = '\0';
335
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 maxlen = sizeof( str ) - 1;
337
Paul Bakker29087132010-03-21 21:03:34 +0000338 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000340 char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
344 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200347 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
349 str[maxlen] = '\0';
350 ssl->f_dbg( ssl->p_dbg, level, str );
351
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200352 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354 crt = crt->next;
355 }
356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#endif /* MBEDTLS_DEBUG_C */