2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Example RSA key generation program
|
|
|
|
*
|
2015-01-23 10:45:19 +01:00
|
|
|
* Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-01-28 18:12:07 +01:00
|
|
|
* This file is part of mbed TLS (https://polarssl.org)
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2009-01-03 22:22:43 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2013-09-20 13:30:43 +02:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-01-19 15:26:37 +01:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
|
|
|
#else
|
|
|
|
#define polarssl_printf printf
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <stdio.h>
|
2015-02-10 13:18:15 +01:00
|
|
|
#include <string.h>
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-12-04 18:09:26 +01:00
|
|
|
#include "polarssl/entropy.h"
|
|
|
|
#include "polarssl/ctr_drbg.h"
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/bignum.h"
|
|
|
|
#include "polarssl/x509.h"
|
|
|
|
#include "polarssl/rsa.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#define KEY_SIZE 1024
|
|
|
|
#define EXPONENT 65537
|
|
|
|
|
2011-12-04 18:09:26 +01:00
|
|
|
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
|
2011-05-26 15:16:06 +02:00
|
|
|
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
|
2011-12-04 18:09:26 +01:00
|
|
|
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
2011-11-18 15:26:47 +01:00
|
|
|
int main( int argc, char *argv[] )
|
2011-05-26 15:16:06 +02:00
|
|
|
{
|
2011-11-18 15:26:47 +01:00
|
|
|
((void) argc);
|
|
|
|
((void) argv);
|
|
|
|
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
|
2011-05-26 15:16:06 +02:00
|
|
|
"POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
|
2011-12-04 18:09:26 +01:00
|
|
|
"POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
|
2011-05-26 15:16:06 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2011-11-18 15:26:47 +01:00
|
|
|
int main( int argc, char *argv[] )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
rsa_context rsa;
|
2011-12-04 18:09:26 +01:00
|
|
|
entropy_context entropy;
|
|
|
|
ctr_drbg_context ctr_drbg;
|
2009-01-03 22:22:43 +01:00
|
|
|
FILE *fpub = NULL;
|
|
|
|
FILE *fpriv = NULL;
|
2013-06-24 13:01:08 +02:00
|
|
|
const char *pers = "rsa_genkey";
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
((void) argc);
|
|
|
|
((void) argv);
|
|
|
|
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( "\n . Seeding the random number generator..." );
|
2009-01-03 22:22:43 +01:00
|
|
|
fflush( stdout );
|
|
|
|
|
2011-12-04 18:09:26 +01:00
|
|
|
entropy_init( &entropy );
|
|
|
|
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
2013-06-24 13:01:08 +02:00
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen( pers ) ) ) != 0 )
|
2011-12-04 18:09:26 +01:00
|
|
|
{
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
2011-12-04 18:09:26 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
|
2009-01-03 22:22:43 +01:00
|
|
|
fflush( stdout );
|
|
|
|
|
2010-08-16 13:56:45 +02:00
|
|
|
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-12-04 18:09:26 +01:00
|
|
|
if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
|
|
|
|
EXPONENT ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
|
2009-01-03 22:22:43 +01:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
|
|
|
|
{
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
|
|
|
|
{
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
|
2009-01-03 22:22:43 +01:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
|
|
|
|
{
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
|
|
|
|
{
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/*
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " ok\n . Generating the certificate..." );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
x509write_init_raw( &cert );
|
|
|
|
x509write_add_pubkey( &cert, &rsa );
|
|
|
|
x509write_add_subject( &cert, "CN='localhost'" );
|
|
|
|
x509write_add_validity( &cert, "2007-09-06 17:00:32",
|
|
|
|
"2010-09-06 17:00:32" );
|
|
|
|
x509write_create_selfsign( &cert, &rsa );
|
|
|
|
x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
|
|
|
|
x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
|
|
|
|
x509write_free_raw( &cert );
|
|
|
|
*/
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " ok\n\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
if( fpub != NULL )
|
|
|
|
fclose( fpub );
|
|
|
|
|
|
|
|
if( fpriv != NULL )
|
|
|
|
fclose( fpriv );
|
|
|
|
|
|
|
|
rsa_free( &rsa );
|
2014-06-18 16:44:11 +02:00
|
|
|
ctr_drbg_free( &ctr_drbg );
|
2013-09-28 15:23:03 +02:00
|
|
|
entropy_free( &entropy );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
#if defined(_WIN32)
|
2015-01-19 15:26:37 +01:00
|
|
|
polarssl_printf( " Press Enter to exit this program.\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2011-12-04 18:09:26 +01:00
|
|
|
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
|
|
|
|
POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|