1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * Debugging routines 4 * 5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 6 * 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 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 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. 18 * 19 * This file is part of mbed TLS (https://tls.mbed.org) 20 */ 21 22 #if !defined(MBEDTLS_CONFIG_FILE) 23 #include "mbedtls/config.h" 24 #else 25 #include MBEDTLS_CONFIG_FILE 26 #endif 27 28 #if defined(MBEDTLS_DEBUG_C) 29 30 #if defined(MBEDTLS_PLATFORM_C) 31 #include "mbedtls/platform.h" 32 #else 33 #include <stdlib.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 39 #endif 40 41 #include "mbedtls/debug.h" 42 #include "mbedtls/error.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 = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 90 91 if( NULL == ssl || 92 NULL == ssl->conf || 93 NULL == ssl->conf->f_dbg || 94 level > debug_threshold ) 95 { 96 return; 97 } 98 99 va_start( argp, format ); 100 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); 101 va_end( argp ); 102 103 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) 104 { 105 str[ret] = '\n'; 106 str[ret + 1] = '\0'; 107 } 108 109 debug_send_line( ssl, level, file, line, str ); 110 } 111 112 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, 113 const char *file, int line, 114 const char *text, int ret ) 115 { 116 char str[DEBUG_BUF_SIZE]; 117 118 if( NULL == ssl || 119 NULL == ssl->conf || 120 NULL == ssl->conf->f_dbg || 121 level > debug_threshold ) 122 { 123 return; 124 } 125 126 /* 127 * With non-blocking I/O and examples that just retry immediately, 128 * the logs would be quickly flooded with WANT_READ, so ignore that. 129 * Don't ignore WANT_WRITE however, since is is usually rare. 130 */ 131 if( ret == MBEDTLS_ERR_SSL_WANT_READ ) 132 return; 133 134 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n", 135 text, ret, -ret ); 136 137 debug_send_line( ssl, level, file, line, str ); 138 } 139 140 void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level, 141 const char *file, int line, const char *text, 142 const unsigned char *buf, size_t len ) 143 { 144 char str[DEBUG_BUF_SIZE]; 145 char txt[17]; 146 size_t i, idx = 0; 147 148 if( NULL == ssl || 149 NULL == ssl->conf || 150 NULL == ssl->conf->f_dbg || 151 level > debug_threshold ) 152 { 153 return; 154 } 155 156 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n", 157 text, (unsigned int) len ); 158 159 debug_send_line( ssl, level, file, line, str ); 160 161 idx = 0; 162 memset( txt, 0, sizeof( txt ) ); 163 for( i = 0; i < len; i++ ) 164 { 165 if( i >= 4096 ) 166 break; 167 168 if( i % 16 == 0 ) 169 { 170 if( i > 0 ) 171 { 172 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); 173 debug_send_line( ssl, level, file, line, str ); 174 175 idx = 0; 176 memset( txt, 0, sizeof( txt ) ); 177 } 178 179 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ", 180 (unsigned int) i ); 181 182 } 183 184 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", 185 (unsigned int) buf[i] ); 186 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; 187 } 188 189 if( len > 0 ) 190 { 191 for( /* i = i */; i % 16 != 0; i++ ) 192 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " ); 193 194 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); 195 debug_send_line( ssl, level, file, line, str ); 196 } 197 } 198 199 #if defined(MBEDTLS_ECP_C) 200 void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level, 201 const char *file, int line, 202 const char *text, const mbedtls_ecp_point *X ) 203 { 204 char str[DEBUG_BUF_SIZE]; 205 206 if( NULL == ssl || 207 NULL == ssl->conf || 208 NULL == ssl->conf->f_dbg || 209 level > debug_threshold ) 210 { 211 return; 212 } 213 214 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text ); 215 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X ); 216 217 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text ); 218 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y ); 219 } 220 #endif /* MBEDTLS_ECP_C */ 221 222 #if defined(MBEDTLS_BIGNUM_C) 223 void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level, 224 const char *file, int line, 225 const char *text, const mbedtls_mpi *X ) 226 { 227 char str[DEBUG_BUF_SIZE]; 228 int j, k, zeros = 1; 229 size_t i, n, idx = 0; 230 231 if( NULL == ssl || 232 NULL == ssl->conf || 233 NULL == ssl->conf->f_dbg || 234 NULL == X || 235 level > debug_threshold ) 236 { 237 return; 238 } 239 240 for( n = X->n - 1; n > 0; n-- ) 241 if( X->p[n] != 0 ) 242 break; 243 244 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- ) 245 if( ( ( X->p[n] >> j ) & 1 ) != 0 ) 246 break; 247 248 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n", 249 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) ); 250 251 debug_send_line( ssl, level, file, line, str ); 252 253 idx = 0; 254 for( i = n + 1, j = 0; i > 0; i-- ) 255 { 256 if( zeros && X->p[i - 1] == 0 ) 257 continue; 258 259 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- ) 260 { 261 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 ) 262 continue; 263 else 264 zeros = 0; 265 266 if( j % 16 == 0 ) 267 { 268 if( j > 0 ) 269 { 270 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); 271 debug_send_line( ssl, level, file, line, str ); 272 idx = 0; 273 } 274 } 275 276 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int) 277 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ); 278 279 j++; 280 } 281 282 } 283 284 if( zeros == 1 ) 285 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" ); 286 287 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); 288 debug_send_line( ssl, level, file, line, str ); 289 } 290 #endif /* MBEDTLS_BIGNUM_C */ 291 292 #if defined(MBEDTLS_X509_CRT_PARSE_C) 293 static void debug_print_pk( const mbedtls_ssl_context *ssl, int level, 294 const char *file, int line, 295 const char *text, const mbedtls_pk_context *pk ) 296 { 297 size_t i; 298 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; 299 char name[16]; 300 301 memset( items, 0, sizeof( items ) ); 302 303 if( mbedtls_pk_debug( pk, items ) != 0 ) 304 { 305 debug_send_line( ssl, level, file, line, 306 "invalid PK context\n" ); 307 return; 308 } 309 310 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ ) 311 { 312 if( items[i].type == MBEDTLS_PK_DEBUG_NONE ) 313 return; 314 315 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); 316 name[sizeof( name ) - 1] = '\0'; 317 318 if( items[i].type == MBEDTLS_PK_DEBUG_MPI ) 319 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value ); 320 else 321 #if defined(MBEDTLS_ECP_C) 322 if( items[i].type == MBEDTLS_PK_DEBUG_ECP ) 323 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value ); 324 else 325 #endif 326 debug_send_line( ssl, level, file, line, 327 "should not happen\n" ); 328 } 329 } 330 331 static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level, 332 const char *file, int line, const char *text ) 333 { 334 char str[DEBUG_BUF_SIZE]; 335 const char *start, *cur; 336 337 start = text; 338 for( cur = text; *cur != '\0'; cur++ ) 339 { 340 if( *cur == '\n' ) 341 { 342 size_t len = cur - start + 1; 343 if( len > DEBUG_BUF_SIZE - 1 ) 344 len = DEBUG_BUF_SIZE - 1; 345 346 memcpy( str, start, len ); 347 str[len] = '\0'; 348 349 debug_send_line( ssl, level, file, line, str ); 350 351 start = cur + 1; 352 } 353 } 354 } 355 356 void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level, 357 const char *file, int line, 358 const char *text, const mbedtls_x509_crt *crt ) 359 { 360 char str[DEBUG_BUF_SIZE]; 361 int i = 0; 362 363 if( NULL == ssl || 364 NULL == ssl->conf || 365 NULL == ssl->conf->f_dbg || 366 NULL == crt || 367 level > debug_threshold ) 368 { 369 return; 370 } 371 372 while( crt != NULL ) 373 { 374 char buf[1024]; 375 376 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i ); 377 debug_send_line( ssl, level, file, line, str ); 378 379 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); 380 debug_print_line_by_line( ssl, level, file, line, buf ); 381 382 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk ); 383 384 crt = crt->next; 385 } 386 } 387 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 388 389 #if defined(MBEDTLS_ECDH_C) 390 static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl, 391 int level, const char *file, 392 int line, 393 const mbedtls_ecdh_context *ecdh, 394 mbedtls_debug_ecdh_attr attr ) 395 { 396 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) 397 const mbedtls_ecdh_context* ctx = ecdh; 398 #else 399 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh; 400 #endif 401 402 switch( attr ) 403 { 404 case MBEDTLS_DEBUG_ECDH_Q: 405 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q", 406 &ctx->Q ); 407 break; 408 case MBEDTLS_DEBUG_ECDH_QP: 409 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp", 410 &ctx->Qp ); 411 break; 412 case MBEDTLS_DEBUG_ECDH_Z: 413 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z", 414 &ctx->z ); 415 break; 416 default: 417 break; 418 } 419 } 420 421 void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level, 422 const char *file, int line, 423 const mbedtls_ecdh_context *ecdh, 424 mbedtls_debug_ecdh_attr attr ) 425 { 426 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) 427 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr ); 428 #else 429 switch( ecdh->var ) 430 { 431 default: 432 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, 433 attr ); 434 } 435 #endif 436 } 437 #endif /* MBEDTLS_ECDH_C */ 438 439 #endif /* MBEDTLS_DEBUG_C */ 440