blob: 8189473e7fa903be3562d808ddb89aae3effcacb [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
Paul Bakkerc73079a2014-04-25 16:34:30 +020048static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020051{
52 debug_threshold = threshold;
53}
54
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020055void mbedtls_debug_print_fmt( const mbedtls_ssl_context *ssl, int level,
56 const char *file, int line,
57 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
59 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020060 char str[DEBUG_BUF_SIZE];
61 int ret;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020062
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020063 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
64 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020067#if defined(_WIN32)
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020068 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020069#else
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020070 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072 va_end( argp );
73
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020074 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
75 {
76 str[ret] = '\n';
77 str[ret + 1] = '\0';
78 }
79
80 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +000081}
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000084 const char *file, int line,
85 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020087 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +020089 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000090 return;
91
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +020092 /*
93 * With non-blocking I/O and examples that just retry immediately,
94 * the logs would be quickly flooded with WANT_READ, so ignore that.
95 * Don't ignore WANT_WRITE however, since is is usually rare.
96 */
97 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
98 return;
99
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200100 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200101 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200103 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000107 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000108 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200110 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100111 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200112 size_t i, 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é-Gonnard9dbaf402015-06-22 11:50:58 +0200117 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200118 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200120 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Paul Bakker92478c32014-04-25 15:18:34 +0200122 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100123 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 for( i = 0; i < len; i++ )
125 {
126 if( i >= 4096 )
127 break;
128
129 if( i % 16 == 0 )
130 {
131 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200132 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200133 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200134 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100135
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 Bakker92478c32014-04-25 15:18:34 +0200138 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200140 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200141 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 }
144
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200145 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200146 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100147 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 }
149
150 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200151 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100152 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200153 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100154
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200155 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200156 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200157 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000158}
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_ECP_C)
161void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100162 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100164{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200165 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100166
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200167 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200168 return;
169
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200170 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100172
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200173 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_BIGNUM_C)
179void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000180 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000182{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200183 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200184 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200185 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200187 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 return;
189
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000190 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 if( X->p[n] != 0 )
192 break;
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000195 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
196 break;
197
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200198 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200201 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Paul Bakker92478c32014-04-25 15:18:34 +0200203 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000204 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 {
Paul Bakker23986e52011-04-24 08:57:21 +0000206 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000207 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200211 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212 continue;
213 else
214 zeros = 0;
215
216 if( j % 16 == 0 )
217 {
218 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200219 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200220 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200221 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200222 idx = 0;
223 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000224 }
225
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200226 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200227 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000228
229 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000231
232 }
233
234 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200235 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200237 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200238 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
243static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200244 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200246{
247 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200249 char name[16];
250
251 memset( items, 0, sizeof( items ) );
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200254 {
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200255 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line,
256 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200257 return;
258 }
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200263 return;
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266 name[sizeof( name ) - 1] = '\0';
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
269 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200270 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_ECP_C)
272 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
273 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200274 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200275#endif
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200276 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line,
277 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278 }
279}
280
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200281static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
282 const char *file, int line, const char *text )
283{
284 char str[DEBUG_BUF_SIZE];
285 const char *start, *cur;
286
287 start = text;
288 for( cur = text; *cur != '\0'; cur++ )
289 {
290 if( *cur == '\n' )
291 {
292 size_t len = cur - start + 1;
293 if( len > DEBUG_BUF_SIZE - 1 )
294 len = DEBUG_BUF_SIZE - 1;
295
296 memcpy( str, start, len );
297 str[len] = '\0';
298
299 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
300
301 start = cur + 1;
302 }
303 }
304}
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000307 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000309{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200310 char str[DEBUG_BUF_SIZE];
311 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200313 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 return;
315
Paul Bakker29087132010-03-21 21:03:34 +0000316 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000318 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200320 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
321 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200322
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200323 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
324 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200326 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328 crt = crt->next;
329 }
330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_DEBUG_C */