Fix key_app_writer
This commit is contained in:
parent
bb323ffc7c
commit
26b4d45f49
2 changed files with 79 additions and 59 deletions
|
@ -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 */
|
||||
|
|
|
@ -40,8 +40,7 @@
|
|||
|
||||
#if !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_X509_PARSE_C) || \
|
||||
!defined(POLARSSL_FS_IO) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
|
||||
!defined(POLARSSL_ERROR_C)
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
((void) argc);
|
||||
|
@ -49,8 +48,8 @@ int main( int argc, char *argv[] )
|
|||
|
||||
printf( "POLARSSL_X509_WRITE_C and/or POLARSSL_X509_PARSE_C and/or "
|
||||
"POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
|
||||
"POLARSSL_ERROR_C not defined.\n");
|
||||
"POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
|
||||
"not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
@ -146,7 +145,7 @@ int main( int argc, char *argv[] )
|
|||
x509write_csr_init( &req );
|
||||
x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
|
||||
pk_init( &key );
|
||||
memset( buf, 0, 1024 );
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
|
@ -267,8 +266,7 @@ int main( int argc, char *argv[] )
|
|||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
error_strerror( ret, buf, 1024 );
|
||||
printf( " failed\n ! ctr_drbg_init returned %d - %s\n", ret, buf );
|
||||
printf( " failed\n ! ctr_drbg_init returned %d", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -282,8 +280,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
|
||||
{
|
||||
error_strerror( ret, buf, 1024 );
|
||||
printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf );
|
||||
printf( " failed\n ! x509write_csr_set_subject_name returned %d", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -299,8 +296,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ret != 0 )
|
||||
{
|
||||
error_strerror( ret, buf, 1024 );
|
||||
printf( " failed\n ! x509parse_keyfile returned %d - %s\n\n", ret, buf );
|
||||
printf( " failed\n ! x509parse_keyfile returned %d", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
@ -317,14 +313,24 @@ int main( int argc, char *argv[] )
|
|||
if( ( ret = write_certificate_request( &req, opt.output_file,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
error_strerror( ret, buf, 1024 );
|
||||
printf( " failed\n ! write_certifcate_request %d - %s\n\n", ret, buf );
|
||||
printf( " failed\n ! write_certifcate_request %d", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( " ok\n" );
|
||||
|
||||
exit:
|
||||
|
||||
if( ret != 0 && ret != 1)
|
||||
{
|
||||
#ifdef POLARSSL_ERROR_C
|
||||
polarssl_strerror( ret, buf, sizeof( buf ) );
|
||||
printf( " - %s\n", buf );
|
||||
#else
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
x509write_csr_free( &req );
|
||||
pk_free( &key );
|
||||
|
||||
|
@ -336,5 +342,4 @@ exit:
|
|||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_X509_WRITE_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO &&
|
||||
POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
|
||||
POLARSSL_ERROR_C */
|
||||
POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */
|
||||
|
|
Loading…
Reference in a new issue