PolarSSL v1.3.9
test_suite_asn1write.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_ASN1_WRITE_C
8
10
11#define GUARD_LEN 4
12#define GUARD_VAL 0x2a
13#endif /* POLARSSL_ASN1_WRITE_C */
14
15
16#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
17#include "polarssl/memory.h"
18#endif
19
20#if defined(POLARSSL_PLATFORM_C)
21#include "polarssl/platform.h"
22#else
23#define polarssl_malloc malloc
24#define polarssl_free free
25#endif
26
27#ifdef _MSC_VER
28#include <basetsd.h>
29typedef 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
61static 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
95static 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
126static unsigned char *zero_alloc( size_t len )
127{
128 void *p;
129 size_t actual_len = len != 0 ? len : 1;
130
131 p = polarssl_malloc( actual_len );
132 assert( p != NULL );
133
134 memset( p, 0x00, actual_len );
135
136 return( p );
137}
138
149static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
150{
151 unsigned char *obuf;
152
153 *olen = strlen(ibuf) / 2;
154
155 if( *olen == 0 )
156 return( zero_alloc( *olen ) );
157
158 obuf = polarssl_malloc( *olen );
159 assert( obuf != NULL );
160
161 (void) unhexify( obuf, ibuf );
162
163 return( obuf );
164}
165
175static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
176{
177#if !defined(__OpenBSD__)
178 size_t i;
179
180 if( rng_state != NULL )
181 rng_state = NULL;
182
183 for( i = 0; i < len; ++i )
184 output[i] = rand();
185#else
186 if( rng_state != NULL )
187 rng_state = NULL;
188
189 arc4random_buf( output, len );
190#endif /* !OpenBSD */
191
192 return( 0 );
193}
194
200static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
201{
202 if( rng_state != NULL )
203 rng_state = NULL;
204
205 memset( output, 0, len );
206
207 return( 0 );
208}
209
210typedef struct
211{
212 unsigned char *buf;
213 size_t length;
215
227static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
228{
229 rnd_buf_info *info = (rnd_buf_info *) rng_state;
230 size_t use_len;
231
232 if( rng_state == NULL )
233 return( rnd_std_rand( NULL, output, len ) );
234
235 use_len = len;
236 if( len > info->length )
237 use_len = info->length;
238
239 if( use_len )
240 {
241 memcpy( output, info->buf, use_len );
242 info->buf += use_len;
243 info->length -= use_len;
244 }
245
246 if( len - use_len > 0 )
247 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
248
249 return( 0 );
250}
251
259typedef struct
260{
261 uint32_t key[16];
262 uint32_t v0, v1;
264
273static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
274{
275 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
276 uint32_t i, *k, sum, delta=0x9E3779B9;
277 unsigned char result[4], *out = output;
278
279 if( rng_state == NULL )
280 return( rnd_std_rand( NULL, output, len ) );
281
282 k = info->key;
283
284 while( len > 0 )
285 {
286 size_t use_len = ( len > 4 ) ? 4 : len;
287 sum = 0;
288
289 for( i = 0; i < 32; i++ )
290 {
291 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
292 sum += delta;
293 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
294 }
295
296 PUT_UINT32_BE( info->v0, result, 0 );
297 memcpy( out, result, use_len );
298 len -= use_len;
299 out += 4;
300 }
301
302 return( 0 );
303}
304
305
306#include <stdio.h>
307#include <string.h>
308
309#if defined(POLARSSL_PLATFORM_C)
310#include "polarssl/platform.h"
311#else
312#define polarssl_printf printf
313#define polarssl_malloc malloc
314#define polarssl_free free
315#endif
316
317static int test_errors = 0;
318
319#ifdef POLARSSL_ASN1_WRITE_C
320
321#define TEST_SUITE_ACTIVE
322
323static int test_assert( int correct, const char *test )
324{
325 if( correct )
326 return( 0 );
327
328 test_errors++;
329 if( test_errors == 1 )
330 printf( "FAILED\n" );
331 printf( " %s\n", test );
332
333 return( 1 );
334}
335
336#define TEST_ASSERT( TEST ) \
337 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
338 if( test_errors) goto exit; \
339 } while (0)
340
341int verify_string( char **str )
342{
343 if( (*str)[0] != '"' ||
344 (*str)[strlen( *str ) - 1] != '"' )
345 {
346 printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
347 return( -1 );
348 }
349
350 (*str)++;
351 (*str)[strlen( *str ) - 1] = '\0';
352
353 return( 0 );
354}
355
356int verify_int( char *str, int *value )
357{
358 size_t i;
359 int minus = 0;
360 int digits = 1;
361 int hex = 0;
362
363 for( i = 0; i < strlen( str ); i++ )
364 {
365 if( i == 0 && str[i] == '-' )
366 {
367 minus = 1;
368 continue;
369 }
370
371 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
372 str[i - 1] == '0' && str[i] == 'x' )
373 {
374 hex = 1;
375 continue;
376 }
377
378 if( ! ( ( str[i] >= '0' && str[i] <= '9' ) ||
379 ( hex && ( ( str[i] >= 'a' && str[i] <= 'f' ) ||
380 ( str[i] >= 'A' && str[i] <= 'F' ) ) ) ) )
381 {
382 digits = 0;
383 break;
384 }
385 }
386
387 if( digits )
388 {
389 if( hex )
390 *value = strtol( str, NULL, 16 );
391 else
392 *value = strtol( str, NULL, 10 );
393
394 return( 0 );
395 }
396
397 if( strcmp( str, "POLARSSL_ERR_ASN1_BUF_TOO_SMALL" ) == 0 )
398 {
400 return( 0 );
401 }
402
403
404 printf( "Expected integer for parameter and got: %s\n", str );
405 return( -1 );
406}
407
408void test_suite_asn1_write_octet_string( char *hex_str, char *hex_asn1,
409 int buf_len, int result )
410{
411 int ret;
412 unsigned char buf[150];
413 unsigned char str[150] = { 0 };
414 unsigned char asn1[150] = { 0 };
415 size_t str_len, asn1_len, i;
416 unsigned char *p;
417
418 memset( buf, GUARD_VAL, sizeof( buf ) );
419
420 str_len = unhexify( str, hex_str );
421 asn1_len = unhexify( asn1, hex_asn1 );
422
423 p = buf + GUARD_LEN + buf_len;
424
425 ret = asn1_write_octet_string( &p, buf + GUARD_LEN, str, str_len );
426
427 /* Check for buffer overwrite on both sides */
428 for( i = 0; i < GUARD_LEN; i++ )
429 {
430 TEST_ASSERT( buf[i] == GUARD_VAL );
431 TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL );
432 }
433
434 if( result >= 0 )
435 {
436 TEST_ASSERT( (size_t) ret == asn1_len );
437 TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len );
438
439 TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 );
440 }
441
442exit:
443 return;
444}
445
446void test_suite_asn1_write_ia5_string( char *str, char *hex_asn1,
447 int buf_len, int result )
448{
449 int ret;
450 unsigned char buf[150];
451 unsigned char asn1[150] = { 0 };
452 size_t str_len, asn1_len, i;
453 unsigned char *p;
454
455 memset( buf, GUARD_VAL, sizeof( buf ) );
456
457 str_len = strlen( str );
458 asn1_len = unhexify( asn1, hex_asn1 );
459
460 p = buf + GUARD_LEN + buf_len;
461
462 ret = asn1_write_ia5_string( &p, buf + GUARD_LEN, str, str_len );
463
464 /* Check for buffer overwrite on both sides */
465 for( i = 0; i < GUARD_LEN; i++ )
466 {
467 TEST_ASSERT( buf[i] == GUARD_VAL );
468 TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL );
469 }
470
471 if( result >= 0 )
472 {
473 TEST_ASSERT( (size_t) ret == asn1_len );
474 TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len );
475
476 TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 );
477 }
478
479exit:
480 return;
481}
482
483
484#endif /* POLARSSL_ASN1_WRITE_C */
485
486
487int dep_check( char *str )
488{
489 if( str == NULL )
490 return( 1 );
491
492
493
494 return( 1 );
495}
496
497int dispatch_test(int cnt, char *params[50])
498{
499 int ret;
500 ((void) cnt);
501 ((void) params);
502
503#if defined(TEST_SUITE_ACTIVE)
504 if( strcmp( params[0], "asn1_write_octet_string" ) == 0 )
505 {
506
507 char *param1 = params[1];
508 char *param2 = params[2];
509 int param3;
510 int param4;
511
512 if( cnt != 5 )
513 {
514 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
515 return( 2 );
516 }
517
518 if( verify_string( &param1 ) != 0 ) return( 2 );
519 if( verify_string( &param2 ) != 0 ) return( 2 );
520 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
521 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
522
523 test_suite_asn1_write_octet_string( param1, param2, param3, param4 );
524 return ( 0 );
525
526 return ( 3 );
527 }
528 else
529 if( strcmp( params[0], "asn1_write_ia5_string" ) == 0 )
530 {
531
532 char *param1 = params[1];
533 char *param2 = params[2];
534 int param3;
535 int param4;
536
537 if( cnt != 5 )
538 {
539 fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
540 return( 2 );
541 }
542
543 if( verify_string( &param1 ) != 0 ) return( 2 );
544 if( verify_string( &param2 ) != 0 ) return( 2 );
545 if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
546 if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
547
548 test_suite_asn1_write_ia5_string( param1, param2, param3, param4 );
549 return ( 0 );
550
551 return ( 3 );
552 }
553 else
554
555 {
556 fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
557 fflush( stdout );
558 return( 1 );
559 }
560#else
561 return( 3 );
562#endif
563 return( ret );
564}
565
566int get_line( FILE *f, char *buf, size_t len )
567{
568 char *ret;
569
570 ret = fgets( buf, len, f );
571 if( ret == NULL )
572 return( -1 );
573
574 if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
575 buf[strlen(buf) - 1] = '\0';
576 if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
577 buf[strlen(buf) - 1] = '\0';
578
579 return( 0 );
580}
581
582int parse_arguments( char *buf, size_t len, char *params[50] )
583{
584 int cnt = 0, i;
585 char *cur = buf;
586 char *p = buf, *q;
587
588 params[cnt++] = cur;
589
590 while( *p != '\0' && p < buf + len )
591 {
592 if( *p == '\\' )
593 {
594 p++;
595 p++;
596 continue;
597 }
598 if( *p == ':' )
599 {
600 if( p + 1 < buf + len )
601 {
602 cur = p + 1;
603 params[cnt++] = cur;
604 }
605 *p = '\0';
606 }
607
608 p++;
609 }
610
611 // Replace newlines, question marks and colons in strings
612 for( i = 0; i < cnt; i++ )
613 {
614 p = params[i];
615 q = params[i];
616
617 while( *p != '\0' )
618 {
619 if( *p == '\\' && *(p + 1) == 'n' )
620 {
621 p += 2;
622 *(q++) = '\n';
623 }
624 else if( *p == '\\' && *(p + 1) == ':' )
625 {
626 p += 2;
627 *(q++) = ':';
628 }
629 else if( *p == '\\' && *(p + 1) == '?' )
630 {
631 p += 2;
632 *(q++) = '?';
633 }
634 else
635 *(q++) = *(p++);
636 }
637 *q = '\0';
638 }
639
640 return( cnt );
641}
642
643int main()
644{
645 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
646 const char *filename = "/builddir/build/BUILD/polarssl-1.3.9/tests/suites/test_suite_asn1write.data";
647 FILE *file;
648 char buf[5000];
649 char *params[50];
650
651#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
652 unsigned char alloc_buf[1000000];
653 memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
654#endif
655
656 file = fopen( filename, "r" );
657 if( file == NULL )
658 {
659 fprintf( stderr, "Failed to open\n" );
660 return( 1 );
661 }
662
663 while( !feof( file ) )
664 {
665 int skip = 0;
666
667 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
668 break;
669 fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
670 fprintf( stdout, " " );
671 for( i = strlen( buf ) + 1; i < 67; i++ )
672 fprintf( stdout, "." );
673 fprintf( stdout, " " );
674 fflush( stdout );
675
676 total_tests++;
677
678 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
679 break;
680 cnt = parse_arguments( buf, strlen(buf), params );
681
682 if( strcmp( params[0], "depends_on" ) == 0 )
683 {
684 for( i = 1; i < cnt; i++ )
685 if( dep_check( params[i] ) != 0 )
686 skip = 1;
687
688 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
689 break;
690 cnt = parse_arguments( buf, strlen(buf), params );
691 }
692
693 if( skip == 0 )
694 {
695 test_errors = 0;
696 ret = dispatch_test( cnt, params );
697 }
698
699 if( skip == 1 || ret == 3 )
700 {
701 total_skipped++;
702 fprintf( stdout, "----\n" );
703 fflush( stdout );
704 }
705 else if( ret == 0 && test_errors == 0 )
706 {
707 fprintf( stdout, "PASS\n" );
708 fflush( stdout );
709 }
710 else if( ret == 2 )
711 {
712 fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
713 fclose(file);
714 exit( 2 );
715 }
716 else
717 total_errors++;
718
719 if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
720 break;
721 if( strlen(buf) != 0 )
722 {
723 fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
724 return( 1 );
725 }
726 }
727 fclose(file);
728
729 fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
730 if( total_errors == 0 )
731 fprintf( stdout, "PASSED" );
732 else
733 fprintf( stdout, "FAILED" );
734
735 fprintf( stdout, " (%d / %d tests (%d skipped))\n",
736 total_tests - total_errors, total_tests, total_skipped );
737
738#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
739#if defined(POLARSSL_MEMORY_DEBUG)
740 memory_buffer_alloc_status();
741#endif
743#endif
744
745 return( total_errors != 0 );
746}
747
748
ASN.1 buffer writing functionality.
int asn1_write_ia5_string(unsigned char **p, unsigned char *start, const char *text, size_t text_len)
Write an IA5 string tag (ASN1_IA5_STRING) and value in ASN.1 format Note: function works backwards in...
int asn1_write_octet_string(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write an octet string tag (ASN1_OCTET_STRING) and value in ASN.1 format Note: function works backward...
Configuration options (set of defines)
#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL
Buffer too small when writing ASN.1 data structure.
Definition asn1.h:60
Memory allocation layer (Deprecated to platform layer)
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
PolarSSL Platform abstraction layer.
unsigned char * buf
Info structure for the pseudo random function.
int verify_int(char *str, int *value)
static int test_assert(int correct, const char *test)
int verify_string(char **str)
#define TEST_ASSERT(TEST)
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
int dep_check(char *str)
int dispatch_test(int cnt, char *params[50])
#define polarssl_malloc
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int get_line(FILE *f, char *buf, size_t len)
static int test_errors
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
static int unhexify(unsigned char *obuf, const char *ibuf)
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int main()
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.