PolarSSL v1.3.2
test_suite_x509write.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_BIGNUM_C
4 #ifdef POLARSSL_FS_IO
5 #ifdef POLARSSL_PK_PARSE_C
6 
7 #include <polarssl/x509_crt.h>
8 #include <polarssl/x509_csr.h>
9 #include <polarssl/pem.h>
10 #include <polarssl/oid.h>
11 #endif /* POLARSSL_BIGNUM_C */
12 #endif /* POLARSSL_FS_IO */
13 #endif /* POLARSSL_PK_PARSE_C */
14 
15 
16 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17 #include "polarssl/memory.h"
18 #endif
19 
20 #if defined(WANT_NOT_RND_MPI)
21 #if defined(POLARSSL_BIGNUM_C)
22 #include "polarssl/bignum.h"
23 #else
24 #error "not_rnd_mpi() need bignum.c"
25 #endif
26 #endif
27 
28 #ifdef _MSC_VER
29 #include <basetsd.h>
30 typedef UINT32 uint32_t;
31 #else
32 #include <inttypes.h>
33 #endif
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 /*
40  * 32-bit integer manipulation macros (big endian)
41  */
42 #ifndef GET_UINT32_BE
43 #define GET_UINT32_BE(n,b,i) \
44 { \
45  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
46  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
47  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
48  | ( (uint32_t) (b)[(i) + 3] ); \
49 }
50 #endif
51 
52 #ifndef PUT_UINT32_BE
53 #define PUT_UINT32_BE(n,b,i) \
54 { \
55  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58  (b)[(i) + 3] = (unsigned char) ( (n) ); \
59 }
60 #endif
61 
62 static int unhexify(unsigned char *obuf, const char *ibuf)
63 {
64  unsigned char c, c2;
65  int len = strlen(ibuf) / 2;
66  assert(!(strlen(ibuf) %1)); // must be even number of bytes
67 
68  while (*ibuf != 0)
69  {
70  c = *ibuf++;
71  if( c >= '0' && c <= '9' )
72  c -= '0';
73  else if( c >= 'a' && c <= 'f' )
74  c -= 'a' - 10;
75  else if( c >= 'A' && c <= 'F' )
76  c -= 'A' - 10;
77  else
78  assert( 0 );
79 
80  c2 = *ibuf++;
81  if( c2 >= '0' && c2 <= '9' )
82  c2 -= '0';
83  else if( c2 >= 'a' && c2 <= 'f' )
84  c2 -= 'a' - 10;
85  else if( c2 >= 'A' && c2 <= 'F' )
86  c2 -= 'A' - 10;
87  else
88  assert( 0 );
89 
90  *obuf++ = ( c << 4 ) | c2;
91  }
92 
93  return len;
94 }
95 
96 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
97 {
98  unsigned char l, h;
99 
100  while (len != 0)
101  {
102  h = (*ibuf) / 16;
103  l = (*ibuf) % 16;
104 
105  if( h < 10 )
106  *obuf++ = '0' + h;
107  else
108  *obuf++ = 'a' + h - 10;
109 
110  if( l < 10 )
111  *obuf++ = '0' + l;
112  else
113  *obuf++ = 'a' + l - 10;
114 
115  ++ibuf;
116  len--;
117  }
118 }
119 
129 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
130 {
131  size_t i;
132 
133  if( rng_state != NULL )
134  rng_state = NULL;
135 
136  for( i = 0; i < len; ++i )
137  output[i] = rand();
138 
139  return( 0 );
140 }
141 
147 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
148 {
149  if( rng_state != NULL )
150  rng_state = NULL;
151 
152  memset( output, 0, len );
153 
154  return( 0 );
155 }
156 
157 typedef struct
158 {
159  unsigned char *buf;
160  size_t length;
161 } rnd_buf_info;
162 
174 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
175 {
176  rnd_buf_info *info = (rnd_buf_info *) rng_state;
177  size_t use_len;
178 
179  if( rng_state == NULL )
180  return( rnd_std_rand( NULL, output, len ) );
181 
182  use_len = len;
183  if( len > info->length )
184  use_len = info->length;
185 
186  if( use_len )
187  {
188  memcpy( output, info->buf, use_len );
189  info->buf += use_len;
190  info->length -= use_len;
191  }
192 
193  if( len - use_len > 0 )
194  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
195 
196  return( 0 );
197 }
198 
206 typedef struct
207 {
208  uint32_t key[16];
209  uint32_t v0, v1;
211 
220 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
221 {
222  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
223  uint32_t i, *k, sum, delta=0x9E3779B9;
224  unsigned char result[4];
225 
226  if( rng_state == NULL )
227  return( rnd_std_rand( NULL, output, len ) );
228 
229  k = info->key;
230 
231  while( len > 0 )
232  {
233  size_t use_len = ( len > 4 ) ? 4 : len;
234  sum = 0;
235 
236  for( i = 0; i < 32; i++ )
237  {
238  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
239  sum += delta;
240  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
241  }
242 
243  PUT_UINT32_BE( info->v0, result, 0 );
244  memcpy( output, result, use_len );
245  len -= use_len;
246  }
247 
248  return( 0 );
249 }
250 
251 #if defined(WANT_NOT_RND_MPI)
252 
260 #define ciL (sizeof(t_uint)) /* chars in limb */
261 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
262 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
263 {
264  char *str = (char *) in;
265  mpi X;
266 
267  /*
268  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
269  * just reconstruct the rest in order to be able to call mpi_read_string()
270  */
271  X.s = 1;
272  X.p = (t_uint *) out;
273  X.n = CHARS_TO_LIMBS( len );
274 
275  /*
276  * If str is too long, mpi_read_string() will try to allocate a new buffer
277  * for X.p, which we want to avoid at all costs.
278  */
279  assert( strlen( str ) / 2 == len );
280 
281  return( mpi_read_string( &X, 16, str ) );
282 }
283 #endif /* WANT_NOT_RND_MPI */
284 
285 
286 #include <stdio.h>
287 #include <string.h>
288 
289 static int test_errors = 0;
290 
291 #ifdef POLARSSL_BIGNUM_C
292 #ifdef POLARSSL_FS_IO
293 #ifdef POLARSSL_PK_PARSE_C
294 
295 #define TEST_SUITE_ACTIVE
296 
297 static int test_assert( int correct, char *test )
298 {
299  if( correct )
300  return( 0 );
301 
302  test_errors++;
303  if( test_errors == 1 )
304  printf( "FAILED\n" );
305  printf( " %s\n", test );
306 
307  return( 1 );
308 }
309 
310 #define TEST_ASSERT( TEST ) \
311  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
312  if( test_errors) return; \
313  } while (0)
314 
315 int verify_string( char **str )
316 {
317  if( (*str)[0] != '"' ||
318  (*str)[strlen( *str ) - 1] != '"' )
319  {
320  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
321  return( -1 );
322  }
323 
324  (*str)++;
325  (*str)[strlen( *str ) - 1] = '\0';
326 
327  return( 0 );
328 }
329 
330 int verify_int( char *str, int *value )
331 {
332  size_t i;
333  int minus = 0;
334  int digits = 1;
335  int hex = 0;
336 
337  for( i = 0; i < strlen( str ); i++ )
338  {
339  if( i == 0 && str[i] == '-' )
340  {
341  minus = 1;
342  continue;
343  }
344 
345  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
346  str[i - 1] == '0' && str[i] == 'x' )
347  {
348  hex = 1;
349  continue;
350  }
351 
352  if( str[i] < '0' || str[i] > '9' )
353  {
354  digits = 0;
355  break;
356  }
357  }
358 
359  if( digits )
360  {
361  if( hex )
362  *value = strtol( str, NULL, 16 );
363  else
364  *value = strtol( str, NULL, 10 );
365 
366  return( 0 );
367  }
368 
369 #ifdef POLARSSL_PEM_WRITE_C
370 #ifdef POLARSSL_X509_CSR_WRITE_C
371  if( strcmp( str, "POLARSSL_MD_SHA224" ) == 0 )
372  {
373  *value = ( POLARSSL_MD_SHA224 );
374  return( 0 );
375  }
376 #endif // POLARSSL_PEM_WRITE_C
377 #endif // POLARSSL_X509_CSR_WRITE_C
378 #ifdef POLARSSL_PEM_WRITE_C
379 #ifdef POLARSSL_X509_CSR_WRITE_C
380  if( strcmp( str, "POLARSSL_MD_SHA384" ) == 0 )
381  {
382  *value = ( POLARSSL_MD_SHA384 );
383  return( 0 );
384  }
385 #endif // POLARSSL_PEM_WRITE_C
386 #endif // POLARSSL_X509_CSR_WRITE_C
387 #ifdef POLARSSL_PEM_WRITE_C
388 #ifdef POLARSSL_X509_CSR_WRITE_C
389  if( strcmp( str, "POLARSSL_MD_MD4" ) == 0 )
390  {
391  *value = ( POLARSSL_MD_MD4 );
392  return( 0 );
393  }
394 #endif // POLARSSL_PEM_WRITE_C
395 #endif // POLARSSL_X509_CSR_WRITE_C
396 #ifdef POLARSSL_PEM_WRITE_C
397 #ifdef POLARSSL_X509_CSR_WRITE_C
398  if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
399  {
400  *value = ( POLARSSL_MD_SHA1 );
401  return( 0 );
402  }
403 #endif // POLARSSL_PEM_WRITE_C
404 #endif // POLARSSL_X509_CSR_WRITE_C
405 #ifdef POLARSSL_PEM_WRITE_C
406 #ifdef POLARSSL_X509_CRT_WRITE_C
407 #ifdef POLARSSL_SHA1_C
408  if( strcmp( str, "POLARSSL_MD_SHA1" ) == 0 )
409  {
410  *value = ( POLARSSL_MD_SHA1 );
411  return( 0 );
412  }
413 #endif // POLARSSL_PEM_WRITE_C
414 #endif // POLARSSL_X509_CRT_WRITE_C
415 #endif // POLARSSL_SHA1_C
416 #ifdef POLARSSL_PEM_WRITE_C
417 #ifdef POLARSSL_X509_CSR_WRITE_C
418  if( strcmp( str, "POLARSSL_MD_SHA512" ) == 0 )
419  {
420  *value = ( POLARSSL_MD_SHA512 );
421  return( 0 );
422  }
423 #endif // POLARSSL_PEM_WRITE_C
424 #endif // POLARSSL_X509_CSR_WRITE_C
425 #ifdef POLARSSL_PEM_WRITE_C
426 #ifdef POLARSSL_X509_CSR_WRITE_C
427  if( strcmp( str, "POLARSSL_MD_SHA256" ) == 0 )
428  {
429  *value = ( POLARSSL_MD_SHA256 );
430  return( 0 );
431  }
432 #endif // POLARSSL_PEM_WRITE_C
433 #endif // POLARSSL_X509_CSR_WRITE_C
434 #ifdef POLARSSL_PEM_WRITE_C
435 #ifdef POLARSSL_X509_CSR_WRITE_C
436  if( strcmp( str, "POLARSSL_MD_MD5" ) == 0 )
437  {
438  *value = ( POLARSSL_MD_MD5 );
439  return( 0 );
440  }
441 #endif // POLARSSL_PEM_WRITE_C
442 #endif // POLARSSL_X509_CSR_WRITE_C
443 
444 
445  printf( "Expected integer for parameter and got: %s\n", str );
446  return( -1 );
447 }
448 
449 #ifdef POLARSSL_PEM_WRITE_C
450 #ifdef POLARSSL_X509_CSR_WRITE_C
451 void test_suite_x509_csr_check( char *key_file, int md_type,
452  char *cert_req_check_file )
453 {
454  pk_context key;
455  x509write_csr req;
456  unsigned char buf[4000];
457  unsigned char check_buf[4000];
458  int ret;
459  size_t olen = 0, pem_len = 0;
460  FILE *f;
461  char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
462  rnd_pseudo_info rnd_info;
463 
464  memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
465 
466  pk_init( &key );
467  TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
468 
469  x509write_csr_init( &req );
470  x509write_csr_set_md_alg( &req, md_type );
471  x509write_csr_set_key( &req, &key );
472  TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
473 
474  ret = x509write_csr_pem( &req, buf, sizeof(buf),
475  rnd_pseudo_rand, &rnd_info );
476  TEST_ASSERT( ret == 0 );
477 
478  pem_len = strlen( (char *) buf );
479 
480  f = fopen( cert_req_check_file, "r" );
481  TEST_ASSERT( f != NULL );
482  olen = fread( check_buf, 1, sizeof( check_buf ), f );
483  fclose( f );
484 
485  TEST_ASSERT( olen >= pem_len - 1 );
486  TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
487 
488  x509write_csr_free( &req );
489  pk_free( &key );
490 }
491 #endif /* POLARSSL_PEM_WRITE_C */
492 #endif /* POLARSSL_X509_CSR_WRITE_C */
493 
494 #ifdef POLARSSL_PEM_WRITE_C
495 #ifdef POLARSSL_X509_CRT_WRITE_C
496 #ifdef POLARSSL_SHA1_C
497 void test_suite_x509_crt_check( char *subject_key_file, char *subject_pwd,
498  char *subject_name, char *issuer_key_file,
499  char *issuer_pwd, char *issuer_name,
500  char *serial_str, char *not_before, char *not_after,
501  int md_type, char *cert_check_file )
502 {
503  pk_context subject_key, issuer_key;
504  x509write_cert crt;
505  unsigned char buf[4000];
506  unsigned char check_buf[5000];
507  mpi serial;
508  int ret;
509  size_t olen = 0, pem_len = 0;
510  FILE *f;
511  rnd_pseudo_info rnd_info;
512 
513  memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
514  mpi_init( &serial );
515  pk_init( &subject_key );
516  pk_init( &issuer_key );
517 
518  TEST_ASSERT( pk_parse_keyfile( &subject_key, subject_key_file,
519  subject_pwd ) == 0 );
520  TEST_ASSERT( pk_parse_keyfile( &issuer_key, issuer_key_file,
521  issuer_pwd ) == 0 );
522  TEST_ASSERT( mpi_read_string( &serial, 10, serial_str ) == 0 );
523 
524  x509write_crt_init( &crt );
525  x509write_crt_set_serial( &crt, &serial );
526  TEST_ASSERT( x509write_crt_set_validity( &crt, not_before,
527  not_after ) == 0 );
528  x509write_crt_set_md_alg( &crt, md_type );
529  TEST_ASSERT( x509write_crt_set_issuer_name( &crt, issuer_name ) == 0 );
530  TEST_ASSERT( x509write_crt_set_subject_name( &crt, subject_name ) == 0 );
531  x509write_crt_set_subject_key( &crt, &subject_key );
532  x509write_crt_set_issuer_key( &crt, &issuer_key );
533 
534  TEST_ASSERT( x509write_crt_set_basic_constraints( &crt, 0, 0 ) == 0 );
537 
538  ret = x509write_crt_pem( &crt, buf, sizeof(buf),
539  rnd_pseudo_rand, &rnd_info );
540  TEST_ASSERT( ret == 0 );
541 
542  pem_len = strlen( (char *) buf );
543 
544  f = fopen( cert_check_file, "r" );
545  TEST_ASSERT( f != NULL );
546  TEST_ASSERT( ( olen = fread( check_buf, 1, sizeof(check_buf), f ) ) <
547  sizeof(check_buf) );
548  fclose( f );
549 
550  TEST_ASSERT( olen >= pem_len - 1 );
551  TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
552 
553  x509write_crt_free( &crt );
554  pk_free( &issuer_key );
555  pk_free( &subject_key );
556  mpi_free( &serial );
557 }
558 #endif /* POLARSSL_PEM_WRITE_C */
559 #endif /* POLARSSL_X509_CRT_WRITE_C */
560 #endif /* POLARSSL_SHA1_C */
561 
562 
563 #endif /* POLARSSL_BIGNUM_C */
564 #endif /* POLARSSL_FS_IO */
565 #endif /* POLARSSL_PK_PARSE_C */
566 
567 
568 int dep_check( char *str )
569 {
570  if( str == NULL )
571  return( 1 );
572 
573  if( strcmp( str, "POLARSSL_SHA256_C" ) == 0 )
574  {
575 #if defined(POLARSSL_SHA256_C)
576  return( 0 );
577 #else
578  return( 1 );
579 #endif
580  }
581  if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
582  {
583 #if defined(POLARSSL_CIPHER_MODE_CBC)
584  return( 0 );
585 #else
586  return( 1 );
587 #endif
588  }
589  if( strcmp( str, "POLARSSL_MD4_C" ) == 0 )
590  {
591 #if defined(POLARSSL_MD4_C)
592  return( 0 );
593 #else
594  return( 1 );
595 #endif
596  }
597  if( strcmp( str, "POLARSSL_DES_C" ) == 0 )
598  {
599 #if defined(POLARSSL_DES_C)
600  return( 0 );
601 #else
602  return( 1 );
603 #endif
604  }
605  if( strcmp( str, "POLARSSL_PKCS1_V15" ) == 0 )
606  {
607 #if defined(POLARSSL_PKCS1_V15)
608  return( 0 );
609 #else
610  return( 1 );
611 #endif
612  }
613  if( strcmp( str, "POLARSSL_MD5_C" ) == 0 )
614  {
615 #if defined(POLARSSL_MD5_C)
616  return( 0 );
617 #else
618  return( 1 );
619 #endif
620  }
621  if( strcmp( str, "POLARSSL_SHA512_C" ) == 0 )
622  {
623 #if defined(POLARSSL_SHA512_C)
624  return( 0 );
625 #else
626  return( 1 );
627 #endif
628  }
629  if( strcmp( str, "POLARSSL_SHA1_C" ) == 0 )
630  {
631 #if defined(POLARSSL_SHA1_C)
632  return( 0 );
633 #else
634  return( 1 );
635 #endif
636  }
637  if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
638  {
639 #if defined(POLARSSL_RSA_C)
640  return( 0 );
641 #else
642  return( 1 );
643 #endif
644  }
645 
646 
647  return( 1 );
648 }
649 
650 int dispatch_test(int cnt, char *params[50])
651 {
652  int ret;
653  ((void) cnt);
654  ((void) params);
655 
656 #if defined(TEST_SUITE_ACTIVE)
657  if( strcmp( params[0], "x509_csr_check" ) == 0 )
658  {
659  #ifdef POLARSSL_PEM_WRITE_C
660  #ifdef POLARSSL_X509_CSR_WRITE_C
661 
662  char *param1 = params[1];
663  int param2;
664  char *param3 = params[3];
665 
666  if( cnt != 4 )
667  {
668  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
669  return( 2 );
670  }
671 
672  if( verify_string( &param1 ) != 0 ) return( 2 );
673  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
674  if( verify_string( &param3 ) != 0 ) return( 2 );
675 
676  test_suite_x509_csr_check( param1, param2, param3 );
677  return ( 0 );
678  #endif /* POLARSSL_PEM_WRITE_C */
679  #endif /* POLARSSL_X509_CSR_WRITE_C */
680 
681  return ( 3 );
682  }
683  else
684  if( strcmp( params[0], "x509_crt_check" ) == 0 )
685  {
686  #ifdef POLARSSL_PEM_WRITE_C
687  #ifdef POLARSSL_X509_CRT_WRITE_C
688  #ifdef POLARSSL_SHA1_C
689 
690  char *param1 = params[1];
691  char *param2 = params[2];
692  char *param3 = params[3];
693  char *param4 = params[4];
694  char *param5 = params[5];
695  char *param6 = params[6];
696  char *param7 = params[7];
697  char *param8 = params[8];
698  char *param9 = params[9];
699  int param10;
700  char *param11 = params[11];
701 
702  if( cnt != 12 )
703  {
704  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 12 );
705  return( 2 );
706  }
707 
708  if( verify_string( &param1 ) != 0 ) return( 2 );
709  if( verify_string( &param2 ) != 0 ) return( 2 );
710  if( verify_string( &param3 ) != 0 ) return( 2 );
711  if( verify_string( &param4 ) != 0 ) return( 2 );
712  if( verify_string( &param5 ) != 0 ) return( 2 );
713  if( verify_string( &param6 ) != 0 ) return( 2 );
714  if( verify_string( &param7 ) != 0 ) return( 2 );
715  if( verify_string( &param8 ) != 0 ) return( 2 );
716  if( verify_string( &param9 ) != 0 ) return( 2 );
717  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
718  if( verify_string( &param11 ) != 0 ) return( 2 );
719 
720  test_suite_x509_crt_check( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
721  return ( 0 );
722  #endif /* POLARSSL_PEM_WRITE_C */
723  #endif /* POLARSSL_X509_CRT_WRITE_C */
724  #endif /* POLARSSL_SHA1_C */
725 
726  return ( 3 );
727  }
728  else
729 
730  {
731  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
732  fflush( stdout );
733  return( 1 );
734  }
735 #else
736  return( 3 );
737 #endif
738  return( ret );
739 }
740 
741 int get_line( FILE *f, char *buf, size_t len )
742 {
743  char *ret;
744 
745  ret = fgets( buf, len, f );
746  if( ret == NULL )
747  return( -1 );
748 
749  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
750  buf[strlen(buf) - 1] = '\0';
751  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
752  buf[strlen(buf) - 1] = '\0';
753 
754  return( 0 );
755 }
756 
757 int parse_arguments( char *buf, size_t len, char *params[50] )
758 {
759  int cnt = 0, i;
760  char *cur = buf;
761  char *p = buf, *q;
762 
763  params[cnt++] = cur;
764 
765  while( *p != '\0' && p < buf + len )
766  {
767  if( *p == '\\' )
768  {
769  *p++;
770  *p++;
771  continue;
772  }
773  if( *p == ':' )
774  {
775  if( p + 1 < buf + len )
776  {
777  cur = p + 1;
778  params[cnt++] = cur;
779  }
780  *p = '\0';
781  }
782 
783  *p++;
784  }
785 
786  // Replace newlines, question marks and colons in strings
787  for( i = 0; i < cnt; i++ )
788  {
789  p = params[i];
790  q = params[i];
791 
792  while( *p != '\0' )
793  {
794  if( *p == '\\' && *(p + 1) == 'n' )
795  {
796  p += 2;
797  *(q++) = '\n';
798  }
799  else if( *p == '\\' && *(p + 1) == ':' )
800  {
801  p += 2;
802  *(q++) = ':';
803  }
804  else if( *p == '\\' && *(p + 1) == '?' )
805  {
806  p += 2;
807  *(q++) = '?';
808  }
809  else
810  *(q++) = *(p++);
811  }
812  *q = '\0';
813  }
814 
815  return( cnt );
816 }
817 
818 int main()
819 {
820  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
821  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_x509write.data";
822  FILE *file;
823  char buf[5000];
824  char *params[50];
825 
826 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
827  unsigned char alloc_buf[1000000];
828  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
829 #endif
830 
831  file = fopen( filename, "r" );
832  if( file == NULL )
833  {
834  fprintf( stderr, "Failed to open\n" );
835  return( 1 );
836  }
837 
838  while( !feof( file ) )
839  {
840  int skip = 0;
841 
842  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
843  break;
844  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
845  fprintf( stdout, " " );
846  for( i = strlen( buf ) + 1; i < 67; i++ )
847  fprintf( stdout, "." );
848  fprintf( stdout, " " );
849  fflush( stdout );
850 
851  total_tests++;
852 
853  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
854  break;
855  cnt = parse_arguments( buf, strlen(buf), params );
856 
857  if( strcmp( params[0], "depends_on" ) == 0 )
858  {
859  for( i = 1; i < cnt; i++ )
860  if( dep_check( params[i] ) != 0 )
861  skip = 1;
862 
863  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
864  break;
865  cnt = parse_arguments( buf, strlen(buf), params );
866  }
867 
868  if( skip == 0 )
869  {
870  test_errors = 0;
871  ret = dispatch_test( cnt, params );
872  }
873 
874  if( skip == 1 || ret == 3 )
875  {
876  total_skipped++;
877  fprintf( stdout, "----\n" );
878  fflush( stdout );
879  }
880  else if( ret == 0 && test_errors == 0 )
881  {
882  fprintf( stdout, "PASS\n" );
883  fflush( stdout );
884  }
885  else if( ret == 2 )
886  {
887  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
888  fclose(file);
889  exit( 2 );
890  }
891  else
892  total_errors++;
893 
894  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
895  break;
896  if( strlen(buf) != 0 )
897  {
898  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
899  return( 1 );
900  }
901  }
902  fclose(file);
903 
904  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
905  if( total_errors == 0 )
906  fprintf( stdout, "PASSED" );
907  else
908  fprintf( stdout, "FAILED" );
909 
910  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
911  total_tests - total_errors, total_tests, total_skipped );
912 
913 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
914 #if defined(POLARSSL_MEMORY_DEBUG)
915  memory_buffer_alloc_status();
916 #endif
917  memory_buffer_alloc_free();
918 #endif
919 
920  return( total_errors != 0 );
921 }
922 
923 
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 x509write_csr_free(x509write_csr *ctx)
Free the contents of a CSR context.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int x509write_crt_set_validity(x509write_cert *ctx, const char *not_before, const char *not_after)
Set the validity period for a Certificate Timestamps should be in string format for UTC timezone i...
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
int s
Definition: bignum.h:173
void x509write_csr_set_md_alg(x509write_csr *ctx, md_type_t md_alg)
Set the MD algorithm to use for the signature (e.g.
Configuration options (set of defines)
int x509write_csr_pem(x509write_csr *ctx, unsigned char *buf, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Write a CSR (Certificate Signing Request) to a PEM string.
void x509write_crt_set_md_alg(x509write_cert *ctx, md_type_t md_alg)
Set the MD algorithm to use for the signature (e.g.
#define PUT_UINT32_BE(n, b, i)
MPI structure.
Definition: bignum.h:171
static int test_assert(int correct, char *test)
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
static int test_errors
Object Identifier (OID) database.
Multi-precision integer library.
int dep_check(char *str)
#define TEST_ASSERT(TEST)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
void x509write_crt_free(x509write_cert *ctx)
Free the contents of a CRT write context.
Privacy Enhanced Mail (PEM) decoding.
int x509write_crt_set_subject_key_identifier(x509write_cert *ctx)
Set the subjectKeyIdentifier extension for a CRT Requires that x509write_crt_set_subject_key() has be...
int x509write_crt_set_issuer_name(x509write_cert *ctx, const char *issuer_name)
Set the issuer name for a Certificate Issuer names should contain a comma-separated list of OID types...
X.509 certificate signing request parsing and writing.
void mpi_free(mpi *X)
Unallocate one MPI.
void x509write_csr_init(x509write_csr *ctx)
Initialize a CSR context.
X.509 certificate parsing and writing.
void x509write_crt_set_issuer_key(x509write_cert *ctx, pk_context *key)
Set the issuer key used for signing the certificate.
int parse_arguments(char *buf, size_t len, char *params[50])
int x509write_crt_set_serial(x509write_cert *ctx, const mpi *serial)
Set the serial number for a Certificate.
void x509write_csr_set_key(x509write_csr *ctx, pk_context *key)
Set the key for a CSR (public key will be included, private key used to sign the CSR when writing it)...
int x509write_crt_set_authority_key_identifier(x509write_cert *ctx)
Set the authorityKeyIdentifier extension for a CRT Requires that x509write_crt_set_issuer_key() has b...
Container for writing a certificate (CRT)
Definition: x509_crt.h:107
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
t_uint * p
Definition: bignum.h:175
int x509write_crt_set_subject_name(x509write_cert *ctx, const char *subject_name)
Set the subject name for a Certificate Subject names should contain a comma-separated list of OID typ...
int verify_string(char **str)
void pk_free(pk_context *ctx)
Free a pk_context.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int x509write_crt_set_basic_constraints(x509write_cert *ctx, int is_ca, int max_pathlen)
Set the basicConstraints extension for a CRT.
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 x509write_csr_set_subject_name(x509write_csr *ctx, const char *subject_name)
Set the subject name for a CSR Subject names should contain a comma-separated list of OID types and v...
void x509write_crt_init(x509write_cert *ctx)
Initialize a CRT writing context.
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
int verify_int(char *str, int *value)
static int unhexify(unsigned char *obuf, const char *ibuf)
void x509write_crt_set_subject_key(x509write_cert *ctx, pk_context *key)
Set the subject public key for the certificate.
int pk_parse_keyfile(pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
int x509write_crt_pem(x509write_cert *ctx, unsigned char *buf, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Write a built up certificate to a X509 PEM string.
Public key container.
Definition: pk.h:177
int get_line(FILE *f, char *buf, size_t len)
Container for writing a CSR.
Definition: x509_csr.h:72