2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Debugging routines
|
|
|
|
*
|
2010-07-18 12:13:04 +02:00
|
|
|
* Copyright (C) 2006-2010, Brainspark B.V.
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
2010-07-18 12:13:04 +02:00
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2009-07-28 19:23:11 +02:00
|
|
|
* All rights reserved.
|
2009-01-04 17:27:10 +01:00
|
|
|
*
|
2009-01-03 22:22:43 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#if defined(POLARSSL_DEBUG_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/debug.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(EFIX64) || defined(EFI32)
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-29 15:22:54 +01:00
|
|
|
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
|
|
|
#if !defined snprintf
|
2009-01-03 22:22:43 +01:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2013-10-29 15:22:54 +01:00
|
|
|
#if !defined vsnprintf
|
2009-01-03 22:22:43 +01:00
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#endif
|
2013-10-29 15:22:54 +01:00
|
|
|
#endif /* _MSC_VER */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
|
2014-04-25 16:34:30 +02:00
|
|
|
static int debug_threshold = 0;
|
2014-04-25 15:04:14 +02:00
|
|
|
|
|
|
|
void debug_set_log_mode( int log_mode )
|
|
|
|
{
|
|
|
|
debug_log_mode = log_mode;
|
|
|
|
}
|
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
void debug_set_threshold( int threshold )
|
|
|
|
{
|
|
|
|
debug_threshold = threshold;
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
char *debug_fmt( const char *format, ... )
|
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
static char str[512];
|
|
|
|
int maxlen = sizeof( str ) - 1;
|
|
|
|
|
|
|
|
va_start( argp, format );
|
|
|
|
vsnprintf( str, maxlen, format, argp );
|
|
|
|
va_end( argp );
|
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
return( str );
|
|
|
|
}
|
|
|
|
|
2010-03-16 22:09:09 +01:00
|
|
|
void debug_print_msg( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line, const char *text )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
int maxlen = sizeof( str ) - 1;
|
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
if( ssl->f_dbg == NULL || level > debug_threshold )
|
2009-01-03 22:22:43 +01:00
|
|
|
return;
|
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
|
|
|
|
{
|
2014-05-06 10:29:28 +02:00
|
|
|
ssl->f_dbg( ssl->p_dbg, level, text );
|
2014-04-25 15:04:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
|
2010-03-16 22:09:09 +01:00
|
|
|
void debug_print_ret( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *text, int ret )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
int maxlen = sizeof( str ) - 1;
|
2014-04-25 15:04:14 +02:00
|
|
|
size_t idx = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
if( ssl->f_dbg == NULL || level > debug_threshold )
|
2009-01-03 22:22:43 +01:00
|
|
|
return;
|
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
|
|
|
|
|
|
|
|
snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
|
|
|
|
text, ret, -ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
|
2010-03-16 22:09:09 +01:00
|
|
|
void debug_print_buf( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line, const char *text,
|
2011-04-24 10:57:21 +02:00
|
|
|
unsigned char *buf, size_t len )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
char str[512];
|
2014-04-25 15:04:14 +02:00
|
|
|
size_t i, maxlen = sizeof( str ) - 1, idx = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
if( ssl->f_dbg == NULL || level > debug_threshold )
|
2009-01-03 22:22:43 +01:00
|
|
|
return;
|
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
|
|
|
|
|
|
|
|
snprintf( str + idx, maxlen - idx, "dumping '%s' (%d bytes)\n",
|
|
|
|
text, (unsigned int) len );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
idx = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
if( i >= 4096 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( i % 16 == 0 )
|
|
|
|
{
|
|
|
|
if( i > 0 )
|
2014-04-25 15:18:34 +02:00
|
|
|
{
|
|
|
|
snprintf( str + idx, maxlen - idx, "\n" );
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
idx = 0;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
|
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
idx += snprintf( str + idx, maxlen - idx, "%04x: ",
|
|
|
|
(unsigned int) i );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
idx += snprintf( str + idx, maxlen - idx, " %02x",
|
|
|
|
(unsigned int) buf[i] );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( len > 0 )
|
2014-04-25 15:18:34 +02:00
|
|
|
{
|
|
|
|
snprintf( str + idx, maxlen - idx, "\n" );
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 14:39:14 +01:00
|
|
|
#if defined(POLARSSL_ECP_C)
|
|
|
|
void debug_print_ecp( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *text, const ecp_point *X )
|
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
int maxlen = sizeof( str ) - 1;
|
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
if( ssl->f_dbg == NULL || level > debug_threshold )
|
|
|
|
return;
|
|
|
|
|
2013-03-20 14:39:14 +01:00
|
|
|
snprintf( str, maxlen, "%s(X)", text );
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
debug_print_mpi( ssl, level, file, line, str, &X->X );
|
|
|
|
|
|
|
|
snprintf( str, maxlen, "%s(Y)", text );
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
debug_print_mpi( ssl, level, file, line, str, &X->Y );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_ECP_C */
|
|
|
|
|
2013-04-18 22:46:23 +02:00
|
|
|
#if defined(POLARSSL_BIGNUM_C)
|
2010-03-16 22:09:09 +01:00
|
|
|
void debug_print_mpi( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *text, const mpi *X )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
char str[512];
|
2011-04-24 10:57:21 +02:00
|
|
|
int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
|
2014-04-25 15:04:14 +02:00
|
|
|
size_t i, n, idx = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
|
2009-01-03 22:22:43 +01:00
|
|
|
return;
|
|
|
|
|
2011-03-14 21:41:31 +01:00
|
|
|
for( n = X->n - 1; n > 0; n-- )
|
2009-01-03 22:22:43 +01:00
|
|
|
if( X->p[n] != 0 )
|
|
|
|
break;
|
|
|
|
|
2011-04-24 11:11:17 +02:00
|
|
|
for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
|
2011-03-14 21:41:31 +01:00
|
|
|
if( ( ( X->p[n] >> j ) & 1 ) != 0 )
|
|
|
|
break;
|
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
|
|
|
|
|
|
|
|
snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
|
|
|
|
text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
idx = 0;
|
2011-04-24 10:57:21 +02:00
|
|
|
for( i = n + 1, j = 0; i > 0; i-- )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
if( zeros && X->p[i - 1] == 0 )
|
2011-03-14 21:41:31 +01:00
|
|
|
continue;
|
|
|
|
|
2011-04-24 11:11:17 +02:00
|
|
|
for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
|
2011-03-14 21:41:31 +01:00
|
|
|
continue;
|
|
|
|
else
|
|
|
|
zeros = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-03-14 21:41:31 +01:00
|
|
|
if( j % 16 == 0 )
|
|
|
|
{
|
|
|
|
if( j > 0 )
|
2014-04-25 15:04:14 +02:00
|
|
|
{
|
2014-04-25 15:18:34 +02:00
|
|
|
snprintf( str + idx, maxlen - idx, "\n" );
|
2014-04-25 15:04:14 +02:00
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
2014-04-25 15:18:34 +02:00
|
|
|
idx = 0;
|
2014-04-25 15:04:14 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
|
|
|
|
( X->p[i - 1] >> (k << 3) ) & 0xFF );
|
2011-03-14 21:41:31 +01:00
|
|
|
|
|
|
|
j++;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2011-03-14 21:41:31 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( zeros == 1 )
|
|
|
|
{
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
{
|
2014-04-25 15:18:34 +02:00
|
|
|
idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
|
2011-03-14 21:41:31 +01:00
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
}
|
2014-04-25 15:18:34 +02:00
|
|
|
idx += snprintf( str + idx, maxlen - idx, " 00" );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
snprintf( str + idx, maxlen - idx, "\n" );
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-04-18 22:46:23 +02:00
|
|
|
#endif /* POLARSSL_BIGNUM_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#if defined(POLARSSL_X509_CRT_PARSE_C)
|
2013-08-14 18:04:18 +02:00
|
|
|
static void debug_print_pk( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *text, const pk_context *pk )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
|
|
|
|
char name[16];
|
|
|
|
|
|
|
|
memset( items, 0, sizeof( items ) );
|
|
|
|
|
|
|
|
if( pk_debug( pk, items ) != 0 )
|
|
|
|
{
|
|
|
|
debug_print_msg( ssl, level, file, line, "invalid PK context" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-17 12:39:05 +02:00
|
|
|
for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
|
2013-08-14 18:04:18 +02:00
|
|
|
{
|
|
|
|
if( items[i].type == POLARSSL_PK_DEBUG_NONE )
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
|
|
|
|
name[sizeof( name ) - 1] = '\0';
|
|
|
|
|
|
|
|
if( items[i].type == POLARSSL_PK_DEBUG_MPI )
|
|
|
|
debug_print_mpi( ssl, level, file, line, name, items[i].value );
|
2013-10-15 11:54:47 +02:00
|
|
|
else
|
|
|
|
#if defined(POLARSSL_ECP_C)
|
|
|
|
if( items[i].type == POLARSSL_PK_DEBUG_ECP )
|
2013-08-14 18:04:18 +02:00
|
|
|
debug_print_ecp( ssl, level, file, line, name, items[i].value );
|
|
|
|
else
|
2013-10-15 11:54:47 +02:00
|
|
|
#endif
|
2013-08-14 18:04:18 +02:00
|
|
|
debug_print_msg( ssl, level, file, line, "should not happen" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-16 22:09:09 +01:00
|
|
|
void debug_print_crt( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line,
|
2013-09-18 14:13:26 +02:00
|
|
|
const char *text, const x509_crt *crt )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-05-02 17:13:40 +02:00
|
|
|
char str[1024], prefix[64];
|
2014-04-25 15:04:14 +02:00
|
|
|
int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
|
2009-01-03 22:22:43 +01:00
|
|
|
return;
|
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
{
|
|
|
|
snprintf( prefix, maxlen, "%s(%04d): ", file, line );
|
|
|
|
prefix[maxlen] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prefix[0] = '\0';
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
maxlen = sizeof( str ) - 1;
|
|
|
|
|
2010-03-21 22:03:34 +01:00
|
|
|
while( crt != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2009-05-02 17:13:40 +02:00
|
|
|
char buf[1024];
|
2013-09-18 13:46:23 +02:00
|
|
|
x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-04-25 15:04:14 +02:00
|
|
|
if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
|
|
|
|
idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
|
|
|
|
|
|
|
|
snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
|
|
|
|
text, ++i, buf );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
|
2013-08-14 18:04:18 +02:00
|
|
|
debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
crt = crt->next;
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif /* POLARSSL_X509_CRT_PARSE_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_DEBUG_C */
|