mbed TLS Build
Dependents: Encypting_Funcional
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 #endif 00039 00040 #include "mbedtls/debug.h" 00041 00042 #include <stdarg.h> 00043 #include <stdio.h> 00044 #include <string.h> 00045 00046 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ 00047 !defined(inline) && !defined(__cplusplus) 00048 #define inline __inline 00049 #endif 00050 00051 #define DEBUG_BUF_SIZE 512 00052 00053 static int debug_threshold = 0; 00054 00055 void mbedtls_debug_set_threshold( int threshold ) 00056 { 00057 debug_threshold = threshold; 00058 } 00059 00060 /* 00061 * All calls to f_dbg must be made via this function 00062 */ 00063 static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level, 00064 const char *file, int line, 00065 const char *str ) 00066 { 00067 /* 00068 * If in a threaded environment, we need a thread identifier. 00069 * Since there is no portable way to get one, use the address of the ssl 00070 * context instead, as it shouldn't be shared between threads. 00071 */ 00072 #if defined(MBEDTLS_THREADING_C) 00073 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */ 00074 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", ssl, str ); 00075 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr ); 00076 #else 00077 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str ); 00078 #endif 00079 } 00080 00081 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, 00082 const char *file, int line, 00083 const char *format, ... ) 00084 { 00085 va_list argp; 00086 char str[DEBUG_BUF_SIZE]; 00087 int ret; 00088 00089 if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold ) 00090 return; 00091 00092 va_start( argp, format ); 00093 #if defined(_WIN32) 00094 #if defined(_TRUNCATE) 00095 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); 00096 #else 00097 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); 00098 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE ) 00099 { 00100 str[DEBUG_BUF_SIZE-1] = '\0'; 00101 ret = -1; 00102 } 00103 #endif 00104 #else 00105 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); 00106 #endif 00107 va_end( argp ); 00108 00109 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) 00110 { 00111 str[ret] = '\n'; 00112 str[ret + 1] = '\0'; 00113 } 00114 00115 debug_send_line( ssl, level, file, line, str ); 00116 } 00117 00118 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, 00119 const char *file, int line, 00120 const char *text, int ret ) 00121 { 00122 char str[DEBUG_BUF_SIZE]; 00123 00124 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) 00125 return; 00126 00127 /* 00128 * With non-blocking I/O and examples that just retry immediately, 00129 * the logs would be quickly flooded with WANT_READ, so ignore that. 00130 * Don't ignore WANT_WRITE however, since is is usually rare. 00131 */ 00132 if( ret == MBEDTLS_ERR_SSL_WANT_READ ) 00133 return; 00134 00135 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n", 00136 text, ret, -ret ); 00137 00138 debug_send_line( ssl, level, file, line, str ); 00139 } 00140 00141 void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level, 00142 const char *file, int line, const char *text, 00143 const unsigned char *buf, size_t len ) 00144 { 00145 char str[DEBUG_BUF_SIZE]; 00146 char txt[17]; 00147 size_t i, idx = 0; 00148 00149 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) 00150 return; 00151 00152 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n", 00153 text, (unsigned int) len ); 00154 00155 debug_send_line( ssl, level, file, line, str ); 00156 00157 idx = 0; 00158 memset( txt, 0, sizeof( txt ) ); 00159 for( i = 0; i < len; i++ ) 00160 { 00161 if( i >= 4096 ) 00162 break; 00163 00164 if( i % 16 == 0 ) 00165 { 00166 if( i > 0 ) 00167 { 00168 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); 00169 debug_send_line( ssl, level, file, line, str ); 00170 00171 idx = 0; 00172 memset( txt, 0, sizeof( txt ) ); 00173 } 00174 00175 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ", 00176 (unsigned int) i ); 00177 00178 } 00179 00180 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", 00181 (unsigned int) buf[i] ); 00182 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; 00183 } 00184 00185 if( len > 0 ) 00186 { 00187 for( /* i = i */; i % 16 != 0; i++ ) 00188 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " ); 00189 00190 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); 00191 debug_send_line( ssl, level, file, line, str ); 00192 } 00193 } 00194 00195 #if defined(MBEDTLS_ECP_C) 00196 void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level, 00197 const char *file, int line, 00198 const char *text, const mbedtls_ecp_point *X ) 00199 { 00200 char str[DEBUG_BUF_SIZE]; 00201 00202 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) 00203 return; 00204 00205 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text ); 00206 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X ); 00207 00208 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text ); 00209 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y ); 00210 } 00211 #endif /* MBEDTLS_ECP_C */ 00212 00213 #if defined(MBEDTLS_BIGNUM_C) 00214 void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level, 00215 const char *file, int line, 00216 const char *text, const mbedtls_mpi *X ) 00217 { 00218 char str[DEBUG_BUF_SIZE]; 00219 int j, k, zeros = 1; 00220 size_t i, n, idx = 0; 00221 00222 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold ) 00223 return; 00224 00225 for( n = X->n - 1; n > 0; n-- ) 00226 if( X->p [n] != 0 ) 00227 break; 00228 00229 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- ) 00230 if( ( ( X->p [n] >> j ) & 1 ) != 0 ) 00231 break; 00232 00233 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n", 00234 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) ); 00235 00236 debug_send_line( ssl, level, file, line, str ); 00237 00238 idx = 0; 00239 for( i = n + 1, j = 0; i > 0; i-- ) 00240 { 00241 if( zeros && X->p [i - 1] == 0 ) 00242 continue; 00243 00244 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- ) 00245 { 00246 if( zeros && ( ( X->p [i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 ) 00247 continue; 00248 else 00249 zeros = 0; 00250 00251 if( j % 16 == 0 ) 00252 { 00253 if( j > 0 ) 00254 { 00255 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); 00256 debug_send_line( ssl, level, file, line, str ); 00257 idx = 0; 00258 } 00259 } 00260 00261 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int) 00262 ( X->p [i - 1] >> ( k << 3 ) ) & 0xFF ); 00263 00264 j++; 00265 } 00266 00267 } 00268 00269 if( zeros == 1 ) 00270 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" ); 00271 00272 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); 00273 debug_send_line( ssl, level, file, line, str ); 00274 } 00275 #endif /* MBEDTLS_BIGNUM_C */ 00276 00277 #if defined(MBEDTLS_X509_CRT_PARSE_C) 00278 static void debug_print_pk( const mbedtls_ssl_context *ssl, int level, 00279 const char *file, int line, 00280 const char *text, const mbedtls_pk_context *pk ) 00281 { 00282 size_t i; 00283 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; 00284 char name[16]; 00285 00286 memset( items, 0, sizeof( items ) ); 00287 00288 if( mbedtls_pk_debug( pk, items ) != 0 ) 00289 { 00290 debug_send_line( ssl, level, file, line, 00291 "invalid PK context\n" ); 00292 return; 00293 } 00294 00295 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ ) 00296 { 00297 if( items[i].type == MBEDTLS_PK_DEBUG_NONE ) 00298 return; 00299 00300 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); 00301 name[sizeof( name ) - 1] = '\0'; 00302 00303 if( items[i].type == MBEDTLS_PK_DEBUG_MPI ) 00304 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value ); 00305 else 00306 #if defined(MBEDTLS_ECP_C) 00307 if( items[i].type == MBEDTLS_PK_DEBUG_ECP ) 00308 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value ); 00309 else 00310 #endif 00311 debug_send_line( ssl, level, file, line, 00312 "should not happen\n" ); 00313 } 00314 } 00315 00316 static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level, 00317 const char *file, int line, const char *text ) 00318 { 00319 char str[DEBUG_BUF_SIZE]; 00320 const char *start, *cur; 00321 00322 start = text; 00323 for( cur = text; *cur != '\0'; cur++ ) 00324 { 00325 if( *cur == '\n' ) 00326 { 00327 size_t len = cur - start + 1; 00328 if( len > DEBUG_BUF_SIZE - 1 ) 00329 len = DEBUG_BUF_SIZE - 1; 00330 00331 memcpy( str, start, len ); 00332 str[len] = '\0'; 00333 00334 debug_send_line( ssl, level, file, line, str ); 00335 00336 start = cur + 1; 00337 } 00338 } 00339 } 00340 00341 void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level, 00342 const char *file, int line, 00343 const char *text, const mbedtls_x509_crt *crt ) 00344 { 00345 char str[DEBUG_BUF_SIZE]; 00346 int i = 0; 00347 00348 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold ) 00349 return; 00350 00351 while( crt != NULL ) 00352 { 00353 char buf[1024]; 00354 00355 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i ); 00356 debug_send_line( ssl, level, file, line, str ); 00357 00358 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); 00359 debug_print_line_by_line( ssl, level, file, line, buf ); 00360 00361 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk ); 00362 00363 crt = crt->next; 00364 } 00365 } 00366 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 00367 00368 #endif /* MBEDTLS_DEBUG_C */
Generated on Wed Jul 13 2022 20:34:20 by 1.7.2