3 #ifdef POLARSSL_PK_PARSE_C
4 #ifdef POLARSSL_BIGNUM_C
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #if defined(WANT_NOT_RND_MPI)
18 #if defined(POLARSSL_BIGNUM_C)
21 #error "not_rnd_mpi() need bignum.c"
27 typedef UINT32 uint32_t;
40 #define GET_UINT32_BE(n,b,i) \
42 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
43 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
44 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
45 | ( (uint32_t) (b)[(i) + 3] ); \
50 #define PUT_UINT32_BE(n,b,i) \
52 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
53 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
54 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
55 (b)[(i) + 3] = (unsigned char) ( (n) ); \
59 static int unhexify(
unsigned char *obuf,
const char *ibuf)
62 int len = strlen(ibuf) / 2;
63 assert(!(strlen(ibuf) %1));
68 if( c >=
'0' && c <=
'9' )
70 else if( c >=
'a' && c <=
'f' )
72 else if( c >=
'A' && c <=
'F' )
78 if( c2 >=
'0' && c2 <=
'9' )
80 else if( c2 >=
'a' && c2 <=
'f' )
82 else if( c2 >=
'A' && c2 <=
'F' )
87 *obuf++ = ( c << 4 ) | c2;
93 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
105 *obuf++ =
'a' + h - 10;
110 *obuf++ =
'a' + l - 10;
126 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
130 if( rng_state != NULL )
133 for( i = 0; i < len; ++i )
144 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
146 if( rng_state != NULL )
149 memset( output, 0, len );
176 if( rng_state == NULL )
185 memcpy( output, info->
buf, use_len );
186 info->
buf += use_len;
190 if( len - use_len > 0 )
191 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
220 uint32_t i, *k, sum, delta=0x9E3779B9;
221 unsigned char result[4];
223 if( rng_state == NULL )
230 size_t use_len = ( len > 4 ) ? 4 : len;
233 for( i = 0; i < 32; i++ )
235 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
237 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
241 memcpy( output, result, use_len );
248 #if defined(WANT_NOT_RND_MPI)
257 #define ciL (sizeof(t_uint))
258 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
259 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
261 char *str = (
char *) in;
270 X.
n = CHARS_TO_LIMBS( len );
276 assert( strlen( str ) / 2 == len );
288 #ifdef POLARSSL_PK_PARSE_C
289 #ifdef POLARSSL_BIGNUM_C
291 #define TEST_SUITE_ACTIVE
299 if( test_errors == 1 )
300 printf(
"FAILED\n" );
301 printf(
" %s\n", test );
306 #define TEST_ASSERT( TEST ) \
307 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
308 if( test_errors) return; \
313 if( (*str)[0] !=
'"' ||
314 (*str)[strlen( *str ) - 1] !=
'"' )
316 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
321 (*str)[strlen( *str ) - 1] =
'\0';
333 for( i = 0; i < strlen( str ); i++ )
335 if( i == 0 && str[i] ==
'-' )
341 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
342 str[i - 1] ==
'0' && str[i] ==
'x' )
348 if( str[i] <
'0' || str[i] >
'9' )
358 *value = strtol( str, NULL, 16 );
360 *value = strtol( str, NULL, 10 );
365 #ifdef POLARSSL_RSA_C
366 #ifdef POLARSSL_FS_IO
367 if( strcmp( str,
"POLARSSL_ERR_PK_PASSWORD_REQUIRED" ) == 0 )
372 #endif // POLARSSL_RSA_C
373 #endif // POLARSSL_FS_IO
374 #ifdef POLARSSL_RSA_C
375 #ifdef POLARSSL_FS_IO
376 if( strcmp( str,
"POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
381 #endif // POLARSSL_RSA_C
382 #endif // POLARSSL_FS_IO
383 #ifdef POLARSSL_RSA_C
384 if( strcmp( str,
"POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
389 #endif // POLARSSL_RSA_C
390 #ifdef POLARSSL_RSA_C
391 #ifdef POLARSSL_FS_IO
392 if( strcmp( str,
"POLARSSL_ERR_PK_PASSWORD_MISMATCH" ) == 0 )
397 #endif // POLARSSL_RSA_C
398 #endif // POLARSSL_FS_IO
401 printf(
"Expected integer for parameter and got: %s\n", str );
405 #ifdef POLARSSL_RSA_C
406 #ifdef POLARSSL_FS_IO
407 void test_suite_pk_parse_keyfile_rsa(
char *key_file,
char *password,
int result )
411 char *pwd = password;
415 if( strcmp( pwd,
"NULL" ) == 0 )
435 #ifdef POLARSSL_RSA_C
436 #ifdef POLARSSL_FS_IO
437 void test_suite_pk_parse_public_keyfile_rsa(
char *key_file,
int result )
461 #ifdef POLARSSL_FS_IO
462 #ifdef POLARSSL_ECP_C
463 void test_suite_pk_parse_public_keyfile_ec(
char *key_file,
int result )
478 eckey =
pk_ec( ctx );
487 #ifdef POLARSSL_FS_IO
488 #ifdef POLARSSL_ECP_C
489 void test_suite_pk_parse_keyfile_ec(
char *key_file,
char *password,
int result )
504 eckey =
pk_ec( ctx );
513 #ifdef POLARSSL_RSA_C
514 void test_suite_pk_parse_key_rsa(
char *key_data,
char *result_str,
int result )
517 unsigned char buf[2000];
518 unsigned char output[2000];
524 memset( buf, 0, 2000 );
525 memset( output, 0, 2000 );
527 data_len =
unhexify( buf, key_data );
530 if( ( result ) == 0 )
549 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
551 #if defined(POLARSSL_MD5_C)
557 if( strcmp( str,
"POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
559 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
565 if( strcmp( str,
"POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
567 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
573 if( strcmp( str,
"POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
575 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
581 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
583 #if defined(POLARSSL_SHA1_C)
589 if( strcmp( str,
"POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
591 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
597 if( strcmp( str,
"POLARSSL_PKCS5_C" ) == 0 )
599 #if defined(POLARSSL_PKCS5_C)
605 if( strcmp( str,
"POLARSSL_PKCS12_C" ) == 0 )
607 #if defined(POLARSSL_PKCS12_C)
613 if( strcmp( str,
"POLARSSL_ECP_C" ) == 0 )
615 #if defined(POLARSSL_ECP_C)
621 if( strcmp( str,
"POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
623 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
629 if( strcmp( str,
"POLARSSL_PEM_PARSE_C" ) == 0 )
631 #if defined(POLARSSL_PEM_PARSE_C)
637 if( strcmp( str,
"POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
639 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
645 if( strcmp( str,
"POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
647 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
653 if( strcmp( str,
"POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
655 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
661 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
663 #if defined(POLARSSL_DES_C)
669 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
671 #if defined(POLARSSL_CIPHER_MODE_CBC)
677 if( strcmp( str,
"POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
679 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
685 if( strcmp( str,
"POLARSSL_ARC4_C" ) == 0 )
687 #if defined(POLARSSL_ARC4_C)
693 if( strcmp( str,
"POLARSSL_AES_C" ) == 0 )
695 #if defined(POLARSSL_AES_C)
712 #if defined(TEST_SUITE_ACTIVE)
713 if( strcmp( params[0],
"pk_parse_keyfile_rsa" ) == 0 )
715 #ifdef POLARSSL_RSA_C
716 #ifdef POLARSSL_FS_IO
718 char *param1 = params[1];
719 char *param2 = params[2];
724 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
730 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
732 test_suite_pk_parse_keyfile_rsa( param1, param2, param3 );
740 if( strcmp( params[0],
"pk_parse_public_keyfile_rsa" ) == 0 )
742 #ifdef POLARSSL_RSA_C
743 #ifdef POLARSSL_FS_IO
745 char *param1 = params[1];
750 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
755 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
757 test_suite_pk_parse_public_keyfile_rsa( param1, param2 );
765 if( strcmp( params[0],
"pk_parse_public_keyfile_ec" ) == 0 )
767 #ifdef POLARSSL_FS_IO
768 #ifdef POLARSSL_ECP_C
770 char *param1 = params[1];
775 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 3 );
780 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
782 test_suite_pk_parse_public_keyfile_ec( param1, param2 );
790 if( strcmp( params[0],
"pk_parse_keyfile_ec" ) == 0 )
792 #ifdef POLARSSL_FS_IO
793 #ifdef POLARSSL_ECP_C
795 char *param1 = params[1];
796 char *param2 = params[2];
801 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
807 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
809 test_suite_pk_parse_keyfile_ec( param1, param2, param3 );
817 if( strcmp( params[0],
"pk_parse_key_rsa" ) == 0 )
819 #ifdef POLARSSL_RSA_C
821 char *param1 = params[1];
822 char *param2 = params[2];
827 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
833 if(
verify_int( params[3], ¶m3 ) != 0 )
return( 2 );
835 test_suite_pk_parse_key_rsa( param1, param2, param3 );
844 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
858 ret = fgets( buf, len, f );
862 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
863 buf[strlen(buf) - 1] =
'\0';
864 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
865 buf[strlen(buf) - 1] =
'\0';
878 while( *p !=
'\0' && p < buf + len )
888 if( p + 1 < buf + len )
900 for( i = 0; i < cnt; i++ )
907 if( *p ==
'\\' && *(p + 1) ==
'n' )
912 else if( *p ==
'\\' && *(p + 1) ==
':' )
917 else if( *p ==
'\\' && *(p + 1) ==
'?' )
933 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
934 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_pkparse.data";
939 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
940 unsigned char alloc_buf[1000000];
941 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
944 file = fopen( filename,
"r" );
947 fprintf( stderr,
"Failed to open\n" );
951 while( !feof( file ) )
955 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
957 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
958 fprintf( stdout,
" " );
959 for( i = strlen( buf ) + 1; i < 67; i++ )
960 fprintf( stdout,
"." );
961 fprintf( stdout,
" " );
966 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
970 if( strcmp( params[0],
"depends_on" ) == 0 )
972 for( i = 1; i < cnt; i++ )
976 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
987 if( skip == 1 || ret == 3 )
990 fprintf( stdout,
"----\n" );
993 else if( ret == 0 && test_errors == 0 )
995 fprintf( stdout,
"PASS\n" );
1000 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
1007 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
1009 if( strlen(buf) != 0 )
1011 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
1017 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
1018 if( total_errors == 0 )
1019 fprintf( stdout,
"PASSED" );
1021 fprintf( stdout,
"FAILED" );
1023 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
1024 total_tests - total_errors, total_tests, total_skipped );
1026 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1027 #if defined(POLARSSL_MEMORY_DEBUG)
1028 memory_buffer_alloc_status();
1030 memory_buffer_alloc_free();
1033 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)
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
Info structure for the pseudo random function.
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, char *test)
int main(int argc, char *argv[])
Object Identifier (OID) database.
Public Key abstraction layer.
Multi-precision integer library.
#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.
#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 parse_arguments(char *buf, size_t len, char *params[50])
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 mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int verify_string(char **str)
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
void pk_free(pk_context *ctx)
Free a pk_context.
int dispatch_test(int cnt, char *params[50])
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.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int get_line(FILE *f, char *buf, size_t len)