3 #ifdef POLARSSL_BIGNUM_C
5 #ifdef POLARSSL_PK_PARSE_C
16 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
20 #if defined(WANT_NOT_RND_MPI)
21 #if defined(POLARSSL_BIGNUM_C)
24 #error "not_rnd_mpi() need bignum.c"
30 typedef UINT32 uint32_t;
43 #define GET_UINT32_BE(n,b,i) \
45 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
46 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
47 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
48 | ( (uint32_t) (b)[(i) + 3] ); \
53 #define PUT_UINT32_BE(n,b,i) \
55 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
56 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
57 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
58 (b)[(i) + 3] = (unsigned char) ( (n) ); \
62 static int unhexify(
unsigned char *obuf,
const char *ibuf)
65 int len = strlen(ibuf) / 2;
66 assert(!(strlen(ibuf) %1));
71 if( c >=
'0' && c <=
'9' )
73 else if( c >=
'a' && c <=
'f' )
75 else if( c >=
'A' && c <=
'F' )
81 if( c2 >=
'0' && c2 <=
'9' )
83 else if( c2 >=
'a' && c2 <=
'f' )
85 else if( c2 >=
'A' && c2 <=
'F' )
90 *obuf++ = ( c << 4 ) | c2;
96 static void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
108 *obuf++ =
'a' + h - 10;
113 *obuf++ =
'a' + l - 10;
129 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
133 if( rng_state != NULL )
136 for( i = 0; i < len; ++i )
147 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
149 if( rng_state != NULL )
152 memset( output, 0, len );
179 if( rng_state == NULL )
188 memcpy( output, info->
buf, use_len );
189 info->
buf += use_len;
193 if( len - use_len > 0 )
194 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
223 uint32_t i, *k, sum, delta=0x9E3779B9;
224 unsigned char result[4];
226 if( rng_state == NULL )
233 size_t use_len = ( len > 4 ) ? 4 : len;
236 for( i = 0; i < 32; i++ )
238 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
240 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
244 memcpy( output, result, use_len );
251 #if defined(WANT_NOT_RND_MPI)
260 #define ciL (sizeof(t_uint))
261 #define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
262 static int not_rnd_mpi(
void *in,
unsigned char *out,
size_t len )
264 char *str = (
char *) in;
273 X.
n = CHARS_TO_LIMBS( len );
279 assert( strlen( str ) / 2 == len );
291 #ifdef POLARSSL_BIGNUM_C
292 #ifdef POLARSSL_FS_IO
293 #ifdef POLARSSL_PK_PARSE_C
295 #define TEST_SUITE_ACTIVE
303 if( test_errors == 1 )
304 printf(
"FAILED\n" );
305 printf(
" %s\n", test );
310 #define TEST_ASSERT( TEST ) \
311 do { test_assert( (TEST) ? 1 : 0, #TEST ); \
312 if( test_errors) return; \
317 if( (*str)[0] !=
'"' ||
318 (*str)[strlen( *str ) - 1] !=
'"' )
320 printf(
"Expected string (with \"\") for parameter and got: %s\n", *str );
325 (*str)[strlen( *str ) - 1] =
'\0';
337 for( i = 0; i < strlen( str ); i++ )
339 if( i == 0 && str[i] ==
'-' )
345 if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
346 str[i - 1] ==
'0' && str[i] ==
'x' )
352 if( str[i] <
'0' || str[i] >
'9' )
362 *value = strtol( str, NULL, 16 );
364 *value = strtol( str, NULL, 10 );
369 #ifdef POLARSSL_PEM_WRITE_C
370 #ifdef POLARSSL_X509_CRT_WRITE_C
371 #ifdef POLARSSL_SHA1_C
372 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
377 #endif // POLARSSL_PEM_WRITE_C
378 #endif // POLARSSL_X509_CRT_WRITE_C
379 #endif // POLARSSL_SHA1_C
380 #ifdef POLARSSL_PEM_WRITE_C
381 #ifdef POLARSSL_X509_CSR_WRITE_C
382 if( strcmp( str,
"POLARSSL_MD_SHA1" ) == 0 )
387 #endif // POLARSSL_PEM_WRITE_C
388 #endif // POLARSSL_X509_CSR_WRITE_C
389 #ifdef POLARSSL_PEM_WRITE_C
390 #ifdef POLARSSL_X509_CSR_WRITE_C
391 if( strcmp( str,
"POLARSSL_MD_MD4" ) == 0 )
396 #endif // POLARSSL_PEM_WRITE_C
397 #endif // POLARSSL_X509_CSR_WRITE_C
398 #ifdef POLARSSL_PEM_WRITE_C
399 #ifdef POLARSSL_X509_CSR_WRITE_C
400 if( strcmp( str,
"POLARSSL_MD_SHA256" ) == 0 )
405 #endif // POLARSSL_PEM_WRITE_C
406 #endif // POLARSSL_X509_CSR_WRITE_C
407 #ifdef POLARSSL_PEM_WRITE_C
408 #ifdef POLARSSL_X509_CSR_WRITE_C
409 if( strcmp( str,
"POLARSSL_MD_MD5" ) == 0 )
414 #endif // POLARSSL_PEM_WRITE_C
415 #endif // POLARSSL_X509_CSR_WRITE_C
416 #ifdef POLARSSL_PEM_WRITE_C
417 #ifdef POLARSSL_X509_CSR_WRITE_C
418 if( strcmp( str,
"POLARSSL_MD_SHA512" ) == 0 )
423 #endif // POLARSSL_PEM_WRITE_C
424 #endif // POLARSSL_X509_CSR_WRITE_C
425 #ifdef POLARSSL_PEM_WRITE_C
426 #ifdef POLARSSL_X509_CSR_WRITE_C
427 if( strcmp( str,
"POLARSSL_MD_SHA224" ) == 0 )
432 #endif // POLARSSL_PEM_WRITE_C
433 #endif // POLARSSL_X509_CSR_WRITE_C
434 #ifdef POLARSSL_PEM_WRITE_C
435 #ifdef POLARSSL_X509_CSR_WRITE_C
436 if( strcmp( str,
"POLARSSL_MD_SHA384" ) == 0 )
441 #endif // POLARSSL_PEM_WRITE_C
442 #endif // POLARSSL_X509_CSR_WRITE_C
445 printf(
"Expected integer for parameter and got: %s\n", str );
449 #ifdef POLARSSL_PEM_WRITE_C
450 #ifdef POLARSSL_X509_CSR_WRITE_C
451 void test_suite_x509_csr_check(
char *key_file,
int md_type,
452 char *cert_req_check_file )
456 unsigned char buf[4000];
457 unsigned char check_buf[4000];
459 size_t olen = 0, pem_len = 0;
461 char *subject_name =
"C=NL,O=PolarSSL,CN=PolarSSL Server 1";
478 pem_len = strlen( (
char *) buf );
480 f = fopen( cert_req_check_file,
"r" );
482 olen = fread( check_buf, 1,
sizeof( check_buf ), f );
486 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
494 #ifdef POLARSSL_PEM_WRITE_C
495 #ifdef POLARSSL_X509_CRT_WRITE_C
496 #ifdef POLARSSL_SHA1_C
497 void test_suite_x509_crt_check(
char *subject_key_file,
char *subject_pwd,
498 char *subject_name,
char *issuer_key_file,
499 char *issuer_pwd,
char *issuer_name,
500 char *serial_str,
char *not_before,
char *not_after,
501 int md_type,
char *cert_check_file )
505 unsigned char buf[4000];
506 unsigned char check_buf[5000];
509 size_t olen = 0, pem_len = 0;
519 subject_pwd ) == 0 );
542 pem_len = strlen( (
char *) buf );
544 f = fopen( cert_check_file,
"r" );
546 TEST_ASSERT( ( olen = fread( check_buf, 1,
sizeof(check_buf), f ) ) <
551 TEST_ASSERT( memcmp( buf, check_buf, pem_len - 1 ) == 0 );
573 if( strcmp( str,
"POLARSSL_CIPHER_MODE_CBC" ) == 0 )
575 #if defined(POLARSSL_CIPHER_MODE_CBC)
581 if( strcmp( str,
"POLARSSL_DES_C" ) == 0 )
583 #if defined(POLARSSL_DES_C)
589 if( strcmp( str,
"POLARSSL_MD4_C" ) == 0 )
591 #if defined(POLARSSL_MD4_C)
597 if( strcmp( str,
"POLARSSL_SHA512_C" ) == 0 )
599 #if defined(POLARSSL_SHA512_C)
605 if( strcmp( str,
"POLARSSL_SHA1_C" ) == 0 )
607 #if defined(POLARSSL_SHA1_C)
613 if( strcmp( str,
"POLARSSL_MD5_C" ) == 0 )
615 #if defined(POLARSSL_MD5_C)
621 if( strcmp( str,
"POLARSSL_SHA256_C" ) == 0 )
623 #if defined(POLARSSL_SHA256_C)
629 if( strcmp( str,
"POLARSSL_RSA_C" ) == 0 )
631 #if defined(POLARSSL_RSA_C)
637 if( strcmp( str,
"POLARSSL_PKCS1_V15" ) == 0 )
639 #if defined(POLARSSL_PKCS1_V15)
656 #if defined(TEST_SUITE_ACTIVE)
657 if( strcmp( params[0],
"x509_csr_check" ) == 0 )
659 #ifdef POLARSSL_PEM_WRITE_C
660 #ifdef POLARSSL_X509_CSR_WRITE_C
662 char *param1 = params[1];
664 char *param3 = params[3];
668 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 4 );
673 if(
verify_int( params[2], ¶m2 ) != 0 )
return( 2 );
676 test_suite_x509_csr_check( param1, param2, param3 );
684 if( strcmp( params[0],
"x509_crt_check" ) == 0 )
686 #ifdef POLARSSL_PEM_WRITE_C
687 #ifdef POLARSSL_X509_CRT_WRITE_C
688 #ifdef POLARSSL_SHA1_C
690 char *param1 = params[1];
691 char *param2 = params[2];
692 char *param3 = params[3];
693 char *param4 = params[4];
694 char *param5 = params[5];
695 char *param6 = params[6];
696 char *param7 = params[7];
697 char *param8 = params[8];
698 char *param9 = params[9];
700 char *param11 = params[11];
704 fprintf( stderr,
"\nIncorrect argument count (%d != %d)\n", cnt, 12 );
717 if(
verify_int( params[10], ¶m10 ) != 0 )
return( 2 );
720 test_suite_x509_crt_check( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11 );
731 fprintf( stdout,
"FAILED\nSkipping unknown test function '%s'\n", params[0] );
745 ret = fgets( buf, len, f );
749 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\n' )
750 buf[strlen(buf) - 1] =
'\0';
751 if( strlen( buf ) && buf[strlen(buf) - 1] ==
'\r' )
752 buf[strlen(buf) - 1] =
'\0';
765 while( *p !=
'\0' && p < buf + len )
775 if( p + 1 < buf + len )
787 for( i = 0; i < cnt; i++ )
794 if( *p ==
'\\' && *(p + 1) ==
'n' )
799 else if( *p ==
'\\' && *(p + 1) ==
':' )
804 else if( *p ==
'\\' && *(p + 1) ==
'?' )
820 int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
821 const char *filename =
"/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_x509write.data";
826 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
827 unsigned char alloc_buf[1000000];
828 memory_buffer_alloc_init( alloc_buf,
sizeof(alloc_buf) );
831 file = fopen( filename,
"r" );
834 fprintf( stderr,
"Failed to open\n" );
838 while( !feof( file ) )
842 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
844 fprintf( stdout,
"%s%.66s", test_errors ?
"\n" :
"", buf );
845 fprintf( stdout,
" " );
846 for( i = strlen( buf ) + 1; i < 67; i++ )
847 fprintf( stdout,
"." );
848 fprintf( stdout,
" " );
853 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
857 if( strcmp( params[0],
"depends_on" ) == 0 )
859 for( i = 1; i < cnt; i++ )
863 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
874 if( skip == 1 || ret == 3 )
877 fprintf( stdout,
"----\n" );
880 else if( ret == 0 && test_errors == 0 )
882 fprintf( stdout,
"PASS\n" );
887 fprintf( stderr,
"FAILED: FATAL PARSE ERROR\n" );
894 if( ( ret =
get_line( file, buf,
sizeof(buf) ) ) != 0 )
896 if( strlen(buf) != 0 )
898 fprintf( stderr,
"Should be empty %d\n", (
int) strlen(buf) );
904 fprintf( stdout,
"\n----------------------------------------------------------------------------\n\n");
905 if( total_errors == 0 )
906 fprintf( stdout,
"PASSED" );
908 fprintf( stdout,
"FAILED" );
910 fprintf( stdout,
" (%d / %d tests (%d skipped))\n",
911 total_tests - total_errors, total_tests, total_skipped );
913 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
914 #if defined(POLARSSL_MEMORY_DEBUG)
915 memory_buffer_alloc_status();
917 memory_buffer_alloc_free();
920 return( total_errors != 0 );
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
void x509write_csr_free(x509write_csr *ctx)
Free the contents of a CSR context.
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
int x509write_crt_set_validity(x509write_cert *ctx, const char *not_before, const char *not_after)
Set the validity period for a Certificate Timestamps should be in string format for UTC timezone i...
Info structure for the pseudo random function.
void x509write_csr_set_md_alg(x509write_csr *ctx, md_type_t md_alg)
Set the MD algorithm to use for the signature (e.g.
Configuration options (set of defines)
int x509write_csr_pem(x509write_csr *ctx, unsigned char *buf, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Write a CSR (Certificate Signing Request) to a PEM string.
void x509write_crt_set_md_alg(x509write_cert *ctx, md_type_t md_alg)
Set the MD algorithm to use for the signature (e.g.
#define PUT_UINT32_BE(n, b, i)
static int test_assert(int correct, char *test)
void mpi_init(mpi *X)
Initialize one MPI.
int main(int argc, char *argv[])
Object Identifier (OID) database.
Multi-precision integer library.
#define TEST_ASSERT(TEST)
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
void x509write_crt_free(x509write_cert *ctx)
Free the contents of a CRT write context.
Privacy Enhanced Mail (PEM) decoding.
int x509write_crt_set_subject_key_identifier(x509write_cert *ctx)
Set the subjectKeyIdentifier extension for a CRT Requires that x509write_crt_set_subject_key() has be...
int x509write_crt_set_issuer_name(x509write_cert *ctx, const char *issuer_name)
Set the issuer name for a Certificate Issuer names should contain a comma-separated list of OID types...
X.509 certificate signing request parsing and writing.
void mpi_free(mpi *X)
Unallocate one MPI.
void x509write_csr_init(x509write_csr *ctx)
Initialize a CSR context.
X.509 certificate parsing and writing.
void x509write_crt_set_issuer_key(x509write_cert *ctx, pk_context *key)
Set the issuer key used for signing the certificate.
int parse_arguments(char *buf, size_t len, char *params[50])
int x509write_crt_set_serial(x509write_cert *ctx, const mpi *serial)
Set the serial number for a Certificate.
void x509write_csr_set_key(x509write_csr *ctx, pk_context *key)
Set the key for a CSR (public key will be included, private key used to sign the CSR when writing it)...
int x509write_crt_set_authority_key_identifier(x509write_cert *ctx)
Set the authorityKeyIdentifier extension for a CRT Requires that x509write_crt_set_issuer_key() has b...
Container for writing a certificate (CRT)
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
int x509write_crt_set_subject_name(x509write_cert *ctx, const char *subject_name)
Set the subject name for a Certificate Subject names should contain a comma-separated list of OID typ...
int verify_string(char **str)
void pk_free(pk_context *ctx)
Free a pk_context.
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
int x509write_crt_set_basic_constraints(x509write_cert *ctx, int is_ca, int max_pathlen)
Set the basicConstraints extension for a CRT.
int dispatch_test(int cnt, char *params[50])
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
int x509write_csr_set_subject_name(x509write_csr *ctx, const char *subject_name)
Set the subject name for a CSR Subject names should contain a comma-separated list of OID types and v...
void x509write_crt_init(x509write_cert *ctx)
Initialize a CRT writing 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.
int verify_int(char *str, int *value)
static int unhexify(unsigned char *obuf, const char *ibuf)
void x509write_crt_set_subject_key(x509write_cert *ctx, pk_context *key)
Set the subject public key for the certificate.
int pk_parse_keyfile(pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
int x509write_crt_pem(x509write_cert *ctx, unsigned char *buf, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Write a built up certificate to a X509 PEM string.
int get_line(FILE *f, char *buf, size_t len)
Container for writing a CSR.