1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_PK_PARSE_C
8 #ifdef POLARSSL_BIGNUM_C
17 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
21 #if defined(POLARSSL_PLATFORM_C)
24 #define polarssl_malloc malloc
25 #define polarssl_free free
30 typedef UINT32 uint32_t;
43 #define GET_UINT32_BE(n,b,i) \
45 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
46 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
47 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
48 | ( (uint32_t) (b)[(i) + 3] ); \
53 #define PUT_UINT32_BE(n,b,i) \
55 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58 (b)[(i) + 3] = (unsigned char) ( (n) ); \
62 static int unhexify(
unsigned char *obuf,
const char *ibuf)
65 int len = strlen(ibuf) / 2;
66 assert(!(strlen(ibuf) %1));
71 if( c >=
'0' && c <=
'9' )
73 else if( c >=
'a' && c <=
'f' )
75 else if( c >=
'A' && c <=
'F' )
81 if( c2 >=
'0' && c2 <=
'9' )
83 else if( c2 >=
'a' && c2 <=
'f' )
85 else if( c2 >=
'A' && c2 <=
'F' )
90 *obuf++ = ( c << 4 ) | c2;
96 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
108 *obuf++ =
'a' + h - 10;
113 *obuf++ =
'a' + l - 10;
130 size_t actual_len = len != 0 ? len : 1;
135 memset( p, 0x00, actual_len );
154 *olen = strlen(ibuf) / 2;
160 assert( obuf != NULL );
176 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
178 #if !defined(__OpenBSD__)
181 if( rng_state != NULL )
184 for( i = 0; i < len; ++i )
187 if( rng_state != NULL )
190 arc4random_buf( output, len );
201 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
203 if( rng_state != NULL )
206 memset( output, 0, len );
233 if( rng_state == NULL )
242 memcpy( output, info->
buf, use_len );
243 info->
buf += use_len;
247 if( len - use_len > 0 )
248 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
277 uint32_t i, *k, sum, delta=0x9E3779B9;
278 unsigned char result[4], *out = output;
280 if( rng_state == NULL )
287 size_t use_len = ( len > 4 ) ? 4 : len;
290 for( i = 0; i < 32; i++ )
292 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
294 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
298 memcpy( out, result, use_len );
310 #if defined(POLARSSL_PLATFORM_C)
313 #define polarssl_printf printf
314 #define polarssl_malloc malloc
315 #define polarssl_free free
320 #ifdef POLARSSL_PK_PARSE_C
321 #ifdef POLARSSL_BIGNUM_C
323 #define TEST_SUITE_ACTIVE
325 static int test_assert(
int correct,
const char *test )
332 printf(
"FAILED\n" );
333 printf(
" %s\n", test );
338 #define TEST_ASSERT( TEST ) \
339 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
340 if( test_errors) goto exit; \
345 if( (*str)[0] !=
'"' ||
346 (*str)[strlen( *str ) - 1] !=
'"' )
348 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
353 (*str)[strlen( *str ) - 1] =
'\0';
365 for( i = 0; i < strlen( str ); i++ )
367 if( i == 0 && str[i] ==
'-' )
373 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
374 str[i - 1] ==
'0' && str[i] ==
'x' )
380 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
381 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
382 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
392 *value = strtol( str, NULL, 16 );
394 *value = strtol( str, NULL, 10 );
399 #ifdef POLARSSL_RSA_C
400 #ifdef POLARSSL_FS_IO
401 if( strcmp( str,
"POLARSSL_ERR_PK_PASSWORD_REQUIRED" ) == 0 )
406 #endif // POLARSSL_RSA_C
407 #endif // POLARSSL_FS_IO
408 #ifdef POLARSSL_RSA_C
409 #ifdef POLARSSL_FS_IO
410 if( strcmp( str,
"POLARSSL_ERR_PK_PASSWORD_MISMATCH" ) == 0 )
415 #endif // POLARSSL_RSA_C
416 #endif // POLARSSL_FS_IO
417 #ifdef POLARSSL_RSA_C
418 if( strcmp( str,
"POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
423 #endif // POLARSSL_RSA_C
424 #ifdef POLARSSL_RSA_C
425 #ifdef POLARSSL_FS_IO
426 if( strcmp( str,
"POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
431 #endif // POLARSSL_RSA_C
432 #endif // POLARSSL_FS_IO
435 printf(
"Expected integer for parameter and got: %s\n", str );
439 #ifdef POLARSSL_RSA_C
440 #ifdef POLARSSL_FS_IO
441 void test_suite_pk_parse_keyfile_rsa(
char *key_file,
char *password,
int result )
445 char *pwd = password;
449 if( strcmp( pwd,
"NULL" ) == 0 )
470 #ifdef POLARSSL_RSA_C
471 #ifdef POLARSSL_FS_IO
472 void test_suite_pk_parse_public_keyfile_rsa(
char *key_file,
int result )
497 #ifdef POLARSSL_FS_IO
498 #ifdef POLARSSL_ECP_C
499 void test_suite_pk_parse_public_keyfile_ec(
char *key_file,
int result )
514 eckey =
pk_ec( ctx );
524 #ifdef POLARSSL_FS_IO
525 #ifdef POLARSSL_ECP_C
526 void test_suite_pk_parse_keyfile_ec(
char *key_file,
char *password,
int result )
541 eckey =
pk_ec( ctx );
551 #ifdef POLARSSL_RSA_C
552 void test_suite_pk_parse_key_rsa(
char *key_data,
char *result_str,
int result )
555 unsigned char buf[2000];
556 unsigned char output[2000];
562 memset( buf, 0, 2000 );
563 memset( output, 0, 2000 );
565 data_len =
unhexify( buf, key_data );
568 if( ( result ) == 0 )
588 if( strcmp( str,
"POLARSSL_PKCS12_C" ) == 0 )
590 #if defined(POLARSSL_PKCS12_C)
596 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
598 #if defined(POLARSSL_ECP_C)
604 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
606 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
612 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
614 #if defined(POLARSSL_CIPHER_MODE_CBC)
620 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
622 #if defined(POLARSSL_MD5_C)
628 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
630 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
636 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256K1_ENABLED" ) == 0 )
638 #if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
644 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
646 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
652 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
654 #if defined(POLARSSL_AES_C)
660 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
662 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
668 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
670 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
676 if( strcmp( str,
"POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
678 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
684 if( strcmp( str,
"POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
686 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
692 if( strcmp( str,
"POLARSSL_ARC4_C" ) == 0 )
694 #if defined(POLARSSL_ARC4_C)
700 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
702 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
708 if( strcmp( str,
"POLARSSL_PEM_PARSE_C" ) == 0 )
710 #if defined(POLARSSL_PEM_PARSE_C)
716 if( strcmp( str,
"POLARSSL_PK_PARSE_EC_EXTENDED" ) == 0 )
718 #if defined(POLARSSL_PK_PARSE_EC_EXTENDED)
724 if( strcmp( str,
"POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
726 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
732 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
734 #if defined(POLARSSL_SHA1_C)
740 if( strcmp( str,
"POLARSSL_PKCS5_C" ) == 0 )
742 #if defined(POLARSSL_PKCS5_C)
748 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
750 #if defined(POLARSSL_DES_C)
767 #if defined(TEST_SUITE_ACTIVE)
768 if( strcmp( params[0],
"pk_parse_keyfile_rsa" ) == 0 )
770 #ifdef POLARSSL_RSA_C
771 #ifdef POLARSSL_FS_IO
773 char *param1 = params[1];
774 char *param2 = params[2];
779 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
785 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
787 test_suite_pk_parse_keyfile_rsa( param1, param2, param3 );
795 if( strcmp( params[0],
"pk_parse_public_keyfile_rsa" ) == 0 )
797 #ifdef POLARSSL_RSA_C
798 #ifdef POLARSSL_FS_IO
800 char *param1 = params[1];
805 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
810 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
812 test_suite_pk_parse_public_keyfile_rsa( param1, param2 );
820 if( strcmp( params[0],
"pk_parse_public_keyfile_ec" ) == 0 )
822 #ifdef POLARSSL_FS_IO
823 #ifdef POLARSSL_ECP_C
825 char *param1 = params[1];
830 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
835 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
837 test_suite_pk_parse_public_keyfile_ec( param1, param2 );
845 if( strcmp( params[0],
"pk_parse_keyfile_ec" ) == 0 )
847 #ifdef POLARSSL_FS_IO
848 #ifdef POLARSSL_ECP_C
850 char *param1 = params[1];
851 char *param2 = params[2];
856 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
862 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
864 test_suite_pk_parse_keyfile_ec( param1, param2, param3 );
872 if( strcmp( params[0],
"pk_parse_key_rsa" ) == 0 )
874 #ifdef POLARSSL_RSA_C
876 char *param1 = params[1];
877 char *param2 = params[2];
882 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
888 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
890 test_suite_pk_parse_key_rsa( param1, param2, param3 );
899 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
913 ret = fgets( buf, len, f );
917 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
918 buf[strlen(buf) - 1] =
'\0';
919 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
920 buf[strlen(buf) - 1] =
'\0';
933 while( *p !=
'\0' && p < buf + len )
943 if( p + 1 < buf + len )
955 for( i = 0; i < cnt; i++ )
962 if( *p ==
'\\' && *(p + 1) ==
'n' )
967 else if( *p ==
'\\' && *(p + 1) ==
':' )
972 else if( *p ==
'\\' && *(p + 1) ==
'?' )
988 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
989 const char *filename =
"/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_pkparse.data";
994 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
995 unsigned char alloc_buf[1000000];
999 file = fopen( filename,
"r" );
1002 fprintf( stderr,
"Failed to open\n" );
1006 while( !feof( file ) )
1010 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1012 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
1013 fprintf( stdout,
" " );
1014 for( i = strlen( buf ) + 1; i < 67; i++ )
1015 fprintf( stdout,
"." );
1016 fprintf( stdout,
" " );
1021 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1025 if( strcmp( params[0],
"depends_on" ) == 0 )
1027 for( i = 1; i < cnt; i++ )
1031 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1042 if( skip == 1 || ret == 3 )
1045 fprintf( stdout,
"----\n" );
1050 fprintf( stdout,
"PASS\n" );
1055 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1062 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1064 if( strlen(buf) != 0 )
1066 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1072 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1073 if( total_errors == 0 )
1074 fprintf( stdout,
"PASSED" );
1076 fprintf( stdout,
"FAILED" );
1078 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1079 total_tests - total_errors, total_tests, total_skipped );
1081 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1082 #if defined(POLARSSL_MEMORY_DEBUG)
1083 memory_buffer_alloc_status();
1088 return( total_errors != 0 );
#define POLARSSL_ERR_PK_KEY_INVALID_FORMAT
Invalid key tag or value.
int ecp_check_privkey(const ecp_group *grp, const mpi *d)
Check that an mpi is a valid private key for this curve.
#define PUT_UINT32_BE(n, b, i)
Memory allocation layer (Deprecated to platform layer)
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
Info structure for the pseudo random function.
int get_line(FILE *f, char *buf, size_t len)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
static int test_assert(int correct, const char *test)
Object Identifier (OID) database.
Public Key abstraction layer.
int parse_arguments(char *buf, size_t len, char *params[50])
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
static int unhexify(unsigned char *obuf, const char *ibuf)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
Privacy Enhanced Mail (PEM) decoding.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
#define POLARSSL_ERR_PK_PASSWORD_REQUIRED
Private key password can't be empty.
#define POLARSSL_ERR_PK_PASSWORD_MISMATCH
Given private key password does not allow for correct decryption.
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
int pk_parse_public_keyfile(pk_context *ctx, const char *path)
Load and parse a public key.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int verify_string(char **str)
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
void pk_free(pk_context *ctx)
Free a pk_context.
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
int pk_parse_key(pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen)
Parse a private key.
int ecp_check_pubkey(const ecp_group *grp, const ecp_point *pt)
Check that a point is a valid public key on this curve.
int verify_int(char *str, int *value)
int pk_parse_keyfile(pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
int dispatch_test(int cnt, char *params[50])
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.