blob: f9b82297153aad9508d30126f1a45b94e42bd019 [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é-Gonnard7b23c512015-08-31 16:11:00 +020046#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && !defined(inline)
47#define inline __inline
48#endif
49
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020050#define DEBUG_BUF_SIZE 512
51
Paul Bakkerc73079a2014-04-25 16:34:30 +020052static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020055{
56 debug_threshold = threshold;
57}
58
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020059/*
60 * All calls to f_dbg must be made via this function
61 */
62static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
63 const char *file, int line,
64 const char *str )
65{
66 /*
67 * If in a threaded environment, we need a thread identifier.
68 * Since there is no portable way to get one, use the address of the ssl
69 * context instead, as it shouldn't be shared between threads.
70 */
71#if defined(MBEDTLS_THREADING_C)
72 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
73 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", ssl, str );
74 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
75#else
76 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
77#endif
78}
79
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020080void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020081 const char *file, int line,
82 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000083{
84 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020085 char str[DEBUG_BUF_SIZE];
86 int ret;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020087
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020088 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
89 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020092#if defined(_WIN32)
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020093#if defined(_TRUNCATE)
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020094 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020095#else
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020096 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
97 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
98 {
99 str[DEBUG_BUF_SIZE-1] = '\0';
100 ret = -1;
101 }
102#endif
103#else
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200104 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 va_end( argp );
107
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200108 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
109 {
110 str[ret] = '\n';
111 str[ret + 1] = '\0';
112 }
113
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200114 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115}
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000118 const char *file, int line,
119 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200121 char str[DEBUG_BUF_SIZE];
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é-Gonnard4cba1a72015-05-11 18:52:25 +0200126 /*
127 * With non-blocking I/O and examples that just retry immediately,
128 * the logs would be quickly flooded with WANT_READ, so ignore that.
129 * Don't ignore WANT_WRITE however, since is is usually rare.
130 */
131 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
132 return;
133
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200134 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200135 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200137 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000142 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000143{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200144 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100145 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200146 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200148 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 return;
150
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200151 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200152 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200154 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Paul Bakker92478c32014-04-25 15:18:34 +0200156 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100157 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 for( i = 0; i < len; i++ )
159 {
160 if( i >= 4096 )
161 break;
162
163 if( i % 16 == 0 )
164 {
165 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200166 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200167 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200168 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100169
Paul Bakker92478c32014-04-25 15:18:34 +0200170 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100171 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200172 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
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é-Gonnard7b23c512015-08-31 16:11:00 +0200190 debug_send_line( ssl, level, file, line, 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é-Gonnard9dbaf402015-06-22 11:50:58 +0200232 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200235 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Paul Bakker92478c32014-04-25 15:18:34 +0200237 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000238 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 {
Paul Bakker23986e52011-04-24 08:57:21 +0000240 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200245 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000246 continue;
247 else
248 zeros = 0;
249
250 if( j % 16 == 0 )
251 {
252 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200253 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200254 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200255 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200256 idx = 0;
257 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000258 }
259
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200260 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200261 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000262
263 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000265
266 }
267
268 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200269 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200271 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200272 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_X509_CRT_PARSE_C)
277static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200280{
281 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200283 char name[16];
284
285 memset( items, 0, sizeof( items ) );
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200288 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200289 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200290 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291 return;
292 }
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200297 return;
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200300 name[sizeof( name ) - 1] = '\0';
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
303 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200304 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_ECP_C)
306 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
307 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200308 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200309#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200310 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200311 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200312 }
313}
314
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200315static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
316 const char *file, int line, const char *text )
317{
318 char str[DEBUG_BUF_SIZE];
319 const char *start, *cur;
320
321 start = text;
322 for( cur = text; *cur != '\0'; cur++ )
323 {
324 if( *cur == '\n' )
325 {
326 size_t len = cur - start + 1;
327 if( len > DEBUG_BUF_SIZE - 1 )
328 len = DEBUG_BUF_SIZE - 1;
329
330 memcpy( str, start, len );
331 str[len] = '\0';
332
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200333 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200334
335 start = cur + 1;
336 }
337 }
338}
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000341 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000343{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200344 char str[DEBUG_BUF_SIZE];
345 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200347 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 return;
349
Paul Bakker29087132010-03-21 21:03:34 +0000350 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000352 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200354 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200355 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200356
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200357 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
358 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200360 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362 crt = crt->next;
363 }
364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#endif /* MBEDTLS_DEBUG_C */