2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Example RSA key generation program
|
|
|
|
*
|
2010-07-18 10:13:04 +00:00
|
|
|
* Copyright (C) 2006-2010, Brainspark B.V.
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
2010-07-18 10:13:04 +00:00
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2009-07-28 17:23:11 +00:00
|
|
|
* All rights reserved.
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2009-01-03 21:22:43 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _CRT_SECURE_NO_DEPRECATE
|
|
|
|
#define _CRT_SECURE_NO_DEPRECATE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2009-01-03 21:51:57 +00:00
|
|
|
#include "polarssl/havege.h"
|
|
|
|
#include "polarssl/bignum.h"
|
|
|
|
#include "polarssl/x509.h"
|
|
|
|
#include "polarssl/rsa.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
#define KEY_SIZE 1024
|
|
|
|
#define EXPONENT 65537
|
|
|
|
|
|
|
|
int main( void )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
rsa_context rsa;
|
|
|
|
havege_state hs;
|
|
|
|
FILE *fpub = NULL;
|
|
|
|
FILE *fpriv = NULL;
|
|
|
|
|
|
|
|
printf( "\n . Seeding the random number generator..." );
|
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
havege_init( &hs );
|
|
|
|
|
|
|
|
printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
|
|
|
|
fflush( stdout );
|
|
|
|
|
2010-08-16 11:56:45 +00:00
|
|
|
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2010-08-16 11:56:45 +00:00
|
|
|
if( ( ret = rsa_gen_key( &rsa, havege_rand, &hs, KEY_SIZE, EXPONENT ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
|
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
|
|
|
|
{
|
|
|
|
printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
|
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
|
|
|
|
{
|
|
|
|
printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
printf( " ok\n . Generating the certificate..." );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
*/
|
|
|
|
printf( " ok\n\n" );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
if( fpub != NULL )
|
|
|
|
fclose( fpub );
|
|
|
|
|
|
|
|
if( fpriv != NULL )
|
|
|
|
fclose( fpriv );
|
|
|
|
|
|
|
|
rsa_free( &rsa );
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
printf( " Press Enter to exit this program.\n" );
|
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|