Merged major refactoring of x509write module into development

This refactoring adds support for proper CSR writing and X509
certificate generation / signing
This commit is contained in:
Paul Bakker
2013-09-12 11:58:04 +02:00
27 changed files with 2942 additions and 511 deletions

View File

@@ -51,7 +51,7 @@
#endif
#endif /* !defined(ECPARAMS) */
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ECDSA_C) || \
#if !defined(POLARSSL_ECDSA_C) || \
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
!defined(ECPARAMS)
int main( int argc, char *argv[] )
@@ -59,9 +59,9 @@ int main( int argc, char *argv[] )
((void) argc);
((void) argv);
printf("POLARSSL_BIGNUM_C and/or POLARSSL_ECDSA_C and/or "
printf("POLARSSL_ECDSA_C and/or "
"POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined,"
"and/or not EC domain parameter available\n" );
"and/or no EC domain parameter available\n" );
return( 0 );
}
#else
@@ -194,6 +194,5 @@ exit:
return( ret );
}
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ECDSA_C &&
POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
#endif /* POLARSSL_ECDSA_C && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
ECPARAMS */

View File

@@ -33,21 +33,16 @@
#include "polarssl/config.h"
#include "polarssl/error.h"
#include "polarssl/rsa.h"
#include "polarssl/x509.h"
#include "polarssl/base64.h"
#include "polarssl/x509write.h"
#include "polarssl/error.h"
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
!defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO)
#if !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO)
int main( int argc, char *argv[] )
{
((void) argc);
((void) argv);
printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
"POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO not defined.\n");
printf( "POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO not defined.\n" );
return( 0 );
}
#else
@@ -82,7 +77,7 @@ struct options
int output_format; /* the output format to use */
} opt;
static int write_public_key( rsa_context *rsa, const char *output_file )
static int write_public_key( pk_context *key, const char *output_file )
{
int ret;
FILE *f;
@@ -94,14 +89,14 @@ static int write_public_key( rsa_context *rsa, const char *output_file )
if( opt.output_format == OUTPUT_FORMAT_PEM )
{
if( ( ret = x509write_pubkey_pem( rsa, output_buf, 16000 ) ) != 0 )
if( ( ret = x509write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
return( ret );
len = strlen( (char *) output_buf );
}
else
{
if( ( ret = x509write_pubkey_der( rsa, output_buf, 16000 ) ) < 0 )
if( ( ret = x509write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
return( ret );
len = ret;
@@ -119,7 +114,7 @@ static int write_public_key( rsa_context *rsa, const char *output_file )
return( 0 );
}
static int write_private_key( rsa_context *rsa, const char *output_file )
static int write_private_key( pk_context *key, const char *output_file )
{
int ret;
FILE *f;
@@ -130,14 +125,14 @@ static int write_private_key( rsa_context *rsa, const char *output_file )
memset(output_buf, 0, 16000);
if( opt.output_format == OUTPUT_FORMAT_PEM )
{
if( ( ret = x509write_key_pem( rsa, output_buf, 16000 ) ) != 0 )
if( ( ret = x509write_key_pem( key, output_buf, 16000 ) ) != 0 )
return( ret );
len = strlen( (char *) output_buf );
}
else
{
if( ( ret = x509write_key_der( rsa, output_buf, 16000 ) ) < 0 )
if( ( ret = x509write_key_der( key, output_buf, 16000 ) ) < 0 )
return( ret );
len = ret;
@@ -168,7 +163,7 @@ static int write_private_key( rsa_context *rsa, const char *output_file )
int main( int argc, char *argv[] )
{
int ret = 0;
rsa_context rsa;
pk_context key;
char buf[1024];
int i;
char *p, *q;
@@ -176,12 +171,13 @@ int main( int argc, char *argv[] )
/*
* Set to sane values
*/
memset( &rsa, 0, sizeof( rsa_context ) );
memset( buf, 0, 1024 );
pk_init( &key );
memset( buf, 0, sizeof( buf ) );
if( argc == 0 )
{
usage:
ret = 1;
printf( USAGE );
goto exit;
}
@@ -254,15 +250,11 @@ int main( int argc, char *argv[] )
printf( "\n . Loading the private key ..." );
fflush( stdout );
ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL );
ret = x509parse_keyfile( &key, opt.filename, NULL );
if( ret != 0 )
{
#ifdef POLARSSL_ERROR_C
polarssl_strerror( ret, buf, 1024 );
#endif
printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf );
rsa_free( &rsa );
printf( " failed\n ! x509parse_key returned %d", ret );
goto exit;
}
@@ -272,14 +264,23 @@ int main( int argc, char *argv[] )
* 1.2 Print the key
*/
printf( " . Key information ...\n" );
mpi_write_file( "N: ", &rsa.N, 16, NULL );
mpi_write_file( "E: ", &rsa.E, 16, NULL );
mpi_write_file( "D: ", &rsa.D, 16, NULL );
mpi_write_file( "P: ", &rsa.P, 16, NULL );
mpi_write_file( "Q: ", &rsa.Q, 16, NULL );
mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL );
mpi_write_file( "QP: ", &rsa.QP, 16, NULL );
#if defined(POLARSSL_RSA_C)
if( pk_get_type( &key ) == POLARSSL_PK_RSA )
{
rsa_context *rsa = pk_rsa( key );
mpi_write_file( "N: ", &rsa->N, 16, NULL );
mpi_write_file( "E: ", &rsa->E, 16, NULL );
mpi_write_file( "D: ", &rsa->D, 16, NULL );
mpi_write_file( "P: ", &rsa->P, 16, NULL );
mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
}
else
#endif
printf("key type not supported yet\n");
}
else if( opt.mode == MODE_PUBLIC )
@@ -290,15 +291,11 @@ int main( int argc, char *argv[] )
printf( "\n . Loading the public key ..." );
fflush( stdout );
ret = x509parse_public_keyfile_rsa( &rsa, opt.filename );
ret = x509parse_public_keyfile( &key, opt.filename );
if( ret != 0 )
{
#ifdef POLARSSL_ERROR_C
polarssl_strerror( ret, buf, 1024 );
#endif
printf( " failed\n ! x509parse_public_key_rsa returned %d - %s\n\n", ret, buf );
rsa_free( &rsa );
printf( " failed\n ! x509parse_public_key returned %d", ret );
goto exit;
}
@@ -308,24 +305,43 @@ int main( int argc, char *argv[] )
* 1.2 Print the key
*/
printf( " . Key information ...\n" );
mpi_write_file( "N: ", &rsa.N, 16, NULL );
mpi_write_file( "E: ", &rsa.E, 16, NULL );
#if defined(POLARSSL_RSA_C)
if( pk_get_type( &key ) == POLARSSL_PK_RSA )
{
rsa_context *rsa = pk_rsa( key );
mpi_write_file( "N: ", &rsa->N, 16, NULL );
mpi_write_file( "E: ", &rsa->E, 16, NULL );
}
else
#endif
printf("key type not supported yet\n");
}
else
goto usage;
if( opt.output_mode == OUTPUT_MODE_PUBLIC )
{
write_public_key( &rsa, opt.output_file );
write_public_key( &key, opt.output_file );
}
if( opt.output_mode == OUTPUT_MODE_PRIVATE )
{
write_private_key( &rsa, opt.output_file );
write_private_key( &key, opt.output_file );
}
exit:
rsa_free( &rsa );
if( ret != 0 && ret != 1)
{
#ifdef POLARSSL_ERROR_C
polarssl_strerror( ret, buf, sizeof( buf ) );
printf( " - %s\n", buf );
#else
printf("\n");
#endif
}
pk_free( &key );
#if defined(_WIN32)
printf( " + Press Enter to exit this program.\n" );
@@ -334,5 +350,4 @@ exit:
return( ret );
}
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */
#endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */