00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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(EFIX64) || defined(EFI32)
00036 #include <stdio.h>
00037 #endif
00038
00039 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
00040 #if !defined snprintf
00041 #define snprintf _snprintf
00042 #endif
00043
00044 #if !defined vsnprintf
00045 #define vsnprintf _vsnprintf
00046 #endif
00047 #endif
00048
00049 char *debug_fmt( const char *format, ... )
00050 {
00051 va_list argp;
00052 static char str[512];
00053 int maxlen = sizeof( str ) - 1;
00054
00055 va_start( argp, format );
00056 vsnprintf( str, maxlen, format, argp );
00057 va_end( argp );
00058
00059 str[maxlen] = '\0';
00060 return( str );
00061 }
00062
00063 void debug_print_msg( const ssl_context *ssl, int level,
00064 const char *file, int line, const char *text )
00065 {
00066 char str[512];
00067 int maxlen = sizeof( str ) - 1;
00068
00069 if( ssl->f_dbg == NULL )
00070 return;
00071
00072 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
00073 str[maxlen] = '\0';
00074 ssl->f_dbg( ssl->p_dbg, level, str );
00075 }
00076
00077 void debug_print_ret( const ssl_context *ssl, int level,
00078 const char *file, int line,
00079 const char *text, int ret )
00080 {
00081 char str[512];
00082 int maxlen = sizeof( str ) - 1;
00083
00084 if( ssl->f_dbg == NULL )
00085 return;
00086
00087 snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
00088 file, line, text, ret, ret );
00089
00090 str[maxlen] = '\0';
00091 ssl->f_dbg( ssl->p_dbg, level, str );
00092 }
00093
00094 void debug_print_buf( const ssl_context *ssl, int level,
00095 const char *file, int line, const char *text,
00096 unsigned char *buf, size_t len )
00097 {
00098 char str[512];
00099 size_t i, maxlen = sizeof( str ) - 1;
00100
00101 if( ssl->f_dbg == NULL )
00102 return;
00103
00104 snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
00105 file, line, text, (unsigned int) len );
00106
00107 str[maxlen] = '\0';
00108 ssl->f_dbg( ssl->p_dbg, level, str );
00109
00110 for( i = 0; i < len; i++ )
00111 {
00112 if( i >= 4096 )
00113 break;
00114
00115 if( i % 16 == 0 )
00116 {
00117 if( i > 0 )
00118 ssl->f_dbg( ssl->p_dbg, level, "\n" );
00119
00120 snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
00121 (unsigned int) i );
00122
00123 str[maxlen] = '\0';
00124 ssl->f_dbg( ssl->p_dbg, level, str );
00125 }
00126
00127 snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
00128
00129 str[maxlen] = '\0';
00130 ssl->f_dbg( ssl->p_dbg, level, str );
00131 }
00132
00133 if( len > 0 )
00134 ssl->f_dbg( ssl->p_dbg, level, "\n" );
00135 }
00136
00137 #if defined(POLARSSL_ECP_C)
00138 void debug_print_ecp( const ssl_context *ssl, int level,
00139 const char *file, int line,
00140 const char *text, const ecp_point *X )
00141 {
00142 char str[512];
00143 int maxlen = sizeof( str ) - 1;
00144
00145 snprintf( str, maxlen, "%s(X)", text );
00146 str[maxlen] = '\0';
00147 debug_print_mpi( ssl, level, file, line, str, &X->X );
00148
00149 snprintf( str, maxlen, "%s(Y)", text );
00150 str[maxlen] = '\0';
00151 debug_print_mpi( ssl, level, file, line, str, &X->Y );
00152
00153 snprintf( str, maxlen, "%s(Z)", text );
00154 str[maxlen] = '\0';
00155 debug_print_mpi( ssl, level, file, line, str, &X->Z );
00156 }
00157 #endif
00158
00159 #if defined(POLARSSL_BIGNUM_C)
00160 void debug_print_mpi( const ssl_context *ssl, int level,
00161 const char *file, int line,
00162 const char *text, const mpi *X )
00163 {
00164 char str[512];
00165 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
00166 size_t i, n;
00167
00168 if( ssl->f_dbg == NULL || X == NULL )
00169 return;
00170
00171 for( n = X->n - 1; n > 0; n-- )
00172 if( X->p[n] != 0 )
00173 break;
00174
00175 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
00176 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
00177 break;
00178
00179 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
00180 file, line, text,
00181 (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
00182
00183 str[maxlen] = '\0';
00184 ssl->f_dbg( ssl->p_dbg, level, str );
00185
00186 for( i = n + 1, j = 0; i > 0; i-- )
00187 {
00188 if( zeros && X->p[i - 1] == 0 )
00189 continue;
00190
00191 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
00192 {
00193 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
00194 continue;
00195 else
00196 zeros = 0;
00197
00198 if( j % 16 == 0 )
00199 {
00200 if( j > 0 )
00201 ssl->f_dbg( ssl->p_dbg, level, "\n" );
00202
00203 snprintf( str, maxlen, "%s(%04d): ", file, line );
00204
00205 str[maxlen] = '\0';
00206 ssl->f_dbg( ssl->p_dbg, level, str );
00207 }
00208
00209 snprintf( str, maxlen, " %02x", (unsigned int)
00210 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
00211
00212 str[maxlen] = '\0';
00213 ssl->f_dbg( ssl->p_dbg, level, str );
00214
00215 j++;
00216 }
00217
00218 }
00219
00220 if( zeros == 1 )
00221 {
00222 snprintf( str, maxlen, "%s(%04d): ", file, line );
00223
00224 str[maxlen] = '\0';
00225 ssl->f_dbg( ssl->p_dbg, level, str );
00226 ssl->f_dbg( ssl->p_dbg, level, " 00" );
00227 }
00228
00229 ssl->f_dbg( ssl->p_dbg, level, "\n" );
00230 }
00231 #endif
00232
00233 #if defined(POLARSSL_X509_CRT_PARSE_C)
00234 static void debug_print_pk( const ssl_context *ssl, int level,
00235 const char *file, int line,
00236 const char *text, const pk_context *pk )
00237 {
00238 size_t i;
00239 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
00240 char name[16];
00241
00242 memset( items, 0, sizeof( items ) );
00243
00244 if( pk_debug( pk, items ) != 0 )
00245 {
00246 debug_print_msg( ssl, level, file, line, "invalid PK context" );
00247 return;
00248 }
00249
00250 for( i = 0; i < sizeof( items ); i++ )
00251 {
00252 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
00253 return;
00254
00255 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
00256 name[sizeof( name ) - 1] = '\0';
00257
00258 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
00259 debug_print_mpi( ssl, level, file, line, name, items[i].value );
00260 else
00261 #if defined(POLARSSL_ECP_C)
00262 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
00263 debug_print_ecp( ssl, level, file, line, name, items[i].value );
00264 else
00265 #endif
00266 debug_print_msg( ssl, level, file, line, "should not happen" );
00267 }
00268 }
00269
00270 void debug_print_crt( const ssl_context *ssl, int level,
00271 const char *file, int line,
00272 const char *text, const x509_crt *crt )
00273 {
00274 char str[1024], prefix[64];
00275 int i = 0, maxlen = sizeof( prefix ) - 1;
00276
00277 if( ssl->f_dbg == NULL || crt == NULL )
00278 return;
00279
00280 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
00281 prefix[maxlen] = '\0';
00282 maxlen = sizeof( str ) - 1;
00283
00284 while( crt != NULL )
00285 {
00286 char buf[1024];
00287 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
00288
00289 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
00290 file, line, text, ++i, buf );
00291
00292 str[maxlen] = '\0';
00293 ssl->f_dbg( ssl->p_dbg, level, str );
00294
00295 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
00296
00297 crt = crt->next;
00298 }
00299 }
00300 #endif
00301
00302 #endif