Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers debug.c Source File

debug.c

00001 /*
00002  *  Debugging routines
00003  *
00004  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 
00022 #if !defined(MBEDTLS_CONFIG_FILE)
00023 #include "mbedtls/config.h"
00024 #else
00025 #include MBEDTLS_CONFIG_FILE
00026 #endif
00027 
00028 #if defined(MBEDTLS_DEBUG_C)
00029 
00030 #if defined(MBEDTLS_PLATFORM_C)
00031 #include "mbedtls/platform.h"
00032 #else
00033 #include <stdlib.h>
00034 #define mbedtls_calloc      calloc
00035 #define mbedtls_free        free
00036 #define mbedtls_time_t      time_t
00037 #define mbedtls_snprintf    snprintf
00038 #define mbedtls_vsnprintf   vsnprintf
00039 #endif
00040 
00041 #include "mbedtls/debug.h"
00042 
00043 #include <stdarg.h>
00044 #include <stdio.h>
00045 #include <string.h>
00046 
00047 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
00048     !defined(inline) && !defined(__cplusplus)
00049 #define inline __inline
00050 #endif
00051 
00052 #define DEBUG_BUF_SIZE      512
00053 
00054 static int debug_threshold = 0;
00055 
00056 void mbedtls_debug_set_threshold( int threshold )
00057 {
00058     debug_threshold = threshold;
00059 }
00060 
00061 /*
00062  * All calls to f_dbg must be made via this function
00063  */
00064 static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
00065                                     const char *file, int line,
00066                                     const char *str )
00067 {
00068     /*
00069      * If in a threaded environment, we need a thread identifier.
00070      * Since there is no portable way to get one, use the address of the ssl
00071      * context instead, as it shouldn't be shared between threads.
00072      */
00073 #if defined(MBEDTLS_THREADING_C)
00074     char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
00075     mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
00076     ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
00077 #else
00078     ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
00079 #endif
00080 }
00081 
00082 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
00083                               const char *file, int line,
00084                               const char *format, ... )
00085 {
00086     va_list argp;
00087     char str[DEBUG_BUF_SIZE];
00088     int ret;
00089 
00090     if( NULL == ssl              ||
00091         NULL == ssl->conf        ||
00092         NULL == ssl->conf->f_dbg ||
00093         level > debug_threshold )
00094     {
00095         return;
00096     }
00097 
00098     va_start( argp, format );
00099     ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
00100     va_end( argp );
00101 
00102     if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
00103     {
00104         str[ret]     = '\n';
00105         str[ret + 1] = '\0';
00106     }
00107 
00108     debug_send_line( ssl, level, file, line, str );
00109 }
00110 
00111 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
00112                       const char *file, int line,
00113                       const char *text, int ret )
00114 {
00115     char str[DEBUG_BUF_SIZE];
00116 
00117     if( NULL == ssl              ||
00118         NULL == ssl->conf        ||
00119         NULL == ssl->conf->f_dbg ||
00120         level > debug_threshold )
00121     {
00122         return;
00123     }
00124 
00125     /*
00126      * With non-blocking I/O and examples that just retry immediately,
00127      * the logs would be quickly flooded with WANT_READ, so ignore that.
00128      * Don't ignore WANT_WRITE however, since is is usually rare.
00129      */
00130     if( ret == MBEDTLS_ERR_SSL_WANT_READ )
00131         return;
00132 
00133     mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
00134               text, ret, -ret );
00135 
00136     debug_send_line( ssl, level, file, line, str );
00137 }
00138 
00139 void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
00140                       const char *file, int line, const char *text,
00141                       const unsigned char *buf, size_t len )
00142 {
00143     char str[DEBUG_BUF_SIZE];
00144     char txt[17];
00145     size_t i, idx = 0;
00146 
00147     if( NULL == ssl              ||
00148         NULL == ssl->conf        ||
00149         NULL == ssl->conf->f_dbg ||
00150         level > debug_threshold )
00151     {
00152         return;
00153     }
00154 
00155     mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
00156               text, (unsigned int) len );
00157 
00158     debug_send_line( ssl, level, file, line, str );
00159 
00160     idx = 0;
00161     memset( txt, 0, sizeof( txt ) );
00162     for( i = 0; i < len; i++ )
00163     {
00164         if( i >= 4096 )
00165             break;
00166 
00167         if( i % 16 == 0 )
00168         {
00169             if( i > 0 )
00170             {
00171                 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "  %s\n", txt );
00172                 debug_send_line( ssl, level, file, line, str );
00173 
00174                 idx = 0;
00175                 memset( txt, 0, sizeof( txt ) );
00176             }
00177 
00178             idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
00179                              (unsigned int) i );
00180 
00181         }
00182 
00183         idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
00184                          (unsigned int) buf[i] );
00185         txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
00186     }
00187 
00188     if( len > 0 )
00189     {
00190         for( /* i = i */; i % 16 != 0; i++ )
00191             idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "   " );
00192 
00193         mbedtls_snprintf( str + idx, sizeof( str ) - idx, "  %s\n", txt );
00194         debug_send_line( ssl, level, file, line, str );
00195     }
00196 }
00197 
00198 #if defined(MBEDTLS_ECP_C)
00199 void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
00200                       const char *file, int line,
00201                       const char *text, const mbedtls_ecp_point *X )
00202 {
00203     char str[DEBUG_BUF_SIZE];
00204 
00205     if( NULL == ssl              ||
00206         NULL == ssl->conf        ||
00207         NULL == ssl->conf->f_dbg ||
00208         level > debug_threshold )
00209     {
00210         return;
00211     }
00212 
00213     mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
00214     mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X  );
00215 
00216     mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
00217     mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y  );
00218 }
00219 #endif /* MBEDTLS_ECP_C */
00220 
00221 #if defined(MBEDTLS_BIGNUM_C)
00222 void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
00223                       const char *file, int line,
00224                       const char *text, const mbedtls_mpi *X )
00225 {
00226     char str[DEBUG_BUF_SIZE];
00227     int j, k, zeros = 1;
00228     size_t i, n, idx = 0;
00229 
00230     if( NULL == ssl              ||
00231         NULL == ssl->conf        ||
00232         NULL == ssl->conf->f_dbg ||
00233         NULL == X                ||
00234         level > debug_threshold )
00235     {
00236         return;
00237     }
00238 
00239     for( n = X->n  - 1; n > 0; n-- )
00240         if( X->p [n] != 0 )
00241             break;
00242 
00243     for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
00244         if( ( ( X->p [n] >> j ) & 1 ) != 0 )
00245             break;
00246 
00247     mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
00248               text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
00249 
00250     debug_send_line( ssl, level, file, line, str );
00251 
00252     idx = 0;
00253     for( i = n + 1, j = 0; i > 0; i-- )
00254     {
00255         if( zeros && X->p [i - 1] == 0 )
00256             continue;
00257 
00258         for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
00259         {
00260             if( zeros && ( ( X->p [i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
00261                 continue;
00262             else
00263                 zeros = 0;
00264 
00265             if( j % 16 == 0 )
00266             {
00267                 if( j > 0 )
00268                 {
00269                     mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
00270                     debug_send_line( ssl, level, file, line, str );
00271                     idx = 0;
00272                 }
00273             }
00274 
00275             idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
00276                              ( X->p [i - 1] >> ( k << 3 ) ) & 0xFF );
00277 
00278             j++;
00279         }
00280 
00281     }
00282 
00283     if( zeros == 1 )
00284         idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
00285 
00286     mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
00287     debug_send_line( ssl, level, file, line, str );
00288 }
00289 #endif /* MBEDTLS_BIGNUM_C */
00290 
00291 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00292 static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
00293                             const char *file, int line,
00294                             const char *text, const mbedtls_pk_context *pk )
00295 {
00296     size_t i;
00297     mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
00298     char name[16];
00299 
00300     memset( items, 0, sizeof( items ) );
00301 
00302     if( mbedtls_pk_debug( pk, items ) != 0 )
00303     {
00304         debug_send_line( ssl, level, file, line,
00305                           "invalid PK context\n" );
00306         return;
00307     }
00308 
00309     for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
00310     {
00311         if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
00312             return;
00313 
00314         mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
00315         name[sizeof( name ) - 1] = '\0';
00316 
00317         if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
00318             mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
00319         else
00320 #if defined(MBEDTLS_ECP_C)
00321         if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
00322             mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
00323         else
00324 #endif
00325             debug_send_line( ssl, level, file, line,
00326                               "should not happen\n" );
00327     }
00328 }
00329 
00330 static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
00331                                       const char *file, int line, const char *text )
00332 {
00333     char str[DEBUG_BUF_SIZE];
00334     const char *start, *cur;
00335 
00336     start = text;
00337     for( cur = text; *cur != '\0'; cur++ )
00338     {
00339         if( *cur == '\n' )
00340         {
00341             size_t len = cur - start + 1;
00342             if( len > DEBUG_BUF_SIZE - 1 )
00343                 len = DEBUG_BUF_SIZE - 1;
00344 
00345             memcpy( str, start, len );
00346             str[len] = '\0';
00347 
00348             debug_send_line( ssl, level, file, line, str );
00349 
00350             start = cur + 1;
00351         }
00352     }
00353 }
00354 
00355 void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
00356                       const char *file, int line,
00357                       const char *text, const mbedtls_x509_crt *crt )
00358 {
00359     char str[DEBUG_BUF_SIZE];
00360     int i = 0;
00361 
00362     if( NULL == ssl              ||
00363         NULL == ssl->conf        ||
00364         NULL == ssl->conf->f_dbg ||
00365         NULL == crt              ||
00366         level > debug_threshold )
00367     {
00368         return;
00369     }
00370 
00371     while( crt != NULL )
00372     {
00373         char buf[1024];
00374 
00375         mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
00376         debug_send_line( ssl, level, file, line, str );
00377 
00378         mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
00379         debug_print_line_by_line( ssl, level, file, line, buf );
00380 
00381         debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
00382 
00383         crt = crt->next;
00384     }
00385 }
00386 #endif /* MBEDTLS_X509_CRT_PARSE_C */
00387 
00388 #if defined(MBEDTLS_ECDH_C)
00389 static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
00390                                                 int level, const char *file,
00391                                                 int line,
00392                                                 const mbedtls_ecdh_context *ecdh,
00393                                                 mbedtls_debug_ecdh_attr attr )
00394 {
00395 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
00396     const mbedtls_ecdh_context* ctx = ecdh;
00397 #else
00398     const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx .mbed_ecdh;
00399 #endif
00400 
00401     switch( attr )
00402     {
00403         case MBEDTLS_DEBUG_ECDH_Q:
00404             mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
00405                                      &ctx->Q  );
00406             break;
00407         case MBEDTLS_DEBUG_ECDH_QP:
00408             mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
00409                                      &ctx->Qp  );
00410             break;
00411         case MBEDTLS_DEBUG_ECDH_Z:
00412             mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
00413                                      &ctx->z  );
00414             break;
00415         default:
00416             break;
00417     }
00418 }
00419 
00420 void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
00421                                 const char *file, int line,
00422                                 const mbedtls_ecdh_context *ecdh,
00423                                 mbedtls_debug_ecdh_attr attr )
00424 {
00425 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
00426     mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
00427 #else
00428     switch( ecdh->var  )
00429     {
00430         default:
00431             mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
00432                                                 attr );
00433     }
00434 #endif
00435 }
00436 #endif /* MBEDTLS_ECDH_C */
00437 
00438 #endif /* MBEDTLS_DEBUG_C */