blob: 070f63a724ef8bdfeb56b80851ab69e0a979bc5c [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32#include <stdarg.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010033#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000038#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020039#include <stdlib.h>
40#define mbedtls_calloc calloc
41#define mbedtls_free free
42#define mbedtls_snprintf snprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000043#endif
44
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020045#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && !defined(inline)
46#define inline __inline
47#endif
48
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020049#define DEBUG_BUF_SIZE 512
50
Paul Bakkerc73079a2014-04-25 16:34:30 +020051static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020054{
55 debug_threshold = threshold;
56}
57
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020058/*
59 * All calls to f_dbg must be made via this function
60 */
61static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
62 const char *file, int line,
63 const char *str )
64{
65 /*
66 * If in a threaded environment, we need a thread identifier.
67 * Since there is no portable way to get one, use the address of the ssl
68 * context instead, as it shouldn't be shared between threads.
69 */
70#if defined(MBEDTLS_THREADING_C)
71 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
72 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", ssl, str );
73 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
74#else
75 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
76#endif
77}
78
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020079void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020080 const char *file, int line,
81 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000082{
83 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020084 char str[DEBUG_BUF_SIZE];
85 int ret;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020086
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020087 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
88 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020091#if defined(_WIN32)
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020092#if defined(_TRUNCATE)
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020093 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020094#else
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020095 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
96 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
97 {
98 str[DEBUG_BUF_SIZE-1] = '\0';
99 ret = -1;
100 }
101#endif
102#else
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200103 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 va_end( argp );
106
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200107 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
108 {
109 str[ret] = '\n';
110 str[ret + 1] = '\0';
111 }
112
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200113 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000117 const char *file, int line,
118 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000119{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200120 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200122 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 return;
124
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200125 /*
126 * With non-blocking I/O and examples that just retry immediately,
127 * the logs would be quickly flooded with WANT_READ, so ignore that.
128 * Don't ignore WANT_WRITE however, since is is usually rare.
129 */
130 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
131 return;
132
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200133 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200134 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200136 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137}
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000140 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000141 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200143 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100144 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200145 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200147 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 return;
149
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200150 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200151 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200153 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Paul Bakker92478c32014-04-25 15:18:34 +0200155 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100156 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 for( i = 0; i < len; i++ )
158 {
159 if( i >= 4096 )
160 break;
161
162 if( i % 16 == 0 )
163 {
164 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200165 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200166 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200167 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100168
Paul Bakker92478c32014-04-25 15:18:34 +0200169 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100170 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200171 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200173 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200174 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 }
177
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200178 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200179 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100180 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 }
182
183 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200184 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100185 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200186 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100187
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200188 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200189 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200190 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000191}
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_ECP_C)
194void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100195 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100197{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200198 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100199
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200200 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200201 return;
202
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200203 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100205
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200206 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_BIGNUM_C)
212void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000213 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200216 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200217 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200218 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200220 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 return;
222
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000223 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 if( X->p[n] != 0 )
225 break;
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000228 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
229 break;
230
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200231 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200234 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Paul Bakker92478c32014-04-25 15:18:34 +0200236 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000237 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 {
Paul Bakker23986e52011-04-24 08:57:21 +0000239 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000240 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200244 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000245 continue;
246 else
247 zeros = 0;
248
249 if( j % 16 == 0 )
250 {
251 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200252 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200253 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200254 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200255 idx = 0;
256 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000257 }
258
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200259 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200260 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000261
262 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000264
265 }
266
267 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200268 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200270 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200271 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_X509_CRT_PARSE_C)
276static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200277 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200279{
280 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200282 char name[16];
283
284 memset( items, 0, sizeof( items ) );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200288 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200289 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200290 return;
291 }
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200296 return;
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200299 name[sizeof( name ) - 1] = '\0';
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
302 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200303 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_ECP_C)
305 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
306 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200307 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200308#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200309 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200310 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200311 }
312}
313
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200314static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
315 const char *file, int line, const char *text )
316{
317 char str[DEBUG_BUF_SIZE];
318 const char *start, *cur;
319
320 start = text;
321 for( cur = text; *cur != '\0'; cur++ )
322 {
323 if( *cur == '\n' )
324 {
325 size_t len = cur - start + 1;
326 if( len > DEBUG_BUF_SIZE - 1 )
327 len = DEBUG_BUF_SIZE - 1;
328
329 memcpy( str, start, len );
330 str[len] = '\0';
331
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200332 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200333
334 start = cur + 1;
335 }
336 }
337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000340 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200343 char str[DEBUG_BUF_SIZE];
344 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200346 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 return;
348
Paul Bakker29087132010-03-21 21:03:34 +0000349 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000351 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200353 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200354 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200355
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200356 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
357 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200359 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
361 crt = crt->next;
362 }
363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#endif /* MBEDTLS_DEBUG_C */