PolarSSL v1.3.2
test_suite_debug.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_DEBUG_C
4 #ifdef POLARSSL_BIGNUM_C
5 #ifdef POLARSSL_SSL_TLS_C
6 #ifdef POLARSSL_RSA_C
7 
8 #include <polarssl/debug.h>
9 
10 struct buffer_data
11 {
12  char buf[2000];
13  char *ptr;
14 };
15 
16 void string_debug(void *data, int level, const char *str)
17 {
18  struct buffer_data *buffer = (struct buffer_data *) data;
19  ((void) level);
20 
21  memcpy(buffer->ptr, str, strlen(str));
22  buffer->ptr += strlen(str);
23 }
24 #endif /* POLARSSL_DEBUG_C */
25 #endif /* POLARSSL_BIGNUM_C */
26 #endif /* POLARSSL_SSL_TLS_C */
27 #endif /* POLARSSL_RSA_C */
28 
29 
30 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
31 #include "polarssl/memory.h"
32 #endif
33 
34 #if defined(WANT_NOT_RND_MPI)
35 #if defined(POLARSSL_BIGNUM_C)
36 #include "polarssl/bignum.h"
37 #else
38 #error "not_rnd_mpi() need bignum.c"
39 #endif
40 #endif
41 
42 #ifdef _MSC_VER
43 #include <basetsd.h>
44 typedef UINT32 uint32_t;
45 #else
46 #include <inttypes.h>
47 #endif
48 
49 #include <assert.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 /*
54  * 32-bit integer manipulation macros (big endian)
55  */
56 #ifndef GET_UINT32_BE
57 #define GET_UINT32_BE(n,b,i) \
58 { \
59  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
60  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
61  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
62  | ( (uint32_t) (b)[(i) + 3] ); \
63 }
64 #endif
65 
66 #ifndef PUT_UINT32_BE
67 #define PUT_UINT32_BE(n,b,i) \
68 { \
69  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
70  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
71  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
72  (b)[(i) + 3] = (unsigned char) ( (n) ); \
73 }
74 #endif
75 
76 static int unhexify(unsigned char *obuf, const char *ibuf)
77 {
78  unsigned char c, c2;
79  int len = strlen(ibuf) / 2;
80  assert(!(strlen(ibuf) %1)); // must be even number of bytes
81 
82  while (*ibuf != 0)
83  {
84  c = *ibuf++;
85  if( c >= '0' && c <= '9' )
86  c -= '0';
87  else if( c >= 'a' && c <= 'f' )
88  c -= 'a' - 10;
89  else if( c >= 'A' && c <= 'F' )
90  c -= 'A' - 10;
91  else
92  assert( 0 );
93 
94  c2 = *ibuf++;
95  if( c2 >= '0' && c2 <= '9' )
96  c2 -= '0';
97  else if( c2 >= 'a' && c2 <= 'f' )
98  c2 -= 'a' - 10;
99  else if( c2 >= 'A' && c2 <= 'F' )
100  c2 -= 'A' - 10;
101  else
102  assert( 0 );
103 
104  *obuf++ = ( c << 4 ) | c2;
105  }
106 
107  return len;
108 }
109 
110 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
111 {
112  unsigned char l, h;
113 
114  while (len != 0)
115  {
116  h = (*ibuf) / 16;
117  l = (*ibuf) % 16;
118 
119  if( h < 10 )
120  *obuf++ = '0' + h;
121  else
122  *obuf++ = 'a' + h - 10;
123 
124  if( l < 10 )
125  *obuf++ = '0' + l;
126  else
127  *obuf++ = 'a' + l - 10;
128 
129  ++ibuf;
130  len--;
131  }
132 }
133 
143 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
144 {
145  size_t i;
146 
147  if( rng_state != NULL )
148  rng_state = NULL;
149 
150  for( i = 0; i < len; ++i )
151  output[i] = rand();
152 
153  return( 0 );
154 }
155 
161 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
162 {
163  if( rng_state != NULL )
164  rng_state = NULL;
165 
166  memset( output, 0, len );
167 
168  return( 0 );
169 }
170 
171 typedef struct
172 {
173  unsigned char *buf;
174  size_t length;
175 } rnd_buf_info;
176 
188 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
189 {
190  rnd_buf_info *info = (rnd_buf_info *) rng_state;
191  size_t use_len;
192 
193  if( rng_state == NULL )
194  return( rnd_std_rand( NULL, output, len ) );
195 
196  use_len = len;
197  if( len > info->length )
198  use_len = info->length;
199 
200  if( use_len )
201  {
202  memcpy( output, info->buf, use_len );
203  info->buf += use_len;
204  info->length -= use_len;
205  }
206 
207  if( len - use_len > 0 )
208  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
209 
210  return( 0 );
211 }
212 
220 typedef struct
221 {
222  uint32_t key[16];
223  uint32_t v0, v1;
225 
234 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
235 {
236  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
237  uint32_t i, *k, sum, delta=0x9E3779B9;
238  unsigned char result[4];
239 
240  if( rng_state == NULL )
241  return( rnd_std_rand( NULL, output, len ) );
242 
243  k = info->key;
244 
245  while( len > 0 )
246  {
247  size_t use_len = ( len > 4 ) ? 4 : len;
248  sum = 0;
249 
250  for( i = 0; i < 32; i++ )
251  {
252  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
253  sum += delta;
254  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
255  }
256 
257  PUT_UINT32_BE( info->v0, result, 0 );
258  memcpy( output, result, use_len );
259  len -= use_len;
260  }
261 
262  return( 0 );
263 }
264 
265 #if defined(WANT_NOT_RND_MPI)
266 
274 #define ciL (sizeof(t_uint)) /* chars in limb */
275 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
276 static int not_rnd_mpi( void *in, unsigned char *out, size_t len )
277 {
278  char *str = (char *) in;
279  mpi X;
280 
281  /*
282  * The 'in' pointer we get is from an MPI prepared by mpi_fill_random(),
283  * just reconstruct the rest in order to be able to call mpi_read_string()
284  */
285  X.s = 1;
286  X.p = (t_uint *) out;
287  X.n = CHARS_TO_LIMBS( len );
288 
289  /*
290  * If str is too long, mpi_read_string() will try to allocate a new buffer
291  * for X.p, which we want to avoid at all costs.
292  */
293  assert( strlen( str ) / 2 == len );
294 
295  return( mpi_read_string( &X, 16, str ) );
296 }
297 #endif /* WANT_NOT_RND_MPI */
298 
299 
300 #include <stdio.h>
301 #include <string.h>
302 
303 static int test_errors = 0;
304 
305 #ifdef POLARSSL_DEBUG_C
306 #ifdef POLARSSL_BIGNUM_C
307 #ifdef POLARSSL_SSL_TLS_C
308 #ifdef POLARSSL_RSA_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 
385 
386  printf( "Expected integer for parameter and got: %s\n", str );
387  return( -1 );
388 }
389 
390 #ifdef POLARSSL_FS_IO
391 #ifdef POLARSSL_X509_CRT_PARSE_C
392 void test_suite_debug_print_crt( char *crt_file, char *file, int line, char *prefix,
393  char *result_str )
394 {
395  x509_crt crt;
396  ssl_context ssl;
397  struct buffer_data buffer;
398 
399  x509_crt_init( &crt );
400  memset( &ssl, 0, sizeof( ssl_context ) );
401  memset( buffer.buf, 0, 2000 );
402  buffer.ptr = buffer.buf;
403 
404  ssl_set_dbg(&ssl, string_debug, &buffer);
405 
406  TEST_ASSERT( x509_crt_parse_file( &crt, crt_file ) == 0 );
407  debug_print_crt( &ssl, 0, file, line, prefix, &crt);
408 
409  TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
410 
411  x509_crt_free( &crt );
412 }
413 #endif /* POLARSSL_FS_IO */
414 #endif /* POLARSSL_X509_CRT_PARSE_C */
415 
416 void test_suite_debug_print_mpi( int radix, char *value, char *file, int line,
417  char *prefix, char *result_str )
418 {
419  ssl_context ssl;
420  struct buffer_data buffer;
421  mpi val;
422 
423  mpi_init( &val );
424 
425  memset( &ssl, 0, sizeof( ssl_context ) );
426  memset( buffer.buf, 0, 2000 );
427  buffer.ptr = buffer.buf;
428 
429  TEST_ASSERT( mpi_read_string( &val, radix, value ) == 0 );
430  ssl_set_dbg(&ssl, string_debug, &buffer);
431 
432  debug_print_mpi( &ssl, 0, file, line, prefix, &val);
433 
434  TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
435 
436  mpi_free( &val );
437 }
438 
439 
440 #endif /* POLARSSL_DEBUG_C */
441 #endif /* POLARSSL_BIGNUM_C */
442 #endif /* POLARSSL_SSL_TLS_C */
443 #endif /* POLARSSL_RSA_C */
444 
445 
446 int dep_check( char *str )
447 {
448  if( str == NULL )
449  return( 1 );
450 
451  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
452  {
453 #if defined(POLARSSL_ECP_C)
454  return( 0 );
455 #else
456  return( 1 );
457 #endif
458  }
459  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
460  {
461 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
462  return( 0 );
463 #else
464  return( 1 );
465 #endif
466  }
467  if( strcmp( str, "POLARSSL_PEM_C" ) == 0 )
468  {
469 #if defined(POLARSSL_PEM_C)
470  return( 0 );
471 #else
472  return( 1 );
473 #endif
474  }
475  if( strcmp( str, "POLARSSL_BASE64_C" ) == 0 )
476  {
477 #if defined(POLARSSL_BASE64_C)
478  return( 0 );
479 #else
480  return( 1 );
481 #endif
482  }
483 
484 
485  return( 1 );
486 }
487 
488 int dispatch_test(int cnt, char *params[50])
489 {
490  int ret;
491  ((void) cnt);
492  ((void) params);
493 
494 #if defined(TEST_SUITE_ACTIVE)
495  if( strcmp( params[0], "debug_print_crt" ) == 0 )
496  {
497  #ifdef POLARSSL_FS_IO
498  #ifdef POLARSSL_X509_CRT_PARSE_C
499 
500  char *param1 = params[1];
501  char *param2 = params[2];
502  int param3;
503  char *param4 = params[4];
504  char *param5 = params[5];
505 
506  if( cnt != 6 )
507  {
508  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
509  return( 2 );
510  }
511 
512  if( verify_string( &param1 ) != 0 ) return( 2 );
513  if( verify_string( &param2 ) != 0 ) return( 2 );
514  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
515  if( verify_string( &param4 ) != 0 ) return( 2 );
516  if( verify_string( &param5 ) != 0 ) return( 2 );
517 
518  test_suite_debug_print_crt( param1, param2, param3, param4, param5 );
519  return ( 0 );
520  #endif /* POLARSSL_FS_IO */
521  #endif /* POLARSSL_X509_CRT_PARSE_C */
522 
523  return ( 3 );
524  }
525  else
526  if( strcmp( params[0], "debug_print_mpi" ) == 0 )
527  {
528 
529  int param1;
530  char *param2 = params[2];
531  char *param3 = params[3];
532  int param4;
533  char *param5 = params[5];
534  char *param6 = params[6];
535 
536  if( cnt != 7 )
537  {
538  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
539  return( 2 );
540  }
541 
542  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
543  if( verify_string( &param2 ) != 0 ) return( 2 );
544  if( verify_string( &param3 ) != 0 ) return( 2 );
545  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
546  if( verify_string( &param5 ) != 0 ) return( 2 );
547  if( verify_string( &param6 ) != 0 ) return( 2 );
548 
549  test_suite_debug_print_mpi( param1, param2, param3, param4, param5, param6 );
550  return ( 0 );
551 
552  return ( 3 );
553  }
554  else
555 
556  {
557  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
558  fflush( stdout );
559  return( 1 );
560  }
561 #else
562  return( 3 );
563 #endif
564  return( ret );
565 }
566 
567 int get_line( FILE *f, char *buf, size_t len )
568 {
569  char *ret;
570 
571  ret = fgets( buf, len, f );
572  if( ret == NULL )
573  return( -1 );
574 
575  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
576  buf[strlen(buf) - 1] = '\0';
577  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
578  buf[strlen(buf) - 1] = '\0';
579 
580  return( 0 );
581 }
582 
583 int parse_arguments( char *buf, size_t len, char *params[50] )
584 {
585  int cnt = 0, i;
586  char *cur = buf;
587  char *p = buf, *q;
588 
589  params[cnt++] = cur;
590 
591  while( *p != '\0' && p < buf + len )
592  {
593  if( *p == '\\' )
594  {
595  *p++;
596  *p++;
597  continue;
598  }
599  if( *p == ':' )
600  {
601  if( p + 1 < buf + len )
602  {
603  cur = p + 1;
604  params[cnt++] = cur;
605  }
606  *p = '\0';
607  }
608 
609  *p++;
610  }
611 
612  // Replace newlines, question marks and colons in strings
613  for( i = 0; i < cnt; i++ )
614  {
615  p = params[i];
616  q = params[i];
617 
618  while( *p != '\0' )
619  {
620  if( *p == '\\' && *(p + 1) == 'n' )
621  {
622  p += 2;
623  *(q++) = '\n';
624  }
625  else if( *p == '\\' && *(p + 1) == ':' )
626  {
627  p += 2;
628  *(q++) = ':';
629  }
630  else if( *p == '\\' && *(p + 1) == '?' )
631  {
632  p += 2;
633  *(q++) = '?';
634  }
635  else
636  *(q++) = *(p++);
637  }
638  *q = '\0';
639  }
640 
641  return( cnt );
642 }
643 
644 int main()
645 {
646  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
647  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_debug.data";
648  FILE *file;
649  char buf[5000];
650  char *params[50];
651 
652 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
653  unsigned char alloc_buf[1000000];
654  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
655 #endif
656 
657  file = fopen( filename, "r" );
658  if( file == NULL )
659  {
660  fprintf( stderr, "Failed to open\n" );
661  return( 1 );
662  }
663 
664  while( !feof( file ) )
665  {
666  int skip = 0;
667 
668  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
669  break;
670  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
671  fprintf( stdout, " " );
672  for( i = strlen( buf ) + 1; i < 67; i++ )
673  fprintf( stdout, "." );
674  fprintf( stdout, " " );
675  fflush( stdout );
676 
677  total_tests++;
678 
679  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
680  break;
681  cnt = parse_arguments( buf, strlen(buf), params );
682 
683  if( strcmp( params[0], "depends_on" ) == 0 )
684  {
685  for( i = 1; i < cnt; i++ )
686  if( dep_check( params[i] ) != 0 )
687  skip = 1;
688 
689  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
690  break;
691  cnt = parse_arguments( buf, strlen(buf), params );
692  }
693 
694  if( skip == 0 )
695  {
696  test_errors = 0;
697  ret = dispatch_test( cnt, params );
698  }
699 
700  if( skip == 1 || ret == 3 )
701  {
702  total_skipped++;
703  fprintf( stdout, "----\n" );
704  fflush( stdout );
705  }
706  else if( ret == 0 && test_errors == 0 )
707  {
708  fprintf( stdout, "PASS\n" );
709  fflush( stdout );
710  }
711  else if( ret == 2 )
712  {
713  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
714  fclose(file);
715  exit( 2 );
716  }
717  else
718  total_errors++;
719 
720  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
721  break;
722  if( strlen(buf) != 0 )
723  {
724  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
725  return( 1 );
726  }
727  }
728  fclose(file);
729 
730  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
731  if( total_errors == 0 )
732  fprintf( stdout, "PASSED" );
733  else
734  fprintf( stdout, "FAILED" );
735 
736  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
737  total_tests - total_errors, total_tests, total_skipped );
738 
739 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
740 #if defined(POLARSSL_MEMORY_DEBUG)
741  memory_buffer_alloc_status();
742 #endif
743  memory_buffer_alloc_free();
744 #endif
745 
746  return( total_errors != 0 );
747 }
748 
749 
void debug_print_crt(const ssl_context *ssl, int level, const char *file, int line, const char *text, const x509_crt *crt)
Memory allocation layer.
uint32_t t_uint
Definition: bignum.h:149
Info structure for the pseudo random function.
Debug functions.
int s
Definition: bignum.h:173
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 ssl_set_dbg(ssl_context *ssl, void(*f_dbg)(void *, int, const char *), void *p_dbg)
Set the debug callback.
void x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
Configuration options (set of defines)
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[])
Multi-precision integer library.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dep_check(char *str)
#define TEST_ASSERT(TEST)
Container for an X.509 certificate.
Definition: x509_crt.h:53
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
void debug_print_mpi(const ssl_context *ssl, int level, const char *file, int line, const char *text, const mpi *X)
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
void mpi_free(mpi *X)
Unallocate one MPI.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int parse_arguments(char *buf, size_t len, char *params[50])
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
t_uint * p
Definition: bignum.h:175
int verify_string(char **str)
#define PUT_UINT32_BE(n, b, i)
int dispatch_test(int cnt, char *params[50])
size_t n
Definition: bignum.h:174
unsigned char * buf
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 test_errors
static int unhexify(unsigned char *obuf, const char *ibuf)
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.