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