00001 #include <polarssl/config.h>
00002
00003 #ifdef POLARSSL_PK_PARSE_C
00004 #ifdef POLARSSL_BIGNUM_C
00005
00006 #include <polarssl/pk.h>
00007 #include <polarssl/pem.h>
00008 #include <polarssl/oid.h>
00009 #endif
00010 #endif
00011
00012
00013 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00014 #include "polarssl/memory.h"
00015 #endif
00016
00017 #if defined(WANT_NOT_RND_MPI)
00018 #if defined(POLARSSL_BIGNUM_C)
00019 #include "polarssl/bignum.h"
00020 #else
00021 #error "not_rnd_mpi() need bignum.c"
00022 #endif
00023 #endif
00024
00025 #ifdef _MSC_VER
00026 #include <basetsd.h>
00027 typedef UINT32 uint32_t;
00028 #else
00029 #include <inttypes.h>
00030 #endif
00031
00032 #include <assert.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035
00036
00037
00038
00039 #ifndef GET_UINT32_BE
00040 #define GET_UINT32_BE(n,b,i) \
00041 { \
00042 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
00043 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
00044 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
00045 | ( (uint32_t) (b)[(i) + 3] ); \
00046 }
00047 #endif
00048
00049 #ifndef PUT_UINT32_BE
00050 #define PUT_UINT32_BE(n,b,i) \
00051 { \
00052 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
00053 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
00054 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
00055 (b)[(i) + 3] = (unsigned char) ( (n) ); \
00056 }
00057 #endif
00058
00059 static int unhexify(unsigned char *obuf, const char *ibuf)
00060 {
00061 unsigned char c, c2;
00062 int len = strlen(ibuf) / 2;
00063 assert(!(strlen(ibuf) %1));
00064
00065 while (*ibuf != 0)
00066 {
00067 c = *ibuf++;
00068 if( c >= '0' && c <= '9' )
00069 c -= '0';
00070 else if( c >= 'a' && c <= 'f' )
00071 c -= 'a' - 10;
00072 else if( c >= 'A' && c <= 'F' )
00073 c -= 'A' - 10;
00074 else
00075 assert( 0 );
00076
00077 c2 = *ibuf++;
00078 if( c2 >= '0' && c2 <= '9' )
00079 c2 -= '0';
00080 else if( c2 >= 'a' && c2 <= 'f' )
00081 c2 -= 'a' - 10;
00082 else if( c2 >= 'A' && c2 <= 'F' )
00083 c2 -= 'A' - 10;
00084 else
00085 assert( 0 );
00086
00087 *obuf++ = ( c << 4 ) | c2;
00088 }
00089
00090 return len;
00091 }
00092
00093 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00094 {
00095 unsigned char l, h;
00096
00097 while (len != 0)
00098 {
00099 h = (*ibuf) / 16;
00100 l = (*ibuf) % 16;
00101
00102 if( h < 10 )
00103 *obuf++ = '0' + h;
00104 else
00105 *obuf++ = 'a' + h - 10;
00106
00107 if( l < 10 )
00108 *obuf++ = '0' + l;
00109 else
00110 *obuf++ = 'a' + l - 10;
00111
00112 ++ibuf;
00113 len--;
00114 }
00115 }
00116
00126 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00127 {
00128 size_t i;
00129
00130 if( rng_state != NULL )
00131 rng_state = NULL;
00132
00133 for( i = 0; i < len; ++i )
00134 output[i] = rand();
00135
00136 return( 0 );
00137 }
00138
00144 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00145 {
00146 if( rng_state != NULL )
00147 rng_state = NULL;
00148
00149 memset( output, 0, len );
00150
00151 return( 0 );
00152 }
00153
00154 typedef struct
00155 {
00156 unsigned char *buf;
00157 size_t length;
00158 } rnd_buf_info;
00159
00171 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00172 {
00173 rnd_buf_info *info = (rnd_buf_info *) rng_state;
00174 size_t use_len;
00175
00176 if( rng_state == NULL )
00177 return( rnd_std_rand( NULL, output, len ) );
00178
00179 use_len = len;
00180 if( len > info->length )
00181 use_len = info->length;
00182
00183 if( use_len )
00184 {
00185 memcpy( output, info->buf, use_len );
00186 info->buf += use_len;
00187 info->length -= use_len;
00188 }
00189
00190 if( len - use_len > 0 )
00191 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00192
00193 return( 0 );
00194 }
00195
00203 typedef struct
00204 {
00205 uint32_t key[16];
00206 uint32_t v0, v1;
00207 } rnd_pseudo_info;
00208
00217 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00218 {
00219 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00220 uint32_t i, *k, sum, delta=0x9E3779B9;
00221 unsigned char result[4];
00222
00223 if( rng_state == NULL )
00224 return( rnd_std_rand( NULL, output, len ) );
00225
00226 k = info->key;
00227
00228 while( len > 0 )
00229 {
00230 size_t use_len = ( len > 4 ) ? 4 : len;
00231 sum = 0;
00232
00233 for( i = 0; i < 32; i++ )
00234 {
00235 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00236 sum += delta;
00237 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00238 }
00239
00240 PUT_UINT32_BE( info->v0, result, 0 );
00241 memcpy( output, result, use_len );
00242 len -= use_len;
00243 }
00244
00245 return( 0 );
00246 }
00247
00248 #if defined(WANT_NOT_RND_MPI)
00249
00257 #define ciL (sizeof(t_uint))
00258 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
00259 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
00260 {
00261 char *str = (char *) in;
00262 mpi X;
00263
00264
00265
00266
00267
00268 X.s = 1;
00269 X.p = (t_uint *) out;
00270 X.n = CHARS_TO_LIMBS( len );
00271
00272
00273
00274
00275
00276 assert( strlen( str ) / 2 == len );
00277
00278 return( mpi_read_string( &X, 16, str ) );
00279 }
00280 #endif
00281
00282
00283 #include <stdio.h>
00284 #include <string.h>
00285
00286 static int test_errors = 0;
00287
00288 #ifdef POLARSSL_PK_PARSE_C
00289 #ifdef POLARSSL_BIGNUM_C
00290
00291 #define TEST_SUITE_ACTIVE
00292
00293 static int test_assert( int correct, char *test )
00294 {
00295 if( correct )
00296 return( 0 );
00297
00298 test_errors++;
00299 if( test_errors == 1 )
00300 printf( "FAILED\n" );
00301 printf( " %s\n", test );
00302
00303 return( 1 );
00304 }
00305
00306 #define TEST_ASSERT( TEST ) \
00307 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
00308 if( test_errors) return; \
00309 } while (0)
00310
00311 int verify_string( char **str )
00312 {
00313 if( (*str)[0] != '"' ||
00314 (*str)[strlen( *str ) - 1] != '"' )
00315 {
00316 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
00317 return( -1 );
00318 }
00319
00320 (*str)++;
00321 (*str)[strlen( *str ) - 1] = '\0';
00322
00323 return( 0 );
00324 }
00325
00326 int verify_int( char *str, int *value )
00327 {
00328 size_t i;
00329 int minus = 0;
00330 int digits = 1;
00331 int hex = 0;
00332
00333 for( i = 0; i < strlen( str ); i++ )
00334 {
00335 if( i == 0 && str[i] == '-' )
00336 {
00337 minus = 1;
00338 continue;
00339 }
00340
00341 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
00342 str[i - 1] == '0' && str[i] == 'x' )
00343 {
00344 hex = 1;
00345 continue;
00346 }
00347
00348 if( str[i] < '0' || str[i] > '9' )
00349 {
00350 digits = 0;
00351 break;
00352 }
00353 }
00354
00355 if( digits )
00356 {
00357 if( hex )
00358 *value = strtol( str, NULL, 16 );
00359 else
00360 *value = strtol( str, NULL, 10 );
00361
00362 return( 0 );
00363 }
00364
00365 #ifdef POLARSSL_RSA_C
00366 #ifdef POLARSSL_FS_IO
00367 if( strcmp( str, "POLARSSL_ERR_PK_PASSWORD_MISMATCH" ) == 0 )
00368 {
00369 *value = ( POLARSSL_ERR_PK_PASSWORD_MISMATCH );
00370 return( 0 );
00371 }
00372 #endif // POLARSSL_RSA_C
00373 #endif // POLARSSL_FS_IO
00374 #ifdef POLARSSL_RSA_C
00375 if( strcmp( str, "POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
00376 {
00377 *value = ( POLARSSL_ERR_PK_KEY_INVALID_FORMAT );
00378 return( 0 );
00379 }
00380 #endif // POLARSSL_RSA_C
00381 #ifdef POLARSSL_RSA_C
00382 #ifdef POLARSSL_FS_IO
00383 if( strcmp( str, "POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
00384 {
00385 *value = ( POLARSSL_ERR_PK_KEY_INVALID_FORMAT );
00386 return( 0 );
00387 }
00388 #endif // POLARSSL_RSA_C
00389 #endif // POLARSSL_FS_IO
00390 #ifdef POLARSSL_RSA_C
00391 #ifdef POLARSSL_FS_IO
00392 if( strcmp( str, "POLARSSL_ERR_PK_PASSWORD_REQUIRED" ) == 0 )
00393 {
00394 *value = ( POLARSSL_ERR_PK_PASSWORD_REQUIRED );
00395 return( 0 );
00396 }
00397 #endif // POLARSSL_RSA_C
00398 #endif // POLARSSL_FS_IO
00399
00400
00401 printf( "Expected integer for parameter and got: %s\n", str );
00402 return( -1 );
00403 }
00404
00405 #ifdef POLARSSL_RSA_C
00406 #ifdef POLARSSL_FS_IO
00407 void test_suite_pk_parse_keyfile_rsa( char *key_file, char *password, int result )
00408 {
00409 pk_context ctx;
00410 int res;
00411 char *pwd = password;
00412
00413 pk_init( &ctx );
00414
00415 if( strcmp( pwd, "NULL" ) == 0 )
00416 pwd = NULL;
00417
00418 res = pk_parse_keyfile( &ctx, key_file, pwd );
00419
00420 TEST_ASSERT( res == result );
00421
00422 if( res == 0 )
00423 {
00424 rsa_context *rsa;
00425 TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_RSA ) );
00426 rsa = pk_rsa( ctx );
00427 TEST_ASSERT( rsa_check_privkey( rsa ) == 0 );
00428 }
00429
00430 pk_free( &ctx );
00431 }
00432 #endif
00433 #endif
00434
00435 #ifdef POLARSSL_RSA_C
00436 #ifdef POLARSSL_FS_IO
00437 void test_suite_pk_parse_public_keyfile_rsa( char *key_file, int result )
00438 {
00439 pk_context ctx;
00440 int res;
00441
00442 pk_init( &ctx );
00443
00444 res = pk_parse_public_keyfile( &ctx, key_file );
00445
00446 TEST_ASSERT( res == result );
00447
00448 if( res == 0 )
00449 {
00450 rsa_context *rsa;
00451 TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_RSA ) );
00452 rsa = pk_rsa( ctx );
00453 TEST_ASSERT( rsa_check_pubkey( rsa ) == 0 );
00454 }
00455
00456 pk_free( &ctx );
00457 }
00458 #endif
00459 #endif
00460
00461 #ifdef POLARSSL_FS_IO
00462 #ifdef POLARSSL_ECP_C
00463 void test_suite_pk_parse_public_keyfile_ec( char *key_file, int result )
00464 {
00465 pk_context ctx;
00466 int res;
00467
00468 pk_init( &ctx );
00469
00470 res = pk_parse_public_keyfile( &ctx, key_file );
00471
00472 TEST_ASSERT( res == result );
00473
00474 if( res == 0 )
00475 {
00476 ecp_keypair *eckey;
00477 TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_ECKEY ) );
00478 eckey = pk_ec( ctx );
00479 TEST_ASSERT( ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
00480 }
00481
00482 pk_free( &ctx );
00483 }
00484 #endif
00485 #endif
00486
00487 #ifdef POLARSSL_FS_IO
00488 #ifdef POLARSSL_ECP_C
00489 void test_suite_pk_parse_keyfile_ec( char *key_file, char *password, int result )
00490 {
00491 pk_context ctx;
00492 int res;
00493
00494 pk_init( &ctx );
00495
00496 res = pk_parse_keyfile( &ctx, key_file, password );
00497
00498 TEST_ASSERT( res == result );
00499
00500 if( res == 0 )
00501 {
00502 ecp_keypair *eckey;
00503 TEST_ASSERT( pk_can_do( &ctx, POLARSSL_PK_ECKEY ) );
00504 eckey = pk_ec( ctx );
00505 TEST_ASSERT( ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
00506 }
00507
00508 pk_free( &ctx );
00509 }
00510 #endif
00511 #endif
00512
00513 #ifdef POLARSSL_RSA_C
00514 void test_suite_pk_parse_key_rsa( char *key_data, char *result_str, int result )
00515 {
00516 pk_context pk;
00517 unsigned char buf[2000];
00518 unsigned char output[2000];
00519 int data_len;
00520 ((void) result_str);
00521
00522 pk_init( &pk );
00523
00524 memset( buf, 0, 2000 );
00525 memset( output, 0, 2000 );
00526
00527 data_len = unhexify( buf, key_data );
00528
00529 TEST_ASSERT( pk_parse_key( &pk, buf, data_len, NULL, 0 ) == ( result ) );
00530 if( ( result ) == 0 )
00531 {
00532 TEST_ASSERT( 1 );
00533 }
00534
00535 pk_free( &pk );
00536 }
00537 #endif
00538
00539
00540 #endif
00541 #endif
00542
00543
00544 int dep_check( char *str )
00545 {
00546 if( str == NULL )
00547 return( 1 );
00548
00549 if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
00550 {
00551 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
00552 return( 0 );
00553 #else
00554 return( 1 );
00555 #endif
00556 }
00557 if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
00558 {
00559 #if defined(POLARSSL_SHA1_C)
00560 return( 0 );
00561 #else
00562 return( 1 );
00563 #endif
00564 }
00565 if( strcmp( str, "POLARSSL_ARC4_C" ) == 0 )
00566 {
00567 #if defined(POLARSSL_ARC4_C)
00568 return( 0 );
00569 #else
00570 return( 1 );
00571 #endif
00572 }
00573 if( strcmp( str, "POLARSSL_DES_C" ) == 0 )
00574 {
00575 #if defined(POLARSSL_DES_C)
00576 return( 0 );
00577 #else
00578 return( 1 );
00579 #endif
00580 }
00581 if( strcmp( str, "POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
00582 {
00583 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
00584 return( 0 );
00585 #else
00586 return( 1 );
00587 #endif
00588 }
00589 if( strcmp( str, "POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
00590 {
00591 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
00592 return( 0 );
00593 #else
00594 return( 1 );
00595 #endif
00596 }
00597 if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
00598 {
00599 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
00600 return( 0 );
00601 #else
00602 return( 1 );
00603 #endif
00604 }
00605 if( strcmp( str, "POLARSSL_PEM_PARSE_C" ) == 0 )
00606 {
00607 #if defined(POLARSSL_PEM_PARSE_C)
00608 return( 0 );
00609 #else
00610 return( 1 );
00611 #endif
00612 }
00613 if( strcmp( str, "POLARSSL_PKCS5_C" ) == 0 )
00614 {
00615 #if defined(POLARSSL_PKCS5_C)
00616 return( 0 );
00617 #else
00618 return( 1 );
00619 #endif
00620 }
00621 if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
00622 {
00623 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
00624 return( 0 );
00625 #else
00626 return( 1 );
00627 #endif
00628 }
00629 if( strcmp( str, "POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
00630 {
00631 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
00632 return( 0 );
00633 #else
00634 return( 1 );
00635 #endif
00636 }
00637 if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
00638 {
00639 #if defined(POLARSSL_AES_C)
00640 return( 0 );
00641 #else
00642 return( 1 );
00643 #endif
00644 }
00645 if( strcmp( str, "POLARSSL_PKCS12_C" ) == 0 )
00646 {
00647 #if defined(POLARSSL_PKCS12_C)
00648 return( 0 );
00649 #else
00650 return( 1 );
00651 #endif
00652 }
00653 if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
00654 {
00655 #if defined(POLARSSL_CIPHER_MODE_CBC)
00656 return( 0 );
00657 #else
00658 return( 1 );
00659 #endif
00660 }
00661 if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
00662 {
00663 #if defined(POLARSSL_MD5_C)
00664 return( 0 );
00665 #else
00666 return( 1 );
00667 #endif
00668 }
00669 if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
00670 {
00671 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
00672 return( 0 );
00673 #else
00674 return( 1 );
00675 #endif
00676 }
00677 if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
00678 {
00679 #if defined(POLARSSL_ECP_C)
00680 return( 0 );
00681 #else
00682 return( 1 );
00683 #endif
00684 }
00685 if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
00686 {
00687 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
00688 return( 0 );
00689 #else
00690 return( 1 );
00691 #endif
00692 }
00693 if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
00694 {
00695 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
00696 return( 0 );
00697 #else
00698 return( 1 );
00699 #endif
00700 }
00701
00702
00703 return( 1 );
00704 }
00705
00706 int dispatch_test(int cnt, char *params[50])
00707 {
00708 int ret;
00709 ((void) cnt);
00710 ((void) params);
00711
00712 #if defined(TEST_SUITE_ACTIVE)
00713 if( strcmp( params[0], "pk_parse_keyfile_rsa" ) == 0 )
00714 {
00715 #ifdef POLARSSL_RSA_C
00716 #ifdef POLARSSL_FS_IO
00717
00718 char *param1 = params[1];
00719 char *param2 = params[2];
00720 int param3;
00721
00722 if( cnt != 4 )
00723 {
00724 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
00725 return( 2 );
00726 }
00727
00728 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00729 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00730 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
00731
00732 test_suite_pk_parse_keyfile_rsa( param1, param2, param3 );
00733 return ( 0 );
00734 #endif
00735 #endif
00736
00737 return ( 3 );
00738 }
00739 else
00740 if( strcmp( params[0], "pk_parse_public_keyfile_rsa" ) == 0 )
00741 {
00742 #ifdef POLARSSL_RSA_C
00743 #ifdef POLARSSL_FS_IO
00744
00745 char *param1 = params[1];
00746 int param2;
00747
00748 if( cnt != 3 )
00749 {
00750 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00751 return( 2 );
00752 }
00753
00754 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00755 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00756
00757 test_suite_pk_parse_public_keyfile_rsa( param1, param2 );
00758 return ( 0 );
00759 #endif
00760 #endif
00761
00762 return ( 3 );
00763 }
00764 else
00765 if( strcmp( params[0], "pk_parse_public_keyfile_ec" ) == 0 )
00766 {
00767 #ifdef POLARSSL_FS_IO
00768 #ifdef POLARSSL_ECP_C
00769
00770 char *param1 = params[1];
00771 int param2;
00772
00773 if( cnt != 3 )
00774 {
00775 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
00776 return( 2 );
00777 }
00778
00779 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00780 if( verify_int( params[2], ¶m2 ) != 0 ) return( 2 );
00781
00782 test_suite_pk_parse_public_keyfile_ec( param1, param2 );
00783 return ( 0 );
00784 #endif
00785 #endif
00786
00787 return ( 3 );
00788 }
00789 else
00790 if( strcmp( params[0], "pk_parse_keyfile_ec" ) == 0 )
00791 {
00792 #ifdef POLARSSL_FS_IO
00793 #ifdef POLARSSL_ECP_C
00794
00795 char *param1 = params[1];
00796 char *param2 = params[2];
00797 int param3;
00798
00799 if( cnt != 4 )
00800 {
00801 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
00802 return( 2 );
00803 }
00804
00805 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00806 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00807 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
00808
00809 test_suite_pk_parse_keyfile_ec( param1, param2, param3 );
00810 return ( 0 );
00811 #endif
00812 #endif
00813
00814 return ( 3 );
00815 }
00816 else
00817 if( strcmp( params[0], "pk_parse_key_rsa" ) == 0 )
00818 {
00819 #ifdef POLARSSL_RSA_C
00820
00821 char *param1 = params[1];
00822 char *param2 = params[2];
00823 int param3;
00824
00825 if( cnt != 4 )
00826 {
00827 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
00828 return( 2 );
00829 }
00830
00831 if( verify_string( ¶m1 ) != 0 ) return( 2 );
00832 if( verify_string( ¶m2 ) != 0 ) return( 2 );
00833 if( verify_int( params[3], ¶m3 ) != 0 ) return( 2 );
00834
00835 test_suite_pk_parse_key_rsa( param1, param2, param3 );
00836 return ( 0 );
00837 #endif
00838
00839 return ( 3 );
00840 }
00841 else
00842
00843 {
00844 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
00845 fflush( stdout );
00846 return( 1 );
00847 }
00848 #else
00849 return( 3 );
00850 #endif
00851 return( ret );
00852 }
00853
00854 int get_line( FILE *f, char *buf, size_t len )
00855 {
00856 char *ret;
00857
00858 ret = fgets( buf, len, f );
00859 if( ret == NULL )
00860 return( -1 );
00861
00862 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
00863 buf[strlen(buf) - 1] = '\0';
00864 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
00865 buf[strlen(buf) - 1] = '\0';
00866
00867 return( 0 );
00868 }
00869
00870 int parse_arguments( char *buf, size_t len, char *params[50] )
00871 {
00872 int cnt = 0, i;
00873 char *cur = buf;
00874 char *p = buf, *q;
00875
00876 params[cnt++] = cur;
00877
00878 while( *p != '\0' && p < buf + len )
00879 {
00880 if( *p == '\\' )
00881 {
00882 *p++;
00883 *p++;
00884 continue;
00885 }
00886 if( *p == ':' )
00887 {
00888 if( p + 1 < buf + len )
00889 {
00890 cur = p + 1;
00891 params[cnt++] = cur;
00892 }
00893 *p = '\0';
00894 }
00895
00896 *p++;
00897 }
00898
00899
00900 for( i = 0; i < cnt; i++ )
00901 {
00902 p = params[i];
00903 q = params[i];
00904
00905 while( *p != '\0' )
00906 {
00907 if( *p == '\\' && *(p + 1) == 'n' )
00908 {
00909 p += 2;
00910 *(q++) = '\n';
00911 }
00912 else if( *p == '\\' && *(p + 1) == ':' )
00913 {
00914 p += 2;
00915 *(q++) = ':';
00916 }
00917 else if( *p == '\\' && *(p + 1) == '?' )
00918 {
00919 p += 2;
00920 *(q++) = '?';
00921 }
00922 else
00923 *(q++) = *(p++);
00924 }
00925 *q = '\0';
00926 }
00927
00928 return( cnt );
00929 }
00930
00931 int main()
00932 {
00933 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
00934 const char *filename = "/home/abuild/rpmbuild/BUILD/polarssl-1.3.2/tests/suites/test_suite_pkparse.data";
00935 FILE *file;
00936 char buf[5000];
00937 char *params[50];
00938
00939 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
00940 unsigned char alloc_buf[1000000];
00941 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
00942 #endif
00943
00944 file = fopen( filename, "r" );
00945 if( file == NULL )
00946 {
00947 fprintf( stderr, "Failed to open\n" );
00948 return( 1 );
00949 }
00950
00951 while( !feof( file ) )
00952 {
00953 int skip = 0;
00954
00955 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00956 break;
00957 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
00958 fprintf( stdout, " " );
00959 for( i = strlen( buf ) + 1; i < 67; i++ )
00960 fprintf( stdout, "." );
00961 fprintf( stdout, " " );
00962 fflush( stdout );
00963
00964 total_tests++;
00965
00966 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00967 break;
00968 cnt = parse_arguments( buf, strlen(buf), params );
00969
00970 if( strcmp( params[0], "depends_on" ) == 0 )
00971 {
00972 for( i = 1; i < cnt; i++ )
00973 if( dep_check( params[i] ) != 0 )
00974 skip = 1;
00975
00976 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
00977 break;
00978 cnt = parse_arguments( buf, strlen(buf), params );
00979 }
00980
00981 if( skip == 0 )
00982 {
00983 test_errors = 0;
00984 ret = dispatch_test( cnt, params );
00985 }
00986
00987 if( skip == 1 || ret == 3 )
00988 {
00989 total_skipped++;
00990 fprintf( stdout, "----\n" );
00991 fflush( stdout );
00992 }
00993 else if( ret == 0 && test_errors == 0 )
00994 {
00995 fprintf( stdout, "PASS\n" );
00996 fflush( stdout );
00997 }
00998 else if( ret == 2 )
00999 {
01000 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
01001 fclose(file);
01002 exit( 2 );
01003 }
01004 else
01005 total_errors++;
01006
01007 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
01008 break;
01009 if( strlen(buf) != 0 )
01010 {
01011 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
01012 return( 1 );
01013 }
01014 }
01015 fclose(file);
01016
01017 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
01018 if( total_errors == 0 )
01019 fprintf( stdout, "PASSED" );
01020 else
01021 fprintf( stdout, "FAILED" );
01022
01023 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
01024 total_tests - total_errors, total_tests, total_skipped );
01025
01026 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
01027 #if defined(POLARSSL_MEMORY_DEBUG)
01028 memory_buffer_alloc_status();
01029 #endif
01030 memory_buffer_alloc_free();
01031 #endif
01032
01033 return( total_errors != 0 );
01034 }
01035
01036