mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * Debugging routines
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22
ansond 0:137634ff4186 23 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 24 #include "polarssl/config.h"
ansond 0:137634ff4186 25 #else
ansond 0:137634ff4186 26 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 27 #endif
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 #if defined(POLARSSL_DEBUG_C)
ansond 0:137634ff4186 30
ansond 0:137634ff4186 31 #include "polarssl/debug.h"
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #include <stdarg.h>
ansond 0:137634ff4186 34 #include <stdio.h>
ansond 0:137634ff4186 35 #include <string.h>
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
ansond 0:137634ff4186 38 #if !defined snprintf
ansond 0:137634ff4186 39 #define snprintf _snprintf
ansond 0:137634ff4186 40 #endif
ansond 0:137634ff4186 41
ansond 0:137634ff4186 42 #if !defined vsnprintf
ansond 0:137634ff4186 43 #define vsnprintf _vsnprintf
ansond 0:137634ff4186 44 #endif
ansond 0:137634ff4186 45 #endif /* _MSC_VER */
ansond 0:137634ff4186 46
ansond 0:137634ff4186 47 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 48 #include "polarssl/platform.h"
ansond 0:137634ff4186 49 #else
ansond 0:137634ff4186 50 #define polarssl_snprintf snprintf
ansond 0:137634ff4186 51 #endif
ansond 0:137634ff4186 52
ansond 0:137634ff4186 53 static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
ansond 0:137634ff4186 54 static int debug_threshold = 0;
ansond 0:137634ff4186 55
ansond 0:137634ff4186 56 void debug_set_log_mode( int log_mode )
ansond 0:137634ff4186 57 {
ansond 0:137634ff4186 58 debug_log_mode = log_mode;
ansond 0:137634ff4186 59 }
ansond 0:137634ff4186 60
ansond 0:137634ff4186 61 void debug_set_threshold( int threshold )
ansond 0:137634ff4186 62 {
ansond 0:137634ff4186 63 debug_threshold = threshold;
ansond 0:137634ff4186 64 }
ansond 0:137634ff4186 65
ansond 0:137634ff4186 66 char *debug_fmt( const char *format, ... )
ansond 0:137634ff4186 67 {
ansond 0:137634ff4186 68 va_list argp;
ansond 0:137634ff4186 69 static char str[512];
ansond 0:137634ff4186 70 int maxlen = sizeof( str ) - 1;
ansond 0:137634ff4186 71
ansond 0:137634ff4186 72 va_start( argp, format );
ansond 0:137634ff4186 73 vsnprintf( str, maxlen, format, argp );
ansond 0:137634ff4186 74 va_end( argp );
ansond 0:137634ff4186 75
ansond 0:137634ff4186 76 str[maxlen] = '\0';
ansond 0:137634ff4186 77 return( str );
ansond 0:137634ff4186 78 }
ansond 0:137634ff4186 79
ansond 0:137634ff4186 80 void debug_print_msg( const ssl_context *ssl, int level,
ansond 0:137634ff4186 81 const char *file, int line, const char *text )
ansond 0:137634ff4186 82 {
ansond 0:137634ff4186 83 char str[512];
ansond 0:137634ff4186 84 int maxlen = sizeof( str ) - 1;
ansond 0:137634ff4186 85
ansond 0:137634ff4186 86 if( ssl->f_dbg == NULL || level > debug_threshold )
ansond 0:137634ff4186 87 return;
ansond 0:137634ff4186 88
ansond 0:137634ff4186 89 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
ansond 0:137634ff4186 90 {
ansond 0:137634ff4186 91 ssl->f_dbg( ssl->p_dbg, level, text );
ansond 0:137634ff4186 92 return;
ansond 0:137634ff4186 93 }
ansond 0:137634ff4186 94
ansond 0:137634ff4186 95 polarssl_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
ansond 0:137634ff4186 96 str[maxlen] = '\0';
ansond 0:137634ff4186 97 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 98 }
ansond 0:137634ff4186 99
ansond 0:137634ff4186 100 void debug_print_ret( const ssl_context *ssl, int level,
ansond 0:137634ff4186 101 const char *file, int line,
ansond 0:137634ff4186 102 const char *text, int ret )
ansond 0:137634ff4186 103 {
ansond 0:137634ff4186 104 char str[512];
ansond 0:137634ff4186 105 int maxlen = sizeof( str ) - 1;
ansond 0:137634ff4186 106 size_t idx = 0;
ansond 0:137634ff4186 107
ansond 0:137634ff4186 108 if( ssl->f_dbg == NULL || level > debug_threshold )
ansond 0:137634ff4186 109 return;
ansond 0:137634ff4186 110
ansond 0:137634ff4186 111 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 112 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 113
ansond 0:137634ff4186 114 polarssl_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
ansond 0:137634ff4186 115 text, ret, -ret );
ansond 0:137634ff4186 116
ansond 0:137634ff4186 117 str[maxlen] = '\0';
ansond 0:137634ff4186 118 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 119 }
ansond 0:137634ff4186 120
ansond 0:137634ff4186 121 void debug_print_buf( const ssl_context *ssl, int level,
ansond 0:137634ff4186 122 const char *file, int line, const char *text,
ansond 0:137634ff4186 123 unsigned char *buf, size_t len )
ansond 0:137634ff4186 124 {
ansond 0:137634ff4186 125 char str[512];
ansond 0:137634ff4186 126 char txt[17];
ansond 0:137634ff4186 127 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
ansond 0:137634ff4186 128
ansond 0:137634ff4186 129 if( ssl->f_dbg == NULL || level > debug_threshold )
ansond 0:137634ff4186 130 return;
ansond 0:137634ff4186 131
ansond 0:137634ff4186 132 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 133 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 134
ansond 0:137634ff4186 135 polarssl_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
ansond 0:137634ff4186 136 text, (unsigned int) len );
ansond 0:137634ff4186 137
ansond 0:137634ff4186 138 str[maxlen] = '\0';
ansond 0:137634ff4186 139 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 140
ansond 0:137634ff4186 141 idx = 0;
ansond 0:137634ff4186 142 memset( txt, 0, sizeof( txt ) );
ansond 0:137634ff4186 143 for( i = 0; i < len; i++ )
ansond 0:137634ff4186 144 {
ansond 0:137634ff4186 145 if( i >= 4096 )
ansond 0:137634ff4186 146 break;
ansond 0:137634ff4186 147
ansond 0:137634ff4186 148 if( i % 16 == 0 )
ansond 0:137634ff4186 149 {
ansond 0:137634ff4186 150 if( i > 0 )
ansond 0:137634ff4186 151 {
ansond 0:137634ff4186 152 polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt );
ansond 0:137634ff4186 153 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 154
ansond 0:137634ff4186 155 idx = 0;
ansond 0:137634ff4186 156 memset( txt, 0, sizeof( txt ) );
ansond 0:137634ff4186 157 }
ansond 0:137634ff4186 158
ansond 0:137634ff4186 159 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 160 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 161
ansond 0:137634ff4186 162 idx += polarssl_snprintf( str + idx, maxlen - idx, "%04x: ",
ansond 0:137634ff4186 163 (unsigned int) i );
ansond 0:137634ff4186 164
ansond 0:137634ff4186 165 }
ansond 0:137634ff4186 166
ansond 0:137634ff4186 167 idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x",
ansond 0:137634ff4186 168 (unsigned int) buf[i] );
ansond 0:137634ff4186 169 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
ansond 0:137634ff4186 170 }
ansond 0:137634ff4186 171
ansond 0:137634ff4186 172 if( len > 0 )
ansond 0:137634ff4186 173 {
ansond 0:137634ff4186 174 for( /* i = i */; i % 16 != 0; i++ )
ansond 0:137634ff4186 175 idx += polarssl_snprintf( str + idx, maxlen - idx, " " );
ansond 0:137634ff4186 176
ansond 0:137634ff4186 177 polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt );
ansond 0:137634ff4186 178 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 179 }
ansond 0:137634ff4186 180 }
ansond 0:137634ff4186 181
ansond 0:137634ff4186 182 #if defined(POLARSSL_ECP_C)
ansond 0:137634ff4186 183 void debug_print_ecp( const ssl_context *ssl, int level,
ansond 0:137634ff4186 184 const char *file, int line,
ansond 0:137634ff4186 185 const char *text, const ecp_point *X )
ansond 0:137634ff4186 186 {
ansond 0:137634ff4186 187 char str[512];
ansond 0:137634ff4186 188 int maxlen = sizeof( str ) - 1;
ansond 0:137634ff4186 189
ansond 0:137634ff4186 190 if( ssl->f_dbg == NULL || level > debug_threshold )
ansond 0:137634ff4186 191 return;
ansond 0:137634ff4186 192
ansond 0:137634ff4186 193 polarssl_snprintf( str, maxlen, "%s(X)", text );
ansond 0:137634ff4186 194 str[maxlen] = '\0';
ansond 0:137634ff4186 195 debug_print_mpi( ssl, level, file, line, str, &X->X );
ansond 0:137634ff4186 196
ansond 0:137634ff4186 197 polarssl_snprintf( str, maxlen, "%s(Y)", text );
ansond 0:137634ff4186 198 str[maxlen] = '\0';
ansond 0:137634ff4186 199 debug_print_mpi( ssl, level, file, line, str, &X->Y );
ansond 0:137634ff4186 200 }
ansond 0:137634ff4186 201 #endif /* POLARSSL_ECP_C */
ansond 0:137634ff4186 202
ansond 0:137634ff4186 203 #if defined(POLARSSL_BIGNUM_C)
ansond 0:137634ff4186 204 void debug_print_mpi( const ssl_context *ssl, int level,
ansond 0:137634ff4186 205 const char *file, int line,
ansond 0:137634ff4186 206 const char *text, const mpi *X )
ansond 0:137634ff4186 207 {
ansond 0:137634ff4186 208 char str[512];
ansond 0:137634ff4186 209 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
ansond 0:137634ff4186 210 size_t i, n, idx = 0;
ansond 0:137634ff4186 211
ansond 0:137634ff4186 212 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
ansond 0:137634ff4186 213 return;
ansond 0:137634ff4186 214
ansond 0:137634ff4186 215 for( n = X->n - 1; n > 0; n-- )
ansond 0:137634ff4186 216 if( X->p[n] != 0 )
ansond 0:137634ff4186 217 break;
ansond 0:137634ff4186 218
ansond 0:137634ff4186 219 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
ansond 0:137634ff4186 220 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
ansond 0:137634ff4186 221 break;
ansond 0:137634ff4186 222
ansond 0:137634ff4186 223 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 224 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 225
ansond 0:137634ff4186 226 polarssl_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
ansond 0:137634ff4186 227 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
ansond 0:137634ff4186 228
ansond 0:137634ff4186 229 str[maxlen] = '\0';
ansond 0:137634ff4186 230 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 231
ansond 0:137634ff4186 232 idx = 0;
ansond 0:137634ff4186 233 for( i = n + 1, j = 0; i > 0; i-- )
ansond 0:137634ff4186 234 {
ansond 0:137634ff4186 235 if( zeros && X->p[i - 1] == 0 )
ansond 0:137634ff4186 236 continue;
ansond 0:137634ff4186 237
ansond 0:137634ff4186 238 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
ansond 0:137634ff4186 239 {
ansond 0:137634ff4186 240 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
ansond 0:137634ff4186 241 continue;
ansond 0:137634ff4186 242 else
ansond 0:137634ff4186 243 zeros = 0;
ansond 0:137634ff4186 244
ansond 0:137634ff4186 245 if( j % 16 == 0 )
ansond 0:137634ff4186 246 {
ansond 0:137634ff4186 247 if( j > 0 )
ansond 0:137634ff4186 248 {
ansond 0:137634ff4186 249 polarssl_snprintf( str + idx, maxlen - idx, "\n" );
ansond 0:137634ff4186 250 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 251 idx = 0;
ansond 0:137634ff4186 252 }
ansond 0:137634ff4186 253
ansond 0:137634ff4186 254 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 255 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 256 }
ansond 0:137634ff4186 257
ansond 0:137634ff4186 258 idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
ansond 0:137634ff4186 259 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
ansond 0:137634ff4186 260
ansond 0:137634ff4186 261 j++;
ansond 0:137634ff4186 262 }
ansond 0:137634ff4186 263
ansond 0:137634ff4186 264 }
ansond 0:137634ff4186 265
ansond 0:137634ff4186 266 if( zeros == 1 )
ansond 0:137634ff4186 267 {
ansond 0:137634ff4186 268 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 269 {
ansond 0:137634ff4186 270 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 271
ansond 0:137634ff4186 272 }
ansond 0:137634ff4186 273 idx += polarssl_snprintf( str + idx, maxlen - idx, " 00" );
ansond 0:137634ff4186 274 }
ansond 0:137634ff4186 275
ansond 0:137634ff4186 276 polarssl_snprintf( str + idx, maxlen - idx, "\n" );
ansond 0:137634ff4186 277 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 278 }
ansond 0:137634ff4186 279 #endif /* POLARSSL_BIGNUM_C */
ansond 0:137634ff4186 280
ansond 0:137634ff4186 281 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 282 static void debug_print_pk( const ssl_context *ssl, int level,
ansond 0:137634ff4186 283 const char *file, int line,
ansond 0:137634ff4186 284 const char *text, const pk_context *pk )
ansond 0:137634ff4186 285 {
ansond 0:137634ff4186 286 size_t i;
ansond 0:137634ff4186 287 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
ansond 0:137634ff4186 288 char name[16];
ansond 0:137634ff4186 289
ansond 0:137634ff4186 290 memset( items, 0, sizeof( items ) );
ansond 0:137634ff4186 291
ansond 0:137634ff4186 292 if( pk_debug( pk, items ) != 0 )
ansond 0:137634ff4186 293 {
ansond 0:137634ff4186 294 debug_print_msg( ssl, level, file, line, "invalid PK context" );
ansond 0:137634ff4186 295 return;
ansond 0:137634ff4186 296 }
ansond 0:137634ff4186 297
ansond 0:137634ff4186 298 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
ansond 0:137634ff4186 299 {
ansond 0:137634ff4186 300 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
ansond 0:137634ff4186 301 return;
ansond 0:137634ff4186 302
ansond 0:137634ff4186 303 polarssl_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
ansond 0:137634ff4186 304 name[sizeof( name ) - 1] = '\0';
ansond 0:137634ff4186 305
ansond 0:137634ff4186 306 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
ansond 0:137634ff4186 307 debug_print_mpi( ssl, level, file, line, name, items[i].value );
ansond 0:137634ff4186 308 else
ansond 0:137634ff4186 309 #if defined(POLARSSL_ECP_C)
ansond 0:137634ff4186 310 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
ansond 0:137634ff4186 311 debug_print_ecp( ssl, level, file, line, name, items[i].value );
ansond 0:137634ff4186 312 else
ansond 0:137634ff4186 313 #endif
ansond 0:137634ff4186 314 debug_print_msg( ssl, level, file, line, "should not happen" );
ansond 0:137634ff4186 315 }
ansond 0:137634ff4186 316 }
ansond 0:137634ff4186 317
ansond 0:137634ff4186 318 void debug_print_crt( const ssl_context *ssl, int level,
ansond 0:137634ff4186 319 const char *file, int line,
ansond 0:137634ff4186 320 const char *text, const x509_crt *crt )
ansond 0:137634ff4186 321 {
ansond 0:137634ff4186 322 char str[1024], prefix[64];
ansond 0:137634ff4186 323 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
ansond 0:137634ff4186 324
ansond 0:137634ff4186 325 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
ansond 0:137634ff4186 326 return;
ansond 0:137634ff4186 327
ansond 0:137634ff4186 328 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 329 {
ansond 0:137634ff4186 330 polarssl_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 331 prefix[maxlen] = '\0';
ansond 0:137634ff4186 332 }
ansond 0:137634ff4186 333 else
ansond 0:137634ff4186 334 prefix[0] = '\0';
ansond 0:137634ff4186 335
ansond 0:137634ff4186 336 maxlen = sizeof( str ) - 1;
ansond 0:137634ff4186 337
ansond 0:137634ff4186 338 while( crt != NULL )
ansond 0:137634ff4186 339 {
ansond 0:137634ff4186 340 char buf[1024];
ansond 0:137634ff4186 341 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
ansond 0:137634ff4186 342
ansond 0:137634ff4186 343 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
ansond 0:137634ff4186 344 idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
ansond 0:137634ff4186 345
ansond 0:137634ff4186 346 polarssl_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
ansond 0:137634ff4186 347 text, ++i, buf );
ansond 0:137634ff4186 348
ansond 0:137634ff4186 349 str[maxlen] = '\0';
ansond 0:137634ff4186 350 ssl->f_dbg( ssl->p_dbg, level, str );
ansond 0:137634ff4186 351
ansond 0:137634ff4186 352 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
ansond 0:137634ff4186 353
ansond 0:137634ff4186 354 crt = crt->next;
ansond 0:137634ff4186 355 }
ansond 0:137634ff4186 356 }
ansond 0:137634ff4186 357 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 358
ansond 0:137634ff4186 359 #endif /* POLARSSL_DEBUG_C */
ansond 0:137634ff4186 360