void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, const mbedtls_mpi *X ) { char str[DEBUG_BUF_SIZE]; int j, k, zeros = 1; size_t i, n, idx = 0; if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold ) return; for( n = X->n - 1; n > 0; n-- ) if( X->p[n] != 0 ) break; for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- ) if( ( ( X->p[n] >> j ) & 1 ) != 0 ) break; mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n", text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) ); debug_send_line( ssl, level, file, line, str ); idx = 0; for( i = n + 1, j = 0; i > 0; i-- ) { if( zeros && X->p[i - 1] == 0 ) continue; for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- ) { if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 ) continue; else zeros = 0; if( j % 16 == 0 ) { if( j > 0 ) { mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); debug_send_line( ssl, level, file, line, str ); idx = 0; } } idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int) ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ); j++; } }
void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, const unsigned char *buf, size_t len ) { char str[DEBUG_BUF_SIZE]; char txt[17]; size_t i, idx = 0; if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) return; mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n", text, (unsigned int) len ); debug_send_line( ssl, level, file, line, str ); idx = 0; memset( txt, 0, sizeof( txt ) ); for( i = 0; i < len; i++ ) { if( i >= 4096 ) break; if( i % 16 == 0 ) { if( i > 0 ) { mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); debug_send_line( ssl, level, file, line, str ); idx = 0; memset( txt, 0, sizeof( txt ) ); } idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ", (unsigned int) i ); } idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int) buf[i] ); txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; } if( len > 0 ) { for( /* i = i */; i % 16 != 0; i++ ) idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " ); mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt ); debug_send_line( ssl, level, file, line, str ); } }
void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *format, ... ) { va_list argp; #ifndef TLS_DEBUG_HEAP_USE char str[DEBUG_BUF_SIZE]; #else char *str = tls_debug_zalloc(DEBUG_BUF_SIZE); if (str == NULL) return; #endif int ret; if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) #ifndef TLS_DEBUG_HEAP_USE return; #else goto exit; #endif va_start( argp, format ); #if defined(_WIN32) #if defined(_TRUNCATE) ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); #else ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE ) { str[DEBUG_BUF_SIZE-1] = '\0'; ret = -1; } #endif #else ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); #endif va_end( argp ); if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) { str[ret] = '\n'; str[ret + 1] = '\0'; } debug_send_line( ssl, level, file, line, str ); #ifdef TLS_DEBUG_HEAP_USE exit: tls_debug_free(str); #endif }
void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, int ret ) { #ifndef TLS_DEBUG_HEAP_USE char str[DEBUG_BUF_SIZE]; #else char *str = tls_debug_zalloc(DEBUG_BUF_SIZE); if (str == NULL) return; #endif if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) #ifndef TLS_DEBUG_HEAP_USE return; #else goto exit; #endif /* * With non-blocking I/O and examples that just retry immediately, * the logs would be quickly flooded with WANT_READ, so ignore that. * Don't ignore WANT_WRITE however, since is is usually rare. */ if( ret == MBEDTLS_ERR_SSL_WANT_READ ) #ifndef TLS_DEBUG_HEAP_USE return; #else goto exit; #endif mbedtls_snprintf( str, DEBUG_BUF_SIZE/*sizeof( str )*/, "%s() returned %d (-0x%04x)\n", text, ret, -ret ); debug_send_line( ssl, level, file, line, str ); #ifdef TLS_DEBUG_HEAP_USE exit: tls_debug_free(str); #endif }
void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, int ret ) { char str[DEBUG_BUF_SIZE]; if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) return; /* * With non-blocking I/O and examples that just retry immediately, * the logs would be quickly flooded with WANT_READ, so ignore that. * Don't ignore WANT_WRITE however, since is is usually rare. */ if( ret == MBEDTLS_ERR_SSL_WANT_READ ) return; mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n", text, ret, -ret ); debug_send_line( ssl, level, file, line, str ); }
void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *format, ... ) { va_list argp; char str[DEBUG_BUF_SIZE]; int ret; if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold ) return; va_start( argp, format ); #if defined(_WIN32) #if defined(_TRUNCATE) && !defined(__MINGW32__) ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); #else ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE ) { str[DEBUG_BUF_SIZE-1] = '\0'; ret = -1; } #endif #else ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); #endif va_end( argp ); if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) { str[ret] = '\n'; str[ret + 1] = '\0'; } debug_send_line( ssl, level, file, line, str ); }
void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, const unsigned char *buf, size_t len ) { #ifndef TLS_DEBUG_HEAP_USE char str[DEBUG_BUF_SIZE]; #else char *str = tls_debug_zalloc(DEBUG_BUF_SIZE); if (str == NULL) return; #endif char txt[17]; size_t i, idx = 0; if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) #ifndef TLS_DEBUG_HEAP_USE return; #else goto exit; #endif mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE/*sizeof( str )*/ - idx, "dumping '%s' (%u bytes)\n", text, (unsigned int) len ); debug_send_line( ssl, level, file, line, str ); idx = 0; memset( txt, 0, sizeof( txt ) ); for( i = 0; i < len; i++ ) { if( i >= 4096 ) break; if( i % 16 == 0 ) { if( i > 0 ) { mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE/*sizeof( str )*/ - idx, " %s\n", txt ); debug_send_line( ssl, level, file, line, str ); idx = 0; memset( txt, 0, sizeof( txt ) ); } idx += mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE/*sizeof( str )*/ - idx, "%04x: ", (unsigned int) i ); } idx += mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE/*sizeof( str )*/ - idx, " %02x", (unsigned int) buf[i] ); txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; } if( len > 0 ) { for( /* i = i */; i % 16 != 0; i++ ) idx += mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE/*sizeof( str )*/ - idx, " " ); mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE/*sizeof( str )*/ - idx, " %s\n", txt ); debug_send_line( ssl, level, file, line, str ); } #ifdef TLS_DEBUG_HEAP_USE exit: tls_debug_free(str); #endif }