blob: 195e0ede213e158ceba3c99e408fa0030d2ed465 [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é-Gonnardd23f5932015-06-23 12:04:52 +020040#include <stdlib.h>
41#define mbedtls_calloc calloc
42#define mbedtls_free free
43#define mbedtls_snprintf snprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000044#endif
45
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020046#define DEBUG_BUF_SIZE 512
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048static int debug_log_mode = MBEDTLS_DEBUG_DFL_MODE;
Paul Bakkerc73079a2014-04-25 16:34:30 +020049static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_debug_set_log_mode( int log_mode )
Paul Bakkereaebbd52014-04-25 15:04:14 +020052{
53 debug_log_mode = log_mode;
54}
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020057{
58 debug_threshold = threshold;
59}
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061char *mbedtls_debug_fmt( const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000062{
63 va_list argp;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020064 char *str = mbedtls_calloc( DEBUG_BUF_SIZE, 1 );
65
66 if( str == NULL )
67 return( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020070#if defined(_WIN32)
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020071 _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020072#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020073 vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020074#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000075 va_end( argp );
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077 return( str );
78}
79
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020080void mbedtls_debug_print_msg_free( const mbedtls_ssl_context *ssl, int level,
81 const char *file, int line, char *text )
82{
83 if( text != NULL )
84 mbedtls_debug_print_msg( ssl, level, file, line, text );
85
86 mbedtls_free( text );
87}
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000090 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000091{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020092 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +020094 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000095 return;
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 if( debug_log_mode == MBEDTLS_DEBUG_LOG_RAW )
Paul Bakkereaebbd52014-04-25 15:04:14 +020098 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020099 ssl->conf->f_dbg( ssl->conf->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200100 return;
101 }
102
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200103 mbedtls_snprintf( str, sizeof( str ), "%s(%04d): %s\n", file, line, text );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200104 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000108 const char *file, int line,
109 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000110{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200111 char str[DEBUG_BUF_SIZE];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200112 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200114 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 return;
116
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200117 /*
118 * With non-blocking I/O and examples that just retry immediately,
119 * the logs would be quickly flooded with WANT_READ, so ignore that.
120 * Don't ignore WANT_WRITE however, since is is usually rare.
121 */
122 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
123 return;
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200126 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200127
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200128 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200129 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200131 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000135 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000136 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200138 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100139 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200140 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200142 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 return;
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200146 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200147
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200148 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200149 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200151 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakker92478c32014-04-25 15:18:34 +0200153 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100154 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 for( i = 0; i < len; i++ )
156 {
157 if( i >= 4096 )
158 break;
159
160 if( i % 16 == 0 )
161 {
162 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200163 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200164 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200165 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100166
Paul Bakker92478c32014-04-25 15:18:34 +0200167 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100168 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200169 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200172 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200173
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200174 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200175 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 }
178
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200179 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200180 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100181 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 }
183
184 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200185 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100186 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200187 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100188
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200189 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200190 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200191 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000192}
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_ECP_C)
195void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100196 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100198{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200199 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100200
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200201 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200202 return;
203
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200204 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100206
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200207 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_BIGNUM_C)
213void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000214 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200217 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200218 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200219 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200221 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 return;
223
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000224 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 if( X->p[n] != 0 )
226 break;
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000229 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
230 break;
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200233 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200234
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200235 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
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é-Gonnard9dbaf402015-06-22 11:50:58 +0200257 mbedtls_snprintf( str + idx, sizeof( str ) - 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 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200263 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000264 }
265
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200266 idx += mbedtls_snprintf( str + idx, sizeof( str ) - 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é-Gonnard9dbaf402015-06-22 11:50:58 +0200278 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000279
Paul Bakkereaebbd52014-04-25 15:04:14 +0200280 }
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200281 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 }
283
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200284 mbedtls_snprintf( str + idx, sizeof( str ) - 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];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200331 int i = 0, 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é-Gonnard9dbaf402015-06-22 11:50:58 +0200338 mbedtls_snprintf( prefix, sizeof( prefix ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200339 }
340 else
341 prefix[0] = '\0';
342
Paul Bakker29087132010-03-21 21:03:34 +0000343 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000345 char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200349 idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200350
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200351 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s #%d:\n%s",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200352 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200354 ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200356 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358 crt = crt->next;
359 }
360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#endif /* MBEDTLS_DEBUG_C */