00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_DEBUG_C
00004 #ifdef POLARSSL_BIGNUM_C
00005 #ifdef POLARSSL_SSL_TLS_C
00006 #ifdef POLARSSL_RSA_C
00007
00008 #include <polarssl/debug.h>
00009
00010 struct buffer_data
00011 {
00012 char buf[2000];
00013 char *ptr;
00014 };
00015
00016 void string_debug(void *data, int level, const char *str)
00017 {
00018 struct buffer_data *buffer = (struct buffer_data *) data;
00019 ((void) level);
00020
00021 memcpy(buffer->ptr, str, strlen(str));
00022 buffer->ptr += strlen(str);
00023 }
00024 #endif
00025 #endif
00026 #endif
00027 #endif
00028
00029
00030 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00031 #include "polarssl/memory.h"
00032 #endif
00033
00034 #if defined(WANT_NOT_RND_MPI)
00035 #if defined(POLARSSL_BIGNUM_C)
00036 #include "polarssl/bignum.h"
00037 #else
00038 #error "not_rnd_mpi() need bignum.c"
00039 #endif
00040 #endif
00041
00042 #ifdef _MSC_VER
00043 #include <basetsd.h>
00044 typedef UINT32 uint32_t;
00045 #else
00046 #include <inttypes.h>
00047 #endif
00048
00049 #include <assert.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052
00053
00054
00055
00056 #ifndef GET_UINT32_BE
00057 #define GET_UINT32_BE(n,b,i) \
00058 { \
00059 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00060 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00061 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00062 | ( (uint32_t) (b)[(i) + 3] ); \
00063 }
00064 #endif
00065
00066 #ifndef PUT_UINT32_BE
00067 #define PUT_UINT32_BE(n,b,i) \
00068 { \
00069 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00070 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00071 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00072 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00073 }
00074 #endif
00075
00076 static int unhexify(unsigned char *obuf, const char *ibuf)
00077 {
00078 unsigned char c, c2;
00079 int len = strlen(ibuf) / 2;
00080 assert(!(strlen(ibuf) %1));
00081
00082 while (*ibuf != 0)
00083 {
00084 c = *ibuf++;
00085 if( c >= '0' && c <= '9' )
00086 c -= '0';
00087 else if( c >= 'a' && c <= 'f' )
00088 c -= 'a' - 10;
00089 else if( c >= 'A' && c <= 'F' )
00090 c -= 'A' - 10;
00091 else
00092 assert( 0 );
00093
00094 c2 = *ibuf++;
00095 if( c2 >= '0' && c2 <= '9' )
00096 c2 -= '0';
00097 else if( c2 >= 'a' && c2 <= 'f' )
00098 c2 -= 'a' - 10;
00099 else if( c2 >= 'A' && c2 <= 'F' )
00100 c2 -= 'A' - 10;
00101 else
00102 assert( 0 );
00103
00104 *obuf++ = ( c << 4 ) | c2;
00105 }
00106
00107 return len;
00108 }
00109
00110 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00111 {
00112 unsigned char l, h;
00113
00114 while (len != 0)
00115 {
00116 h = (*ibuf) / 16;
00117 l = (*ibuf) % 16;
00118
00119 if( h < 10 )
00120 *obuf++ = '0' + h;
00121 else
00122 *obuf++ = 'a' + h - 10;
00123
00124 if( l < 10 )
00125 *obuf++ = '0' + l;
00126 else
00127 *obuf++ = 'a' + l - 10;
00128
00129 ++ibuf;
00130 len--;
00131 }
00132 }
00133
00143 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00144 {
00145 size_t i;
00146
00147 if( rng_state != NULL )
00148 rng_state = NULL;
00149
00150 for( i = 0; i < len; ++i )
00151 output[i] = rand();
00152
00153 return( 0 );
00154 }
00155
00161 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00162 {
00163 if( rng_state != NULL )
00164 rng_state = NULL;
00165
00166 memset( output, 0, len );
00167
00168 return( 0 );
00169 }
00170
00171 typedef struct
00172 {
00173 unsigned char *buf;
00174 size_t length;
00175 } rnd_buf_info;
00176
00188 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00189 {
00190 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00191 size_t use_len;
00192
00193 if( rng_state == NULL )
00194 return( rnd_std_rand( NULL, output, len ) );
00195
00196 use_len = len;
00197 if( len > info->length )
00198 use_len = info->length;
00199
00200 if( use_len )
00201 {
00202 memcpy( output, info->buf, use_len );
00203 info->buf += use_len;
00204 info->length -= use_len;
00205 }
00206
00207 if( len - use_len > 0 )
00208 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00209
00210 return( 0 );
00211 }
00212
00220 typedef struct
00221 {
00222 uint32_t key[16];
00223 uint32_t v0, v1;
00224 } rnd_pseudo_info;
00225
00234 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00235 {
00236 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00237 uint32_t i, *k, sum, delta=0x9E3779B9;
00238 unsigned char result[4];
00239
00240 if( rng_state == NULL )
00241 return( rnd_std_rand( NULL, output, len ) );
00242
00243 k = info->key;
00244
00245 while( len > 0 )
00246 {
00247 size_t use_len = ( len > 4 ) ? 4 : len;
00248 sum = 0;
00249
00250 for( i = 0; i < 32; i++ )
00251 {
00252 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00253 sum += delta;
00254 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00255 }
00256
00257 PUT_UINT32_BE( info->v0, result, 0 );
00258 memcpy( output, result, use_len );
00259 len -= use_len;
00260 }
00261
00262 return( 0 );
00263 }
00264
00265 #if defined(WANT_NOT_RND_MPI)
00266
00274 #define ciL (sizeof(t_uint))
00275 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00276 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00277 {
00278 char *str = (char *) in;
00279 mpi X;
00280
00281
00282
00283
00284
00285 X.s = 1;
00286 X.p = (t_uint *) out;
00287 X.n = CHARS_TO_LIMBS( len );
00288
00289
00290
00291
00292
00293 assert( strlen( str ) / 2 == len );
00294
00295 return( mpi_read_string( &X, 16, str ) );
00296 }
00297 #endif
00298
00299
00300 #include <stdio.h>
00301 #include <string.h>
00302
00303 static int test_errors = 0;
00304
00305 #ifdef POLARSSL_DEBUG_C
00306 #ifdef POLARSSL_BIGNUM_C
00307 #ifdef POLARSSL_SSL_TLS_C
00308 #ifdef POLARSSL_RSA_C
00309
00310 #define TEST_SUITE_ACTIVE
00311
00312 static int test_assert( int correct, char *test )
00313 {
00314 if( correct )
00315 return( 0 );
00316
00317 test_errors++;
00318 if( test_errors == 1 )
00319 printf( "FAILED\n" );
00320 printf( " %s\n", test );
00321
00322 return( 1 );
00323 }
00324
00325 #define TEST_ASSERT( TEST ) \
00326 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00327 if( test_errors) return; \
00328 } while (0)
00329
00330 int verify_string( char **str )
00331 {
00332 if( (*str)[0] != '"' ||
00333 (*str)[strlen( *str ) - 1] != '"' )
00334 {
00335 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00336 return( -1 );
00337 }
00338
00339 (*str)++;
00340 (*str)[strlen( *str ) - 1] = '\0';
00341
00342 return( 0 );
00343 }
00344
00345 int verify_int( char *str, int *value )
00346 {
00347 size_t i;
00348 int minus = 0;
00349 int digits = 1;
00350 int hex = 0;
00351
00352 for( i = 0; i < strlen( str ); i++ )
00353 {
00354 if( i == 0 && str[i] == '-' )
00355 {
00356 minus = 1;
00357 continue;
00358 }
00359
00360 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00361 str[i - 1] == '0' && str[i] == 'x' )
00362 {
00363 hex = 1;
00364 continue;
00365 }
00366
00367 if( str[i] < '0' || str[i] > '9' )
00368 {
00369 digits = 0;
00370 break;
00371 }
00372 }
00373
00374 if( digits )
00375 {
00376 if( hex )
00377 *value = strtol( str, NULL, 16 );
00378 else
00379 *value = strtol( str, NULL, 10 );
00380
00381 return( 0 );
00382 }
00383
00384
00385
00386 printf( "Expected integer for parameter and got: %s\n", str );
00387 return( -1 );
00388 }
00389
00390 #ifdef POLARSSL_FS_IO
00391 #ifdef POLARSSL_X509_CRT_PARSE_C
00392 void test_suite_debug_print_crt( char *crt_file, char *file, int line, char *prefix,
00393 char *result_str )
00394 {
00395 x509_crt crt;
00396 ssl_context ssl;
00397 struct buffer_data buffer;
00398
00399 x509_crt_init( &crt );
00400 memset( &ssl, 0, sizeof( ssl_context ) );
00401 memset( buffer.buf, 0, 2000 );
00402 buffer.ptr = buffer.buf;
00403
00404 ssl_set_dbg(&ssl, string_debug, &buffer);
00405
00406 TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
00407 debug_print_crt( &ssl, 0, file, line, prefix, &crt);
00408
00409 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
00410
00411 x509_crt_free( &crt );
00412 }
00413 #endif
00414 #endif
00415
00416 void test_suite_debug_print_mpi( int radix, char *value, char *file, int line,
00417 char *prefix, char *result_str )
00418 {
00419 ssl_context ssl;
00420 struct buffer_data buffer;
00421 mpi val;
00422
00423 mpi_init( &val );
00424
00425 memset( &ssl, 0, sizeof( ssl_context ) );
00426 memset( buffer.buf, 0, 2000 );
00427 buffer.ptr = buffer.buf;
00428
00429 TEST_ASSERT( mpi_read_string( &val, radix, value ) == 0 );
00430 ssl_set_dbg(&ssl, string_debug, &buffer);
00431
00432 debug_print_mpi( &ssl, 0, file, line, prefix, &val);
00433
00434 TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
00435
00436 mpi_free( &val );
00437 }
00438
00439
00440 #endif
00441 #endif
00442 #endif
00443 #endif
00444
00445
00446 int dep_check( char *str )
00447 {
00448 if( str == NULL )
00449 return( 1 );
00450
00451 if( strcmp( str, "POLARSSL_BASE64_C" ) == 0 )
00452 {
00453 #if defined(POLARSSL_BASE64_C)
00454 return( 0 );
00455 #else
00456 return( 1 );
00457 #endif
00458 }
00459 if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
00460 {
00461 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
00462 return( 0 );
00463 #else
00464 return( 1 );
00465 #endif
00466 }
00467 if( strcmp( str, "POLARSSL_PEM_C" ) == 0 )
00468 {
00469 #if defined(POLARSSL_PEM_C)
00470 return( 0 );
00471 #else
00472 return( 1 );
00473 #endif
00474 }
00475 if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
00476 {
00477 #if defined(POLARSSL_ECP_C)
00478 return( 0 );
00479 #else
00480 return( 1 );
00481 #endif
00482 }
00483
00484
00485 return( 1 );
00486 }
00487
00488 int dispatch_test(int cnt, char *params[50])
00489 {
00490 int ret;
00491 ((void) cnt);
00492 ((void) params);
00493
00494 #if defined(TEST_SUITE_ACTIVE)
00495 if( strcmp( params[0], "debug_print_crt" ) == 0 )
00496 {
00497 #ifdef POLARSSL_FS_IO
00498 #ifdef POLARSSL_X509_CRT_PARSE_C
00499
00500 char *param1 = params[1];
00501 char *param2 = params[2];
00502 int param3;
00503 char *param4 = params[4];
00504 char *param5 = params[5];
00505
00506 if( cnt != 6 )
00507 {
00508 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
00509 return( 2 );
00510 }
00511
00512 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00513 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00514 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
00515 if( verify_string( ¶m4 ) != 0 ) return( 2 );
00516 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00517
00518 test_suite_debug_print_crt( param1, param2, param3, param4, param5 );
00519 return ( 0 );
00520 #endif
00521 #endif
00522
00523 return ( 3 );
00524 }
00525 else
00526 if( strcmp( params[0], "debug_print_mpi" ) == 0 )
00527 {
00528
00529 int param1;
00530 char *param2 = params[2];
00531 char *param3 = params[3];
00532 int param4;
00533 char *param5 = params[5];
00534 char *param6 = params[6];
00535
00536 if( cnt != 7 )
00537 {
00538 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
00539 return( 2 );
00540 }
00541
00542 if( verify_int( params[1], ¶m1 ) != 0 ) return( 2 );
00543 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00544 if( verify_string( ¶m3 ) != 0 ) return( 2 );
00545 if( verify_int( params[4], ¶m4 ) != 0 ) return( 2 );
00546 if( verify_string( ¶m5 ) != 0 ) return( 2 );
00547 if( verify_string( ¶m6 ) != 0 ) return( 2 );
00548
00549 test_suite_debug_print_mpi( param1, param2, param3, param4, param5, param6 );
00550 return ( 0 );
00551
00552 return ( 3 );
00553 }
00554 else
00555
00556 {
00557 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00558 fflush( stdout );
00559 return( 1 );
00560 }
00561 #else
00562 return( 3 );
00563 #endif
00564 return( ret );
00565 }
00566
00567 int get_line( FILE *f, char *buf, size_t len )
00568 {
00569 char *ret;
00570
00571 ret = fgets( buf, len, f );
00572 if( ret == NULL )
00573 return( -1 );
00574
00575 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00576 buf[strlen(buf) - 1] = '\0';
00577 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00578 buf[strlen(buf) - 1] = '\0';
00579
00580 return( 0 );
00581 }
00582
00583 int parse_arguments( char *buf, size_t len, char *params[50] )
00584 {
00585 int cnt = 0, i;
00586 char *cur = buf;
00587 char *p = buf, *q;
00588
00589 params[cnt++] = cur;
00590
00591 while( *p != '\0' && p < buf + len )
00592 {
00593 if( *p == '\\' )
00594 {
00595 *p++;
00596 *p++;
00597 continue;
00598 }
00599 if( *p == ':' )
00600 {
00601 if( p + 1 < buf + len )
00602 {
00603 cur = p + 1;
00604 params[cnt++] = cur;
00605 }
00606 *p = '\0';
00607 }
00608
00609 *p++;
00610 }
00611
00612
00613 for( i = 0; i < cnt; i++ )
00614 {
00615 p = params[i];
00616 q = params[i];
00617
00618 while( *p != '\0' )
00619 {
00620 if( *p == '\\' && *(p + 1) == 'n' )
00621 {
00622 p += 2;
00623 *(q++) = '\n';
00624 }
00625 else if( *p == '\\' && *(p + 1) == ':' )
00626 {
00627 p += 2;
00628 *(q++) = ':';
00629 }
00630 else if( *p == '\\' && *(p + 1) == '?' )
00631 {
00632 p += 2;
00633 *(q++) = '?';
00634 }
00635 else
00636 *(q++) = *(p++);
00637 }
00638 *q = '\0';
00639 }
00640
00641 return( cnt );
00642 }
00643
00644 int main()
00645 {
00646 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
00647 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_debug.data";
00648 FILE *file;
00649 char buf[5000];
00650 char *params[50];
00651
00652 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00653 unsigned char alloc_buf[1000000];
00654 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
00655 #endif
00656
00657 file = fopen( filename, "r" );
00658 if( file == NULL )
00659 {
00660 fprintf( stderr, "Failed to open\n" );
00661 return( 1 );
00662 }
00663
00664 while( !feof( file ) )
00665 {
00666 int skip = 0;
00667
00668 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00669 break;
00670 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
00671 fprintf( stdout, " " );
00672 for( i = strlen( buf ) + 1; i < 67; i++ )
00673 fprintf( stdout, "." );
00674 fprintf( stdout, " " );
00675 fflush( stdout );
00676
00677 total_tests++;
00678
00679 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00680 break;
00681 cnt = parse_arguments( buf, strlen(buf), params );
00682
00683 if( strcmp( params[0], "depends_on" ) == 0 )
00684 {
00685 for( i = 1; i < cnt; i++ )
00686 if( dep_check( params[i] ) != 0 )
00687 skip = 1;
00688
00689 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00690 break;
00691 cnt = parse_arguments( buf, strlen(buf), params );
00692 }
00693
00694 if( skip == 0 )
00695 {
00696 test_errors = 0;
00697 ret = dispatch_test( cnt, params );
00698 }
00699
00700 if( skip == 1 || ret == 3 )
00701 {
00702 total_skipped++;
00703 fprintf( stdout, "----\n" );
00704 fflush( stdout );
00705 }
00706 else if( ret == 0 && test_errors == 0 )
00707 {
00708 fprintf( stdout, "PASS\n" );
00709 fflush( stdout );
00710 }
00711 else if( ret == 2 )
00712 {
00713 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
00714 fclose(file);
00715 exit( 2 );
00716 }
00717 else
00718 total_errors++;
00719
00720 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00721 break;
00722 if( strlen(buf) != 0 )
00723 {
00724 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
00725 return( 1 );
00726 }
00727 }
00728 fclose(file);
00729
00730 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
00731 if( total_errors == 0 )
00732 fprintf( stdout, "PASSED" );
00733 else
00734 fprintf( stdout, "FAILED" );
00735
00736 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
00737 total_tests - total_errors, total_tests, total_skipped );
00738
00739 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00740 #if defined(POLARSSL_MEMORY_DEBUG)
00741 memory_buffer_alloc_status();
00742 #endif
00743 memory_buffer_alloc_free();
00744 #endif
00745
00746 return( total_errors != 0 );
00747 }
00748
00749