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