1 #if !defined(POLARSSL_CONFIG_FILE)
4 #include POLARSSL_CONFIG_FILE
7 #ifdef POLARSSL_PEM_WRITE_C
14 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
18 #if defined(POLARSSL_PLATFORM_C)
21 #define polarssl_malloc malloc
22 #define polarssl_free free
27 typedef UINT32 uint32_t;
40 #define GET_UINT32_BE(n,b,i) \
42 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
43 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
44 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
45 | ( (uint32_t) (b)[(i) + 3] ); \
50 #define PUT_UINT32_BE(n,b,i) \
52 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
53 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
54 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
55 (b)[(i) + 3] = (unsigned char) ( (n) ); \
59 static int unhexify(
unsigned char *obuf,
const char *ibuf)
62 int len = strlen(ibuf) / 2;
63 assert(!(strlen(ibuf) %1));
68 if( c >=
'0' && c <=
'9' )
70 else if( c >=
'a' && c <=
'f' )
72 else if( c >=
'A' && c <=
'F' )
78 if( c2 >=
'0' && c2 <=
'9' )
80 else if( c2 >=
'a' && c2 <=
'f' )
82 else if( c2 >=
'A' && c2 <=
'F' )
87 *obuf++ = ( c << 4 ) | c2;
93 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
105 *obuf++ =
'a' + h - 10;
110 *obuf++ =
'a' + l - 10;
127 size_t actual_len = len != 0 ? len : 1;
132 memset( p, 0x00, actual_len );
151 *olen = strlen(ibuf) / 2;
157 assert( obuf != NULL );
173 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
175 #if !defined(__OpenBSD__)
178 if( rng_state != NULL )
181 for( i = 0; i < len; ++i )
184 if( rng_state != NULL )
187 arc4random_buf( output, len );
198 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
200 if( rng_state != NULL )
203 memset( output, 0, len );
230 if( rng_state == NULL )
239 memcpy( output, info->
buf, use_len );
240 info->
buf += use_len;
244 if( len - use_len > 0 )
245 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
274 uint32_t i, *k, sum, delta=0x9E3779B9;
275 unsigned char result[4], *out = output;
277 if( rng_state == NULL )
284 size_t use_len = ( len > 4 ) ? 4 : len;
287 for( i = 0; i < 32; i++ )
289 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
291 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
295 memcpy( out, result, use_len );
307 #if defined(POLARSSL_PLATFORM_C)
310 #define polarssl_printf printf
311 #define polarssl_malloc malloc
312 #define polarssl_free free
317 #ifdef POLARSSL_PEM_WRITE_C
319 #define TEST_SUITE_ACTIVE
321 static int test_assert(
int correct,
const char *test )
328 printf(
"FAILED\n" );
329 printf(
" %s\n", test );
334 #define TEST_ASSERT( TEST ) \
335 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
336 if( test_errors) goto exit; \
341 if( (*str)[0] !=
'"' ||
342 (*str)[strlen( *str ) - 1] !=
'"' )
344 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
349 (*str)[strlen( *str ) - 1] =
'\0';
361 for( i = 0; i < strlen( str ); i++ )
363 if( i == 0 && str[i] ==
'-' )
369 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
370 str[i - 1] ==
'0' && str[i] ==
'x' )
376 if( ! ( ( str[i] >=
'0' && str[i] <=
'9' ) ||
377 ( hex && ( ( str[i] >=
'a' && str[i] <=
'f' ) ||
378 ( str[i] >=
'A' && str[i] <=
'F' ) ) ) ) )
388 *value = strtol( str, NULL, 16 );
390 *value = strtol( str, NULL, 10 );
397 printf(
"Expected integer for parameter and got: %s\n", str );
401 void test_suite_pem_write_buffer(
char *start,
char *end,
char *buf_str,
char *result_str )
403 unsigned char buf[5000];
404 unsigned char *check_buf = NULL;
406 size_t buf_len, olen = 0, olen2 = 0;
408 memset( buf, 0,
sizeof( buf ) );
412 ret = pem_write_buffer( start, end, buf, buf_len, NULL, 0, &olen );
418 memset( check_buf, 0, olen );
419 ret = pem_write_buffer( start, end, buf, buf_len, check_buf, olen, &olen2 );
422 TEST_ASSERT( olen > strlen( (
char*) result_str ) );
424 TEST_ASSERT( strncmp( (
char *) check_buf, (
char *) result_str, olen ) == 0 );
450 #if defined(TEST_SUITE_ACTIVE)
451 if( strcmp( params[0],
"pem_write_buffer" ) == 0 )
454 char *param1 = params[1];
455 char *param2 = params[2];
456 char *param3 = params[3];
457 char *param4 = params[4];
461 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 5 );
470 test_suite_pem_write_buffer( param1, param2, param3, param4 );
478 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
492 ret = fgets( buf, len, f );
496 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
497 buf[strlen(buf) - 1] =
'\0';
498 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
499 buf[strlen(buf) - 1] =
'\0';
512 while( *p !=
'\0' && p < buf + len )
522 if( p + 1 < buf + len )
534 for( i = 0; i < cnt; i++ )
541 if( *p ==
'\\' && *(p + 1) ==
'n' )
546 else if( *p ==
'\\' && *(p + 1) ==
':' )
551 else if( *p ==
'\\' && *(p + 1) ==
'?' )
567 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
568 const char *filename =
"/home/iurt/rpmbuild/BUILD/polarssl-1.3.8/tests/suites/test_suite_pem.data";
573 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
574 unsigned char alloc_buf[1000000];
578 file = fopen( filename,
"r" );
581 fprintf( stderr,
"Failed to open\n" );
585 while( !feof( file ) )
589 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
591 fprintf( stdout,
"%s%.66s",
test_errors ?
"\n" :
"", buf );
592 fprintf( stdout,
" " );
593 for( i = strlen( buf ) + 1; i < 67; i++ )
594 fprintf( stdout,
"." );
595 fprintf( stdout,
" " );
600 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
604 if( strcmp( params[0],
"depends_on" ) == 0 )
606 for( i = 1; i < cnt; i++ )
610 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
621 if( skip == 1 || ret == 3 )
624 fprintf( stdout,
"----\n" );
629 fprintf( stdout,
"PASS\n" );
634 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
641 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
643 if( strlen(buf) != 0 )
645 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
651 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
652 if( total_errors == 0 )
653 fprintf( stdout,
"PASSED" );
655 fprintf( stdout,
"FAILED" );
657 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
658 total_tests - total_errors, total_tests, total_skipped );
660 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
661 #if defined(POLARSSL_MEMORY_DEBUG)
662 memory_buffer_alloc_status();
667 return( total_errors != 0 );
Memory allocation layer (Deprecated to platform layer)
static int unhexify(unsigned char *obuf, const char *ibuf)
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 rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
Info structure for the pseudo random function.
void memory_buffer_alloc_free(void)
Free the mutex for thread-safety and clear remaining memory.
Configuration options (set of defines)
static int test_assert(int correct, const char *test)
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int main(int argc, char *argv[])
int memory_buffer_alloc_init(unsigned char *buf, size_t len)
Initialize use of stack-based memory allocator.
#define TEST_ASSERT(TEST)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
Privacy Enhanced Mail (PEM) decoding.
#define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL
Output buffer too small.
static unsigned char * zero_alloc(size_t len)
Allocate and zeroize a buffer.
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
RFC 1521 base64 encoding/decoding.
int verify_string(char **str)
int dispatch_test(int cnt, char *params[50])
static unsigned char * unhexify_alloc(const char *ibuf, size_t *olen)
Allocate and fill a buffer from hex data.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int verify_int(char *str, int *value)
int get_line(FILE *f, char *buf, size_t len)