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