1 #include "simpleOpensslSigner.h"
5 #include <openssl/ssl.h>
6 #include <openssl/err.h>
7 #include <openssl/bio.h>
8 #include <openssl/bn.h>
9 #include <openssl/engine.h>
10 #include <openssl/x509v3.h>
14 std::shared_ptr<int> SimpleOpensslSigner::lib_ref(
15 new int( SSL_library_init() ),
20 CRYPTO_cleanup_all_ex_data();
23 std::shared_ptr<X509> loadX509FromFile( std::string filename ) {
24 FILE* f = fopen( filename.c_str(), "r" );
27 return std::shared_ptr<X509>();
30 X509* key = PEM_read_X509( f, NULL, NULL, 0 );
34 return std::shared_ptr<X509>();
37 return std::shared_ptr<X509>(
44 std::shared_ptr<EVP_PKEY> loadPkeyFromFile( std::string filename ) {
45 FILE* f = fopen( filename.c_str(), "r" );
48 return std::shared_ptr<EVP_PKEY>();
51 EVP_PKEY* key = PEM_read_PrivateKey( f, NULL, NULL, 0 );
55 return std::shared_ptr<EVP_PKEY>();
58 return std::shared_ptr<EVP_PKEY>(
60 []( EVP_PKEY * ref ) {
65 std::shared_ptr<X509> SimpleOpensslSigner::caCert = loadX509FromFile( "assured.crt" );
67 std::shared_ptr<EVP_PKEY> SimpleOpensslSigner::caKey = loadPkeyFromFile( "assured.key" );
69 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
71 throw "CA-key not found";
74 std::shared_ptr<X509Req> req = X509Req::parse( cert->csr_content );
76 int i = req->verify();
79 throw "Signature problems ... ";
81 throw "Signature did not match";
83 std::cerr << "Signature ok" << std::endl;
86 // Construct the Certificate
87 X509Cert c = X509Cert();
88 std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
89 X509* ret = retsh.get();
92 throw "Creating X509 failed.";
95 c.setIssuerNameFrom( caCert );
96 c.setPubkeyFrom( req );
97 c.setSerialNumber( 4711 );
98 c.setTimes( 0, 60 * 60 * 24 * 10 );
99 c.setExtensions( caCert, cert->SANs );
101 std::string output = c.sign( caKey );
103 std::shared_ptr<SignedCertificate> output = c.sign( caKey );