Skip to content

Instantly share code, notes, and snippets.

@vtjnash
Created October 24, 2016 20:10
Show Gist options
  • Save vtjnash/a41fad4e9feada7fbc36f126ea16b061 to your computer and use it in GitHub Desktop.
Save vtjnash/a41fad4e9feada7fbc36f126ea16b061 to your computer and use it in GitHub Desktop.
make mbedtls debug logic usable
Apply the below patch to mbedtls.
Reconfigure with `cmake -D CMAKE_BUILD_TYPE=Debug .` (in the scratch build directory).
Rebuild / reinstall.
Enable debugging with `ccall((:mbedtls_debug_set_threshold, :libmbedtls), Void, (Cint,), 4)`.
diff -rup mbedtls-2.3.0-gpl/library/debug.c mbedtls-2.3.0-gpl-debug/library/debug.c
--- mbedtls-2.3.0-gpl/library/debug.c 2016-06-28 08:42:05.000000000 -0400
+++ mbedtls-2.3.0-gpl-debug/library/debug.c 2016-10-24 15:52:33.152253092 -0400
@@ -59,6 +59,11 @@ void mbedtls_debug_set_threshold( int th
debug_threshold = threshold;
}
+static void debug_fprintf(void *a, int b, const char *c, int d, const char *msg)
+{
+ fprintf(stderr, "%s\n", msg);
+}
+
/*
* All calls to f_dbg must be made via this function
*/
@@ -88,8 +93,10 @@ void mbedtls_debug_print_msg( const mbed
char str[DEBUG_BUF_SIZE];
int ret;
- if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold )
+ if( NULL == ssl || NULL == ssl->conf || level > debug_threshold )
return;
+ if( NULL == ssl->conf->f_dbg )
+ ((mbedtls_ssl_config*)ssl->conf)->f_dbg = debug_fprintf;
va_start( argp, format );
#if defined(_WIN32)
@@ -123,8 +130,10 @@ void mbedtls_debug_print_ret( const mbed
{
char str[DEBUG_BUF_SIZE];
- if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
+ if( ssl->conf == NULL || level > debug_threshold )
return;
+ if( NULL == ssl->conf->f_dbg )
+ ((mbedtls_ssl_config*)ssl->conf)->f_dbg = debug_fprintf;
/*
* With non-blocking I/O and examples that just retry immediately,
@@ -148,8 +157,10 @@ void mbedtls_debug_print_buf( const mbed
char txt[17];
size_t i, idx = 0;
- if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
+ if( ssl->conf == NULL || level > debug_threshold )
return;
+ if( NULL == ssl->conf->f_dbg )
+ ((mbedtls_ssl_config*)ssl->conf)->f_dbg = debug_fprintf;
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
text, (unsigned int) len );
@@ -201,8 +212,10 @@ void mbedtls_debug_print_ecp( const mbed
{
char str[DEBUG_BUF_SIZE];
- if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
+ if( ssl->conf == NULL || level > debug_threshold )
return;
+ if( NULL == ssl->conf->f_dbg )
+ ((mbedtls_ssl_config*)ssl->conf)->f_dbg = debug_fprintf;
mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
@@ -221,8 +234,10 @@ void mbedtls_debug_print_mpi( const mbed
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 )
+ if( ssl->conf == NULL || X == NULL || level > debug_threshold )
return;
+ if( NULL == ssl->conf->f_dbg )
+ ((mbedtls_ssl_config*)ssl->conf)->f_dbg = debug_fprintf;
for( n = X->n - 1; n > 0; n-- )
if( X->p[n] != 0 )
@@ -347,8 +362,10 @@ void mbedtls_debug_print_crt( const mbed
char str[DEBUG_BUF_SIZE];
int i = 0;
- if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
+ if( ssl->conf == NULL || crt == NULL || level > debug_threshold )
return;
+ if( NULL == ssl->conf->f_dbg )
+ ((mbedtls_ssl_config*)ssl->conf)->f_dbg = debug_fprintf;
while( crt != NULL )
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment