1 /* 2 * Debugging routines 3 * 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 * SPDX-License-Identifier: GPL-2.0 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 24 #if !defined(MBEDTLS_CONFIG_FILE) 25 #include "mbedtls/config.h" 26 #else 27 #include MBEDTLS_CONFIG_FILE 28 #endif 29 30 #if defined(MBEDTLS_DEBUG_C) 31 32 #if defined(MBEDTLS_PLATFORM_C) 33 #include "mbedtls/platform.h" 34 #else 35 #include <stdlib.h> 36 #define mbedtls_calloc calloc 37 #define mbedtls_free free 38 #define mbedtls_time_t time_t 39 #define mbedtls_snprintf snprintf 40 #endif 41 42 #include "mbedtls/debug.h" 43 44 #include <stdarg.h> 45 #include <stdio.h> 46 #include <string.h> 47 48 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ 49 !defined(inline) && !defined(__cplusplus) 50 #define inline __inline 51 #endif 52 53 #define DEBUG_BUF_SIZE 512 54 55 static int debug_threshold = 0; 56 57 void mbedtls_debug_set_threshold( int threshold ) 58 { 59 debug_threshold = threshold; 60 } 61 62 /* 63 * All calls to f_dbg must be made via this function 64 */ 65 static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level, 66 const char *file, int line, 67 const char *str ) 68 { 69 /* 70 * If in a threaded environment, we need a thread identifier. 71 * Since there is no portable way to get one, use the address of the ssl 72 * context instead, as it shouldn't be shared between threads. 73 */ 74 #if defined(MBEDTLS_THREADING_C) 75 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */ 76 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str ); 77 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr ); 78 #else 79 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str ); 80 #endif 81 } 82 83 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, 84 const char *file, int line, 85 const char *format, ... ) 86 { 87 va_list argp; 88 char str[DEBUG_BUF_SIZE]; 89 int ret; 90 91 if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold ) 92 return; 93 94 va_start( argp, format ); 95 #if defined(_WIN32) 96 #if defined(_TRUNCATE) 97 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); 98 #else 99 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); 100 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE ) 101 { 102 str[DEBUG_BUF_SIZE-1] = '\0'; 103 ret = -1; 104 } 105 #endif 106 #else 107 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); 108 #endif 109 va_end( argp ); 110 111 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) 112 { 113 str[ret] = '\n'; 114 str[ret + 1] = '\0'; 115 } 116 117 debug_send_line( ssl, level, file, line, str ); 118 } 119 120 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, 121 const char *file, int line, 122 const char *text, int ret ) 123 { 124 char str[DEBUG_BUF_SIZE]; 125 126 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) 127 return; 128 129 /* 130 * With non-blocking I/O and examples that just retry immediately, 131 * the logs would be quickly flooded with WANT_READ, so ignore that. 132 * Don't ignore WANT_WRITE however, since is is usually rare. 133 */ 134 if( ret == MBEDTLS_ERR_SSL_WANT_READ ) 135 return; 136 137 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n", 138 text, ret, -ret ); 139 140 debug_send_line( ssl, level, file, line, str ); 141 } 142 143 void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level, 144 const char *file, int line, const char *text, 145 const unsigned char *buf, size_t len ) 146 { 147 char str[DEBUG_BUF_SIZE]; 148 char txt[17]; 149 size_t i, idx = 0; 150 151 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) 152 return; 153 154 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n", 155 text, (unsigned int) len ); 156 157 debug_send_line( ssl, level, file, line, str ); 158 159 idx = 0; 160 memset( txt, 0, sizeof( txt ) ); 161 for( i = 0; i < len; i++ ) 162 { 163 if( i >= 4096 ) 164 break; 165 166 if( i % 16 == 0 ) 167 { 168 if( i > 0 ) 169 { 170 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); 171 debug_send_line( ssl, level, file, line, str ); 172 173 idx = 0; 174 memset( txt, 0, sizeof( txt ) ); 175 } 176 177 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ", 178 (unsigned int) i ); 179 180 } 181 182 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", 183 (unsigned int) buf[i] ); 184 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; 185 } 186 187 if( len > 0 ) 188 { 189 for( /* i = i */; i % 16 != 0; i++ ) 190 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " ); 191 192 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); 193 debug_send_line( ssl, level, file, line, str ); 194 } 195 } 196 197 #if defined(MBEDTLS_ECP_C) 198 void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level, 199 const char *file, int line, 200 const char *text, const mbedtls_ecp_point *X ) 201 { 202 char str[DEBUG_BUF_SIZE]; 203 204 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) 205 return; 206 207 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text ); 208 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X ); 209 210 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text ); 211 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y ); 212 } 213 #endif /* MBEDTLS_ECP_C */ 214 215 #if defined(MBEDTLS_BIGNUM_C) 216 void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level, 217 const char *file, int line, 218 const char *text, const mbedtls_mpi *X ) 219 { 220 char str[DEBUG_BUF_SIZE]; 221 int j, k, zeros = 1; 222 size_t i, n, idx = 0; 223 224 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold ) 225 return; 226 227 for( n = X->n - 1; n > 0; n-- ) 228 if( X->p[n] != 0 ) 229 break; 230 231 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- ) 232 if( ( ( X->p[n] >> j ) & 1 ) != 0 ) 233 break; 234 235 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n", 236 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) ); 237 238 debug_send_line( ssl, level, file, line, str ); 239 240 idx = 0; 241 for( i = n + 1, j = 0; i > 0; i-- ) 242 { 243 if( zeros && X->p[i - 1] == 0 ) 244 continue; 245 246 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- ) 247 { 248 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 ) 249 continue; 250 else 251 zeros = 0; 252 253 if( j % 16 == 0 ) 254 { 255 if( j > 0 ) 256 { 257 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); 258 debug_send_line( ssl, level, file, line, str ); 259 idx = 0; 260 } 261 } 262 263 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int) 264 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ); 265 266 j++; 267 } 268 269 } 270 271 if( zeros == 1 ) 272 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" ); 273 274 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); 275 debug_send_line( ssl, level, file, line, str ); 276 } 277 #endif /* MBEDTLS_BIGNUM_C */ 278 279 #if defined(MBEDTLS_X509_CRT_PARSE_C) 280 static void debug_print_pk( const mbedtls_ssl_context *ssl, int level, 281 const char *file, int line, 282 const char *text, const mbedtls_pk_context *pk ) 283 { 284 size_t i; 285 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; 286 char name[16]; 287 288 memset( items, 0, sizeof( items ) ); 289 290 if( mbedtls_pk_debug( pk, items ) != 0 ) 291 { 292 debug_send_line( ssl, level, file, line, 293 "invalid PK context\n" ); 294 return; 295 } 296 297 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ ) 298 { 299 if( items[i].type == MBEDTLS_PK_DEBUG_NONE ) 300 return; 301 302 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); 303 name[sizeof( name ) - 1] = '\0'; 304 305 if( items[i].type == MBEDTLS_PK_DEBUG_MPI ) 306 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value ); 307 else 308 #if defined(MBEDTLS_ECP_C) 309 if( items[i].type == MBEDTLS_PK_DEBUG_ECP ) 310 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value ); 311 else 312 #endif 313 debug_send_line( ssl, level, file, line, 314 "should not happen\n" ); 315 } 316 } 317 318 static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level, 319 const char *file, int line, const char *text ) 320 { 321 char str[DEBUG_BUF_SIZE]; 322 const char *start, *cur; 323 324 start = text; 325 for( cur = text; *cur != '\0'; cur++ ) 326 { 327 if( *cur == '\n' ) 328 { 329 size_t len = cur - start + 1; 330 if( len > DEBUG_BUF_SIZE - 1 ) 331 len = DEBUG_BUF_SIZE - 1; 332 333 memcpy( str, start, len ); 334 str[len] = '\0'; 335 336 debug_send_line( ssl, level, file, line, str ); 337 338 start = cur + 1; 339 } 340 } 341 } 342 343 void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level, 344 const char *file, int line, 345 const char *text, const mbedtls_x509_crt *crt ) 346 { 347 char str[DEBUG_BUF_SIZE]; 348 int i = 0; 349 350 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold ) 351 return; 352 353 while( crt != NULL ) 354 { 355 char buf[1024]; 356 357 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i ); 358 debug_send_line( ssl, level, file, line, str ); 359 360 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); 361 debug_print_line_by_line( ssl, level, file, line, buf ); 362 363 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk ); 364 365 crt = crt->next; 366 } 367 } 368 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 369 370 #endif /* MBEDTLS_DEBUG_C */ 371