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