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