blob: 0aeb0e487edc339fc357c436da7d5e49da591045 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000039#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define mbedtls_snprintf snprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000041#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043static int debug_log_mode = MBEDTLS_DEBUG_DFL_MODE;
Paul Bakkerc73079a2014-04-25 16:34:30 +020044static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046void mbedtls_debug_set_log_mode( int log_mode )
Paul Bakkereaebbd52014-04-25 15:04:14 +020047{
48 debug_log_mode = log_mode;
49}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020052{
53 debug_threshold = threshold;
54}
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056char *mbedtls_debug_fmt( const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000057{
58 va_list argp;
59 static char str[512];
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020062#if defined(_WIN32)
63 _vsnprintf_s( str, sizeof( str ), _TRUNCATE, format, argp );
64#else
65 vsnprintf( str, sizeof( str ), format, argp );
66#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067 va_end( argp );
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069 return( str );
70}
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000073 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000074{
75 char str[512];
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +020077 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000078 return;
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 if( debug_log_mode == MBEDTLS_DEBUG_LOG_RAW )
Paul Bakkereaebbd52014-04-25 15:04:14 +020081 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020082 ssl->conf->f_dbg( ssl->conf->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +020083 return;
84 }
85
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020086 mbedtls_snprintf( str, sizeof( str ), "%s(%04d): %s\n", file, line, text );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020087 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000091 const char *file, int line,
92 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000093{
94 char str[512];
Paul Bakkereaebbd52014-04-25 15:04:14 +020095 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +020097 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000098 return;
99
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200100 /*
101 * With non-blocking I/O and examples that just retry immediately,
102 * the logs would be quickly flooded with WANT_READ, so ignore that.
103 * Don't ignore WANT_WRITE however, since is is usually rare.
104 */
105 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
106 return;
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200109 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200110
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200111 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200112 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200114 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115}
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000118 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000119 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
121 char str[512];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100122 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200123 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200125 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 return;
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200129 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200130
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200131 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200132 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200134 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Paul Bakker92478c32014-04-25 15:18:34 +0200136 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100137 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 for( i = 0; i < len; i++ )
139 {
140 if( i >= 4096 )
141 break;
142
143 if( i % 16 == 0 )
144 {
145 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200146 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200147 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200148 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100149
Paul Bakker92478c32014-04-25 15:18:34 +0200150 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100151 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200152 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200155 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200156
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200157 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200158 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 }
161
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200162 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200163 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100164 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 }
166
167 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200168 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100169 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200170 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100171
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200172 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200173 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200174 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000175}
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_ECP_C)
178void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100179 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100181{
182 char str[512];
Paul Bakker41c83d32013-03-20 14:39:14 +0100183
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200184 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200185 return;
186
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200187 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100189
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200190 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_BIGNUM_C)
196void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000197 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
200 char str[512];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200201 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200202 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200204 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 return;
206
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000207 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 if( X->p[n] != 0 )
209 break;
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
213 break;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200216 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200217
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200218 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200221 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Paul Bakker92478c32014-04-25 15:18:34 +0200223 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000224 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 {
Paul Bakker23986e52011-04-24 08:57:21 +0000226 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000227 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200231 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000232 continue;
233 else
234 zeros = 0;
235
236 if( j % 16 == 0 )
237 {
238 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200239 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200240 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200241 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200242 idx = 0;
243 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200246 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247 }
248
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200249 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200250 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251
252 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000254
255 }
256
257 if( zeros == 1 )
258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Paul Bakkereaebbd52014-04-25 15:04:14 +0200260 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200261 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000262
Paul Bakkereaebbd52014-04-25 15:04:14 +0200263 }
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200264 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 }
266
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200267 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200268 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_X509_CRT_PARSE_C)
273static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200274 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200276{
277 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200279 char name[16];
280
281 memset( items, 0, sizeof( items ) );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_debug_print_msg( ssl, level, file, line, "invalid PK context" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200286 return;
287 }
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200292 return;
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200295 name[sizeof( name ) - 1] = '\0';
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
298 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200299 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_ECP_C)
301 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
302 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200303 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_debug_print_msg( ssl, level, file, line, "should not happen" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200306 }
307}
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000310 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000312{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000313 char str[1024], prefix[64];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200314 int i = 0, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200316 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 return;
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Paul Bakkereaebbd52014-04-25 15:04:14 +0200320 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200321 mbedtls_snprintf( prefix, sizeof( prefix ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200322 }
323 else
324 prefix[0] = '\0';
325
Paul Bakker29087132010-03-21 21:03:34 +0000326 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328 char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200332 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200333
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200334 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s #%d:\n%s",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200335 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200337 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200339 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341 crt = crt->next;
342 }
343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#endif /* MBEDTLS_DEBUG_C */