4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_DEBUG_C)
30 #if defined(MBEDTLS_PLATFORM_C)
31 #include "mbedtls/platform.h"
34 #define mbedtls_calloc calloc
35 #define mbedtls_free free
36 #define mbedtls_time_t time_t
37 #define mbedtls_snprintf snprintf
38 #define mbedtls_vsnprintf vsnprintf
41 #include "mbedtls/debug.h"
47 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
48 !defined(inline) && !defined(__cplusplus)
49 #define inline __inline
52 #define DEBUG_BUF_SIZE 512
54 static int debug_threshold = 0;
56 void mbedtls_debug_set_threshold( int threshold )
58 debug_threshold = threshold;
62 * All calls to f_dbg must be made via this function
64 static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
65 const char *file, int line,
69 * If in a threaded environment, we need a thread identifier.
70 * Since there is no portable way to get one, use the address of the ssl
71 * context instead, as it shouldn't be shared between threads.
73 #if defined(MBEDTLS_THREADING_C)
74 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
75 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
76 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
78 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
82 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
83 const char *file, int line,
84 const char *format, ... )
87 char str[DEBUG_BUF_SIZE];
92 NULL == ssl->conf->f_dbg ||
93 level > debug_threshold )
98 va_start( argp, format );
99 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
102 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
108 debug_send_line( ssl, level, file, line, str );
111 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
112 const char *file, int line,
113 const char *text, int ret )
115 char str[DEBUG_BUF_SIZE];
119 NULL == ssl->conf->f_dbg ||
120 level > debug_threshold )
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.
130 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
133 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
136 debug_send_line( ssl, level, file, line, str );
139 void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
140 const char *file, int line, const char *text,
141 const unsigned char *buf, size_t len )
143 char str[DEBUG_BUF_SIZE];
149 NULL == ssl->conf->f_dbg ||
150 level > debug_threshold )
155 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
156 text, (unsigned int) len );
158 debug_send_line( ssl, level, file, line, str );
161 memset( txt, 0, sizeof( txt ) );
162 for( i = 0; i < len; i++ )
171 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
172 debug_send_line( ssl, level, file, line, str );
175 memset( txt, 0, sizeof( txt ) );
178 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
183 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
184 (unsigned int) buf[i] );
185 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
190 for( /* i = i */; i % 16 != 0; i++ )
191 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
193 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
194 debug_send_line( ssl, level, file, line, str );
198 #if defined(MBEDTLS_ECP_C)
199 void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
200 const char *file, int line,
201 const char *text, const mbedtls_ecp_point *X )
203 char str[DEBUG_BUF_SIZE];
207 NULL == ssl->conf->f_dbg ||
208 level > debug_threshold )
213 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
214 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
216 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
217 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
219 #endif /* MBEDTLS_ECP_C */
221 #if defined(MBEDTLS_BIGNUM_C)
222 void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
223 const char *file, int line,
224 const char *text, const mbedtls_mpi *X )
226 char str[DEBUG_BUF_SIZE];
228 size_t i, n, idx = 0;
232 NULL == ssl->conf->f_dbg ||
234 level > debug_threshold )
239 for( n = X->n - 1; n > 0; n-- )
243 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
244 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
247 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
248 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
250 debug_send_line( ssl, level, file, line, str );
253 for( i = n + 1, j = 0; i > 0; i-- )
255 if( zeros && X->p[i - 1] == 0 )
258 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
260 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
269 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
270 debug_send_line( ssl, level, file, line, str );
275 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
276 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
284 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
286 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
287 debug_send_line( ssl, level, file, line, str );
289 #endif /* MBEDTLS_BIGNUM_C */
291 #if defined(MBEDTLS_X509_CRT_PARSE_C)
292 static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
293 const char *file, int line,
294 const char *text, const mbedtls_pk_context *pk )
297 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
300 memset( items, 0, sizeof( items ) );
302 if( mbedtls_pk_debug( pk, items ) != 0 )
304 debug_send_line( ssl, level, file, line,
305 "invalid PK context\n" );
309 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
311 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
314 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
315 name[sizeof( name ) - 1] = '\0';
317 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
318 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
320 #if defined(MBEDTLS_ECP_C)
321 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
322 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
325 debug_send_line( ssl, level, file, line,
326 "should not happen\n" );
330 static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
331 const char *file, int line, const char *text )
333 char str[DEBUG_BUF_SIZE];
334 const char *start, *cur;
337 for( cur = text; *cur != '\0'; cur++ )
341 size_t len = cur - start + 1;
342 if( len > DEBUG_BUF_SIZE - 1 )
343 len = DEBUG_BUF_SIZE - 1;
345 memcpy( str, start, len );
348 debug_send_line( ssl, level, file, line, str );
355 void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
356 const char *file, int line,
357 const char *text, const mbedtls_x509_crt *crt )
359 char str[DEBUG_BUF_SIZE];
364 NULL == ssl->conf->f_dbg ||
366 level > debug_threshold )
375 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
376 debug_send_line( ssl, level, file, line, str );
378 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
379 debug_print_line_by_line( ssl, level, file, line, buf );
381 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
386 #endif /* MBEDTLS_X509_CRT_PARSE_C */
388 #if defined(MBEDTLS_ECDH_C)
389 static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
390 int level, const char *file,
392 const mbedtls_ecdh_context *ecdh,
393 mbedtls_debug_ecdh_attr attr )
395 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
396 const mbedtls_ecdh_context* ctx = ecdh;
398 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
403 case MBEDTLS_DEBUG_ECDH_Q:
404 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
407 case MBEDTLS_DEBUG_ECDH_QP:
408 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
411 case MBEDTLS_DEBUG_ECDH_Z:
412 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
420 void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
421 const char *file, int line,
422 const mbedtls_ecdh_context *ecdh,
423 mbedtls_debug_ecdh_attr attr )
425 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
426 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
431 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
436 #endif /* MBEDTLS_ECDH_C */
438 #endif /* MBEDTLS_DEBUG_C */