- Main Page
- Data Structures
- Files

# debug.c

Go to the documentation of this file.

    00001 /*
    00002  *  Debugging routines
    00003  *
    00004  *  Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
    00005  *  All rights reserved.
    00006  *
    00007  *  Joined copyright on original XySSL code with: Christophe Devine
    00008  *
    00009  *  This program is free software; you can redistribute it and/or modify
    00010  *  it under the terms of the GNU General Public License as published by
    00011  *  the Free Software Foundation; either version 2 of the License, or
    00012  *  (at your option) any later version.
    00013  *
    00014  *  This program is distributed in the hope that it will be useful,
    00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017  *  GNU General Public License for more details.
    00018  *
    00019  *  You should have received a copy of the GNU General Public License along
    00020  *  with this program; if not, write to the Free Software Foundation, Inc.,
    00021  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    00022  */
    00023
    00024 #include "polarssl/config.h"
    00025
    00026 #if defined(POLARSSL_DEBUG_C)
    00027
    00028 #include "polarssl/debug.h"
    00029
    00030 #include <stdarg.h>
    00031 #include <stdlib.h>
    00032
    00033 #if defined _MSC_VER && !defined  snprintf
    00034 #define  snprintf  _snprintf
    00035 #endif
    00036
    00037 #if defined _MSC_VER && !defined vsnprintf
    00038 #define vsnprintf _vsnprintf
    00039 #endif
    00040
    00041 char *debug_fmt( const char *format, ... )
    00042 {
    00043     va_list argp;
    00044     static char str[512];
    00045     int maxlen = sizeof( str ) - 1;
    00046
    00047     va_start( argp, format );
    00048     vsnprintf( str, maxlen, format, argp );
    00049     va_end( argp );
    00050
    00051     str[maxlen] = '\0';
    00052     return( str );
    00053 }
    00054
    00055 void debug_print_msg( ssl_context *ssl, int level,
    00056                       char *file, int line, char *text )
    00057 {
    00058     char str[512];
    00059     int maxlen = sizeof( str ) - 1;
    00060
    00061     if( ssl->f_dbg == NULL )
    00062         return;
    00063
    00064     snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
    00065     str[maxlen] = '\0';
    00066     ssl->f_dbg( ssl->p_dbg, level, str );
    00067 }
    00068
    00069 void debug_print_ret( ssl_context *ssl, int level,
    00070                       char *file, int line, char *text, int ret )
    00071 {
    00072     char str[512];
    00073     int maxlen = sizeof( str ) - 1;
    00074
    00075     if( ssl->f_dbg == NULL )
    00076         return;
    00077
    00078     snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
    00079               file, line, text, ret, ret );
    00080
    00081     str[maxlen] = '\0';
    00082     ssl->f_dbg( ssl->p_dbg, level, str );
    00083 }
    00084
    00085 void debug_print_buf( ssl_context *ssl, int level,
    00086                       char *file, int line, char *text,
    00087                       unsigned char *buf, int len )
    00088 {
    00089     char str[512];
    00090     int i, maxlen = sizeof( str ) - 1;
    00091
    00092     if( ssl->f_dbg == NULL || len < 0 )
    00093         return;
    00094
    00095     snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
    00096               file, line, text, len );
    00097
    00098     str[maxlen] = '\0';
    00099     ssl->f_dbg( ssl->p_dbg, level, str );
    00100
    00101     for( i = 0; i < len; i++ )
    00102     {
    00103         if( i >= 4096 )
    00104             break;
    00105
    00106         if( i % 16 == 0 )
    00107         {
    00108             if( i > 0 )
    00109                 ssl->f_dbg( ssl->p_dbg, level, "\n" );
    00110
    00111             snprintf( str, maxlen, "%s(%04d): %04x: ", file, line, i );
    00112
    00113             str[maxlen] = '\0';
    00114             ssl->f_dbg( ssl->p_dbg, level, str );
    00115         }
    00116
    00117         snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
    00118
    00119         str[maxlen] = '\0';
    00120         ssl->f_dbg( ssl->p_dbg, level, str );
    00121     }
    00122
    00123     if( len > 0 )
    00124         ssl->f_dbg( ssl->p_dbg, level, "\n" );
    00125 }
    00126
    00127 void debug_print_mpi( ssl_context *ssl, int level,
    00128                       char *file, int line, char *text, mpi *X )
    00129 {
    00130     char str[512];
    00131     int i, j, k, n, maxlen = sizeof( str ) - 1;
    00132
    00133     if( ssl->f_dbg == NULL || X == NULL )
    00134         return;
    00135
    00136     for( n = X->n - 1; n >= 0; n-- )
    00137         if( X->p[n] != 0 )
    00138             break;
    00139
    00140     snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
    00141               file, line, text, ((n + 1) * sizeof( t_int )) << 3 );
    00142
    00143     str[maxlen] = '\0';
    00144     ssl->f_dbg( ssl->p_dbg, level, str );
    00145
    00146     for( i = n, j = 0; i >= 0; i--, j++ )
    00147     {
    00148         if( j % ( 16 / sizeof( t_int ) ) == 0 )
    00149         {
    00150             if( j > 0 )
    00151                 ssl->f_dbg( ssl->p_dbg, level, "\n" );
    00152
    00153             snprintf( str, maxlen, "%s(%04d): ", file, line );
    00154
    00155             str[maxlen] = '\0';
    00156             ssl->f_dbg( ssl->p_dbg, level, str );
    00157         }
    00158
    00159         for( k = sizeof( t_int ) - 1; k >= 0; k-- )
    00160         {
    00161             snprintf( str, maxlen, " %02x", (unsigned int)
    00162                       ( X->p[i] >> (k << 3) ) & 0xFF );
    00163
    00164             str[maxlen] = '\0';
    00165             ssl->f_dbg( ssl->p_dbg, level, str );
    00166         }
    00167     }
    00168
    00169     ssl->f_dbg( ssl->p_dbg, level, "\n" );
    00170 }
    00171
    00172 void debug_print_crt( ssl_context *ssl, int level,
    00173                       char *file, int line, char *text, x509_cert *crt )
    00174 {
    00175     char str[1024], prefix[64];
    00176     int i = 0, maxlen = sizeof( prefix ) - 1;
    00177
    00178     if( ssl->f_dbg == NULL || crt == NULL )
    00179         return;
    00180
    00181     snprintf( prefix, maxlen, "%s(%04d): ", file, line );
    00182     prefix[maxlen] = '\0';
    00183     maxlen = sizeof( str ) - 1;
    00184
    00185     while( crt != NULL && crt->next != NULL )
    00186     {
    00187         char buf[1024];
    00188         x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
    00189
    00190         snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
    00191                   file, line, text, ++i, buf );
    00192
    00193         str[maxlen] = '\0';
    00194         ssl->f_dbg( ssl->p_dbg, level, str );
    00195
    00196         debug_print_mpi( ssl, level, file, line,
    00197                          "crt->rsa.N", &crt->rsa.N );
    00198
    00199         debug_print_mpi( ssl, level, file, line,
    00200                          "crt->rsa.E", &crt->rsa.E );
    00201
    00202         crt = crt->next;
    00203     }
    00204 }
    00205
    00206 #endif