3 #ifdef POLARSSL_BIGNUM_C
5 #ifdef POLARSSL_PK_PARSE_C
16 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
22 typedef UINT32 uint32_t;
35 #define GET_UINT32_BE(n,b,i) \
37 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
38 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
39 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
40 | ( (uint32_t) (b)[(i) + 3] ); \
45 #define PUT_UINT32_BE(n,b,i) \
47 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
48 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
49 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
50 (b)[(i) + 3] = (unsigned char) ( (n) ); \
54 static int unhexify(
unsigned char *obuf,
const char *ibuf)
57 int len = strlen(ibuf) / 2;
58 assert(!(strlen(ibuf) %1));
63 if( c >=
'0' && c <=
'9' )
65 else if( c >=
'a' && c <=
'f' )
67 else if( c >=
'A' && c <=
'F' )
73 if( c2 >=
'0' && c2 <=
'9' )
75 else if( c2 >=
'a' && c2 <=
'f' )
77 else if( c2 >=
'A' && c2 <=
'F' )
82 *obuf++ = ( c << 4 ) | c2;
88 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
100 *obuf++ =
'a' + h - 10;
105 *obuf++ =
'a' + l - 10;
121 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
125 if( rng_state != NULL )
128 for( i = 0; i < len; ++i )
139 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
141 if( rng_state != NULL )
144 memset( output, 0, len );
171 if( rng_state == NULL )
180 memcpy( output, info->
buf, use_len );
181 info->
buf += use_len;
185 if( len - use_len > 0 )
186 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
215 uint32_t i, *k, sum, delta=0x9E3779B9;
216 unsigned char result[4];
218 if( rng_state == NULL )
225 size_t use_len = ( len > 4 ) ? 4 : len;
228 for( i = 0; i < 32; i++ )
230 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
232 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
236 memcpy( output, result, use_len );
252 static int not_rnd(
void *in,
unsigned char *out,
size_t len )
255 const char *ibuf = in;
257 assert( len == strlen(ibuf) / 2 );
258 assert(!(strlen(ibuf) %1));
260 obuf = out + (len - 1);
264 if( c >=
'0' && c <=
'9' )
266 else if( c >=
'a' && c <=
'f' )
268 else if( c >=
'A' && c <=
'F' )
274 if( c2 >=
'0' && c2 <=
'9' )
276 else if( c2 >=
'a' && c2 <=
'f' )
278 else if( c2 >=
'A' && c2 <=
'F' )
283 *obuf-- = ( c << 4 ) | c2;
295 #ifdef POLARSSL_BIGNUM_C
296 #ifdef POLARSSL_FS_IO
297 #ifdef POLARSSL_PK_PARSE_C
299 #define TEST_SUITE_ACTIVE
308 printf(
"FAILED\n" );
309 printf(
" %s\n", test );
314 #define TEST_ASSERT( TEST ) \
315 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
316 if( test_errors) return; \
321 if( (*str)[0] !=
'"' ||
322 (*str)[strlen( *str ) - 1] !=
'"' )
324 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
329 (*str)[strlen( *str ) - 1] =
'\0';
341 for( i = 0; i < strlen( str ); i++ )
343 if( i == 0 && str[i] ==
'-' )
349 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
350 str[i - 1] ==
'0' && str[i] ==
'x' )
356 if( str[i] <
'0' || str[i] >
'9' )
366 *value = strtol( str, NULL, 16 );
368 *value = strtol( str, NULL, 10 );
373 if( strcmp( str,
"POLARSSL_MD_MD4" ) == 0 )
378 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
383 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
388 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
393 if( strcmp( str,
"POLARSSL_MD_SHA224" ) == 0 )
398 if( strcmp( str,
"POLARSSL_MD_SHA384" ) == 0 )
403 if( strcmp( str,
"POLARSSL_MD_MD5" ) == 0 )
410 printf(
"Expected integer for parameter and got: %s\n", str );
414 #ifdef POLARSSL_PEM_WRITE_C
415 #ifdef POLARSSL_X509_CSR_WRITE_C
416 void test_suite_x509_csr_check(
char *key_file,
int md_type,
417 char *cert_req_check_file )
421 unsigned char buf[4000];
422 unsigned char check_buf[4000];
424 size_t olen = 0, pem_len = 0;
426 char *subject_name =
"C=NL,O=PolarSSL,CN=PolarSSL Server 1";
443 pem_len = strlen( (
char *) buf );
445 f = fopen( cert_req_check_file,
"r" );
447 olen = fread( check_buf, 1,
sizeof( check_buf ), f );
451 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
459 #ifdef POLARSSL_PEM_WRITE_C
460 #ifdef POLARSSL_X509_CRT_WRITE_C
461 void test_suite_x509_crt_check(
char *subject_key_file,
char *subject_pwd,
462 char *subject_name,
char *issuer_key_file,
463 char *issuer_pwd,
char *issuer_name,
464 char *serial_str,
char *not_before,
char *not_after,
465 int md_type,
char *cert_check_file )
469 unsigned char buf[4000];
470 unsigned char check_buf[5000];
473 size_t olen = 0, pem_len = 0;
483 subject_pwd ) == 0 );
506 pem_len = strlen( (
char *) buf );
508 f = fopen( cert_check_file,
"r" );
510 TEST_ASSERT( ( olen = fread( check_buf, 1,
sizeof(check_buf), f ) ) <
515 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
536 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
538 #if defined(POLARSSL_MD4_C)
544 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
546 #if defined(POLARSSL_SHA1_C)
552 if( strcmp( str,
"POLARSSL_PKCS1_V15" ) == 0 )
554 #if defined(POLARSSL_PKCS1_V15)
560 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
562 #if defined(POLARSSL_DES_C)
568 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
570 #if defined(POLARSSL_CIPHER_MODE_CBC)
576 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
578 #if defined(POLARSSL_MD5_C)
584 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
586 #if defined(POLARSSL_SHA256_C)
592 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
594 #if defined(POLARSSL_RSA_C)
600 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
602 #if defined(POLARSSL_SHA512_C)
619 #if defined(TEST_SUITE_ACTIVE)
620 if( strcmp( params[0],
"x509_csr_check" ) == 0 )
622 #ifdef POLARSSL_PEM_WRITE_C
623 #ifdef POLARSSL_X509_CSR_WRITE_C
625 char *param1 = params[1];
627 char *param3 = params[3];
631 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
636 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
639 test_suite_x509_csr_check( param1, param2, param3 );
647 if( strcmp( params[0],
"x509_crt_check" ) == 0 )
649 #ifdef POLARSSL_PEM_WRITE_C
650 #ifdef POLARSSL_X509_CRT_WRITE_C
652 char *param1 = params[1];
653 char *param2 = params[2];
654 char *param3 = params[3];
655 char *param4 = params[4];
656 char *param5 = params[5];
657 char *param6 = params[6];
658 char *param7 = params[7];
659 char *param8 = params[8];
660 char *param9 = params[9];
662 char *param11 = params[11];
666 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 12 );
679 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
682 test_suite_x509_crt_check( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
692 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
706 ret = fgets( buf, len, f );
710 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
711 buf[strlen(buf) - 1] =
'\0';
712 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
713 buf[strlen(buf) - 1] =
'\0';
726 while( *p !=
'\0' && p < buf + len )
736 if( p + 1 < buf + len )
748 for( i = 0; i < cnt; i++ )
755 if( *p ==
'\\' && *(p + 1) ==
'n' )
760 else if( *p ==
'\\' && *(p + 1) ==
':' )
765 else if( *p ==
'\\' && *(p + 1) ==
'?' )
781 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
782 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_x509write.data";
787 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
788 unsigned char alloc_buf[1000000];
789 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
792 file = fopen( filename,
"r" );
795 fprintf( stderr,
"Failed to open\n" );
799 while( !feof( file ) )
803 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
805 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
806 fprintf( stdout,
" " );
807 for( i = strlen( buf ) + 1; i < 67; i++ )
808 fprintf( stdout,
"." );
809 fprintf( stdout,
" " );
814 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
818 if( strcmp( params[0],
"depends_on" ) == 0 )
820 for( i = 1; i < cnt; i++ )
824 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
835 if( skip == 1 || ret == 3 )
838 fprintf( stdout,
"----\n" );
843 fprintf( stdout,
"PASS\n" );
848 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
855 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
857 if( strlen(buf) != 0 )
859 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
865 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
866 if( total_errors == 0 )
867 fprintf( stdout,
"PASSED" );
869 fprintf( stdout,
"FAILED" );
871 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
872 total_tests - total_errors, total_tests, total_skipped );
874 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
875 #if defined(POLARSSL_MEMORY_DEBUG)
876 memory_buffer_alloc_status();
878 memory_buffer_alloc_free();
881 return( total_errors != 0 );