PolarSSL v1.3.8
test_suite_pkwrite.c
Go to the documentation of this file.
1 #if !defined(POLARSSL_CONFIG_FILE)
2 #include <polarssl/config.h>
3 #else
4 #include POLARSSL_CONFIG_FILE
5 #endif
6 
7 #ifdef POLARSSL_PK_WRITE_C
8 #ifdef POLARSSL_BIGNUM_C
9 #ifdef POLARSSL_FS_IO
10 
11 #include <polarssl/pk.h>
12 #include <polarssl/pem.h>
13 #include <polarssl/oid.h>
14 #endif /* POLARSSL_PK_WRITE_C */
15 #endif /* POLARSSL_BIGNUM_C */
16 #endif /* POLARSSL_FS_IO */
17 
18 
19 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
20 #include "polarssl/memory.h"
21 #endif
22 
23 #if defined(POLARSSL_PLATFORM_C)
24 #include "polarssl/platform.h"
25 #else
26 #define polarssl_malloc malloc
27 #define polarssl_free free
28 #endif
29 
30 #ifdef _MSC_VER
31 #include <basetsd.h>
32 typedef UINT32 uint32_t;
33 #else
34 #include <inttypes.h>
35 #endif
36 
37 #include <assert.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 /*
42  * 32-bit integer manipulation macros (big endian)
43  */
44 #ifndef GET_UINT32_BE
45 #define GET_UINT32_BE(n,b,i) \
46 { \
47  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
48  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
49  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
50  | ( (uint32_t) (b)[(i) + 3] ); \
51 }
52 #endif
53 
54 #ifndef PUT_UINT32_BE
55 #define PUT_UINT32_BE(n,b,i) \
56 { \
57  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
58  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
59  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
60  (b)[(i) + 3] = (unsigned char) ( (n) ); \
61 }
62 #endif
63 
64 static int unhexify(unsigned char *obuf, const char *ibuf)
65 {
66  unsigned char c, c2;
67  int len = strlen(ibuf) / 2;
68  assert(!(strlen(ibuf) %1)); // must be even number of bytes
69 
70  while (*ibuf != 0)
71  {
72  c = *ibuf++;
73  if( c >= '0' && c <= '9' )
74  c -= '0';
75  else if( c >= 'a' && c <= 'f' )
76  c -= 'a' - 10;
77  else if( c >= 'A' && c <= 'F' )
78  c -= 'A' - 10;
79  else
80  assert( 0 );
81 
82  c2 = *ibuf++;
83  if( c2 >= '0' && c2 <= '9' )
84  c2 -= '0';
85  else if( c2 >= 'a' && c2 <= 'f' )
86  c2 -= 'a' - 10;
87  else if( c2 >= 'A' && c2 <= 'F' )
88  c2 -= 'A' - 10;
89  else
90  assert( 0 );
91 
92  *obuf++ = ( c << 4 ) | c2;
93  }
94 
95  return len;
96 }
97 
98 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
99 {
100  unsigned char l, h;
101 
102  while (len != 0)
103  {
104  h = (*ibuf) / 16;
105  l = (*ibuf) % 16;
106 
107  if( h < 10 )
108  *obuf++ = '0' + h;
109  else
110  *obuf++ = 'a' + h - 10;
111 
112  if( l < 10 )
113  *obuf++ = '0' + l;
114  else
115  *obuf++ = 'a' + l - 10;
116 
117  ++ibuf;
118  len--;
119  }
120 }
121 
129 static unsigned char *zero_alloc( size_t len )
130 {
131  void *p;
132  size_t actual_len = len != 0 ? len : 1;
133 
134  p = polarssl_malloc( actual_len );
135  assert( p != NULL );
136 
137  memset( p, 0x00, actual_len );
138 
139  return( p );
140 }
141 
152 static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
153 {
154  unsigned char *obuf;
155 
156  *olen = strlen(ibuf) / 2;
157 
158  if( *olen == 0 )
159  return( zero_alloc( *olen ) );
160 
161  obuf = polarssl_malloc( *olen );
162  assert( obuf != NULL );
163 
164  (void) unhexify( obuf, ibuf );
165 
166  return( obuf );
167 }
168 
178 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
179 {
180 #if !defined(__OpenBSD__)
181  size_t i;
182 
183  if( rng_state != NULL )
184  rng_state = NULL;
185 
186  for( i = 0; i < len; ++i )
187  output[i] = rand();
188 #else
189  if( rng_state != NULL )
190  rng_state = NULL;
191 
192  arc4random_buf( output, len );
193 #endif /* !OpenBSD */
194 
195  return( 0 );
196 }
197 
203 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
204 {
205  if( rng_state != NULL )
206  rng_state = NULL;
207 
208  memset( output, 0, len );
209 
210  return( 0 );
211 }
212 
213 typedef struct
214 {
215  unsigned char *buf;
216  size_t length;
217 } rnd_buf_info;
218 
230 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
231 {
232  rnd_buf_info *info = (rnd_buf_info *) rng_state;
233  size_t use_len;
234 
235  if( rng_state == NULL )
236  return( rnd_std_rand( NULL, output, len ) );
237 
238  use_len = len;
239  if( len > info->length )
240  use_len = info->length;
241 
242  if( use_len )
243  {
244  memcpy( output, info->buf, use_len );
245  info->buf += use_len;
246  info->length -= use_len;
247  }
248 
249  if( len - use_len > 0 )
250  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
251 
252  return( 0 );
253 }
254 
262 typedef struct
263 {
264  uint32_t key[16];
265  uint32_t v0, v1;
267 
276 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
277 {
278  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
279  uint32_t i, *k, sum, delta=0x9E3779B9;
280  unsigned char result[4], *out = output;
281 
282  if( rng_state == NULL )
283  return( rnd_std_rand( NULL, output, len ) );
284 
285  k = info->key;
286 
287  while( len > 0 )
288  {
289  size_t use_len = ( len > 4 ) ? 4 : len;
290  sum = 0;
291 
292  for( i = 0; i < 32; i++ )
293  {
294  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
295  sum += delta;
296  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
297  }
298 
299  PUT_UINT32_BE( info->v0, result, 0 );
300  memcpy( out, result, use_len );
301  len -= use_len;
302  out += 4;
303  }
304 
305  return( 0 );
306 }
307 
308 
309 #include <stdio.h>
310 #include <string.h>
311 
312 #if defined(POLARSSL_PLATFORM_C)
313 #include "polarssl/platform.h"
314 #else
315 #define polarssl_printf printf
316 #define polarssl_malloc malloc
317 #define polarssl_free free
318 #endif
319 
320 static int test_errors = 0;
321 
322 #ifdef POLARSSL_PK_WRITE_C
323 #ifdef POLARSSL_BIGNUM_C
324 #ifdef POLARSSL_FS_IO
325 
326 #define TEST_SUITE_ACTIVE
327 
328 static int test_assert( int correct, const char *test )
329 {
330  if( correct )
331  return( 0 );
332 
333  test_errors++;
334  if( test_errors == 1 )
335  printf( "FAILED\n" );
336  printf( " %s\n", test );
337 
338  return( 1 );
339 }
340 
341 #define TEST_ASSERT( TEST ) \
342  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
343  if( test_errors) goto exit; \
344  } while (0)
345 
346 int verify_string( char **str )
347 {
348  if( (*str)[0] != '"' ||
349  (*str)[strlen( *str ) - 1] != '"' )
350  {
351  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
352  return( -1 );
353  }
354 
355  (*str)++;
356  (*str)[strlen( *str ) - 1] = '\0';
357 
358  return( 0 );
359 }
360 
361 int verify_int( char *str, int *value )
362 {
363  size_t i;
364  int minus = 0;
365  int digits = 1;
366  int hex = 0;
367 
368  for( i = 0; i < strlen( str ); i++ )
369  {
370  if( i == 0 && str[i] == '-' )
371  {
372  minus = 1;
373  continue;
374  }
375 
376  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
377  str[i - 1] == '0' && str[i] == 'x' )
378  {
379  hex = 1;
380  continue;
381  }
382 
383  if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
384  ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
385  ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
386  {
387  digits = 0;
388  break;
389  }
390  }
391 
392  if( digits )
393  {
394  if( hex )
395  *value = strtol( str, NULL, 16 );
396  else
397  *value = strtol( str, NULL, 10 );
398 
399  return( 0 );
400  }
401 
402 
403 
404  printf( "Expected integer for parameter and got: %s\n", str );
405  return( -1 );
406 }
407 
408 #ifdef POLARSSL_PEM_WRITE_C
409 void test_suite_pk_write_pubkey_check( char *key_file )
410 {
411  pk_context key;
412  unsigned char buf[5000];
413  unsigned char check_buf[5000];
414  int ret;
415  FILE *f;
416  size_t ilen;
417 
418  memset( buf, 0, sizeof( buf ) );
419  memset( check_buf, 0, sizeof( check_buf ) );
420 
421  pk_init( &key );
422  TEST_ASSERT( pk_parse_public_keyfile( &key, key_file ) == 0 );
423 
424  ret = pk_write_pubkey_pem( &key, buf, sizeof( buf ) - 1);
425  TEST_ASSERT( ret >= 0 );
426 
427  f = fopen( key_file, "r" );
428  TEST_ASSERT( f != NULL );
429  ilen = fread( check_buf, 1, sizeof( check_buf ) - 1, f );
430  fclose( f );
431 
432  TEST_ASSERT( ilen == strlen( (char *) buf ) );
433  TEST_ASSERT( strncmp( (char *) buf, (char *) check_buf, sizeof( buf ) ) == 0 );
434 
435 exit:
436  pk_free( &key );
437 }
438 #endif /* POLARSSL_PEM_WRITE_C */
439 
440 #ifdef POLARSSL_PEM_WRITE_C
441 void test_suite_pk_write_key_check( char *key_file )
442 {
443  pk_context key;
444  unsigned char buf[5000];
445  unsigned char check_buf[5000];
446  int ret;
447  FILE *f;
448  size_t ilen;
449 
450  memset( buf, 0, sizeof( buf ) );
451  memset( check_buf, 0, sizeof( check_buf ) );
452 
453  pk_init( &key );
454  TEST_ASSERT( pk_parse_keyfile( &key, key_file, NULL ) == 0 );
455 
456  ret = pk_write_key_pem( &key, buf, sizeof( buf ) - 1);
457  TEST_ASSERT( ret >= 0 );
458 
459  f = fopen( key_file, "r" );
460  TEST_ASSERT( f != NULL );
461  ilen = fread( check_buf, 1, sizeof( check_buf ) - 1, f );
462  fclose( f );
463 
464  TEST_ASSERT( ilen == strlen( (char *) buf ) );
465  TEST_ASSERT( strncmp( (char *) buf, (char *) check_buf, sizeof( buf ) ) == 0 );
466 
467 exit:
468  pk_free( &key );
469 }
470 #endif /* POLARSSL_PEM_WRITE_C */
471 
472 
473 #endif /* POLARSSL_PK_WRITE_C */
474 #endif /* POLARSSL_BIGNUM_C */
475 #endif /* POLARSSL_FS_IO */
476 
477 
478 int dep_check( char *str )
479 {
480  if( str == NULL )
481  return( 1 );
482 
483  if( strcmp( str, "POLARSSL_ECP_DP_SECP192R1_ENABLED" ) == 0 )
484  {
485 #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
486  return( 0 );
487 #else
488  return( 1 );
489 #endif
490  }
491  if( strcmp( str, "POLARSSL_RSA_C" ) == 0 )
492  {
493 #if defined(POLARSSL_RSA_C)
494  return( 0 );
495 #else
496  return( 1 );
497 #endif
498  }
499  if( strcmp( str, "POLARSSL_BASE64_C" ) == 0 )
500  {
501 #if defined(POLARSSL_BASE64_C)
502  return( 0 );
503 #else
504  return( 1 );
505 #endif
506  }
507  if( strcmp( str, "POLARSSL_ECP_C" ) == 0 )
508  {
509 #if defined(POLARSSL_ECP_C)
510  return( 0 );
511 #else
512  return( 1 );
513 #endif
514  }
515 
516 
517  return( 1 );
518 }
519 
520 int dispatch_test(int cnt, char *params[50])
521 {
522  int ret;
523  ((void) cnt);
524  ((void) params);
525 
526 #if defined(TEST_SUITE_ACTIVE)
527  if( strcmp( params[0], "pk_write_pubkey_check" ) == 0 )
528  {
529  #ifdef POLARSSL_PEM_WRITE_C
530 
531  char *param1 = params[1];
532 
533  if( cnt != 2 )
534  {
535  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
536  return( 2 );
537  }
538 
539  if( verify_string( &param1 ) != 0 ) return( 2 );
540 
541  test_suite_pk_write_pubkey_check( param1 );
542  return ( 0 );
543  #endif /* POLARSSL_PEM_WRITE_C */
544 
545  return ( 3 );
546  }
547  else
548  if( strcmp( params[0], "pk_write_key_check" ) == 0 )
549  {
550  #ifdef POLARSSL_PEM_WRITE_C
551 
552  char *param1 = params[1];
553 
554  if( cnt != 2 )
555  {
556  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 2 );
557  return( 2 );
558  }
559 
560  if( verify_string( &param1 ) != 0 ) return( 2 );
561 
562  test_suite_pk_write_key_check( param1 );
563  return ( 0 );
564  #endif /* POLARSSL_PEM_WRITE_C */
565 
566  return ( 3 );
567  }
568  else
569 
570  {
571  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
572  fflush( stdout );
573  return( 1 );
574  }
575 #else
576  return( 3 );
577 #endif
578  return( ret );
579 }
580 
581 int get_line( FILE *f, char *buf, size_t len )
582 {
583  char *ret;
584 
585  ret = fgets( buf, len, f );
586  if( ret == NULL )
587  return( -1 );
588 
589  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
590  buf[strlen(buf) - 1] = '\0';
591  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
592  buf[strlen(buf) - 1] = '\0';
593 
594  return( 0 );
595 }
596 
597 int parse_arguments( char *buf, size_t len, char *params[50] )
598 {
599  int cnt = 0, i;
600  char *cur = buf;
601  char *p = buf, *q;
602 
603  params[cnt++] = cur;
604 
605  while( *p != '\0' && p < buf + len )
606  {
607  if( *p == '\\' )
608  {
609  p++;
610  p++;
611  continue;
612  }
613  if( *p == ':' )
614  {
615  if( p + 1 < buf + len )
616  {
617  cur = p + 1;
618  params[cnt++] = cur;
619  }
620  *p = '\0';
621  }
622 
623  p++;
624  }
625 
626  // Replace newlines, question marks and colons in strings
627  for( i = 0; i < cnt; i++ )
628  {
629  p = params[i];
630  q = params[i];
631 
632  while( *p != '\0' )
633  {
634  if( *p == '\\' && *(p + 1) == 'n' )
635  {
636  p += 2;
637  *(q++) = '\n';
638  }
639  else if( *p == '\\' && *(p + 1) == ':' )
640  {
641  p += 2;
642  *(q++) = ':';
643  }
644  else if( *p == '\\' && *(p + 1) == '?' )
645  {
646  p += 2;
647  *(q++) = '?';
648  }
649  else
650  *(q++) = *(p++);
651  }
652  *q = '\0';
653  }
654 
655  return( cnt );
656 }
657 
658 int main()
659 {
660  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
661  const char *filename = "/tmp/B.43ee1cd1-d3e3-4a1b-8449-a820e9bbf54f/BUILD/polarssl-1.3.8/tests/suites/test_suite_pkwrite.data";
662  FILE *file;
663  char buf[5000];
664  char *params[50];
665 
666 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
667  unsigned char alloc_buf[1000000];
668  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
669 #endif
670 
671  file = fopen( filename, "r" );
672  if( file == NULL )
673  {
674  fprintf( stderr, "Failed to open\n" );
675  return( 1 );
676  }
677 
678  while( !feof( file ) )
679  {
680  int skip = 0;
681 
682  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
683  break;
684  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
685  fprintf( stdout, " " );
686  for( i = strlen( buf ) + 1; i < 67; i++ )
687  fprintf( stdout, "." );
688  fprintf( stdout, " " );
689  fflush( stdout );
690 
691  total_tests++;
692 
693  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
694  break;
695  cnt = parse_arguments( buf, strlen(buf), params );
696 
697  if( strcmp( params[0], "depends_on" ) == 0 )
698  {
699  for( i = 1; i < cnt; i++ )
700  if( dep_check( params[i] ) != 0 )
701  skip = 1;
702 
703  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
704  break;
705  cnt = parse_arguments( buf, strlen(buf), params );
706  }
707 
708  if( skip == 0 )
709  {
710  test_errors = 0;
711  ret = dispatch_test( cnt, params );
712  }
713 
714  if( skip == 1 || ret == 3 )
715  {
716  total_skipped++;
717  fprintf( stdout, "----\n" );
718  fflush( stdout );
719  }
720  else if( ret == 0 && test_errors == 0 )
721  {
722  fprintf( stdout, "PASS\n" );
723  fflush( stdout );
724  }
725  else if( ret == 2 )
726  {
727  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
728  fclose(file);
729  exit( 2 );
730  }
731  else
732  total_errors++;
733 
734  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
735  break;
736  if( strlen(buf) != 0 )
737  {
738  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
739  return( 1 );
740  }
741  }
742  fclose(file);
743 
744  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
745  if( total_errors == 0 )
746  fprintf( stdout, "PASSED" );
747  else
748  fprintf( stdout, "FAILED" );
749 
750  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
751  total_tests - total_errors, total_tests, total_skipped );
752 
753 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
754 #if defined(POLARSSL_MEMORY_DEBUG)
755  memory_buffer_alloc_status();
756 #endif
758 #endif
759 
760  return( total_errors != 0 );
761 }
762 
763 
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
Memory allocation layer (Deprecated to platform layer)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
Info structure for the pseudo random function.
int parse_arguments(char *buf, size_t len, char *params[50])
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int pk_write_key_pem(pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
Configuration options (set of defines)
PolarSSL Platform abstraction layer.
static int test_assert(int correct, const char *test)
Object Identifier (OID) database.
Public Key abstraction layer.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
#define PUT_UINT32_BE(n, b, i)
int main()
Privacy Enhanced Mail (PEM) decoding.
int pk_parse_public_keyfile(pk_context *ctx, const char *path)
Load and parse a public key.
static int unhexify(unsigned char *obuf, const char *ibuf)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
#define polarssl_malloc
static int test_errors
int verify_string(char **str)
void pk_free(pk_context *ctx)
Free a pk_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.
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
unsigned char * buf
int dep_check(char *str)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int pk_write_pubkey_pem(pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
int verify_int(char *str, int *value)
int pk_parse_keyfile(pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
int dispatch_test(int cnt, char *params[50])
int get_line(FILE *f, char *buf, size_t len)
Public key container.
Definition: pk.h:194