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