blob: c71f4dfbc4eb08b43b32366c17994144f1f3c2ed [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
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +020086 if( ssl->conf == NULL || ssl->conf->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 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020091 ssl->conf->f_dbg( ssl->conf->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';
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020097 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
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
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200108 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 return;
110
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200111 /*
112 * With non-blocking I/O and examples that just retry immediately,
113 * the logs would be quickly flooded with WANT_READ, so ignore that.
114 * Don't ignore WANT_WRITE however, since is is usually rare.
115 */
116 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
117 return;
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
120 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200123 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125 str[maxlen] = '\0';
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200126 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127}
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000130 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000131 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132{
133 char str[512];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100134 char txt[17];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200135 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200137 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 return;
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
141 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200144 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 str[maxlen] = '\0';
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200147 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Paul Bakker92478c32014-04-25 15:18:34 +0200149 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100150 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 for( i = 0; i < len; i++ )
152 {
153 if( i >= 4096 )
154 break;
155
156 if( i % 16 == 0 )
157 {
158 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200161 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100162
Paul Bakker92478c32014-04-25 15:18:34 +0200163 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100164 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200165 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
168 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 idx += mbedtls_snprintf( str + idx, maxlen - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200171 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 }
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200176 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100177 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 }
179
180 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200181 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100182 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 idx += mbedtls_snprintf( str + idx, maxlen - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200186 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200187 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000188}
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_ECP_C)
191void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100192 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100194{
195 char str[512];
196 int maxlen = sizeof( str ) - 1;
197
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200198 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200199 return;
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_snprintf( str, maxlen, "%s(X)", text );
Paul Bakker41c83d32013-03-20 14:39:14 +0100202 str[maxlen] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_snprintf( str, maxlen, "%s(Y)", text );
Paul Bakker41c83d32013-03-20 14:39:14 +0100206 str[maxlen] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_BIGNUM_C)
212void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000213 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
216 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000217 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200218 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200220 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 return;
222
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000223 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 if( X->p[n] != 0 )
225 break;
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000228 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
229 break;
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
232 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
235 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 str[maxlen] = '\0';
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200238 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Paul Bakker92478c32014-04-25 15:18:34 +0200240 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000241 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 {
Paul Bakker23986e52011-04-24 08:57:21 +0000243 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000244 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200248 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000249 continue;
250 else
251 zeros = 0;
252
253 if( j % 16 == 0 )
254 {
255 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200258 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200259 idx = 0;
260 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
263 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000264 }
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200267 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000268
269 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000271
272 }
273
274 if( zeros == 1 )
275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Paul Bakkereaebbd52014-04-25 15:04:14 +0200277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000279
Paul Bakkereaebbd52014-04-25 15:04:14 +0200280 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 idx += mbedtls_snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 }
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200285 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#if defined(MBEDTLS_X509_CRT_PARSE_C)
290static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200293{
294 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200296 char name[16];
297
298 memset( items, 0, sizeof( items ) );
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_debug_print_msg( ssl, level, file, line, "invalid PK context" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200303 return;
304 }
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200309 return;
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200312 name[sizeof( name ) - 1] = '\0';
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
315 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_ECP_C)
318 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
319 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200320 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_debug_print_msg( ssl, level, file, line, "should not happen" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200323 }
324}
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000327 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000329{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200331 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200333 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 return;
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Paul Bakkereaebbd52014-04-25 15:04:14 +0200337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200339 prefix[maxlen] = '\0';
340 }
341 else
342 prefix[0] = '\0';
343
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 maxlen = sizeof( str ) - 1;
345
Paul Bakker29087132010-03-21 21:03:34 +0000346 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348 char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
352 idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200355 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
357 str[maxlen] = '\0';
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200358 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200360 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362 crt = crt->next;
363 }
364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#endif /* MBEDTLS_DEBUG_C */