blob: 1660978d2b80766cfded334c33ac341c5d557c68 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020055char *mbedtls_debug_fmt( const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000056{
57 va_list argp;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020058 char *str = mbedtls_calloc( DEBUG_BUF_SIZE, 1 );
59
60 if( str == NULL )
61 return( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020064#if defined(_WIN32)
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020065 _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020066#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020067 vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000069 va_end( argp );
70
Paul Bakker5121ce52009-01-03 21:22:43 +000071 return( str );
72}
73
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020074void mbedtls_debug_print_msg_free( const mbedtls_ssl_context *ssl, int level,
75 const char *file, int line, char *text )
76{
77 if( text != NULL )
78 mbedtls_debug_print_msg( ssl, level, file, line, text );
79
80 mbedtls_free( text );
81}
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000084 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020086 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +020088 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000089 return;
90
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020091 mbedtls_snprintf( str, sizeof( str ), "%s\n", text );
Paul Bakkereaebbd52014-04-25 15:04:14 +020092
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +020093 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000097 const char *file, int line,
98 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200100 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200102 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 return;
104
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200105 /*
106 * With non-blocking I/O and examples that just retry immediately,
107 * the logs would be quickly flooded with WANT_READ, so ignore that.
108 * Don't ignore WANT_WRITE however, since is is usually rare.
109 */
110 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
111 return;
112
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200113 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200114 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200116 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117}
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000120 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000121 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200123 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100124 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200125 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200127 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 return;
129
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200130 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200131 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200133 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Paul Bakker92478c32014-04-25 15:18:34 +0200135 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100136 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 for( i = 0; i < len; i++ )
138 {
139 if( i >= 4096 )
140 break;
141
142 if( i % 16 == 0 )
143 {
144 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200145 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200146 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200147 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100148
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 Bakker92478c32014-04-25 15:18:34 +0200151 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200153 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200154 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 }
157
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200158 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200159 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100160 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 }
162
163 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200164 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100165 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200166 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100167
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200168 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200169 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200170 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000171}
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_ECP_C)
174void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100175 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100177{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200178 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100179
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200180 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200181 return;
182
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200183 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100185
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200186 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_BIGNUM_C)
192void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000193 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000195{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200196 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200197 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200198 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200200 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 return;
202
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000203 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 if( X->p[n] != 0 )
205 break;
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
209 break;
210
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200211 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200214 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Paul Bakker92478c32014-04-25 15:18:34 +0200216 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000217 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 {
Paul Bakker23986e52011-04-24 08:57:21 +0000219 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000220 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200224 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000225 continue;
226 else
227 zeros = 0;
228
229 if( j % 16 == 0 )
230 {
231 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200232 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200233 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200234 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200235 idx = 0;
236 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000237 }
238
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200239 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200240 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241
242 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000244
245 }
246
247 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200248 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200250 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200251 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_X509_CRT_PARSE_C)
256static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200257 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200259{
260 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200262 char name[16];
263
264 memset( items, 0, sizeof( items ) );
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_debug_print_msg( ssl, level, file, line, "invalid PK context" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200269 return;
270 }
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200275 return;
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278 name[sizeof( name ) - 1] = '\0';
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
281 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200282 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_ECP_C)
284 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
285 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200286 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200287#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_debug_print_msg( ssl, level, file, line, "should not happen" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200289 }
290}
291
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200292static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
293 const char *file, int line, const char *text )
294{
295 char str[DEBUG_BUF_SIZE];
296 const char *start, *cur;
297
298 start = text;
299 for( cur = text; *cur != '\0'; cur++ )
300 {
301 if( *cur == '\n' )
302 {
303 size_t len = cur - start + 1;
304 if( len > DEBUG_BUF_SIZE - 1 )
305 len = DEBUG_BUF_SIZE - 1;
306
307 memcpy( str, start, len );
308 str[len] = '\0';
309
310 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
311
312 start = cur + 1;
313 }
314 }
315}
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000318 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200321 char str[DEBUG_BUF_SIZE];
322 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200324 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 return;
326
Paul Bakker29087132010-03-21 21:03:34 +0000327 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000329 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200331 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
332 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200333
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200334 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
335 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200337 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
339 crt = crt->next;
340 }
341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#endif /* MBEDTLS_DEBUG_C */