PolarSSL v1.3.2
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 #if defined(WANT_NOT_RND_MPI)
38 #if defined(POLARSSL_BIGNUM_C)
39 #include "polarssl/bignum.h"
40 #else
41 #error "not_rnd_mpi() need bignum.c"
42 #endif
43 #endif
44 
45 #ifdef _MSC_VER
46 #include <basetsd.h>
47 typedef UINT32 uint32_t;
48 #else
49 #include <inttypes.h>
50 #endif
51 
52 #include <assert.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 /*
57  * 32-bit integer manipulation macros (big endian)
58  */
59 #ifndef GET_UINT32_BE
60 #define GET_UINT32_BE(n,b,i) \
61 { \
62  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
63  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
64  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
65  | ( (uint32_t) (b)[(i) + 3] ); \
66 }
67 #endif
68 
69 #ifndef PUT_UINT32_BE
70 #define PUT_UINT32_BE(n,b,i) \
71 { \
72  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
73  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
74  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
75  (b)[(i) + 3] = (unsigned char) ( (n) ); \
76 }
77 #endif
78 
79 static int unhexify(unsigned char *obuf, const char *ibuf)
80 {
81  unsigned char c, c2;
82  int len = strlen(ibuf) / 2;
83  assert(!(strlen(ibuf) %1)); // must be even number of bytes
84 
85  while (*ibuf != 0)
86  {
87  c = *ibuf++;
88  if( c >= '0' && c <= '9' )
89  c -= '0';
90  else if( c >= 'a' && c <= 'f' )
91  c -= 'a' - 10;
92  else if( c >= 'A' && c <= 'F' )
93  c -= 'A' - 10;
94  else
95  assert( 0 );
96 
97  c2 = *ibuf++;
98  if( c2 >= '0' && c2 <= '9' )
99  c2 -= '0';
100  else if( c2 >= 'a' && c2 <= 'f' )
101  c2 -= 'a' - 10;
102  else if( c2 >= 'A' && c2 <= 'F' )
103  c2 -= 'A' - 10;
104  else
105  assert( 0 );
106 
107  *obuf++ = ( c << 4 ) | c2;
108  }
109 
110  return len;
111 }
112 
113 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
114 {
115  unsigned char l, h;
116 
117  while (len != 0)
118  {
119  h = (*ibuf) / 16;
120  l = (*ibuf) % 16;
121 
122  if( h < 10 )
123  *obuf++ = '0' + h;
124  else
125  *obuf++ = 'a' + h - 10;
126 
127  if( l < 10 )
128  *obuf++ = '0' + l;
129  else
130  *obuf++ = 'a' + l - 10;
131 
132  ++ibuf;
133  len--;
134  }
135 }
136 
146 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
147 {
148  size_t i;
149 
150  if( rng_state != NULL )
151  rng_state = NULL;
152 
153  for( i = 0; i < len; ++i )
154  output[i] = rand();
155 
156  return( 0 );
157 }
158 
164 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
165 {
166  if( rng_state != NULL )
167  rng_state = NULL;
168 
169  memset( output, 0, len );
170 
171  return( 0 );
172 }
173 
174 typedef struct
175 {
176  unsigned char *buf;
177  size_t length;
178 } rnd_buf_info;
179 
191 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
192 {
193  rnd_buf_info *info = (rnd_buf_info *) rng_state;
194  size_t use_len;
195 
196  if( rng_state == NULL )
197  return( rnd_std_rand( NULL, output, len ) );
198 
199  use_len = len;
200  if( len > info->length )
201  use_len = info->length;
202 
203  if( use_len )
204  {
205  memcpy( output, info->buf, use_len );
206  info->buf += use_len;
207  info->length -= use_len;
208  }
209 
210  if( len - use_len > 0 )
211  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
212 
213  return( 0 );
214 }
215 
223 typedef struct
224 {
225  uint32_t key[16];
226  uint32_t v0, v1;
228 
237 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
238 {
239  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
240  uint32_t i, *k, sum, delta=0x9E3779B9;
241  unsigned char result[4];
242 
243  if( rng_state == NULL )
244  return( rnd_std_rand( NULL, output, len ) );
245 
246  k = info->key;
247 
248  while( len > 0 )
249  {
250  size_t use_len = ( len > 4 ) ? 4 : len;
251  sum = 0;
252 
253  for( i = 0; i < 32; i++ )
254  {
255  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
256  sum += delta;
257  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
258  }
259 
260  PUT_UINT32_BE( info->v0, result, 0 );
261  memcpy( output, result, use_len );
262  len -= use_len;
263  }
264 
265  return( 0 );
266 }
267 
268 #if defined(WANT_NOT_RND_MPI)
269 
277 #define ciL (sizeof(t_uint)) /* chars in limb */
278 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
279 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
280 {
281  char *str = (char *) in;
282  mpi X;
283 
284  /*
285  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
286  * just reconstruct the rest in order to be able to call mpi_read_string()
287  */
288  X.s = 1;
289  X.p = (t_uint *) out;
290  X.n = CHARS_TO_LIMBS( len );
291 
292  /*
293  * If str is too long, mpi_read_string() will try to allocate a new buffer
294  * for X.p, which we want to avoid at all costs.
295  */
296  assert( strlen( str ) / 2 == len );
297 
298  return( mpi_read_string( &X, 16, str ) );
299 }
300 #endif /* WANT_NOT_RND_MPI */
301 
302 
303 #include <stdio.h>
304 #include <string.h>
305 
306 static int test_errors = 0;
307 
308 #ifdef POLARSSL_BIGNUM_C
309 
310 #define TEST_SUITE_ACTIVE
311 
312 static int test_assert( int correct, char *test )
313 {
314  if( correct )
315  return( 0 );
316 
317  test_errors++;
318  if( test_errors == 1 )
319  printf( "FAILED\n" );
320  printf( " %s\n", test );
321 
322  return( 1 );
323 }
324 
325 #define TEST_ASSERT( TEST ) \
326  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
327  if( test_errors) return; \
328  } while (0)
329 
330 int verify_string( char **str )
331 {
332  if( (*str)[0] != '"' ||
333  (*str)[strlen( *str ) - 1] != '"' )
334  {
335  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
336  return( -1 );
337  }
338 
339  (*str)++;
340  (*str)[strlen( *str ) - 1] = '\0';
341 
342  return( 0 );
343 }
344 
345 int verify_int( char *str, int *value )
346 {
347  size_t i;
348  int minus = 0;
349  int digits = 1;
350  int hex = 0;
351 
352  for( i = 0; i < strlen( str ); i++ )
353  {
354  if( i == 0 && str[i] == '-' )
355  {
356  minus = 1;
357  continue;
358  }
359 
360  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
361  str[i - 1] == '0' && str[i] == 'x' )
362  {
363  hex = 1;
364  continue;
365  }
366 
367  if( str[i] < '0' || str[i] > '9' )
368  {
369  digits = 0;
370  break;
371  }
372  }
373 
374  if( digits )
375  {
376  if( hex )
377  *value = strtol( str, NULL, 16 );
378  else
379  *value = strtol( str, NULL, 10 );
380 
381  return( 0 );
382  }
383 
384 #ifdef POLARSSL_FS_IO
385 #ifdef POLARSSL_X509_CRT_PARSE_C
386 #ifdef POLARSSL_X509_CRL_PARSE_C
387  if( strcmp( str, "BADCERT_REVOKED | BADCRL_EXPIRED | BADCERT_CN_MISMATCH" ) == 0 )
388  {
390  return( 0 );
391  }
392 #endif // POLARSSL_FS_IO
393 #endif // POLARSSL_X509_CRT_PARSE_C
394 #endif // POLARSSL_X509_CRL_PARSE_C
395 #ifdef POLARSSL_X509_CRT_PARSE_C
396  if( strcmp( str, "POLARSSL_ERR_PK_UNKNOWN_PK_ALG" ) == 0 )
397  {
398  *value = ( POLARSSL_ERR_PK_UNKNOWN_PK_ALG );
399  return( 0 );
400  }
401 #endif // POLARSSL_X509_CRT_PARSE_C
402 #ifdef POLARSSL_X509_CRT_PARSE_C
403  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
404  {
406  return( 0 );
407  }
408 #endif // POLARSSL_X509_CRT_PARSE_C
409 #ifdef POLARSSL_X509_CRT_PARSE_C
410  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
411  {
413  return( 0 );
414  }
415 #endif // POLARSSL_X509_CRT_PARSE_C
416 #ifdef POLARSSL_X509_CRT_PARSE_C
417  if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_VERSION" ) == 0 )
418  {
420  return( 0 );
421  }
422 #endif // POLARSSL_X509_CRT_PARSE_C
423 #ifdef POLARSSL_X509_CRL_PARSE_C
424  if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_VERSION" ) == 0 )
425  {
427  return( 0 );
428  }
429 #endif // POLARSSL_X509_CRL_PARSE_C
430 #ifdef POLARSSL_X509_CRT_PARSE_C
431  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA " ) == 0 )
432  {
434  return( 0 );
435  }
436 #endif // POLARSSL_X509_CRT_PARSE_C
437 #ifdef POLARSSL_X509_CRT_PARSE_C
438  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
439  {
441  return( 0 );
442  }
443 #endif // POLARSSL_X509_CRT_PARSE_C
444 #ifdef POLARSSL_X509_CRL_PARSE_C
445  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
446  {
448  return( 0 );
449  }
450 #endif // POLARSSL_X509_CRL_PARSE_C
451 #ifdef POLARSSL_FS_IO
452 #ifdef POLARSSL_X509_CRT_PARSE_C
453 #ifdef POLARSSL_X509_CRL_PARSE_C
454  if( strcmp( str, "BADCRL_EXPIRED" ) == 0 )
455  {
456  *value = ( BADCRL_EXPIRED );
457  return( 0 );
458  }
459 #endif // POLARSSL_FS_IO
460 #endif // POLARSSL_X509_CRT_PARSE_C
461 #endif // POLARSSL_X509_CRL_PARSE_C
462 #ifdef POLARSSL_X509_CRT_PARSE_C
463  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY" ) == 0 )
464  {
465  *value = ( POLARSSL_ERR_PK_INVALID_PUBKEY );
466  return( 0 );
467  }
468 #endif // POLARSSL_X509_CRT_PARSE_C
469 #ifdef POLARSSL_FS_IO
470 #ifdef POLARSSL_X509_CRT_PARSE_C
471 #ifdef POLARSSL_X509_CRL_PARSE_C
472  if( strcmp( str, "BADCERT_NOT_TRUSTED" ) == 0 )
473  {
474  *value = ( BADCERT_NOT_TRUSTED );
475  return( 0 );
476  }
477 #endif // POLARSSL_FS_IO
478 #endif // POLARSSL_X509_CRT_PARSE_C
479 #endif // POLARSSL_X509_CRL_PARSE_C
480 #ifdef POLARSSL_X509_CRT_PARSE_C
481  if( strcmp( str, "POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
482  {
484  return( 0 );
485  }
486 #endif // POLARSSL_X509_CRT_PARSE_C
487 #ifdef POLARSSL_FS_IO
488 #ifdef POLARSSL_X509_CRT_PARSE_C
489 #ifdef POLARSSL_X509_CRL_PARSE_C
490  if( strcmp( str, "BADCERT_REVOKED" ) == 0 )
491  {
492  *value = ( BADCERT_REVOKED );
493  return( 0 );
494  }
495 #endif // POLARSSL_FS_IO
496 #endif // POLARSSL_X509_CRT_PARSE_C
497 #endif // POLARSSL_X509_CRL_PARSE_C
498 #ifdef POLARSSL_X509_CRT_PARSE_C
499  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
500  {
502  return( 0 );
503  }
504 #endif // POLARSSL_X509_CRT_PARSE_C
505 #ifdef POLARSSL_X509_CRT_PARSE_C
506  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
507  {
509  return( 0 );
510  }
511 #endif // POLARSSL_X509_CRT_PARSE_C
512 #ifdef POLARSSL_FS_IO
513 #ifdef POLARSSL_X509_CRT_PARSE_C
514 #ifdef POLARSSL_X509_CRL_PARSE_C
515  if( strcmp( str, "BADCERT_REVOKED | BADCRL_EXPIRED" ) == 0 )
516  {
517  *value = ( BADCERT_REVOKED | BADCRL_EXPIRED );
518  return( 0 );
519  }
520 #endif // POLARSSL_FS_IO
521 #endif // POLARSSL_X509_CRT_PARSE_C
522 #endif // POLARSSL_X509_CRL_PARSE_C
523 #ifdef POLARSSL_X509_CRT_PARSE_C
524  if( strcmp( str, "POLARSSL_ERR_X509_SIG_MISMATCH" ) == 0 )
525  {
526  *value = ( POLARSSL_ERR_X509_SIG_MISMATCH );
527  return( 0 );
528  }
529 #endif // POLARSSL_X509_CRT_PARSE_C
530 #ifdef POLARSSL_X509_CRL_PARSE_C
531  if( strcmp( str, "POLARSSL_ERR_X509_SIG_MISMATCH" ) == 0 )
532  {
533  *value = ( POLARSSL_ERR_X509_SIG_MISMATCH );
534  return( 0 );
535  }
536 #endif // POLARSSL_X509_CRL_PARSE_C
537 #ifdef POLARSSL_FS_IO
538 #ifdef POLARSSL_X509_CRT_PARSE_C
539 #ifdef POLARSSL_X509_CRL_PARSE_C
540  if( strcmp( str, "BADCERT_OTHER" ) == 0 )
541  {
542  *value = ( BADCERT_OTHER );
543  return( 0 );
544  }
545 #endif // POLARSSL_FS_IO
546 #endif // POLARSSL_X509_CRT_PARSE_C
547 #endif // POLARSSL_X509_CRL_PARSE_C
548 #ifdef POLARSSL_X509_CRT_PARSE_C
549  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE" ) == 0 )
550  {
551  *value = ( POLARSSL_ERR_X509_INVALID_DATE );
552  return( 0 );
553  }
554 #endif // POLARSSL_X509_CRT_PARSE_C
555 #ifdef POLARSSL_X509_CRT_PARSE_C
556  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
557  {
559  return( 0 );
560  }
561 #endif // POLARSSL_X509_CRT_PARSE_C
562 #ifdef POLARSSL_X509_CRT_PARSE_C
563  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
564  {
566  return( 0 );
567  }
568 #endif // POLARSSL_X509_CRT_PARSE_C
569 #ifdef POLARSSL_X509_CRT_PARSE_C
570  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
571  {
573  return( 0 );
574  }
575 #endif // POLARSSL_X509_CRT_PARSE_C
576 #ifdef POLARSSL_X509_CRL_PARSE_C
577  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
578  {
580  return( 0 );
581  }
582 #endif // POLARSSL_X509_CRL_PARSE_C
583 #ifdef POLARSSL_X509_CRL_PARSE_C
584  if( strcmp( str, " 1" ) == 0 )
585  {
586  *value = ( 1 );
587  return( 0 );
588  }
589 #endif // POLARSSL_X509_CRL_PARSE_C
590 #ifdef POLARSSL_X509_CRT_PARSE_C
591  if( strcmp( str, " 1" ) == 0 )
592  {
593  *value = ( 1 );
594  return( 0 );
595  }
596 #endif // POLARSSL_X509_CRT_PARSE_C
597 #ifdef POLARSSL_X509_CRT_PARSE_C
598  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_INVALID_LENGTH" ) == 0 )
599  {
601  return( 0 );
602  }
603 #endif // POLARSSL_X509_CRT_PARSE_C
604 #ifdef POLARSSL_X509_CRT_PARSE_C
605  if( strcmp( str, "POLARSSL_ERR_PK_KEY_INVALID_FORMAT + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
606  {
608  return( 0 );
609  }
610 #endif // POLARSSL_X509_CRT_PARSE_C
611 #ifdef POLARSSL_FS_IO
612 #ifdef POLARSSL_X509_CRT_PARSE_C
613 #ifdef POLARSSL_X509_CRL_PARSE_C
614  if( strcmp( str, "POLARSSL_ERR_X509_CERT_VERIFY_FAILED" ) == 0 )
615  {
617  return( 0 );
618  }
619 #endif // POLARSSL_FS_IO
620 #endif // POLARSSL_X509_CRT_PARSE_C
621 #endif // POLARSSL_X509_CRL_PARSE_C
622 #ifdef POLARSSL_X509_CRT_PARSE_C
623  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_INVALID_DATA" ) == 0 )
624  {
626  return( 0 );
627  }
628 #endif // POLARSSL_X509_CRT_PARSE_C
629 #ifdef POLARSSL_X509_CRL_PARSE_C
630  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
631  {
633  return( 0 );
634  }
635 #endif // POLARSSL_X509_CRL_PARSE_C
636 #ifdef POLARSSL_X509_CRT_PARSE_C
637  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
638  {
640  return( 0 );
641  }
642 #endif // POLARSSL_X509_CRT_PARSE_C
643 #ifdef POLARSSL_X509_CRT_PARSE_C
644  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
645  {
647  return( 0 );
648  }
649 #endif // POLARSSL_X509_CRT_PARSE_C
650 #ifdef POLARSSL_X509_CRL_PARSE_C
651  if( strcmp( str, "POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
652  {
653  *value = ( POLARSSL_ERR_ASN1_OUT_OF_DATA );
654  return( 0 );
655  }
656 #endif // POLARSSL_X509_CRL_PARSE_C
657 #ifdef POLARSSL_X509_CRL_PARSE_C
658  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
659  {
661  return( 0 );
662  }
663 #endif // POLARSSL_X509_CRL_PARSE_C
664 #ifdef POLARSSL_X509_CRT_PARSE_C
665  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
666  {
668  return( 0 );
669  }
670 #endif // POLARSSL_X509_CRT_PARSE_C
671 #ifdef POLARSSL_X509_CRT_PARSE_C
672  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
673  {
675  return( 0 );
676  }
677 #endif // POLARSSL_X509_CRT_PARSE_C
678 #ifdef POLARSSL_X509_CRL_PARSE_C
679  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
680  {
682  return( 0 );
683  }
684 #endif // POLARSSL_X509_CRL_PARSE_C
685 #ifdef POLARSSL_X509_CRT_PARSE_C
686  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
687  {
689  return( 0 );
690  }
691 #endif // POLARSSL_X509_CRT_PARSE_C
692 #ifdef POLARSSL_X509_CRT_PARSE_C
693  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_LENGTH_MISMATCH" ) == 0 )
694  {
696  return( 0 );
697  }
698 #endif // POLARSSL_X509_CRT_PARSE_C
699 #ifdef POLARSSL_X509_CRT_PARSE_C
700  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_INVALID_DATA" ) == 0 )
701  {
703  return( 0 );
704  }
705 #endif // POLARSSL_X509_CRT_PARSE_C
706 #ifdef POLARSSL_X509_CRT_PARSE_C
707  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_EXTENSIONS + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
708  {
710  return( 0 );
711  }
712 #endif // POLARSSL_X509_CRT_PARSE_C
713 #ifdef POLARSSL_X509_CRT_PARSE_C
714  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
715  {
717  return( 0 );
718  }
719 #endif // POLARSSL_X509_CRT_PARSE_C
720 #ifdef POLARSSL_X509_CRT_PARSE_C
721  if( strcmp( str, "POLARSSL_ERR_PK_INVALID_PUBKEY + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
722  {
724  return( 0 );
725  }
726 #endif // POLARSSL_X509_CRT_PARSE_C
727 #ifdef POLARSSL_FS_IO
728 #ifdef POLARSSL_X509_CRT_PARSE_C
729 #ifdef POLARSSL_X509_CRL_PARSE_C
730  if( strcmp( str, "BADCERT_CN_MISMATCH + BADCERT_NOT_TRUSTED" ) == 0 )
731  {
733  return( 0 );
734  }
735 #endif // POLARSSL_FS_IO
736 #endif // POLARSSL_X509_CRT_PARSE_C
737 #endif // POLARSSL_X509_CRL_PARSE_C
738 #ifdef POLARSSL_X509_CRT_PARSE_C
739  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
740  {
742  return( 0 );
743  }
744 #endif // POLARSSL_X509_CRT_PARSE_C
745 #ifdef POLARSSL_X509_CRL_PARSE_C
746  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
747  {
749  return( 0 );
750  }
751 #endif // POLARSSL_X509_CRL_PARSE_C
752 #ifdef POLARSSL_X509_CRT_PARSE_C
753  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_DATE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
754  {
756  return( 0 );
757  }
758 #endif // POLARSSL_X509_CRT_PARSE_C
759 #ifdef POLARSSL_X509_CRT_PARSE_C
760  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT" ) == 0 )
761  {
763  return( 0 );
764  }
765 #endif // POLARSSL_X509_CRT_PARSE_C
766 #ifdef POLARSSL_X509_CRL_PARSE_C
767  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_FORMAT" ) == 0 )
768  {
770  return( 0 );
771  }
772 #endif // POLARSSL_X509_CRL_PARSE_C
773 #ifdef POLARSSL_FS_IO
774 #ifdef POLARSSL_X509_CRT_PARSE_C
775 #ifdef POLARSSL_X509_CRL_PARSE_C
776  if( strcmp( str, "BADCERT_REVOKED | BADCERT_CN_MISMATCH" ) == 0 )
777  {
778  *value = ( BADCERT_REVOKED | BADCERT_CN_MISMATCH );
779  return( 0 );
780  }
781 #endif // POLARSSL_FS_IO
782 #endif // POLARSSL_X509_CRT_PARSE_C
783 #endif // POLARSSL_X509_CRL_PARSE_C
784 #ifdef POLARSSL_X509_CRT_PARSE_C
785  if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + POLARSSL_ERR_OID_NOT_FOUND" ) == 0 )
786  {
788  return( 0 );
789  }
790 #endif // POLARSSL_X509_CRT_PARSE_C
791 #ifdef POLARSSL_FS_IO
792 #ifdef POLARSSL_X509_CRT_PARSE_C
793 #ifdef POLARSSL_X509_CRL_PARSE_C
794  if( strcmp( str, "BADCERT_CN_MISMATCH" ) == 0 )
795  {
796  *value = ( BADCERT_CN_MISMATCH );
797  return( 0 );
798  }
799 #endif // POLARSSL_FS_IO
800 #endif // POLARSSL_X509_CRT_PARSE_C
801 #endif // POLARSSL_X509_CRL_PARSE_C
802 #ifdef POLARSSL_X509_CRT_PARSE_C
803  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_VERSION + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
804  {
806  return( 0 );
807  }
808 #endif // POLARSSL_X509_CRT_PARSE_C
809 #ifdef POLARSSL_X509_CRT_PARSE_C
810  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SERIAL + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
811  {
813  return( 0 );
814  }
815 #endif // POLARSSL_X509_CRT_PARSE_C
816 #ifdef POLARSSL_X509_CRL_PARSE_C
817  if( strcmp( str, "POLARSSL_ERR_X509_UNKNOWN_SIG_ALG" ) == 0 )
818  {
820  return( 0 );
821  }
822 #endif // POLARSSL_X509_CRL_PARSE_C
823 #ifdef POLARSSL_X509_CRT_PARSE_C
824  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
825  {
827  return( 0 );
828  }
829 #endif // POLARSSL_X509_CRT_PARSE_C
830 #ifdef POLARSSL_X509_CRL_PARSE_C
831  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_NAME + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
832  {
834  return( 0 );
835  }
836 #endif // POLARSSL_X509_CRL_PARSE_C
837 #ifdef POLARSSL_X509_CRT_PARSE_C
838  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_SIGNATURE + POLARSSL_ERR_ASN1_OUT_OF_DATA" ) == 0 )
839  {
841  return( 0 );
842  }
843 #endif // POLARSSL_X509_CRT_PARSE_C
844 #ifdef POLARSSL_X509_CRT_PARSE_C
845  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
846  {
848  return( 0 );
849  }
850 #endif // POLARSSL_X509_CRT_PARSE_C
851 #ifdef POLARSSL_X509_CRL_PARSE_C
852  if( strcmp( str, "POLARSSL_ERR_X509_INVALID_ALG + POLARSSL_ERR_ASN1_UNEXPECTED_TAG" ) == 0 )
853  {
855  return( 0 );
856  }
857 #endif // POLARSSL_X509_CRL_PARSE_C
858 
859 
860  printf( "Expected integer for parameter and got: %s\n", str );
861  return( -1 );
862 }
863 
864 #ifdef POLARSSL_FS_IO
865 #ifdef POLARSSL_X509_CRT_PARSE_C
866 void test_suite_x509_cert_info( char *crt_file, char *result_str )
867 {
868  x509_crt crt;
869  char buf[2000];
870  int res;
871 
872  x509_crt_init( &crt );
873  memset( buf, 0, 2000 );
874 
875  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
876  res = x509_crt_info( buf, 2000, "", &crt );
877 
878  x509_crt_free( &crt );
879 
880  TEST_ASSERT( res != -1 );
881  TEST_ASSERT( res != -2 );
882 
883  TEST_ASSERT( strcmp( buf, result_str ) == 0 );
884 }
885 #endif /* POLARSSL_FS_IO */
886 #endif /* POLARSSL_X509_CRT_PARSE_C */
887 
888 #ifdef POLARSSL_FS_IO
889 #ifdef POLARSSL_X509_CRL_PARSE_C
890 void test_suite_x509_crl_info( char *crl_file, char *result_str )
891 {
892  x509_crl crl;
893  char buf[2000];
894  int res;
895 
896  x509_crl_init( &crl );
897  memset( buf, 0, 2000 );
898 
899  TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
900  res = x509_crl_info( buf, 2000, "", &crl );
901 
902  x509_crl_free( &crl );
903 
904  TEST_ASSERT( res != -1 );
905  TEST_ASSERT( res != -2 );
906 
907  TEST_ASSERT( strcmp( buf, result_str ) == 0 );
908 }
909 #endif /* POLARSSL_FS_IO */
910 #endif /* POLARSSL_X509_CRL_PARSE_C */
911 
912 #ifdef POLARSSL_FS_IO
913 #ifdef POLARSSL_X509_CRT_PARSE_C
914 #ifdef POLARSSL_X509_CRL_PARSE_C
915 void test_suite_x509_verify( char *crt_file, char *ca_file, char *crl_file,
916  char *cn_name_str, int result, int flags_result,
917  char *verify_callback )
918 {
919  x509_crt crt;
920  x509_crt ca;
921  x509_crl crl;
922  int flags = 0;
923  int res;
924  int (*f_vrfy)(void *, x509_crt *, int, int *) = NULL;
925  char * cn_name = NULL;
926 
927  x509_crt_init( &crt );
928  x509_crt_init( &ca );
929  x509_crl_init( &crl );
930 
931  if( strcmp( cn_name_str, "NULL" ) != 0 )
932  cn_name = cn_name_str;
933 
934  if( strcmp( verify_callback, "NULL" ) == 0 )
935  f_vrfy = NULL;
936  else if( strcmp( verify_callback, "verify_none" ) == 0 )
937  f_vrfy = verify_none;
938  else if( strcmp( verify_callback, "verify_all" ) == 0 )
939  f_vrfy = verify_all;
940  else
941  TEST_ASSERT( "No known verify callback selected" == 0 );
942 
943  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
944  TEST_ASSERT( x509_crt_parse_file( &ca, ca_file ) == 0 );
945  TEST_ASSERT( x509_crl_parse_file( &crl, crl_file ) == 0 );
946 
947  res = x509_crt_verify( &crt, &ca, &crl, cn_name, &flags, f_vrfy, NULL );
948 
949  x509_crt_free( &crt );
950  x509_crt_free( &ca );
951  x509_crl_free( &crl );
952 
953  TEST_ASSERT( res == ( result ) );
954  TEST_ASSERT( flags == ( flags_result ) );
955 }
956 #endif /* POLARSSL_FS_IO */
957 #endif /* POLARSSL_X509_CRT_PARSE_C */
958 #endif /* POLARSSL_X509_CRL_PARSE_C */
959 
960 #ifdef POLARSSL_FS_IO
961 #ifdef POLARSSL_X509_USE_C
962 void test_suite_x509_dn_gets( char *crt_file, char *entity, char *result_str )
963 {
964  x509_crt crt;
965  char buf[2000];
966  int res = 0;
967 
968  x509_crt_init( &crt );
969  memset( buf, 0, 2000 );
970 
971  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
972  if( strcmp( entity, "subject" ) == 0 )
973  res = x509_dn_gets( buf, 2000, &crt.subject );
974  else if( strcmp( entity, "issuer" ) == 0 )
975  res = x509_dn_gets( buf, 2000, &crt.issuer );
976  else
977  TEST_ASSERT( "Unknown entity" == 0 );
978 
979  x509_crt_free( &crt );
980 
981  TEST_ASSERT( res != -1 );
982  TEST_ASSERT( res != -2 );
983 
984  TEST_ASSERT( strcmp( buf, result_str ) == 0 );
985 }
986 #endif /* POLARSSL_FS_IO */
987 #endif /* POLARSSL_X509_USE_C */
988 
989 #ifdef POLARSSL_FS_IO
990 #ifdef POLARSSL_X509_USE_C
991 void test_suite_x509_time_expired( char *crt_file, char *entity, int result )
992 {
993  x509_crt crt;
994 
995  x509_crt_init( &crt );
996 
997  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
998 
999  if( strcmp( entity, "valid_from" ) == 0 )
1000  TEST_ASSERT( x509_time_expired( &crt.valid_from ) == result );
1001  else if( strcmp( entity, "valid_to" ) == 0 )
1002  TEST_ASSERT( x509_time_expired( &crt.valid_to ) == result );
1003  else
1004  TEST_ASSERT( "Unknown entity" == 0 );
1005 
1006  x509_crt_free( &crt );
1007 }
1008 #endif /* POLARSSL_FS_IO */
1009 #endif /* POLARSSL_X509_USE_C */
1010 
1011 #ifdef POLARSSL_X509_CRT_PARSE_C
1012 void test_suite_x509parse_crt( char *crt_data, char *result_str, int result )
1013 {
1014  x509_crt crt;
1015  unsigned char buf[2000];
1016  unsigned char output[2000];
1017  int data_len, res;
1018 
1019  x509_crt_init( &crt );
1020  memset( buf, 0, 2000 );
1021  memset( output, 0, 2000 );
1022 
1023  data_len = unhexify( buf, crt_data );
1024 
1025  TEST_ASSERT( x509_crt_parse( &crt, buf, data_len ) == ( result ) );
1026  if( ( result ) == 0 )
1027  {
1028  res = x509_crt_info( (char *) output, 2000, "", &crt );
1029 
1030  TEST_ASSERT( res != -1 );
1031  TEST_ASSERT( res != -2 );
1032 
1033  TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
1034  }
1035 
1036  x509_crt_free( &crt );
1037 }
1038 #endif /* POLARSSL_X509_CRT_PARSE_C */
1039 
1040 #ifdef POLARSSL_X509_CRL_PARSE_C
1041 void test_suite_x509parse_crl( char *crl_data, char *result_str, int result )
1042 {
1043  x509_crl crl;
1044  unsigned char buf[2000];
1045  unsigned char output[2000];
1046  int data_len, res;
1047 
1048  x509_crl_init( &crl );
1049  memset( buf, 0, 2000 );
1050  memset( output, 0, 2000 );
1051 
1052  data_len = unhexify( buf, crl_data );
1053 
1054  TEST_ASSERT( x509_crl_parse( &crl, buf, data_len ) == ( result ) );
1055  if( ( result ) == 0 )
1056  {
1057  res = x509_crl_info( (char *) output, 2000, "", &crl );
1058 
1059  TEST_ASSERT( res != -1 );
1060  TEST_ASSERT( res != -2 );
1061 
1062  TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
1063  }
1064 
1065  x509_crl_free( &crl );
1066 }
1067 #endif /* POLARSSL_X509_CRL_PARSE_C */
1068 
1069 #ifdef POLARSSL_X509_CRT_PARSE_C
1070 #ifdef POLARSSL_SELF_TEST
1071 void test_suite_x509_selftest()
1072 {
1073  TEST_ASSERT( x509_self_test( 0 ) == 0 );
1074 }
1075 #endif /* POLARSSL_X509_CRT_PARSE_C */
1076 #endif /* POLARSSL_SELF_TEST */
1077 
1078 
1079 #endif /* POLARSSL_BIGNUM_C */
1080 
1081 
1082 int dep_check( char *str )
1083 {
1084  if( str == NULL )
1085  return( 1 );
1086 
1087  if( strcmp( str, "POLARSSL_ECDSA_C" ) == 0 )
1088  {
1089 #if defined(POLARSSL_ECDSA_C)
1090  return( 0 );
1091 #else
1092  return( 1 );
1093 #endif
1094  }
1095  if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
1096  {
1097 #if defined(POLARSSL_PKCS1_V15)
1098  return( 0 );
1099 #else
1100  return( 1 );
1101 #endif
1102  }
1103  if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
1104  {
1105 #if defined(POLARSSL_SHA512_C)
1106  return( 0 );
1107 #else
1108  return( 1 );
1109 #endif
1110  }
1111  if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
1112  {
1113 #if defined(POLARSSL_RSA_C)
1114  return( 0 );
1115 #else
1116  return( 1 );
1117 #endif
1118  }
1119  if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
1120  {
1121 #if defined(POLARSSL_MD5_C)
1122  return( 0 );
1123 #else
1124  return( 1 );
1125 #endif
1126  }
1127  if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
1128  {
1129 #if defined(POLARSSL_MD4_C)
1130  return( 0 );
1131 #else
1132  return( 1 );
1133 #endif
1134  }
1135  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
1136  {
1137 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
1138  return( 0 );
1139 #else
1140  return( 1 );
1141 #endif
1142  }
1143  if( strcmp( str, "POLARSSL_PEM_PARSE_C" ) == 0 )
1144  {
1145 #if defined(POLARSSL_PEM_PARSE_C)
1146  return( 0 );
1147 #else
1148  return( 1 );
1149 #endif
1150  }
1151  if( strcmp( str, "POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3" ) == 0 )
1152  {
1153 #if defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
1154  return( 0 );
1155 #else
1156  return( 1 );
1157 #endif
1158  }
1159  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
1160  {
1161 #if defined(POLARSSL_ECP_C)
1162  return( 0 );
1163 #else
1164  return( 1 );
1165 #endif
1166  }
1167  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
1168  {
1169 #if defined(POLARSSL_SHA1_C)
1170  return( 0 );
1171 #else
1172  return( 1 );
1173 #endif
1174  }
1175  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
1176  {
1177 #if defined(POLARSSL_SHA256_C)
1178  return( 0 );
1179 #else
1180  return( 1 );
1181 #endif
1182  }
1183  if( strcmp( str, "POLARSSL_ECP_DP_SECP256R1_ENABLED" ) == 0 )
1184  {
1185 #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
1186  return( 0 );
1187 #else
1188  return( 1 );
1189 #endif
1190  }
1191 
1192 
1193  return( 1 );
1194 }
1195 
1196 int dispatch_test(int cnt, char *params[50])
1197 {
1198  int ret;
1199  ((void) cnt);
1200  ((void) params);
1201 
1202 #if defined(TEST_SUITE_ACTIVE)
1203  if( strcmp( params[0], "x509_cert_info" ) == 0 )
1204  {
1205  #ifdef POLARSSL_FS_IO
1206  #ifdef POLARSSL_X509_CRT_PARSE_C
1207 
1208  char *param1 = params[1];
1209  char *param2 = params[2];
1210 
1211  if( cnt != 3 )
1212  {
1213  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1214  return( 2 );
1215  }
1216 
1217  if( verify_string( &param1 ) != 0 ) return( 2 );
1218  if( verify_string( &param2 ) != 0 ) return( 2 );
1219 
1220  test_suite_x509_cert_info( param1, param2 );
1221  return ( 0 );
1222  #endif /* POLARSSL_FS_IO */
1223  #endif /* POLARSSL_X509_CRT_PARSE_C */
1224 
1225  return ( 3 );
1226  }
1227  else
1228  if( strcmp( params[0], "x509_crl_info" ) == 0 )
1229  {
1230  #ifdef POLARSSL_FS_IO
1231  #ifdef POLARSSL_X509_CRL_PARSE_C
1232 
1233  char *param1 = params[1];
1234  char *param2 = params[2];
1235 
1236  if( cnt != 3 )
1237  {
1238  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 3 );
1239  return( 2 );
1240  }
1241 
1242  if( verify_string( &param1 ) != 0 ) return( 2 );
1243  if( verify_string( &param2 ) != 0 ) return( 2 );
1244 
1245  test_suite_x509_crl_info( param1, param2 );
1246  return ( 0 );
1247  #endif /* POLARSSL_FS_IO */
1248  #endif /* POLARSSL_X509_CRL_PARSE_C */
1249 
1250  return ( 3 );
1251  }
1252  else
1253  if( strcmp( params[0], "x509_verify" ) == 0 )
1254  {
1255  #ifdef POLARSSL_FS_IO
1256  #ifdef POLARSSL_X509_CRT_PARSE_C
1257  #ifdef POLARSSL_X509_CRL_PARSE_C
1258 
1259  char *param1 = params[1];
1260  char *param2 = params[2];
1261  char *param3 = params[3];
1262  char *param4 = params[4];
1263  int param5;
1264  int param6;
1265  char *param7 = params[7];
1266 
1267  if( cnt != 8 )
1268  {
1269  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 8 );
1270  return( 2 );
1271  }
1272 
1273  if( verify_string( &param1 ) != 0 ) return( 2 );
1274  if( verify_string( &param2 ) != 0 ) return( 2 );
1275  if( verify_string( &param3 ) != 0 ) return( 2 );
1276  if( verify_string( &param4 ) != 0 ) return( 2 );
1277  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1278  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1279  if( verify_string( &param7 ) != 0 ) return( 2 );
1280 
1281  test_suite_x509_verify( param1, param2, param3, param4, param5, param6, param7 );
1282  return ( 0 );
1283  #endif /* POLARSSL_FS_IO */
1284  #endif /* POLARSSL_X509_CRT_PARSE_C */
1285  #endif /* POLARSSL_X509_CRL_PARSE_C */
1286 
1287  return ( 3 );
1288  }
1289  else
1290  if( strcmp( params[0], "x509_dn_gets" ) == 0 )
1291  {
1292  #ifdef POLARSSL_FS_IO
1293  #ifdef POLARSSL_X509_USE_C
1294 
1295  char *param1 = params[1];
1296  char *param2 = params[2];
1297  char *param3 = params[3];
1298 
1299  if( cnt != 4 )
1300  {
1301  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1302  return( 2 );
1303  }
1304 
1305  if( verify_string( &param1 ) != 0 ) return( 2 );
1306  if( verify_string( &param2 ) != 0 ) return( 2 );
1307  if( verify_string( &param3 ) != 0 ) return( 2 );
1308 
1309  test_suite_x509_dn_gets( param1, param2, param3 );
1310  return ( 0 );
1311  #endif /* POLARSSL_FS_IO */
1312  #endif /* POLARSSL_X509_USE_C */
1313 
1314  return ( 3 );
1315  }
1316  else
1317  if( strcmp( params[0], "x509_time_expired" ) == 0 )
1318  {
1319  #ifdef POLARSSL_FS_IO
1320  #ifdef POLARSSL_X509_USE_C
1321 
1322  char *param1 = params[1];
1323  char *param2 = params[2];
1324  int param3;
1325 
1326  if( cnt != 4 )
1327  {
1328  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1329  return( 2 );
1330  }
1331 
1332  if( verify_string( &param1 ) != 0 ) return( 2 );
1333  if( verify_string( &param2 ) != 0 ) return( 2 );
1334  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1335 
1336  test_suite_x509_time_expired( param1, param2, param3 );
1337  return ( 0 );
1338  #endif /* POLARSSL_FS_IO */
1339  #endif /* POLARSSL_X509_USE_C */
1340 
1341  return ( 3 );
1342  }
1343  else
1344  if( strcmp( params[0], "x509parse_crt" ) == 0 )
1345  {
1346  #ifdef POLARSSL_X509_CRT_PARSE_C
1347 
1348  char *param1 = params[1];
1349  char *param2 = params[2];
1350  int param3;
1351 
1352  if( cnt != 4 )
1353  {
1354  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1355  return( 2 );
1356  }
1357 
1358  if( verify_string( &param1 ) != 0 ) return( 2 );
1359  if( verify_string( &param2 ) != 0 ) return( 2 );
1360  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1361 
1362  test_suite_x509parse_crt( param1, param2, param3 );
1363  return ( 0 );
1364  #endif /* POLARSSL_X509_CRT_PARSE_C */
1365 
1366  return ( 3 );
1367  }
1368  else
1369  if( strcmp( params[0], "x509parse_crl" ) == 0 )
1370  {
1371  #ifdef POLARSSL_X509_CRL_PARSE_C
1372 
1373  char *param1 = params[1];
1374  char *param2 = params[2];
1375  int param3;
1376 
1377  if( cnt != 4 )
1378  {
1379  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1380  return( 2 );
1381  }
1382 
1383  if( verify_string( &param1 ) != 0 ) return( 2 );
1384  if( verify_string( &param2 ) != 0 ) return( 2 );
1385  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1386 
1387  test_suite_x509parse_crl( param1, param2, param3 );
1388  return ( 0 );
1389  #endif /* POLARSSL_X509_CRL_PARSE_C */
1390 
1391  return ( 3 );
1392  }
1393  else
1394  if( strcmp( params[0], "x509_selftest" ) == 0 )
1395  {
1396  #ifdef POLARSSL_X509_CRT_PARSE_C
1397  #ifdef POLARSSL_SELF_TEST
1398 
1399 
1400  if( cnt != 1 )
1401  {
1402  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1403  return( 2 );
1404  }
1405 
1406 
1407  test_suite_x509_selftest( );
1408  return ( 0 );
1409  #endif /* POLARSSL_X509_CRT_PARSE_C */
1410  #endif /* POLARSSL_SELF_TEST */
1411 
1412  return ( 3 );
1413  }
1414  else
1415 
1416  {
1417  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1418  fflush( stdout );
1419  return( 1 );
1420  }
1421 #else
1422  return( 3 );
1423 #endif
1424  return( ret );
1425 }
1426 
1427 int get_line( FILE *f, char *buf, size_t len )
1428 {
1429  char *ret;
1430 
1431  ret = fgets( buf, len, f );
1432  if( ret == NULL )
1433  return( -1 );
1434 
1435  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1436  buf[strlen(buf) - 1] = '\0';
1437  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1438  buf[strlen(buf) - 1] = '\0';
1439 
1440  return( 0 );
1441 }
1442 
1443 int parse_arguments( char *buf, size_t len, char *params[50] )
1444 {
1445  int cnt = 0, i;
1446  char *cur = buf;
1447  char *p = buf, *q;
1448 
1449  params[cnt++] = cur;
1450 
1451  while( *p != '\0' && p < buf + len )
1452  {
1453  if( *p == '\\' )
1454  {
1455  *p++;
1456  *p++;
1457  continue;
1458  }
1459  if( *p == ':' )
1460  {
1461  if( p + 1 < buf + len )
1462  {
1463  cur = p + 1;
1464  params[cnt++] = cur;
1465  }
1466  *p = '\0';
1467  }
1468 
1469  *p++;
1470  }
1471 
1472  // Replace newlines, question marks and colons in strings
1473  for( i = 0; i < cnt; i++ )
1474  {
1475  p = params[i];
1476  q = params[i];
1477 
1478  while( *p != '\0' )
1479  {
1480  if( *p == '\\' && *(p + 1) == 'n' )
1481  {
1482  p += 2;
1483  *(q++) = '\n';
1484  }
1485  else if( *p == '\\' && *(p + 1) == ':' )
1486  {
1487  p += 2;
1488  *(q++) = ':';
1489  }
1490  else if( *p == '\\' && *(p + 1) == '?' )
1491  {
1492  p += 2;
1493  *(q++) = '?';
1494  }
1495  else
1496  *(q++) = *(p++);
1497  }
1498  *q = '\0';
1499  }
1500 
1501  return( cnt );
1502 }
1503 
1504 int main()
1505 {
1506  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1507  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_x509parse.data";
1508  FILE *file;
1509  char buf[5000];
1510  char *params[50];
1511 
1512 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1513  unsigned char alloc_buf[1000000];
1514  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1515 #endif
1516 
1517  file = fopen( filename, "r" );
1518  if( file == NULL )
1519  {
1520  fprintf( stderr, "Failed to open\n" );
1521  return( 1 );
1522  }
1523 
1524  while( !feof( file ) )
1525  {
1526  int skip = 0;
1527 
1528  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1529  break;
1530  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1531  fprintf( stdout, " " );
1532  for( i = strlen( buf ) + 1; i < 67; i++ )
1533  fprintf( stdout, "." );
1534  fprintf( stdout, " " );
1535  fflush( stdout );
1536 
1537  total_tests++;
1538 
1539  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1540  break;
1541  cnt = parse_arguments( buf, strlen(buf), params );
1542 
1543  if( strcmp( params[0], "depends_on" ) == 0 )
1544  {
1545  for( i = 1; i < cnt; i++ )
1546  if( dep_check( params[i] ) != 0 )
1547  skip = 1;
1548 
1549  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1550  break;
1551  cnt = parse_arguments( buf, strlen(buf), params );
1552  }
1553 
1554  if( skip == 0 )
1555  {
1556  test_errors = 0;
1557  ret = dispatch_test( cnt, params );
1558  }
1559 
1560  if( skip == 1 || ret == 3 )
1561  {
1562  total_skipped++;
1563  fprintf( stdout, "----\n" );
1564  fflush( stdout );
1565  }
1566  else if( ret == 0 && test_errors == 0 )
1567  {
1568  fprintf( stdout, "PASS\n" );
1569  fflush( stdout );
1570  }
1571  else if( ret == 2 )
1572  {
1573  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1574  fclose(file);
1575  exit( 2 );
1576  }
1577  else
1578  total_errors++;
1579 
1580  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1581  break;
1582  if( strlen(buf) != 0 )
1583  {
1584  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1585  return( 1 );
1586  }
1587  }
1588  fclose(file);
1589 
1590  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1591  if( total_errors == 0 )
1592  fprintf( stdout, "PASSED" );
1593  else
1594  fprintf( stdout, "FAILED" );
1595 
1596  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1597  total_tests - total_errors, total_tests, total_skipped );
1598 
1599 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1600 #if defined(POLARSSL_MEMORY_DEBUG)
1601  memory_buffer_alloc_status();
1602 #endif
1603  memory_buffer_alloc_free();
1604 #endif
1605 
1606  return( total_errors != 0 );
1607 }
1608 
1609 
#define POLARSSL_ERR_PK_INVALID_ALG
The algorithm tag or value is invalid.
Definition: pk.h:57
int x509_time_expired(const x509_time *time)
Check a given x509_time against the system time and check if it is valid.
#define POLARSSL_ERR_PK_KEY_INVALID_FORMAT
Invalid key tag or value.
Definition: pk.h:52
void x509_crl_init(x509_crl *crl)
Initialize a CRL (chain)
Memory allocation layer.
#define POLARSSL_ERR_X509_INVALID_DATE
The date tag or value is invalid.
Definition: x509.h:55
uint32_t t_uint
Definition: bignum.h:149
#define BADCERT_OTHER
Other reason (can be used by verify callback)
Definition: x509.h:80
Info structure for the pseudo random function.
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
Definition: asn1.h:53
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int s
Definition: bignum.h:173
#define POLARSSL_ERR_X509_INVALID_FORMAT
The CRT/CRL/CSR format is invalid, e.g.
Definition: x509.h:50
int x509_crt_parse(x509_crt *chain, const unsigned char *buf, size_t buflen)
Parse one or more certificates and add them to the chained list.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
Configuration options (set of defines)
#define POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
Signature algorithm (oid) is unsupported.
Definition: x509.h:59
#define POLARSSL_ERR_ASN1_INVALID_DATA
Data is invalid.
Definition: asn1.h:54
MPI structure.
Definition: bignum.h:171
#define POLARSSL_ERR_X509_INVALID_SIGNATURE
The signature tag or value invalid.
Definition: x509.h:56
static int test_assert(int correct, char *test)
#define POLARSSL_ERR_ASN1_INVALID_LENGTH
Error when trying to determine the length or invalid length.
Definition: asn1.h:52
#define POLARSSL_ERR_X509_UNKNOWN_VERSION
CRT/CRL/CSR has an unsupported version number.
Definition: x509.h:58
int main(int argc, char *argv[])
int x509_crl_parse_file(x509_crl *chain, const char *path)
Load one or more CRLs and add them to the chained list.
Object Identifier (OID) database.
int x509_crl_parse(x509_crl *chain, const unsigned char *buf, size_t buflen)
Parse one or more CRLs and add them to the chained list.
int x509_crl_info(char *buf, size_t size, const char *prefix, const x509_crl *crl)
Returns an informational string about the CRL.
Multi-precision integer library.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
static int test_errors
#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED
Certificate verification failed, e.g.
Definition: x509.h:61
Container for an X.509 certificate.
Definition: x509_crt.h:53
#define POLARSSL_ERR_OID_NOT_FOUND
OID is not found.
Definition: oid.h:46
Privacy Enhanced Mail (PEM) decoding.
x509_time valid_from
Start time of certificate validity.
Definition: x509_crt.h:68
int x509_dn_gets(char *buf, size_t size, const x509_name *dn)
Store the certificate DN in printable form into buf; no more than size characters will be written...
void x509_crl_free(x509_crl *crl)
Unallocate all CRL data.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
x509_name subject
The parsed subject data (named information object).
Definition: x509_crt.h:66
int x509_crt_verify(x509_crt *crt, x509_crt *trust_ca, x509_crl *ca_crl, const char *cn, int *flags, int(*f_vrfy)(void *, x509_crt *, int, int *), void *p_vrfy)
Verify the certificate signature.
x509_time valid_to
End time of certificate validity.
Definition: x509_crt.h:69
X.509 certificate parsing and writing.
#define POLARSSL_ERR_X509_INVALID_ALG
The algorithm tag or value is invalid.
Definition: x509.h:53
int parse_arguments(char *buf, size_t len, char *params[50])
#define POLARSSL_ERR_ASN1_OUT_OF_DATA
Out of data when parsing an ASN1 data structure.
Definition: asn1.h:50
#define BADCERT_NOT_TRUSTED
The certificate is not correctly signed by the trusted CA.
Definition: x509.h:75
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
static int unhexify(unsigned char *obuf, const char *ibuf)
t_uint * p
Definition: bignum.h:175
int verify_string(char **str)
x509_name issuer
The parsed issuer data (named information object).
Definition: x509_crt.h:65
#define POLARSSL_ERR_X509_INVALID_NAME
The name tag or value is invalid.
Definition: x509.h:54
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
#define BADCERT_REVOKED
The certificate has been revoked (is on a CRL).
Definition: x509.h:73
#define BADCRL_EXPIRED
CRL is expired.
Definition: x509.h:77
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
X.509 certificate revocation list parsing.
#define POLARSSL_ERR_X509_INVALID_VERSION
The CRT/CRL/CSR version element is invalid.
Definition: x509.h:51
unsigned char * buf
Certificate revocation list structure.
Definition: x509_crl.h:69
#define POLARSSL_ERR_X509_INVALID_EXTENSIONS
The extension tag or value is invalid.
Definition: x509.h:57
#define PUT_UINT32_BE(n, b, i)
#define BADCERT_CN_MISMATCH
The certificate Common Name (CN) does not match with the expected CN.
Definition: x509.h:74
int x509_self_test(int verbose)
Checkup routine.
int verify_int(char *str, int *value)
int x509_crt_info(char *buf, size_t size, const char *prefix, const x509_crt *crt)
Returns an informational string about the certificate.
#define POLARSSL_ERR_X509_INVALID_SERIAL
The serial tag or value is invalid.
Definition: x509.h:52
#define POLARSSL_ERR_PK_INVALID_PUBKEY
The pubkey tag or value is invalid (only RSA and EC are supported).
Definition: pk.h:56
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
Definition: asn1.h:51
#define POLARSSL_ERR_PK_UNKNOWN_PK_ALG
Key algorithm is unsupported (only RSA and EC are supported).
Definition: pk.h:53
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
#define POLARSSL_ERR_X509_SIG_MISMATCH
Signature algorithms do not match.
Definition: x509.h:60
int get_line(FILE *f, char *buf, size_t len)
int x509_crt_parse_file(x509_crt *chain, const char *path)
Load one or more certificates and add them to the chained list.