2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Debugging routines
|
|
|
|
*
|
2010-07-18 10:13:04 +00:00
|
|
|
* Copyright (C) 2006-2010, Brainspark B.V.
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
2010-07-18 10:13:04 +00:00
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2009-07-28 17:23:11 +00:00
|
|
|
* All rights reserved.
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2009-01-03 21:22:43 +00: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.
|
|
|
|
*/
|
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#include "polarssl/config.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_DEBUG_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#include "polarssl/debug.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#if defined _MSC_VER && !defined snprintf
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined _MSC_VER && !defined vsnprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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 21:09:09 +00:00
|
|
|
void debug_print_msg( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line, const char *text )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
int maxlen = sizeof( str ) - 1;
|
|
|
|
|
|
|
|
if( ssl->f_dbg == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
|
2010-03-16 21:09:09 +00:00
|
|
|
void debug_print_ret( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *text, int ret )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
int maxlen = sizeof( str ) - 1;
|
|
|
|
|
|
|
|
if( ssl->f_dbg == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
|
|
|
|
file, line, text, ret, ret );
|
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
|
2010-03-16 21:09:09 +00:00
|
|
|
void debug_print_buf( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line, const char *text,
|
2009-01-03 21:22:43 +00:00
|
|
|
unsigned char *buf, int len )
|
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
int i, maxlen = sizeof( str ) - 1;
|
|
|
|
|
|
|
|
if( ssl->f_dbg == NULL || len < 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
|
|
|
|
file, line, text, len );
|
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
if( i >= 4096 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( i % 16 == 0 )
|
|
|
|
{
|
|
|
|
if( i > 0 )
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, "\n" );
|
|
|
|
|
|
|
|
snprintf( str, maxlen, "%s(%04d): %04x: ", file, line, i );
|
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
|
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( len > 0 )
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, "\n" );
|
|
|
|
}
|
|
|
|
|
2010-03-16 21:09:09 +00: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 21:22:43 +00:00
|
|
|
{
|
|
|
|
char str[512];
|
|
|
|
int i, j, k, n, maxlen = sizeof( str ) - 1;
|
|
|
|
|
|
|
|
if( ssl->f_dbg == NULL || X == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for( n = X->n - 1; n >= 0; n-- )
|
|
|
|
if( X->p[n] != 0 )
|
|
|
|
break;
|
|
|
|
|
2010-03-16 21:16:16 +00:00
|
|
|
snprintf( str, maxlen, "%s(%04d): value of '%s' (%lu bits) is:\n",
|
|
|
|
file, line, text,
|
|
|
|
(unsigned long) ((n + 1) * sizeof( t_int )) << 3 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
|
|
|
|
for( i = n, j = 0; i >= 0; i--, j++ )
|
|
|
|
{
|
|
|
|
if( j % ( 16 / sizeof( t_int ) ) == 0 )
|
|
|
|
{
|
|
|
|
if( j > 0 )
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, "\n" );
|
|
|
|
|
|
|
|
snprintf( str, maxlen, "%s(%04d): ", file, line );
|
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( k = sizeof( t_int ) - 1; k >= 0; k-- )
|
|
|
|
{
|
|
|
|
snprintf( str, maxlen, " %02x", (unsigned int)
|
|
|
|
( X->p[i] >> (k << 3) ) & 0xFF );
|
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, "\n" );
|
|
|
|
}
|
|
|
|
|
2010-03-16 21:09:09 +00:00
|
|
|
void debug_print_crt( const ssl_context *ssl, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *text, const x509_cert *crt )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2009-05-02 15:13:40 +00:00
|
|
|
char str[1024], prefix[64];
|
2009-01-03 21:22:43 +00:00
|
|
|
int i = 0, maxlen = sizeof( prefix ) - 1;
|
|
|
|
|
|
|
|
if( ssl->f_dbg == NULL || crt == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf( prefix, maxlen, "%s(%04d): ", file, line );
|
|
|
|
prefix[maxlen] = '\0';
|
|
|
|
maxlen = sizeof( str ) - 1;
|
|
|
|
|
2010-03-21 21:03:34 +00:00
|
|
|
while( crt != NULL )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2009-05-02 15:13:40 +00:00
|
|
|
char buf[1024];
|
|
|
|
x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
|
2009-05-02 15:13:40 +00:00
|
|
|
file, line, text, ++i, buf );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
str[maxlen] = '\0';
|
|
|
|
ssl->f_dbg( ssl->p_dbg, level, str );
|
|
|
|
|
|
|
|
debug_print_mpi( ssl, level, file, line,
|
|
|
|
"crt->rsa.N", &crt->rsa.N );
|
|
|
|
|
|
|
|
debug_print_mpi( ssl, level, file, line,
|
|
|
|
"crt->rsa.E", &crt->rsa.E );
|
|
|
|
|
|
|
|
crt = crt->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|