blob: 2220e331787b4eb3b580008c92a56410a8a7d7ef [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, 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é-Gonnarda16e7c42015-06-29 20:14:19 +020055void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020056 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é-Gonnarda4f055f2015-07-08 16:46:13 +020068#if defined(_TRUNCATE)
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020069 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020070#else
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020071 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
72 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
73 {
74 str[DEBUG_BUF_SIZE-1] = '\0';
75 ret = -1;
76 }
77#endif
78#else
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020079 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000081 va_end( argp );
82
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020083 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
84 {
85 str[ret] = '\n';
86 str[ret + 1] = '\0';
87 }
88
89 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +000090}
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000093 const char *file, int line,
94 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000095{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020096 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +020098 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000099 return;
100
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200101 /*
102 * With non-blocking I/O and examples that just retry immediately,
103 * the logs would be quickly flooded with WANT_READ, so ignore that.
104 * Don't ignore WANT_WRITE however, since is is usually rare.
105 */
106 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
107 return;
108
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200109 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200110 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200112 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113}
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000116 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000117 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000118{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200119 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100120 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200121 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200123 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 return;
125
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200126 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200127 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200129 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Paul Bakker92478c32014-04-25 15:18:34 +0200131 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100132 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 for( i = 0; i < len; i++ )
134 {
135 if( i >= 4096 )
136 break;
137
138 if( i % 16 == 0 )
139 {
140 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200141 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200142 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200143 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100144
Paul Bakker92478c32014-04-25 15:18:34 +0200145 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100146 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200147 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200149 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200150 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 }
153
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200154 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200155 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100156 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 }
158
159 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200160 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100161 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200162 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100163
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200164 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200165 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200166 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_ECP_C)
170void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100171 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100173{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200174 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100175
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200176 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200177 return;
178
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200179 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100181
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200182 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_BIGNUM_C)
188void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000189 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200192 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200193 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200194 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200196 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 return;
198
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000199 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 if( X->p[n] != 0 )
201 break;
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000204 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
205 break;
206
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200207 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200210 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Paul Bakker92478c32014-04-25 15:18:34 +0200212 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000213 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 {
Paul Bakker23986e52011-04-24 08:57:21 +0000215 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000216 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200220 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000221 continue;
222 else
223 zeros = 0;
224
225 if( j % 16 == 0 )
226 {
227 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200228 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200229 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200230 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200231 idx = 0;
232 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000233 }
234
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200235 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200236 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000237
238 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000240
241 }
242
243 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200244 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200246 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200247 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
252static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200253 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200255{
256 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200258 char name[16];
259
260 memset( items, 0, sizeof( items ) );
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200263 {
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200264 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line,
265 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266 return;
267 }
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200272 return;
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200275 name[sizeof( name ) - 1] = '\0';
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
278 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200279 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_ECP_C)
281 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
282 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200283 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200284#endif
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200285 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line,
286 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287 }
288}
289
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200290static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
291 const char *file, int line, const char *text )
292{
293 char str[DEBUG_BUF_SIZE];
294 const char *start, *cur;
295
296 start = text;
297 for( cur = text; *cur != '\0'; cur++ )
298 {
299 if( *cur == '\n' )
300 {
301 size_t len = cur - start + 1;
302 if( len > DEBUG_BUF_SIZE - 1 )
303 len = DEBUG_BUF_SIZE - 1;
304
305 memcpy( str, start, len );
306 str[len] = '\0';
307
308 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
309
310 start = cur + 1;
311 }
312 }
313}
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000316 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000318{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200319 char str[DEBUG_BUF_SIZE];
320 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200322 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 return;
324
Paul Bakker29087132010-03-21 21:03:34 +0000325 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000327 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200329 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
330 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200331
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200332 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
333 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200335 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 crt = crt->next;
338 }
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#endif /* MBEDTLS_DEBUG_C */