PolarSSL v1.3.1
test_suite_x509parse.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_BIGNUM_C
4 
5 #include <polarssl/x509_crt.h>
6 #include <polarssl/x509_crl.h>
7 #include <polarssl/pem.h>
8 #include <polarssl/oid.h>
9 
10 int verify_none( void *data, x509_crt *crt, int certificate_depth, int *flags )
11 {
12  ((void) data);
13  ((void) crt);
14  ((void) certificate_depth);
15  *flags |= BADCERT_OTHER;
16 
17  return 0;
18 }
19 
20 int verify_all( void *data, x509_crt *crt, int certificate_depth, int *flags )
21 {
22  ((void) data);
23  ((void) crt);
24  ((void) certificate_depth);
25  *flags = 0;
26 
27  return 0;
28 }
29 
30 #endif /* POLARSSL_BIGNUM_C */
31 
32 
33 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
34 #include "polarssl/memory.h"
35 #endif
36 
37 #ifdef _MSC_VER
38 #include <basetsd.h>
39 typedef UINT32 uint32_t;
40 #else
41 #include <inttypes.h>
42 #endif
43 
44 #include <assert.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 /*
49  * 32-bit integer manipulation macros (big endian)
50  */
51 #ifndef GET_UINT32_BE
52 #define GET_UINT32_BE(n,b,i) \
53 { \
54  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
55  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
56  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
57  | ( (uint32_t) (b)[(i) + 3] ); \
58 }
59 #endif
60 
61 #ifndef PUT_UINT32_BE
62 #define PUT_UINT32_BE(n,b,i) \
63 { \
64  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
65  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
66  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
67  (b)[(i) + 3] = (unsigned char) ( (n) ); \
68 }
69 #endif
70 
71 static int unhexify(unsigned char *obuf, const char *ibuf)
72 {
73  unsigned char c, c2;
74  int len = strlen(ibuf) / 2;
75  assert(!(strlen(ibuf) %1)); // must be even number of bytes
76 
77  while (*ibuf != 0)
78  {
79  c = *ibuf++;
80  if( c >= '0' && c <= '9' )
81  c -= '0';
82  else if( c >= 'a' && c <= 'f' )
83  c -= 'a' - 10;
84  else if( c >= 'A' && c <= 'F' )
85  c -= 'A' - 10;
86  else
87  assert( 0 );
88 
89  c2 = *ibuf++;
90  if( c2 >= '0' && c2 <= '9' )
91  c2 -= '0';
92  else if( c2 >= 'a' && c2 <= 'f' )
93  c2 -= 'a' - 10;
94  else if( c2 >= 'A' && c2 <= 'F' )
95  c2 -= 'A' - 10;
96  else
97  assert( 0 );
98 
99  *obuf++ = ( c << 4 ) | c2;
100  }
101 
102  return len;
103 }
104 
105 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
106 {
107  unsigned char l, h;
108 
109  while (len != 0)
110  {
111  h = (*ibuf) / 16;
112  l = (*ibuf) % 16;
113 
114  if( h < 10 )
115  *obuf++ = '0' + h;
116  else
117  *obuf++ = 'a' + h - 10;
118 
119  if( l < 10 )
120  *obuf++ = '0' + l;
121  else
122  *obuf++ = 'a' + l - 10;
123 
124  ++ibuf;
125  len--;
126  }
127 }
128 
138 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
139 {
140  size_t i;
141 
142  if( rng_state != NULL )
143  rng_state = NULL;
144 
145  for( i = 0; i < len; ++i )
146  output[i] = rand();
147 
148  return( 0 );
149 }
150 
156 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
157 {
158  if( rng_state != NULL )
159  rng_state = NULL;
160 
161  memset( output, 0, len );
162 
163  return( 0 );
164 }
165 
166 typedef struct
167 {
168  unsigned char *buf;
169  size_t length;
170 } rnd_buf_info;
171 
183 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
184 {
185  rnd_buf_info *info = (rnd_buf_info *) rng_state;
186  size_t use_len;
187 
188  if( rng_state == NULL )
189  return( rnd_std_rand( NULL, output, len ) );
190 
191  use_len = len;
192  if( len > info->length )
193  use_len = info->length;
194 
195  if( use_len )
196  {
197  memcpy( output, info->buf, use_len );
198  info->buf += use_len;
199  info->length -= use_len;
200  }
201 
202  if( len - use_len > 0 )
203  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
204 
205  return( 0 );
206 }
207 
215 typedef struct
216 {
217  uint32_t key[16];
218  uint32_t v0, v1;
220 
229 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
230 {
231  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
232  uint32_t i, *k, sum, delta=0x9E3779B9;
233  unsigned char result[4];
234 
235  if( rng_state == NULL )
236  return( rnd_std_rand( NULL, output, len ) );
237 
238  k = info->key;
239 
240  while( len > 0 )
241  {
242  size_t use_len = ( len > 4 ) ? 4 : len;
243  sum = 0;
244 
245  for( i = 0; i < 32; i++ )
246  {
247  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
248  sum += delta;
249  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
250  }
251 
252  PUT_UINT32_BE( info->v0, result, 0 );
253  memcpy( output, result, use_len );
254  len -= use_len;
255  }
256 
257  return( 0 );
258 }
259 
269 static int not_rnd( void *in, unsigned char *out, size_t len )
270 {
271  unsigned char *obuf;
272  const char *ibuf = in;
273  unsigned char c, c2;
274  assert( len == strlen(ibuf) / 2 );
275  assert(!(strlen(ibuf) %1)); // must be even number of bytes
276 
277  obuf = out + (len - 1); // sic
278  while (*ibuf != 0)
279  {
280  c = *ibuf++;
281  if( c >= '0' && c <= '9' )
282  c -= '0';
283  else if( c >= 'a' && c <= 'f' )
284  c -= 'a' - 10;
285  else if( c >= 'A' && c <= 'F' )
286  c -= 'A' - 10;
287  else
288  assert( 0 );
289 
290  c2 = *ibuf++;
291  if( c2 >= '0' && c2 <= '9' )
292  c2 -= '0';
293  else if( c2 >= 'a' && c2 <= 'f' )
294  c2 -= 'a' - 10;
295  else if( c2 >= 'A' && c2 <= 'F' )
296  c2 -= 'A' - 10;
297  else
298  assert( 0 );
299 
300  *obuf-- = ( c << 4 ) | c2; // sic
301  }
302 
303  return( 0 );
304 }
305 
306 
307 #include <stdio.h>
308 #include <string.h>
309 
310 static int test_errors = 0;
311 
312 #ifdef POLARSSL_BIGNUM_C
313 
314 #define TEST_SUITE_ACTIVE
315 
316 static int test_assert( int correct, char *test )
317 {
318  if( correct )
319  return( 0 );
320 
321  test_errors++;
322  if( test_errors == 1 )
323  printf( "FAILED\n" );
324  printf( " %s\n", test );
325 
326  return( 1 );
327 }
328 
329 #define TEST_ASSERT( TEST ) \
330  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
331  if( test_errors) return; \
332  } while (0)
333 
334 int verify_string( char **str )
335 {
336  if( (*str)[0] != '"' ||
337  (*str)[strlen( *str ) - 1] != '"' )
338  {
339  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
340  return( -1 );
341  }
342 
343  (*str)++;
344  (*str)[strlen( *str ) - 1] = '\0';
345 
346  return( 0 );
347 }
348 
349 int verify_int( char *str, int *value )
350 {
351  size_t i;
352  int minus = 0;
353  int digits = 1;
354  int hex = 0;
355 
356  for( i = 0; i < strlen( str ); i++ )
357  {
358  if( i == 0 && str[i] == '-' )
359  {
360  minus = 1;
361  continue;
362  }
363 
364  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
365  str[i - 1] == '0' && str[i] == 'x' )
366  {
367  hex = 1;
368  continue;
369  }
370 
371  if( str[i] < '0' || str[i] > '9' )
372  {
373  digits = 0;
374  break;
375  }
376  }
377 
378  if( digits )
379  {
380  if( hex )
381  *value = strtol( str, NULL, 16 );
382  else
383  *value = strtol( str, NULL, 10 );
384 
385  return( 0 );
386  }
387 
388  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
389  {
391  return( 0 );
392  }
393  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
394  {
396  return( 0 );
397  }
398  if( strcmp( str, "POLARSSL_ERR_X509_CERT_VERIFY_FAILED" ) == 0 )
399  {
401  return( 0 );
402  }
403  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
404  {
406  return( 0 );
407  }
408  if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_VERSION" ) == 0 )
409  {
411  return( 0 );
412  }
413  if( strcmp( str, "POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
414  {
416  return( 0 );
417  }
418  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
419  {
421  return( 0 );
422  }
423  if( strcmp( str, "BADCERT_REVOKED | BADCERT_CN_MISMATCH" ) == 0 )
424  {
425  *value = ( BADCERT_REVOKED | BADCERT_CN_MISMATCH );
426  return( 0 );
427  }
428  if( strcmp( str, "POLARSSL_ERR_PK_UNKNOWN_PK_ALG" ) == 0 )
429  {
430  *value = ( POLARSSL_ERR_PK_UNKNOWN_PK_ALG );
431  return( 0 );
432  }
433  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
434  {
436  return( 0 );
437  }
438  if( strcmp( str, "POLARSSL_ERR_X509_SIG_MISMATCH" ) == 0 )
439  {
440  *value = ( POLARSSL_ERR_X509_SIG_MISMATCH );
441  return( 0 );
442  }
443  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
444  {
446  return( 0 );
447  }
448  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
449  {
451  return( 0 );
452  }
453  if( strcmp( str, "POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
454  {
455  *value = ( POLARSSL_ERR_ASN1_OUT_OF_DATA );
456  return( 0 );
457  }
458  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA " ) == 0 )
459  {
461  return( 0 );
462  }
463  if( strcmp( str, "BADCERT_CN_MISMATCH" ) == 0 )
464  {
465  *value = ( BADCERT_CN_MISMATCH );
466  return( 0 );
467  }
468  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
469  {
471  return( 0 );
472  }
473  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
474  {
476  return( 0 );
477  }
478  if( strcmp( str, "BADCERT_NOT_TRUSTED" ) == 0 )
479  {
480  *value = ( BADCERT_NOT_TRUSTED );
481  return( 0 );
482  }
483  if( strcmp( str, "POLARSSL_ERR_PK_KEY_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
484  {
486  return( 0 );
487  }
488  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
489  {
491  return( 0 );
492  }
493  if( strcmp( str, " 1" ) == 0 )
494  {
495  *value = ( 1 );
496  return( 0 );
497  }
498  if( strcmp( str, "BADCRL_EXPIRED" ) == 0 )
499  {
500  *value = ( BADCRL_EXPIRED );
501  return( 0 );
502  }
503  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
504  {
506  return( 0 );
507  }
508  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
509  {
511  return( 0 );
512  }
513  if( strcmp( str, "BADCERT_REVOKED | BADCRL_EXPIRED" ) == 0 )
514  {
515  *value = ( BADCERT_REVOKED | BADCRL_EXPIRED );
516  return( 0 );
517  }
518  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
519  {
521  return( 0 );
522  }
523  if( strcmp( str, "BADCERT_REVOKED" ) == 0 )
524  {
525  *value = ( BADCERT_REVOKED );
526  return( 0 );
527  }
528  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
529  {
531  return( 0 );
532  }
533  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
534  {
536  return( 0 );
537  }
538  if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + POLARSSL_ERR_OID_NOT_FOUND" ) == 0 )
539  {
541  return( 0 );
542  }
543  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_INVALID_DATA" ) == 0 )
544  {
546  return( 0 );
547  }
548  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
549  {
551  return( 0 );
552  }
553  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
554  {
556  return( 0 );
557  }
558  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
559  {
561  return( 0 );
562  }
563  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
564  {
566  return( 0 );
567  }
568  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE" ) == 0 )
569  {
570  *value = ( POLARSSL_ERR_X509_INVALID_DATE );
571  return( 0 );
572  }
573  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
574  {
576  return( 0 );
577  }
578  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
579  {
581  return( 0 );
582  }
583  if( strcmp( str, "BADCERT_OTHER" ) == 0 )
584  {
585  *value = ( BADCERT_OTHER );
586  return( 0 );
587  }
588  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT" ) == 0 )
589  {
591  return( 0 );
592  }
593  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
594  {
596  return( 0 );
597  }
598  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_INVALID_DATA" ) == 0 )
599  {
601  return( 0 );
602  }
603  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
604  {
606  return( 0 );
607  }
608  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
609  {
611  return( 0 );
612  }
613  if( strcmp( str, "BADCERT_REVOKED | BADCRL_EXPIRED | BADCERT_CN_MISMATCH" ) == 0 )
614  {
616  return( 0 );
617  }
618  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
619  {
621  return( 0 );
622  }
623  if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_SIG_ALG" ) == 0 )
624  {
626  return( 0 );
627  }
628  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
629  {
631  return( 0 );
632  }
633  if( strcmp( str, "BADCERT_CN_MISMATCH + BADCERT_NOT_TRUSTED" ) == 0 )
634  {
636  return( 0 );
637  }
638  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY" ) == 0 )
639  {
640  *value = ( POLARSSL_ERR_PK_INVALID_PUBKEY );
641  return( 0 );
642  }
643 
644 
645  printf( "Expected integer for parameter and got: %s\n", str );
646  return( -1 );
647 }
648 
649 #ifdef POLARSSL_FS_IO
650 #ifdef POLARSSL_X509_CRT_PARSE_C
651 void test_suite_x509_cert_info( char *crt_file, char *result_str )
652 {
653  x509_crt crt;
654  char buf[2000];
655  int res;
656 
657  x509_crt_init( &crt );
658  memset( buf, 0, 2000 );
659 
660  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
661  res = x509_crt_info( buf, 2000, "", &crt );
662 
663  x509_crt_free( &crt );
664 
665  TEST_ASSERT( res != -1 );
666  TEST_ASSERT( res != -2 );
667 
668  TEST_ASSERT( strcmp( buf, result_str ) == 0 );
669 }
670 #endif /* POLARSSL_FS_IO */
671 #endif /* POLARSSL_X509_CRT_PARSE_C */
672 
673 #ifdef POLARSSL_FS_IO
674 #ifdef POLARSSL_X509_CRL_PARSE_C
675 void test_suite_x509_crl_info( char *crl_file, char *result_str )
676 {
677  x509_crl crl;
678  char buf[2000];
679  int res;
680 
681  x509_crl_init( &crl );
682  memset( buf, 0, 2000 );
683 
684  TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
685  res = x509_crl_info( buf, 2000, "", &crl );
686 
687  x509_crl_free( &crl );
688 
689  TEST_ASSERT( res != -1 );
690  TEST_ASSERT( res != -2 );
691 
692  TEST_ASSERT( strcmp( buf, result_str ) == 0 );
693 }
694 #endif /* POLARSSL_FS_IO */
695 #endif /* POLARSSL_X509_CRL_PARSE_C */
696 
697 #ifdef POLARSSL_FS_IO
698 #ifdef POLARSSL_X509_CRT_PARSE_C
699 #ifdef POLARSSL_X509_CRL_PARSE_C
700 void test_suite_x509_verify( char *crt_file, char *ca_file, char *crl_file,
701  char *cn_name_str, int result, int flags_result,
702  char *verify_callback )
703 {
704  x509_crt crt;
705  x509_crt ca;
706  x509_crl crl;
707  int flags = 0;
708  int res;
709  int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
710  char * cn_name = NULL;
711 
712  x509_crt_init( &crt );
713  x509_crt_init( &ca );
714  x509_crl_init( &crl );
715 
716  if( strcmp( cn_name_str, "NULL" ) != 0 )
717  cn_name = cn_name_str;
718 
719  if( strcmp( verify_callback, "NULL" ) == 0 )
720  f_vrfy = NULL;
721  else if( strcmp( verify_callback, "verify_none" ) == 0 )
722  f_vrfy = verify_none;
723  else if( strcmp( verify_callback, "verify_all" ) == 0 )
724  f_vrfy = verify_all;
725  else
726  TEST_ASSERT( "No known verify callback selected" == 0 );
727 
728  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
729  TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
730  TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
731 
732  res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
733 
734  x509_crt_free( &crt );
735  x509_crt_free( &ca );
736  x509_crl_free( &crl );
737 
738  TEST_ASSERT( res == ( result ) );
739  TEST_ASSERT( flags == ( flags_result ) );
740 }
741 #endif /* POLARSSL_FS_IO */
742 #endif /* POLARSSL_X509_CRT_PARSE_C */
743 #endif /* POLARSSL_X509_CRL_PARSE_C */
744 
745 #ifdef POLARSSL_FS_IO
746 #ifdef POLARSSL_X509_USE_C
747 void test_suite_x509_dn_gets( char *crt_file, char *entity, char *result_str )
748 {
749  x509_crt crt;
750  char buf[2000];
751  int res = 0;
752 
753  x509_crt_init( &crt );
754  memset( buf, 0, 2000 );
755 
756  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
757  if( strcmp( entity, "subject" ) == 0 )
758  res = x509_dn_gets( buf, 2000, &crt.subject );
759  else if( strcmp( entity, "issuer" ) == 0 )
760  res = x509_dn_gets( buf, 2000, &crt.issuer );
761  else
762  TEST_ASSERT( "Unknown entity" == 0 );
763 
764  x509_crt_free( &crt );
765 
766  TEST_ASSERT( res != -1 );
767  TEST_ASSERT( res != -2 );
768 
769  TEST_ASSERT( strcmp( buf, result_str ) == 0 );
770 }
771 #endif /* POLARSSL_FS_IO */
772 #endif /* POLARSSL_X509_USE_C */
773 
774 #ifdef POLARSSL_FS_IO
775 #ifdef POLARSSL_X509_USE_C
776 void test_suite_x509_time_expired( char *crt_file, char *entity, int result )
777 {
778  x509_crt crt;
779 
780  x509_crt_init( &crt );
781 
782  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
783 
784  if( strcmp( entity, "valid_from" ) == 0 )
785  TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
786  else if( strcmp( entity, "valid_to" ) == 0 )
787  TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
788  else
789  TEST_ASSERT( "Unknown entity" == 0 );
790 
791  x509_crt_free( &crt );
792 }
793 #endif /* POLARSSL_FS_IO */
794 #endif /* POLARSSL_X509_USE_C */
795 
796 #ifdef POLARSSL_X509_CRT_PARSE_C
797 void test_suite_x509parse_crt( char *crt_data, char *result_str, int result )
798 {
799  x509_crt crt;
800  unsigned char buf[2000];
801  unsigned char output[2000];
802  int data_len, res;
803 
804  x509_crt_init( &crt );
805  memset( buf, 0, 2000 );
806  memset( output, 0, 2000 );
807 
808  data_len = unhexify( buf, crt_data );
809 
810  TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
811  if( ( result ) == 0 )
812  {
813  res = x509_crt_info( (char *) output, 2000, "", &crt );
814 
815  TEST_ASSERT( res != -1 );
816  TEST_ASSERT( res != -2 );
817 
818  TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
819  }
820 
821  x509_crt_free( &crt );
822 }
823 #endif /* POLARSSL_X509_CRT_PARSE_C */
824 
825 #ifdef POLARSSL_X509_CRL_PARSE_C
826 void test_suite_x509parse_crl( char *crl_data, char *result_str, int result )
827 {
828  x509_crl crl;
829  unsigned char buf[2000];
830  unsigned char output[2000];
831  int data_len, res;
832 
833  x509_crl_init( &crl );
834  memset( buf, 0, 2000 );
835  memset( output, 0, 2000 );
836 
837  data_len = unhexify( buf, crl_data );
838 
839  TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
840  if( ( result ) == 0 )
841  {
842  res = x509_crl_info( (char *) output, 2000, "", &crl );
843 
844  TEST_ASSERT( res != -1 );
845  TEST_ASSERT( res != -2 );
846 
847  TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
848  }
849 
850  x509_crl_free( &crl );
851 }
852 #endif /* POLARSSL_X509_CRL_PARSE_C */
853 
854 #ifdef POLARSSL_X509_CRT_PARSE_C
855 #ifdef POLARSSL_SELF_TEST
856 void test_suite_x509_selftest()
857 {
858  TEST_ASSERT( x509_self_test( 0 ) == 0 );
859 }
860 #endif /* POLARSSL_X509_CRT_PARSE_C */
861 #endif /* POLARSSL_SELF_TEST */
862 
863 
864 #endif /* POLARSSL_BIGNUM_C */
865 
866 
867 int dep_check( char *str )
868 {
869  if( str == NULL )
870  return( 1 );
871 
872  if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
873  {
874 #if defined(POLARSSL_MD4_C)
875  return( 0 );
876 #else
877  return( 1 );
878 #endif
879  }
880  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
881  {
882 #if defined(POLARSSL_SHA1_C)
883  return( 0 );
884 #else
885  return( 1 );
886 #endif
887  }
888  if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
889  {
890 #if defined(POLARSSL_PKCS1_V15)
891  return( 0 );
892 #else
893  return( 1 );
894 #endif
895  }
896  if( strcmp( str, "POLARSSL_PEM_PARSE_C" ) == 0 )
897  {
898 #if defined(POLARSSL_PEM_PARSE_C)
899  return( 0 );
900 #else
901  return( 1 );
902 #endif
903  }
904  if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
905  {
906 #if defined(POLARSSL_MD5_C)
907  return( 0 );
908 #else
909  return( 1 );
910 #endif
911  }
912  if( strcmp( str, "POLARSSL_ECDSA_C" ) == 0 )
913  {
914 #if defined(POLARSSL_ECDSA_C)
915  return( 0 );
916 #else
917  return( 1 );
918 #endif
919  }
920  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
921  {
922 #if defined(POLARSSL_SHA256_C)
923  return( 0 );
924 #else
925  return( 1 );
926 #endif
927  }
928  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
929  {
930 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
931  return( 0 );
932 #else
933  return( 1 );
934 #endif
935  }
936  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
937  {
938 #if defined(POLARSSL_ECP_C)
939  return( 0 );
940 #else
941  return( 1 );
942 #endif
943  }
944  if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
945  {
946 #if defined(POLARSSL_RSA_C)
947  return( 0 );
948 #else
949  return( 1 );
950 #endif
951  }
952  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
953  {
954 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
955  return( 0 );
956 #else
957  return( 1 );
958 #endif
959  }
960  if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
961  {
962 #if defined(POLARSSL_SHA512_C)
963  return( 0 );
964 #else
965  return( 1 );
966 #endif
967  }
968  if( strcmp( str, "POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3" ) == 0 )
969  {
970 #if defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
971  return( 0 );
972 #else
973  return( 1 );
974 #endif
975  }
976 
977 
978  return( 1 );
979 }
980 
981 int dispatch_test(int cnt, char *params[50])
982 {
983  int ret;
984  ((void) cnt);
985  ((void) params);
986 
987 #if defined(TEST_SUITE_ACTIVE)
988  if( strcmp( params[0], "x509_cert_info" ) == 0 )
989  {
990  #ifdef POLARSSL_FS_IO
991  #ifdef POLARSSL_X509_CRT_PARSE_C
992 
993  char *param1 = params[1];
994  char *param2 = params[2];
995 
996  if( cnt != 3 )
997  {
998  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
999  return( 2 );
1000  }
1001 
1002  if( verify_string( &param1 ) != 0 ) return( 2 );
1003  if( verify_string( &param2 ) != 0 ) return( 2 );
1004 
1005  test_suite_x509_cert_info( param1, param2 );
1006  return ( 0 );
1007  #endif /* POLARSSL_FS_IO */
1008  #endif /* POLARSSL_X509_CRT_PARSE_C */
1009 
1010  return ( 3 );
1011  }
1012  else
1013  if( strcmp( params[0], "x509_crl_info" ) == 0 )
1014  {
1015  #ifdef POLARSSL_FS_IO
1016  #ifdef POLARSSL_X509_CRL_PARSE_C
1017 
1018  char *param1 = params[1];
1019  char *param2 = params[2];
1020 
1021  if( cnt != 3 )
1022  {
1023  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1024  return( 2 );
1025  }
1026 
1027  if( verify_string( &param1 ) != 0 ) return( 2 );
1028  if( verify_string( &param2 ) != 0 ) return( 2 );
1029 
1030  test_suite_x509_crl_info( param1, param2 );
1031  return ( 0 );
1032  #endif /* POLARSSL_FS_IO */
1033  #endif /* POLARSSL_X509_CRL_PARSE_C */
1034 
1035  return ( 3 );
1036  }
1037  else
1038  if( strcmp( params[0], "x509_verify" ) == 0 )
1039  {
1040  #ifdef POLARSSL_FS_IO
1041  #ifdef POLARSSL_X509_CRT_PARSE_C
1042  #ifdef POLARSSL_X509_CRL_PARSE_C
1043 
1044  char *param1 = params[1];
1045  char *param2 = params[2];
1046  char *param3 = params[3];
1047  char *param4 = params[4];
1048  int param5;
1049  int param6;
1050  char *param7 = params[7];
1051 
1052  if( cnt != 8 )
1053  {
1054  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1055  return( 2 );
1056  }
1057 
1058  if( verify_string( &param1 ) != 0 ) return( 2 );
1059  if( verify_string( &param2 ) != 0 ) return( 2 );
1060  if( verify_string( &param3 ) != 0 ) return( 2 );
1061  if( verify_string( &param4 ) != 0 ) return( 2 );
1062  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1063  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1064  if( verify_string( &param7 ) != 0 ) return( 2 );
1065 
1066  test_suite_x509_verify( param1, param2, param3, param4, param5, param6, param7 );
1067  return ( 0 );
1068  #endif /* POLARSSL_FS_IO */
1069  #endif /* POLARSSL_X509_CRT_PARSE_C */
1070  #endif /* POLARSSL_X509_CRL_PARSE_C */
1071 
1072  return ( 3 );
1073  }
1074  else
1075  if( strcmp( params[0], "x509_dn_gets" ) == 0 )
1076  {
1077  #ifdef POLARSSL_FS_IO
1078  #ifdef POLARSSL_X509_USE_C
1079 
1080  char *param1 = params[1];
1081  char *param2 = params[2];
1082  char *param3 = params[3];
1083 
1084  if( cnt != 4 )
1085  {
1086  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1087  return( 2 );
1088  }
1089 
1090  if( verify_string( &param1 ) != 0 ) return( 2 );
1091  if( verify_string( &param2 ) != 0 ) return( 2 );
1092  if( verify_string( &param3 ) != 0 ) return( 2 );
1093 
1094  test_suite_x509_dn_gets( param1, param2, param3 );
1095  return ( 0 );
1096  #endif /* POLARSSL_FS_IO */
1097  #endif /* POLARSSL_X509_USE_C */
1098 
1099  return ( 3 );
1100  }
1101  else
1102  if( strcmp( params[0], "x509_time_expired" ) == 0 )
1103  {
1104  #ifdef POLARSSL_FS_IO
1105  #ifdef POLARSSL_X509_USE_C
1106 
1107  char *param1 = params[1];
1108  char *param2 = params[2];
1109  int param3;
1110 
1111  if( cnt != 4 )
1112  {
1113  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1114  return( 2 );
1115  }
1116 
1117  if( verify_string( &param1 ) != 0 ) return( 2 );
1118  if( verify_string( &param2 ) != 0 ) return( 2 );
1119  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1120 
1121  test_suite_x509_time_expired( param1, param2, param3 );
1122  return ( 0 );
1123  #endif /* POLARSSL_FS_IO */
1124  #endif /* POLARSSL_X509_USE_C */
1125 
1126  return ( 3 );
1127  }
1128  else
1129  if( strcmp( params[0], "x509parse_crt" ) == 0 )
1130  {
1131  #ifdef POLARSSL_X509_CRT_PARSE_C
1132 
1133  char *param1 = params[1];
1134  char *param2 = params[2];
1135  int param3;
1136 
1137  if( cnt != 4 )
1138  {
1139  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1140  return( 2 );
1141  }
1142 
1143  if( verify_string( &param1 ) != 0 ) return( 2 );
1144  if( verify_string( &param2 ) != 0 ) return( 2 );
1145  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1146 
1147  test_suite_x509parse_crt( param1, param2, param3 );
1148  return ( 0 );
1149  #endif /* POLARSSL_X509_CRT_PARSE_C */
1150 
1151  return ( 3 );
1152  }
1153  else
1154  if( strcmp( params[0], "x509parse_crl" ) == 0 )
1155  {
1156  #ifdef POLARSSL_X509_CRL_PARSE_C
1157 
1158  char *param1 = params[1];
1159  char *param2 = params[2];
1160  int param3;
1161 
1162  if( cnt != 4 )
1163  {
1164  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1165  return( 2 );
1166  }
1167 
1168  if( verify_string( &param1 ) != 0 ) return( 2 );
1169  if( verify_string( &param2 ) != 0 ) return( 2 );
1170  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1171 
1172  test_suite_x509parse_crl( param1, param2, param3 );
1173  return ( 0 );
1174  #endif /* POLARSSL_X509_CRL_PARSE_C */
1175 
1176  return ( 3 );
1177  }
1178  else
1179  if( strcmp( params[0], "x509_selftest" ) == 0 )
1180  {
1181  #ifdef POLARSSL_X509_CRT_PARSE_C
1182  #ifdef POLARSSL_SELF_TEST
1183 
1184 
1185  if( cnt != 1 )
1186  {
1187  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1188  return( 2 );
1189  }
1190 
1191 
1192  test_suite_x509_selftest( );
1193  return ( 0 );
1194  #endif /* POLARSSL_X509_CRT_PARSE_C */
1195  #endif /* POLARSSL_SELF_TEST */
1196 
1197  return ( 3 );
1198  }
1199  else
1200 
1201  {
1202  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1203  fflush( stdout );
1204  return( 1 );
1205  }
1206 #else
1207  return( 3 );
1208 #endif
1209  return( ret );
1210 }
1211 
1212 int get_line( FILE *f, char *buf, size_t len )
1213 {
1214  char *ret;
1215 
1216  ret = fgets( buf, len, f );
1217  if( ret == NULL )
1218  return( -1 );
1219 
1220  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1221  buf[strlen(buf) - 1] = '\0';
1222  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1223  buf[strlen(buf) - 1] = '\0';
1224 
1225  return( 0 );
1226 }
1227 
1228 int parse_arguments( char *buf, size_t len, char *params[50] )
1229 {
1230  int cnt = 0, i;
1231  char *cur = buf;
1232  char *p = buf, *q;
1233 
1234  params[cnt++] = cur;
1235 
1236  while( *p != '\0' && p < buf + len )
1237  {
1238  if( *p == '\\' )
1239  {
1240  *p++;
1241  *p++;
1242  continue;
1243  }
1244  if( *p == ':' )
1245  {
1246  if( p + 1 < buf + len )
1247  {
1248  cur = p + 1;
1249  params[cnt++] = cur;
1250  }
1251  *p = '\0';
1252  }
1253 
1254  *p++;
1255  }
1256 
1257  // Replace newlines, question marks and colons in strings
1258  for( i = 0; i < cnt; i++ )
1259  {
1260  p = params[i];
1261  q = params[i];
1262 
1263  while( *p != '\0' )
1264  {
1265  if( *p == '\\' && *(p + 1) == 'n' )
1266  {
1267  p += 2;
1268  *(q++) = '\n';
1269  }
1270  else if( *p == '\\' && *(p + 1) == ':' )
1271  {
1272  p += 2;
1273  *(q++) = ':';
1274  }
1275  else if( *p == '\\' && *(p + 1) == '?' )
1276  {
1277  p += 2;
1278  *(q++) = '?';
1279  }
1280  else
1281  *(q++) = *(p++);
1282  }
1283  *q = '\0';
1284  }
1285 
1286  return( cnt );
1287 }
1288 
1289 int main()
1290 {
1291  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1292  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_x509parse.data";
1293  FILE *file;
1294  char buf[5000];
1295  char *params[50];
1296 
1297 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1298  unsigned char alloc_buf[1000000];
1299  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1300 #endif
1301 
1302  file = fopen( filename, "r" );
1303  if( file == NULL )
1304  {
1305  fprintf( stderr, "Failed to open\n" );
1306  return( 1 );
1307  }
1308 
1309  while( !feof( file ) )
1310  {
1311  int skip = 0;
1312 
1313  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1314  break;
1315  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1316  fprintf( stdout, " " );
1317  for( i = strlen( buf ) + 1; i < 67; i++ )
1318  fprintf( stdout, "." );
1319  fprintf( stdout, " " );
1320  fflush( stdout );
1321 
1322  total_tests++;
1323 
1324  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1325  break;
1326  cnt = parse_arguments( buf, strlen(buf), params );
1327 
1328  if( strcmp( params[0], "depends_on" ) == 0 )
1329  {
1330  for( i = 1; i < cnt; i++ )
1331  if( dep_check( params[i] ) != 0 )
1332  skip = 1;
1333 
1334  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1335  break;
1336  cnt = parse_arguments( buf, strlen(buf), params );
1337  }
1338 
1339  if( skip == 0 )
1340  {
1341  test_errors = 0;
1342  ret = dispatch_test( cnt, params );
1343  }
1344 
1345  if( skip == 1 || ret == 3 )
1346  {
1347  total_skipped++;
1348  fprintf( stdout, "----\n" );
1349  fflush( stdout );
1350  }
1351  else if( ret == 0 && test_errors == 0 )
1352  {
1353  fprintf( stdout, "PASS\n" );
1354  fflush( stdout );
1355  }
1356  else if( ret == 2 )
1357  {
1358  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1359  fclose(file);
1360  exit( 2 );
1361  }
1362  else
1363  total_errors++;
1364 
1365  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1366  break;
1367  if( strlen(buf) != 0 )
1368  {
1369  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1370  return( 1 );
1371  }
1372  }
1373  fclose(file);
1374 
1375  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1376  if( total_errors == 0 )
1377  fprintf( stdout, "PASSED" );
1378  else
1379  fprintf( stdout, "FAILED" );
1380 
1381  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1382  total_tests - total_errors, total_tests, total_skipped );
1383 
1384 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1385 #if defined(POLARSSL_MEMORY_DEBUG)
1386  memory_buffer_alloc_status();
1387 #endif
1388  memory_buffer_alloc_free();
1389 #endif
1390 
1391  return( total_errors != 0 );
1392 }
1393 
1394