Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
debug.c
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 #if !defined(POLARSSL_CONFIG_FILE) 00027 #include "polarssl/config.h" 00028 #else 00029 #include POLARSSL_CONFIG_FILE 00030 #endif 00031 00032 #if defined(POLARSSL_DEBUG_C) 00033 00034 #include "polarssl/debug.h" 00035 00036 #include <stdarg.h> 00037 #include <stdlib.h> 00038 00039 #if defined(EFIX64) || defined(EFI32) 00040 #include <stdio.h> 00041 #endif 00042 00043 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) 00044 #if !defined snprintf 00045 #define snprintf _snprintf 00046 #endif 00047 00048 #if !defined vsnprintf 00049 #define vsnprintf _vsnprintf 00050 #endif 00051 #endif /* _MSC_VER */ 00052 00053 static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE; 00054 static int debug_threshold = 0; 00055 00056 void debug_set_log_mode( int log_mode ) 00057 { 00058 debug_log_mode = log_mode; 00059 } 00060 00061 void debug_set_threshold( int threshold ) 00062 { 00063 debug_threshold = threshold; 00064 } 00065 00066 char *debug_fmt( const char *format, ... ) 00067 { 00068 va_list argp; 00069 static char str[512]; 00070 int maxlen = sizeof( str ) - 1; 00071 00072 va_start( argp, format ); 00073 vsnprintf( str, maxlen, format, argp ); 00074 va_end( argp ); 00075 00076 str[maxlen] = '\0'; 00077 return( str ); 00078 } 00079 00080 void debug_print_msg( const ssl_context *ssl, int level, 00081 const char *file, int line, const char *text ) 00082 { 00083 char str[512]; 00084 int maxlen = sizeof( str ) - 1; 00085 00086 if( ssl->f_dbg == NULL || level > debug_threshold ) 00087 return; 00088 00089 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW ) 00090 { 00091 ssl->f_dbg( ssl->p_dbg, level, str ); 00092 return; 00093 } 00094 00095 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); 00096 str[maxlen] = '\0'; 00097 ssl->f_dbg( ssl->p_dbg, level, str ); 00098 } 00099 00100 void debug_print_ret( const ssl_context *ssl, int level, 00101 const char *file, int line, 00102 const char *text, int ret ) 00103 { 00104 char str[512]; 00105 int maxlen = sizeof( str ) - 1; 00106 size_t idx = 0; 00107 00108 if( ssl->f_dbg == NULL || level > debug_threshold ) 00109 return; 00110 00111 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00112 idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); 00113 00114 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n", 00115 text, ret, -ret ); 00116 00117 str[maxlen] = '\0'; 00118 ssl->f_dbg( ssl->p_dbg, level, str ); 00119 } 00120 00121 void debug_print_buf( const ssl_context *ssl, int level, 00122 const char *file, int line, const char *text, 00123 unsigned char *buf, size_t len ) 00124 { 00125 char str[512]; 00126 size_t i, maxlen = sizeof( str ) - 1, idx = 0; 00127 00128 if( ssl->f_dbg == NULL || level > debug_threshold ) 00129 return; 00130 00131 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00132 idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); 00133 00134 snprintf( str + idx, maxlen - idx, "dumping '%s' (%d bytes)\n", 00135 text, (unsigned int) len ); 00136 00137 str[maxlen] = '\0'; 00138 ssl->f_dbg( ssl->p_dbg, level, str ); 00139 00140 idx = 0; 00141 for( i = 0; i < len; i++ ) 00142 { 00143 if( i >= 4096 ) 00144 break; 00145 00146 if( i % 16 == 0 ) 00147 { 00148 if( i > 0 ) 00149 { 00150 snprintf( str + idx, maxlen - idx, "\n" ); 00151 ssl->f_dbg( ssl->p_dbg, level, str ); 00152 idx = 0; 00153 } 00154 00155 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00156 idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); 00157 00158 idx += snprintf( str + idx, maxlen - idx, "%04x: ", 00159 (unsigned int) i ); 00160 00161 } 00162 00163 idx += snprintf( str + idx, maxlen - idx, " %02x", 00164 (unsigned int) buf[i] ); 00165 } 00166 00167 if( len > 0 ) 00168 { 00169 snprintf( str + idx, maxlen - idx, "\n" ); 00170 ssl->f_dbg( ssl->p_dbg, level, str ); 00171 } 00172 } 00173 00174 #if defined(POLARSSL_ECP_C) 00175 void debug_print_ecp( const ssl_context *ssl, int level, 00176 const char *file, int line, 00177 const char *text, const ecp_point *X ) 00178 { 00179 char str[512]; 00180 int maxlen = sizeof( str ) - 1; 00181 00182 if( ssl->f_dbg == NULL || level > debug_threshold ) 00183 return; 00184 00185 snprintf( str, maxlen, "%s(X)", text ); 00186 str[maxlen] = '\0'; 00187 debug_print_mpi( ssl, level, file, line, str, &X->X ); 00188 00189 snprintf( str, maxlen, "%s(Y)", text ); 00190 str[maxlen] = '\0'; 00191 debug_print_mpi( ssl, level, file, line, str, &X->Y ); 00192 } 00193 #endif /* POLARSSL_ECP_C */ 00194 00195 #if defined(POLARSSL_BIGNUM_C) 00196 void debug_print_mpi( const ssl_context *ssl, int level, 00197 const char *file, int line, 00198 const char *text, const mpi *X ) 00199 { 00200 char str[512]; 00201 int j, k, maxlen = sizeof( str ) - 1, zeros = 1; 00202 size_t i, n, idx = 0; 00203 00204 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold ) 00205 return; 00206 00207 for( n = X->n - 1; n > 0; n-- ) 00208 if( X->p [n] != 0 ) 00209 break; 00210 00211 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- ) 00212 if( ( ( X->p [n] >> j ) & 1 ) != 0 ) 00213 break; 00214 00215 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00216 idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); 00217 00218 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n", 00219 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) ); 00220 00221 str[maxlen] = '\0'; 00222 ssl->f_dbg( ssl->p_dbg, level, str ); 00223 00224 idx = 0; 00225 for( i = n + 1, j = 0; i > 0; i-- ) 00226 { 00227 if( zeros && X->p [i - 1] == 0 ) 00228 continue; 00229 00230 for( k = sizeof( t_uint ) - 1; k >= 0; k-- ) 00231 { 00232 if( zeros && ( ( X->p [i - 1] >> (k << 3) ) & 0xFF ) == 0 ) 00233 continue; 00234 else 00235 zeros = 0; 00236 00237 if( j % 16 == 0 ) 00238 { 00239 if( j > 0 ) 00240 { 00241 snprintf( str + idx, maxlen - idx, "\n" ); 00242 ssl->f_dbg( ssl->p_dbg, level, str ); 00243 idx = 0; 00244 } 00245 00246 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00247 idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); 00248 } 00249 00250 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) 00251 ( X->p [i - 1] >> (k << 3) ) & 0xFF ); 00252 00253 j++; 00254 } 00255 00256 } 00257 00258 if( zeros == 1 ) 00259 { 00260 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00261 { 00262 idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); 00263 00264 } 00265 idx += snprintf( str + idx, maxlen - idx, " 00" ); 00266 } 00267 00268 snprintf( str + idx, maxlen - idx, "\n" ); 00269 ssl->f_dbg( ssl->p_dbg, level, str ); 00270 } 00271 #endif /* POLARSSL_BIGNUM_C */ 00272 00273 #if defined(POLARSSL_X509_CRT_PARSE_C) 00274 static void debug_print_pk( const ssl_context *ssl, int level, 00275 const char *file, int line, 00276 const char *text, const pk_context *pk ) 00277 { 00278 size_t i; 00279 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS]; 00280 char name[16]; 00281 00282 memset( items, 0, sizeof( items ) ); 00283 00284 if( pk_debug( pk, items ) != 0 ) 00285 { 00286 debug_print_msg( ssl, level, file, line, "invalid PK context" ); 00287 return; 00288 } 00289 00290 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ ) 00291 { 00292 if( items[i].type == POLARSSL_PK_DEBUG_NONE ) 00293 return; 00294 00295 snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); 00296 name[sizeof( name ) - 1] = '\0'; 00297 00298 if( items[i].type == POLARSSL_PK_DEBUG_MPI ) 00299 debug_print_mpi( ssl, level, file, line, name, items[i].value ); 00300 else 00301 #if defined(POLARSSL_ECP_C) 00302 if( items[i].type == POLARSSL_PK_DEBUG_ECP ) 00303 debug_print_ecp( ssl, level, file, line, name, items[i].value ); 00304 else 00305 #endif 00306 debug_print_msg( ssl, level, file, line, "should not happen" ); 00307 } 00308 } 00309 00310 void debug_print_crt( const ssl_context *ssl, int level, 00311 const char *file, int line, 00312 const char *text, const x509_crt *crt ) 00313 { 00314 char str[1024], prefix[64]; 00315 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0; 00316 00317 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold ) 00318 return; 00319 00320 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00321 { 00322 snprintf( prefix, maxlen, "%s(%04d): ", file, line ); 00323 prefix[maxlen] = '\0'; 00324 } 00325 else 00326 prefix[0] = '\0'; 00327 00328 maxlen = sizeof( str ) - 1; 00329 00330 while( crt != NULL ) 00331 { 00332 char buf[1024]; 00333 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt ); 00334 00335 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) 00336 idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); 00337 00338 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s", 00339 text, ++i, buf ); 00340 00341 str[maxlen] = '\0'; 00342 ssl->f_dbg( ssl->p_dbg, level, str ); 00343 00344 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk ); 00345 00346 crt = crt->next; 00347 } 00348 } 00349 #endif /* POLARSSL_X509_CRT_PARSE_C */ 00350 00351 #endif /* POLARSSL_DEBUG_C */ 00352 00353
Generated on Tue Jul 12 2022 19:40:15 by
1.7.2