PolarSSL v1.3.1
test_suite_pkwrite.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_PK_WRITE_C
4 #ifdef POLARSSL_BIGNUM_C
5 #ifdef POLARSSL_FS_IO
6 
7 #include <polarssl/pk.h>
8 #include <polarssl/pem.h>
9 #include <polarssl/oid.h>
10 #endif /* POLARSSL_PK_WRITE_C */
11 #endif /* POLARSSL_BIGNUM_C */
12 #endif /* POLARSSL_FS_IO */
13 
14 
15 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
16 #include "polarssl/memory.h"
17 #endif
18 
19 #ifdef _MSC_VER
20 #include <basetsd.h>
21 typedef UINT32 uint32_t;
22 #else
23 #include <inttypes.h>
24 #endif
25 
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 /*
31  * 32-bit integer manipulation macros (big endian)
32  */
33 #ifndef GET_UINT32_BE
34 #define GET_UINT32_BE(n,b,i) \
35 { \
36  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
37  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
38  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
39  | ( (uint32_t) (b)[(i) + 3] ); \
40 }
41 #endif
42 
43 #ifndef PUT_UINT32_BE
44 #define PUT_UINT32_BE(n,b,i) \
45 { \
46  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
47  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
48  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
49  (b)[(i) + 3] = (unsigned char) ( (n) ); \
50 }
51 #endif
52 
53 static int unhexify(unsigned char *obuf, const char *ibuf)
54 {
55  unsigned char c, c2;
56  int len = strlen(ibuf) / 2;
57  assert(!(strlen(ibuf) %1)); // must be even number of bytes
58 
59  while (*ibuf != 0)
60  {
61  c = *ibuf++;
62  if( c >= '0' && c <= '9' )
63  c -= '0';
64  else if( c >= 'a' && c <= 'f' )
65  c -= 'a' - 10;
66  else if( c >= 'A' && c <= 'F' )
67  c -= 'A' - 10;
68  else
69  assert( 0 );
70 
71  c2 = *ibuf++;
72  if( c2 >= '0' && c2 <= '9' )
73  c2 -= '0';
74  else if( c2 >= 'a' && c2 <= 'f' )
75  c2 -= 'a' - 10;
76  else if( c2 >= 'A' && c2 <= 'F' )
77  c2 -= 'A' - 10;
78  else
79  assert( 0 );
80 
81  *obuf++ = ( c << 4 ) | c2;
82  }
83 
84  return len;
85 }
86 
87 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
88 {
89  unsigned char l, h;
90 
91  while (len != 0)
92  {
93  h = (*ibuf) / 16;
94  l = (*ibuf) % 16;
95 
96  if( h < 10 )
97  *obuf++ = '0' + h;
98  else
99  *obuf++ = 'a' + h - 10;
100 
101  if( l < 10 )
102  *obuf++ = '0' + l;
103  else
104  *obuf++ = 'a' + l - 10;
105 
106  ++ibuf;
107  len--;
108  }
109 }
110 
120 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
121 {
122  size_t i;
123 
124  if( rng_state != NULL )
125  rng_state = NULL;
126 
127  for( i = 0; i < len; ++i )
128  output[i] = rand();
129 
130  return( 0 );
131 }
132 
138 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
139 {
140  if( rng_state != NULL )
141  rng_state = NULL;
142 
143  memset( output, 0, len );
144 
145  return( 0 );
146 }
147 
148 typedef struct
149 {
150  unsigned char *buf;
151  size_t length;
152 } rnd_buf_info;
153 
165 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
166 {
167  rnd_buf_info *info = (rnd_buf_info *) rng_state;
168  size_t use_len;
169 
170  if( rng_state == NULL )
171  return( rnd_std_rand( NULL, output, len ) );
172 
173  use_len = len;
174  if( len > info->length )
175  use_len = info->length;
176 
177  if( use_len )
178  {
179  memcpy( output, info->buf, use_len );
180  info->buf += use_len;
181  info->length -= use_len;
182  }
183 
184  if( len - use_len > 0 )
185  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
186 
187  return( 0 );
188 }
189 
197 typedef struct
198 {
199  uint32_t key[16];
200  uint32_t v0, v1;
202 
211 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
212 {
213  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
214  uint32_t i, *k, sum, delta=0x9E3779B9;
215  unsigned char result[4];
216 
217  if( rng_state == NULL )
218  return( rnd_std_rand( NULL, output, len ) );
219 
220  k = info->key;
221 
222  while( len > 0 )
223  {
224  size_t use_len = ( len > 4 ) ? 4 : len;
225  sum = 0;
226 
227  for( i = 0; i < 32; i++ )
228  {
229  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
230  sum += delta;
231  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
232  }
233 
234  PUT_UINT32_BE( info->v0, result, 0 );
235  memcpy( output, result, use_len );
236  len -= use_len;
237  }
238 
239  return( 0 );
240 }
241 
251 static int not_rnd( void *in, unsigned char *out, size_t len )
252 {
253  unsigned char *obuf;
254  const char *ibuf = in;
255  unsigned char c, c2;
256  assert( len == strlen(ibuf) / 2 );
257  assert(!(strlen(ibuf) %1)); // must be even number of bytes
258 
259  obuf = out + (len - 1); // sic
260  while (*ibuf != 0)
261  {
262  c = *ibuf++;
263  if( c >= '0' && c <= '9' )
264  c -= '0';
265  else if( c >= 'a' && c <= 'f' )
266  c -= 'a' - 10;
267  else if( c >= 'A' && c <= 'F' )
268  c -= 'A' - 10;
269  else
270  assert( 0 );
271 
272  c2 = *ibuf++;
273  if( c2 >= '0' && c2 <= '9' )
274  c2 -= '0';
275  else if( c2 >= 'a' && c2 <= 'f' )
276  c2 -= 'a' - 10;
277  else if( c2 >= 'A' && c2 <= 'F' )
278  c2 -= 'A' - 10;
279  else
280  assert( 0 );
281 
282  *obuf-- = ( c << 4 ) | c2; // sic
283  }
284 
285  return( 0 );
286 }
287 
288 
289 #include <stdio.h>
290 #include <string.h>
291 
292 static int test_errors = 0;
293 
294 #ifdef POLARSSL_PK_WRITE_C
295 #ifdef POLARSSL_BIGNUM_C
296 #ifdef POLARSSL_FS_IO
297 
298 #define TEST_SUITE_ACTIVE
299 
300 static int test_assert( int correct, char *test )
301 {
302  if( correct )
303  return( 0 );
304 
305  test_errors++;
306  if( test_errors == 1 )
307  printf( "FAILED\n" );
308  printf( " %s\n", test );
309 
310  return( 1 );
311 }
312 
313 #define TEST_ASSERT( TEST ) \
314  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
315  if( test_errors) return; \
316  } while (0)
317 
318 int verify_string( char **str )
319 {
320  if( (*str)[0] != '"' ||
321  (*str)[strlen( *str ) - 1] != '"' )
322  {
323  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
324  return( -1 );
325  }
326 
327  (*str)++;
328  (*str)[strlen( *str ) - 1] = '\0';
329 
330  return( 0 );
331 }
332 
333 int verify_int( char *str, int *value )
334 {
335  size_t i;
336  int minus = 0;
337  int digits = 1;
338  int hex = 0;
339 
340  for( i = 0; i < strlen( str ); i++ )
341  {
342  if( i == 0 && str[i] == '-' )
343  {
344  minus = 1;
345  continue;
346  }
347 
348  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
349  str[i - 1] == '0' && str[i] == 'x' )
350  {
351  hex = 1;
352  continue;
353  }
354 
355  if( str[i] < '0' || str[i] > '9' )
356  {
357  digits = 0;
358  break;
359  }
360  }
361 
362  if( digits )
363  {
364  if( hex )
365  *value = strtol( str, NULL, 16 );
366  else
367  *value = strtol( str, NULL, 10 );
368 
369  return( 0 );
370  }
371 
372 
373 
374  printf( "Expected integer for parameter and got: %s\n", str );
375  return( -1 );
376 }
377 
378 void test_suite_pk_write_pubkey_check( char *key_file )
379 {
380  pk_context key;
381  unsigned char buf[5000];
382  unsigned char check_buf[5000];
383  int ret;
384  FILE *f;
385 
386  memset( buf, 0, sizeof( buf ) );
387  memset( check_buf, 0, sizeof( check_buf ) );
388 
389  pk_init( &key );
390  TEST_ASSERT( pk_parse_public_keyfile( &key, key_file ) == 0 );
391 
392  ret = pk_write_pubkey_pem( &key, buf, sizeof( buf ) - 1);
393  TEST_ASSERT( ret >= 0 );
394 
395  f = fopen( key_file, "r" );
396  TEST_ASSERT( f != NULL );
397  fread( check_buf, 1, sizeof( check_buf ) - 1, f );
398  fclose( f );
399 
400  TEST_ASSERT( strncmp( (char *) buf, (char *) check_buf, sizeof( buf ) ) == 0 );
401 
402  pk_free( &key );
403 }
404 
405 void test_suite_pk_write_key_check( char *key_file )
406 {
407  pk_context key;
408  unsigned char buf[5000];
409  unsigned char check_buf[5000];
410  int ret;
411  FILE *f;
412 
413  memset( buf, 0, sizeof( buf ) );
414  memset( check_buf, 0, sizeof( check_buf ) );
415 
416  pk_init( &key );
417  TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
418 
419  ret = pk_write_key_pem( &key, buf, sizeof( buf ) - 1);
420  TEST_ASSERT( ret >= 0 );
421 
422  f = fopen( key_file, "r" );
423  TEST_ASSERT( f != NULL );
424  fread( check_buf, 1, sizeof( check_buf ) - 1, f );
425  fclose( f );
426 
427  TEST_ASSERT( strncmp( (char *) buf, (char *) check_buf, sizeof( buf ) ) == 0 );
428 
429  pk_free( &key );
430 }
431 
432 
433 #endif /* POLARSSL_PK_WRITE_C */
434 #endif /* POLARSSL_BIGNUM_C */
435 #endif /* POLARSSL_FS_IO */
436 
437 
438 int dep_check( char *str )
439 {
440  if( str == NULL )
441  return( 1 );
442 
443  if( strcmp( str, "POLARSSL_BASE64_C" ) == 0 )
444  {
445 #if defined(POLARSSL_BASE64_C)
446  return( 0 );
447 #else
448  return( 1 );
449 #endif
450  }
451  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
452  {
453 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
454  return( 0 );
455 #else
456  return( 1 );
457 #endif
458  }
459  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
460  {
461 #if defined(POLARSSL_ECP_C)
462  return( 0 );
463 #else
464  return( 1 );
465 #endif
466  }
467  if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
468  {
469 #if defined(POLARSSL_RSA_C)
470  return( 0 );
471 #else
472  return( 1 );
473 #endif
474  }
475 
476 
477  return( 1 );
478 }
479 
480 int dispatch_test(int cnt, char *params[50])
481 {
482  int ret;
483  ((void) cnt);
484  ((void) params);
485 
486 #if defined(TEST_SUITE_ACTIVE)
487  if( strcmp( params[0], "pk_write_pubkey_check" ) == 0 )
488  {
489 
490  char *param1 = params[1];
491 
492  if( cnt != 2 )
493  {
494  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
495  return( 2 );
496  }
497 
498  if( verify_string( &param1 ) != 0 ) return( 2 );
499 
500  test_suite_pk_write_pubkey_check( param1 );
501  return ( 0 );
502 
503  return ( 3 );
504  }
505  else
506  if( strcmp( params[0], "pk_write_key_check" ) == 0 )
507  {
508 
509  char *param1 = params[1];
510 
511  if( cnt != 2 )
512  {
513  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
514  return( 2 );
515  }
516 
517  if( verify_string( &param1 ) != 0 ) return( 2 );
518 
519  test_suite_pk_write_key_check( param1 );
520  return ( 0 );
521 
522  return ( 3 );
523  }
524  else
525 
526  {
527  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
528  fflush( stdout );
529  return( 1 );
530  }
531 #else
532  return( 3 );
533 #endif
534  return( ret );
535 }
536 
537 int get_line( FILE *f, char *buf, size_t len )
538 {
539  char *ret;
540 
541  ret = fgets( buf, len, f );
542  if( ret == NULL )
543  return( -1 );
544 
545  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
546  buf[strlen(buf) - 1] = '\0';
547  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
548  buf[strlen(buf) - 1] = '\0';
549 
550  return( 0 );
551 }
552 
553 int parse_arguments( char *buf, size_t len, char *params[50] )
554 {
555  int cnt = 0, i;
556  char *cur = buf;
557  char *p = buf, *q;
558 
559  params[cnt++] = cur;
560 
561  while( *p != '\0' && p < buf + len )
562  {
563  if( *p == '\\' )
564  {
565  *p++;
566  *p++;
567  continue;
568  }
569  if( *p == ':' )
570  {
571  if( p + 1 < buf + len )
572  {
573  cur = p + 1;
574  params[cnt++] = cur;
575  }
576  *p = '\0';
577  }
578 
579  *p++;
580  }
581 
582  // Replace newlines, question marks and colons in strings
583  for( i = 0; i < cnt; i++ )
584  {
585  p = params[i];
586  q = params[i];
587 
588  while( *p != '\0' )
589  {
590  if( *p == '\\' && *(p + 1) == 'n' )
591  {
592  p += 2;
593  *(q++) = '\n';
594  }
595  else if( *p == '\\' && *(p + 1) == ':' )
596  {
597  p += 2;
598  *(q++) = ':';
599  }
600  else if( *p == '\\' && *(p + 1) == '?' )
601  {
602  p += 2;
603  *(q++) = '?';
604  }
605  else
606  *(q++) = *(p++);
607  }
608  *q = '\0';
609  }
610 
611  return( cnt );
612 }
613 
614 int main()
615 {
616  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
617  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_pkwrite.data";
618  FILE *file;
619  char buf[5000];
620  char *params[50];
621 
622 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
623  unsigned char alloc_buf[1000000];
624  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
625 #endif
626 
627  file = fopen( filename, "r" );
628  if( file == NULL )
629  {
630  fprintf( stderr, "Failed to open\n" );
631  return( 1 );
632  }
633 
634  while( !feof( file ) )
635  {
636  int skip = 0;
637 
638  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
639  break;
640  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
641  fprintf( stdout, " " );
642  for( i = strlen( buf ) + 1; i < 67; i++ )
643  fprintf( stdout, "." );
644  fprintf( stdout, " " );
645  fflush( stdout );
646 
647  total_tests++;
648 
649  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
650  break;
651  cnt = parse_arguments( buf, strlen(buf), params );
652 
653  if( strcmp( params[0], "depends_on" ) == 0 )
654  {
655  for( i = 1; i < cnt; i++ )
656  if( dep_check( params[i] ) != 0 )
657  skip = 1;
658 
659  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
660  break;
661  cnt = parse_arguments( buf, strlen(buf), params );
662  }
663 
664  if( skip == 0 )
665  {
666  test_errors = 0;
667  ret = dispatch_test( cnt, params );
668  }
669 
670  if( skip == 1 || ret == 3 )
671  {
672  total_skipped++;
673  fprintf( stdout, "----\n" );
674  fflush( stdout );
675  }
676  else if( ret == 0 && test_errors == 0 )
677  {
678  fprintf( stdout, "PASS\n" );
679  fflush( stdout );
680  }
681  else if( ret == 2 )
682  {
683  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
684  fclose(file);
685  exit( 2 );
686  }
687  else
688  total_errors++;
689 
690  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
691  break;
692  if( strlen(buf) != 0 )
693  {
694  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
695  return( 1 );
696  }
697  }
698  fclose(file);
699 
700  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
701  if( total_errors == 0 )
702  fprintf( stdout, "PASSED" );
703  else
704  fprintf( stdout, "FAILED" );
705 
706  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
707  total_tests - total_errors, total_tests, total_skipped );
708 
709 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
710 #if defined(POLARSSL_MEMORY_DEBUG)
711  memory_buffer_alloc_status();
712 #endif
713  memory_buffer_alloc_free();
714 #endif
715 
716  return( total_errors != 0 );
717 }
718 
719