PolarSSL v1.3.9
test_suite_pkparse.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 #ifdef POLARSSL_PK_PARSE_C
8 #ifdef POLARSSL_BIGNUM_C
9 
10 #include <polarssl/pk.h>
11 #include <polarssl/pem.h>
12 #include <polarssl/oid.h>
13 #endif /* POLARSSL_PK_PARSE_C */
14 #endif /* POLARSSL_BIGNUM_C */
15 
16 
17 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
18 #include "polarssl/memory.h"
19 #endif
20 
21 #if defined(POLARSSL_PLATFORM_C)
22 #include "polarssl/platform.h"
23 #else
24 #define polarssl_malloc malloc
25 #define polarssl_free free
26 #endif
27 
28 #ifdef _MSC_VER
29 #include <basetsd.h>
30 typedef UINT32 uint32_t;
31 #else
32 #include <inttypes.h>
33 #endif
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 /*
40  * 32-bit integer manipulation macros (big endian)
41  */
42 #ifndef GET_UINT32_BE
43 #define GET_UINT32_BE(n,b,i) \
44 { \
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] ); \
49 }
50 #endif
51 
52 #ifndef PUT_UINT32_BE
53 #define PUT_UINT32_BE(n,b,i) \
54 { \
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) ); \
59 }
60 #endif
61 
62 static int unhexify(unsigned char *obuf, const char *ibuf)
63 {
64  unsigned char c, c2;
65  int len = strlen(ibuf) / 2;
66  assert(!(strlen(ibuf) %1)); // must be even number of bytes
67 
68  while (*ibuf != 0)
69  {
70  c = *ibuf++;
71  if( c >= '0' && c <= '9' )
72  c -= '0';
73  else if( c >= 'a' && c <= 'f' )
74  c -= 'a' - 10;
75  else if( c >= 'A' && c <= 'F' )
76  c -= 'A' - 10;
77  else
78  assert( 0 );
79 
80  c2 = *ibuf++;
81  if( c2 >= '0' && c2 <= '9' )
82  c2 -= '0';
83  else if( c2 >= 'a' && c2 <= 'f' )
84  c2 -= 'a' - 10;
85  else if( c2 >= 'A' && c2 <= 'F' )
86  c2 -= 'A' - 10;
87  else
88  assert( 0 );
89 
90  *obuf++ = ( c << 4 ) | c2;
91  }
92 
93  return len;
94 }
95 
96 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
97 {
98  unsigned char l, h;
99 
100  while (len != 0)
101  {
102  h = (*ibuf) / 16;
103  l = (*ibuf) % 16;
104 
105  if( h < 10 )
106  *obuf++ = '0' + h;
107  else
108  *obuf++ = 'a' + h - 10;
109 
110  if( l < 10 )
111  *obuf++ = '0' + l;
112  else
113  *obuf++ = 'a' + l - 10;
114 
115  ++ibuf;
116  len--;
117  }
118 }
119 
127 static unsigned char *zero_alloc( size_t len )
128 {
129  void *p;
130  size_t actual_len = len != 0 ? len : 1;
131 
132  p = polarssl_malloc( actual_len );
133  assert( p != NULL );
134 
135  memset( p, 0x00, actual_len );
136 
137  return( p );
138 }
139 
150 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
151 {
152  unsigned char *obuf;
153 
154  *olen = strlen(ibuf) / 2;
155 
156  if( *olen == 0 )
157  return( zero_alloc( *olen ) );
158 
159  obuf = polarssl_malloc( *olen );
160  assert( obuf != NULL );
161 
162  (void) unhexify( obuf, ibuf );
163 
164  return( obuf );
165 }
166 
176 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
177 {
178 #if !defined(__OpenBSD__)
179  size_t i;
180 
181  if( rng_state != NULL )
182  rng_state = NULL;
183 
184  for( i = 0; i < len; ++i )
185  output[i] = rand();
186 #else
187  if( rng_state != NULL )
188  rng_state = NULL;
189 
190  arc4random_buf( output, len );
191 #endif /* !OpenBSD */
192 
193  return( 0 );
194 }
195 
201 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
202 {
203  if( rng_state != NULL )
204  rng_state = NULL;
205 
206  memset( output, 0, len );
207 
208  return( 0 );
209 }
210 
211 typedef struct
212 {
213  unsigned char *buf;
214  size_t length;
215 } rnd_buf_info;
216 
228 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
229 {
230  rnd_buf_info *info = (rnd_buf_info *) rng_state;
231  size_t use_len;
232 
233  if( rng_state == NULL )
234  return( rnd_std_rand( NULL, output, len ) );
235 
236  use_len = len;
237  if( len > info->length )
238  use_len = info->length;
239 
240  if( use_len )
241  {
242  memcpy( output, info->buf, use_len );
243  info->buf += use_len;
244  info->length -= use_len;
245  }
246 
247  if( len - use_len > 0 )
248  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
249 
250  return( 0 );
251 }
252 
260 typedef struct
261 {
262  uint32_t key[16];
263  uint32_t v0, v1;
265 
274 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
275 {
276  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
277  uint32_t i, *k, sum, delta=0x9E3779B9;
278  unsigned char result[4], *out = output;
279 
280  if( rng_state == NULL )
281  return( rnd_std_rand( NULL, output, len ) );
282 
283  k = info->key;
284 
285  while( len > 0 )
286  {
287  size_t use_len = ( len > 4 ) ? 4 : len;
288  sum = 0;
289 
290  for( i = 0; i < 32; i++ )
291  {
292  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
293  sum += delta;
294  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
295  }
296 
297  PUT_UINT32_BE( info->v0, result, 0 );
298  memcpy( out, result, use_len );
299  len -= use_len;
300  out += 4;
301  }
302 
303  return( 0 );
304 }
305 
306 
307 #include <stdio.h>
308 #include <string.h>
309 
310 #if defined(POLARSSL_PLATFORM_C)
311 #include "polarssl/platform.h"
312 #else
313 #define polarssl_printf printf
314 #define polarssl_malloc malloc
315 #define polarssl_free free
316 #endif
317 
318 static int test_errors = 0;
319 
320 #ifdef POLARSSL_PK_PARSE_C
321 #ifdef POLARSSL_BIGNUM_C
322 
323 #define TEST_SUITE_ACTIVE
324 
325 static int test_assert( int correct, const char *test )
326 {
327  if( correct )
328  return( 0 );
329 
330  test_errors++;
331  if( test_errors == 1 )
332  printf( "FAILED\n" );
333  printf( " %s\n", test );
334 
335  return( 1 );
336 }
337 
338 #define TEST_ASSERT( TEST ) \
339  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
340  if( test_errors) goto exit; \
341  } while (0)
342 
343 int verify_string( char **str )
344 {
345  if( (*str)[0] != '"' ||
346  (*str)[strlen( *str ) - 1] != '"' )
347  {
348  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
349  return( -1 );
350  }
351 
352  (*str)++;
353  (*str)[strlen( *str ) - 1] = '\0';
354 
355  return( 0 );
356 }
357 
358 int verify_int( char *str, int *value )
359 {
360  size_t i;
361  int minus = 0;
362  int digits = 1;
363  int hex = 0;
364 
365  for( i = 0; i < strlen( str ); i++ )
366  {
367  if( i == 0 && str[i] == '-' )
368  {
369  minus = 1;
370  continue;
371  }
372 
373  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
374  str[i - 1] == '0' && str[i] == 'x' )
375  {
376  hex = 1;
377  continue;
378  }
379 
380  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
381  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
382  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
383  {
384  digits = 0;
385  break;
386  }
387  }
388 
389  if( digits )
390  {
391  if( hex )
392  *value = strtol( str, NULL, 16 );
393  else
394  *value = strtol( str, NULL, 10 );
395 
396  return( 0 );
397  }
398 
399 #ifdef POLARSSL_RSA_C
400  if( strcmp( str, "POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
401  {
403  return( 0 );
404  }
405 #endif // POLARSSL_RSA_C
406 #ifdef POLARSSL_RSA_C
407 #ifdef POLARSSL_FS_IO
408  if( strcmp( str, "POLARSSL_ERR_PK_KEY_INVALID_FORMAT" ) == 0 )
409  {
411  return( 0 );
412  }
413 #endif // POLARSSL_RSA_C
414 #endif // POLARSSL_FS_IO
415 #ifdef POLARSSL_RSA_C
416 #ifdef POLARSSL_FS_IO
417  if( strcmp( str, "POLARSSL_ERR_PK_PASSWORD_REQUIRED" ) == 0 )
418  {
420  return( 0 );
421  }
422 #endif // POLARSSL_RSA_C
423 #endif // POLARSSL_FS_IO
424 #ifdef POLARSSL_RSA_C
425 #ifdef POLARSSL_FS_IO
426  if( strcmp( str, "POLARSSL_ERR_PK_PASSWORD_MISMATCH" ) == 0 )
427  {
429  return( 0 );
430  }
431 #endif // POLARSSL_RSA_C
432 #endif // POLARSSL_FS_IO
433 
434 
435  printf( "Expected integer for parameter and got: %s\n", str );
436  return( -1 );
437 }
438 
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 )
442 {
443  pk_context ctx;
444  int res;
445  char *pwd = password;
446 
447  pk_init( &ctx );
448 
449  if( strcmp( pwd, "NULL" ) == 0 )
450  pwd = NULL;
451 
452  res = pk_parse_keyfile( &ctx, key_file, pwd );
453 
454  TEST_ASSERT( res == result );
455 
456  if( res == 0 )
457  {
458  rsa_context *rsa;
460  rsa = pk_rsa( ctx );
461  TEST_ASSERT( rsa_check_privkey( rsa ) == 0 );
462  }
463 
464 exit:
465  pk_free( &ctx );
466 }
467 #endif /* POLARSSL_RSA_C */
468 #endif /* POLARSSL_FS_IO */
469 
470 #ifdef POLARSSL_RSA_C
471 #ifdef POLARSSL_FS_IO
472 void test_suite_pk_parse_public_keyfile_rsa( char *key_file, int result )
473 {
474  pk_context ctx;
475  int res;
476 
477  pk_init( &ctx );
478 
479  res = pk_parse_public_keyfile( &ctx, key_file );
480 
481  TEST_ASSERT( res == result );
482 
483  if( res == 0 )
484  {
485  rsa_context *rsa;
487  rsa = pk_rsa( ctx );
488  TEST_ASSERT( rsa_check_pubkey( rsa ) == 0 );
489  }
490 
491 exit:
492  pk_free( &ctx );
493 }
494 #endif /* POLARSSL_RSA_C */
495 #endif /* POLARSSL_FS_IO */
496 
497 #ifdef POLARSSL_FS_IO
498 #ifdef POLARSSL_ECP_C
499 void test_suite_pk_parse_public_keyfile_ec( char *key_file, int result )
500 {
501  pk_context ctx;
502  int res;
503 
504  pk_init( &ctx );
505 
506  res = pk_parse_public_keyfile( &ctx, key_file );
507 
508  TEST_ASSERT( res == result );
509 
510  if( res == 0 )
511  {
512  ecp_keypair *eckey;
514  eckey = pk_ec( ctx );
515  TEST_ASSERT( ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
516  }
517 
518 exit:
519  pk_free( &ctx );
520 }
521 #endif /* POLARSSL_FS_IO */
522 #endif /* POLARSSL_ECP_C */
523 
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 )
527 {
528  pk_context ctx;
529  int res;
530 
531  pk_init( &ctx );
532 
533  res = pk_parse_keyfile( &ctx, key_file, password );
534 
535  TEST_ASSERT( res == result );
536 
537  if( res == 0 )
538  {
539  ecp_keypair *eckey;
541  eckey = pk_ec( ctx );
542  TEST_ASSERT( ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
543  }
544 
545 exit:
546  pk_free( &ctx );
547 }
548 #endif /* POLARSSL_FS_IO */
549 #endif /* POLARSSL_ECP_C */
550 
551 #ifdef POLARSSL_RSA_C
552 void test_suite_pk_parse_key_rsa( char *key_data, char *result_str, int result )
553 {
554  pk_context pk;
555  unsigned char buf[2000];
556  unsigned char output[2000];
557  int data_len;
558  ((void) result_str);
559 
560  pk_init( &pk );
561 
562  memset( buf, 0, 2000 );
563  memset( output, 0, 2000 );
564 
565  data_len = unhexify( buf, key_data );
566 
567  TEST_ASSERT( pk_parse_key( &pk, buf, data_len, NULL, 0 ) == ( result ) );
568  if( ( result ) == 0 )
569  {
570  TEST_ASSERT( 1 );
571  }
572 
573 exit:
574  pk_free( &pk );
575 }
576 #endif /* POLARSSL_RSA_C */
577 
578 
579 #endif /* POLARSSL_PK_PARSE_C */
580 #endif /* POLARSSL_BIGNUM_C */
581 
582 
583 int dep_check( char *str )
584 {
585  if( str == NULL )
586  return( 1 );
587 
588  if( strcmp( str, "POLARSSL_ECP_DP_BP384R1_ENABLED" ) == 0 )
589  {
590 #if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
591  return( 0 );
592 #else
593  return( 1 );
594 #endif
595  }
596  if( strcmp( str, "POLARSSL_ECP_DP_BP256R1_ENABLED" ) == 0 )
597  {
598 #if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
599  return( 0 );
600 #else
601  return( 1 );
602 #endif
603  }
604  if( strcmp( str, "POLARSSL_PKCS5_C" ) == 0 )
605  {
606 #if defined(POLARSSL_PKCS5_C)
607  return( 0 );
608 #else
609  return( 1 );
610 #endif
611  }
612  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
613  {
614 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
615  return( 0 );
616 #else
617  return( 1 );
618 #endif
619  }
620  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
621  {
622 #if defined(POLARSSL_ECP_C)
623  return( 0 );
624 #else
625  return( 1 );
626 #endif
627  }
628  if( strcmp( str, "POLARSSL_ECP_DP_SECP256K1_ENABLED" ) == 0 )
629  {
630 #if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
631  return( 0 );
632 #else
633  return( 1 );
634 #endif
635  }
636  if( strcmp( str, "POLARSSL_ECP_DP_BP512R1_ENABLED" ) == 0 )
637  {
638 #if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
639  return( 0 );
640 #else
641  return( 1 );
642 #endif
643  }
644  if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
645  {
646 #if defined(POLARSSL_CIPHER_MODE_CBC)
647  return( 0 );
648 #else
649  return( 1 );
650 #endif
651  }
652  if( strcmp( str, "POLARSSL_PKCS12_C" ) == 0 )
653  {
654 #if defined(POLARSSL_PKCS12_C)
655  return( 0 );
656 #else
657  return( 1 );
658 #endif
659  }
660  if( strcmp( str, "POLARSSL_ECP_DP_SECP224R1_ENABLED" ) == 0 )
661  {
662 #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
663  return( 0 );
664 #else
665  return( 1 );
666 #endif
667  }
668  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
669  {
670 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
671  return( 0 );
672 #else
673  return( 1 );
674 #endif
675  }
676  if( strcmp( str, "POLARSSL_ECP_DP_SECP521R1_ENABLED" ) == 0 )
677  {
678 #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
679  return( 0 );
680 #else
681  return( 1 );
682 #endif
683  }
684  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
685  {
686 #if defined(POLARSSL_SHA1_C)
687  return( 0 );
688 #else
689  return( 1 );
690 #endif
691  }
692  if( strcmp( str, "POLARSSL_DES_C" ) == 0 )
693  {
694 #if defined(POLARSSL_DES_C)
695  return( 0 );
696 #else
697  return( 1 );
698 #endif
699  }
700  if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
701  {
702 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
703  return( 0 );
704 #else
705  return( 1 );
706 #endif
707  }
708  if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
709  {
710 #if defined(POLARSSL_MD5_C)
711  return( 0 );
712 #else
713  return( 1 );
714 #endif
715  }
716  if( strcmp( str, "POLARSSL_AES_C" ) == 0 )
717  {
718 #if defined(POLARSSL_AES_C)
719  return( 0 );
720 #else
721  return( 1 );
722 #endif
723  }
724  if( strcmp( str, "POLARSSL_ARC4_C" ) == 0 )
725  {
726 #if defined(POLARSSL_ARC4_C)
727  return( 0 );
728 #else
729  return( 1 );
730 #endif
731  }
732  if( strcmp( str, "POLARSSL_PK_PARSE_EC_EXTENDED" ) == 0 )
733  {
734 #if defined(POLARSSL_PK_PARSE_EC_EXTENDED)
735  return( 0 );
736 #else
737  return( 1 );
738 #endif
739  }
740  if( strcmp( str, "POLARSSL_PEM_PARSE_C" ) == 0 )
741  {
742 #if defined(POLARSSL_PEM_PARSE_C)
743  return( 0 );
744 #else
745  return( 1 );
746 #endif
747  }
748  if( strcmp( str, "POLARSSL_ECP_DP_SECP384R1_ENABLED" ) == 0 )
749  {
750 #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
751  return( 0 );
752 #else
753  return( 1 );
754 #endif
755  }
756 
757 
758  return( 1 );
759 }
760 
761 int dispatch_test(int cnt, char *params[50])
762 {
763  int ret;
764  ((void) cnt);
765  ((void) params);
766 
767 #if defined(TEST_SUITE_ACTIVE)
768  if( strcmp( params[0], "pk_parse_keyfile_rsa" ) == 0 )
769  {
770  #ifdef POLARSSL_RSA_C
771  #ifdef POLARSSL_FS_IO
772 
773  char *param1 = params[1];
774  char *param2 = params[2];
775  int param3;
776 
777  if( cnt != 4 )
778  {
779  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
780  return( 2 );
781  }
782 
783  if( verify_string( &param1 ) != 0 ) return( 2 );
784  if( verify_string( &param2 ) != 0 ) return( 2 );
785  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
786 
787  test_suite_pk_parse_keyfile_rsa( param1, param2, param3 );
788  return ( 0 );
789  #endif /* POLARSSL_RSA_C */
790  #endif /* POLARSSL_FS_IO */
791 
792  return ( 3 );
793  }
794  else
795  if( strcmp( params[0], "pk_parse_public_keyfile_rsa" ) == 0 )
796  {
797  #ifdef POLARSSL_RSA_C
798  #ifdef POLARSSL_FS_IO
799 
800  char *param1 = params[1];
801  int param2;
802 
803  if( cnt != 3 )
804  {
805  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
806  return( 2 );
807  }
808 
809  if( verify_string( &param1 ) != 0 ) return( 2 );
810  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
811 
812  test_suite_pk_parse_public_keyfile_rsa( param1, param2 );
813  return ( 0 );
814  #endif /* POLARSSL_RSA_C */
815  #endif /* POLARSSL_FS_IO */
816 
817  return ( 3 );
818  }
819  else
820  if( strcmp( params[0], "pk_parse_public_keyfile_ec" ) == 0 )
821  {
822  #ifdef POLARSSL_FS_IO
823  #ifdef POLARSSL_ECP_C
824 
825  char *param1 = params[1];
826  int param2;
827 
828  if( cnt != 3 )
829  {
830  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
831  return( 2 );
832  }
833 
834  if( verify_string( &param1 ) != 0 ) return( 2 );
835  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
836 
837  test_suite_pk_parse_public_keyfile_ec( param1, param2 );
838  return ( 0 );
839  #endif /* POLARSSL_FS_IO */
840  #endif /* POLARSSL_ECP_C */
841 
842  return ( 3 );
843  }
844  else
845  if( strcmp( params[0], "pk_parse_keyfile_ec" ) == 0 )
846  {
847  #ifdef POLARSSL_FS_IO
848  #ifdef POLARSSL_ECP_C
849 
850  char *param1 = params[1];
851  char *param2 = params[2];
852  int param3;
853 
854  if( cnt != 4 )
855  {
856  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
857  return( 2 );
858  }
859 
860  if( verify_string( &param1 ) != 0 ) return( 2 );
861  if( verify_string( &param2 ) != 0 ) return( 2 );
862  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
863 
864  test_suite_pk_parse_keyfile_ec( param1, param2, param3 );
865  return ( 0 );
866  #endif /* POLARSSL_FS_IO */
867  #endif /* POLARSSL_ECP_C */
868 
869  return ( 3 );
870  }
871  else
872  if( strcmp( params[0], "pk_parse_key_rsa" ) == 0 )
873  {
874  #ifdef POLARSSL_RSA_C
875 
876  char *param1 = params[1];
877  char *param2 = params[2];
878  int param3;
879 
880  if( cnt != 4 )
881  {
882  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
883  return( 2 );
884  }
885 
886  if( verify_string( &param1 ) != 0 ) return( 2 );
887  if( verify_string( &param2 ) != 0 ) return( 2 );
888  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
889 
890  test_suite_pk_parse_key_rsa( param1, param2, param3 );
891  return ( 0 );
892  #endif /* POLARSSL_RSA_C */
893 
894  return ( 3 );
895  }
896  else
897 
898  {
899  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
900  fflush( stdout );
901  return( 1 );
902  }
903 #else
904  return( 3 );
905 #endif
906  return( ret );
907 }
908 
909 int get_line( FILE *f, char *buf, size_t len )
910 {
911  char *ret;
912 
913  ret = fgets( buf, len, f );
914  if( ret == NULL )
915  return( -1 );
916 
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';
921 
922  return( 0 );
923 }
924 
925 int parse_arguments( char *buf, size_t len, char *params[50] )
926 {
927  int cnt = 0, i;
928  char *cur = buf;
929  char *p = buf, *q;
930 
931  params[cnt++] = cur;
932 
933  while( *p != '\0' && p < buf + len )
934  {
935  if( *p == '\\' )
936  {
937  p++;
938  p++;
939  continue;
940  }
941  if( *p == ':' )
942  {
943  if( p + 1 < buf + len )
944  {
945  cur = p + 1;
946  params[cnt++] = cur;
947  }
948  *p = '\0';
949  }
950 
951  p++;
952  }
953 
954  // Replace newlines, question marks and colons in strings
955  for( i = 0; i < cnt; i++ )
956  {
957  p = params[i];
958  q = params[i];
959 
960  while( *p != '\0' )
961  {
962  if( *p == '\\' && *(p + 1) == 'n' )
963  {
964  p += 2;
965  *(q++) = '\n';
966  }
967  else if( *p == '\\' && *(p + 1) == ':' )
968  {
969  p += 2;
970  *(q++) = ':';
971  }
972  else if( *p == '\\' && *(p + 1) == '?' )
973  {
974  p += 2;
975  *(q++) = '?';
976  }
977  else
978  *(q++) = *(p++);
979  }
980  *q = '\0';
981  }
982 
983  return( cnt );
984 }
985 
986 int main()
987 {
988  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
989  const char *filename = "/tmp/B.1c06a028-9709-4951-a65b-b24142b09746/BUILD/polarssl-1.3.9/tests/suites/test_suite_pkparse.data";
990  FILE *file;
991  char buf[5000];
992  char *params[50];
993 
994 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
995  unsigned char alloc_buf[1000000];
996  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
997 #endif
998 
999  file = fopen( filename, "r" );
1000  if( file == NULL )
1001  {
1002  fprintf( stderr, "Failed to open\n" );
1003  return( 1 );
1004  }
1005 
1006  while( !feof( file ) )
1007  {
1008  int skip = 0;
1009 
1010  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1011  break;
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, " " );
1017  fflush( stdout );
1018 
1019  total_tests++;
1020 
1021  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1022  break;
1023  cnt = parse_arguments( buf, strlen(buf), params );
1024 
1025  if( strcmp( params[0], "depends_on" ) == 0 )
1026  {
1027  for( i = 1; i < cnt; i++ )
1028  if( dep_check( params[i] ) != 0 )
1029  skip = 1;
1030 
1031  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1032  break;
1033  cnt = parse_arguments( buf, strlen(buf), params );
1034  }
1035 
1036  if( skip == 0 )
1037  {
1038  test_errors = 0;
1039  ret = dispatch_test( cnt, params );
1040  }
1041 
1042  if( skip == 1 || ret == 3 )
1043  {
1044  total_skipped++;
1045  fprintf( stdout, "----\n" );
1046  fflush( stdout );
1047  }
1048  else if( ret == 0 && test_errors == 0 )
1049  {
1050  fprintf( stdout, "PASS\n" );
1051  fflush( stdout );
1052  }
1053  else if( ret == 2 )
1054  {
1055  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1056  fclose(file);
1057  exit( 2 );
1058  }
1059  else
1060  total_errors++;
1061 
1062  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1063  break;
1064  if( strlen(buf) != 0 )
1065  {
1066  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1067  return( 1 );
1068  }
1069  }
1070  fclose(file);
1071 
1072  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1073  if( total_errors == 0 )
1074  fprintf( stdout, "PASSED" );
1075  else
1076  fprintf( stdout, "FAILED" );
1077 
1078  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1079  total_tests - total_errors, total_tests, total_skipped );
1080 
1081 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1082 #if defined(POLARSSL_MEMORY_DEBUG)
1083  memory_buffer_alloc_status();
1084 #endif
1086 #endif
1087 
1088  return( total_errors != 0 );
1089 }
1090 
1091 
#define POLARSSL_ERR_PK_KEY_INVALID_FORMAT
Invalid key tag or value.
Definition: pk.h:56
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 dep_check(char *str)
#define polarssl_malloc
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.
ecp_group grp
Definition: ecp.h:165
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
ECP key pair structure.
Definition: ecp.h:163
mpi d
Definition: ecp.h:166
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
PolarSSL Platform abstraction layer.
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
Definition: pk.h:84
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)
RSA context structure.
Definition: rsa.h:83
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.
Definition: pk.h:58
#define POLARSSL_ERR_PK_PASSWORD_MISMATCH
Given private key password does not allow for correct decryption.
Definition: pk.h:59
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
int main()
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)
static int test_errors
int verify_string(char **str)
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
Definition: pk.h:74
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)
unsigned char * buf
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.
ecp_point Q
Definition: ecp.h:167
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.
Public key container.
Definition: pk.h:194