14 #include "certificategenerator.h"
16 #if defined( _WIN32 ) || defined ( _WIN32_WCE )
17 # include "../config.h.win"
22 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
27 #include <openssl/pem.h>
28 #include <openssl/conf.h>
29 #include <openssl/x509v3.h>
30 #ifndef OPENSSL_NO_ENGINE
31 #include <openssl/engine.h>
48 const std::string& key,
49 const std::string& path,
50 int bits,
int days,
int serial )
52 if( domain.empty() || key.empty() || path.empty()
53 || bits < 1024 || days < 0 || serial <= 0 )
55 printf(
"sth is wrong with the generated certificate\n" );
60 CRYPTO_mem_ctrl( CRYPTO_MEM_CHECK_ON );
63 FILE* fp = fopen( key.c_str(),
"r" );
67 EVP_PKEY* pk = PEM_read_PrivateKey( fp, 0, 0, 0 );
73 printf(
"PEM_read_PrivateKey failed\n" );
79 printf(
"X509_new failed\n" );
83 X509_set_version( x, 2 );
84 ASN1_INTEGER_set( X509_get_serialNumber( x ), serial );
86 X509_gmtime_adj( X509_get_notBefore( x ), 0 );
87 X509_gmtime_adj( X509_get_notAfter( x ),
88 static_cast<long>( 60 * 60 * 24 * days ) );
89 X509_set_pubkey( x, pk );
91 X509_NAME* name = X509_get_subject_name( x );
92 X509_NAME_add_entry_by_txt( name,
"CN", MBSTRING_ASC,
93 reinterpret_cast<const unsigned char*>( domain.c_str() ),
95 X509_set_issuer_name( x, name );
97 if( !X509_sign( x, pk, EVP_sha1() ) )
99 printf(
"X509_sign failed\n" );
103 fp = fopen( path.c_str(),
"w" );
107 PEM_write_X509( fp, x );
111 printf(
"no openssl!\n");