1 #include "simpleOpensslSigner.h"
6 #include <openssl/ssl.h>
7 #include <openssl/err.h>
8 #include <openssl/bio.h>
9 #include <openssl/bn.h>
10 #include <openssl/engine.h>
11 #include <openssl/x509v3.h>
17 extern std::vector<Profile> profiles;
19 std::shared_ptr<int> SimpleOpensslSigner::lib_ref = ssl_lib_ref;
21 SimpleOpensslSigner::SimpleOpensslSigner( Profile& prof ) : prof( prof ) {
24 SimpleOpensslSigner::~SimpleOpensslSigner() {
27 std::shared_ptr<BIGNUM> SimpleOpensslSigner::nextSerial( uint16_t profile ) {
28 std::string res = readFile( "serial" );
36 throw "Initing serial failed";
39 if( !BN_hex2bn( &bn, res.c_str() + 1 ) ) {
40 throw "Parsing serial failed.";
44 std::shared_ptr<BIGNUM> serial = std::shared_ptr<BIGNUM>( bn, BN_free );
46 std::shared_ptr<unsigned char> data = std::shared_ptr<unsigned char>( ( unsigned char* ) malloc( BN_num_bytes( serial.get() ) + 20 ), free );
47 int len = BN_bn2bin( serial.get(), data.get() );
49 data.get()[len] = 0x0;
50 data.get()[len + 1] = 0x0; // signer id
52 data.get()[len + 2] = profile >> 8;
53 data.get()[len + 3] = profile & 0xFF; // profile id
55 if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
56 throw "Big number math failed while calcing serials.";
59 std::shared_ptr<char> serStr = std::shared_ptr<char>(
60 BN_bn2hex( serial.get() ),
64 writeFile( "serial", serStr.get() );
66 return std::shared_ptr<BIGNUM>( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free );
69 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
70 std::stringstream signlog;
73 throw "CA-key not found";
76 signlog << "FINE: CA-key is correctly loaded." << std::endl;
78 std::shared_ptr<X509Req> req;
80 if( cert->csr_type == "SPKAC" ) {
81 req = X509Req::parseSPKAC( cert->csr_content );
82 } else if( cert->csr_type == "CSR" ) {
83 req = X509Req::parseCSR( cert->csr_content );
85 throw "Error, unknown REQ rype " + ( cert->csr_type );
88 int i = req->verify();
91 throw "Signature problems ... ";
93 throw "Signature did not match";
95 signlog << "FINE: Signature ok" << std::endl;
98 // Construct the Certificate
99 X509Cert c = X509Cert();
100 std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
101 X509* ret = retsh.get();
104 throw "Creating X509 failed.";
107 X509_NAME* subjectP = X509_NAME_new();
110 throw "malloc failure";
113 for( std::shared_ptr<AVA> a : cert->AVAs ) {
114 signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
116 if( a->name == "CN" ) {
117 c.addRDN( NID_commonName, a->value );
118 } else if( a->name == "EMAIL" ) {
119 c.addRDN( NID_pkcs9_emailAddress, a->value );
120 } else if( a->name == "C" ) {
121 c.addRDN( NID_countryName, a->value );
122 } else if( a->name == "L" ) {
123 c.addRDN( NID_localityName, a->value );
124 } else if( a->name == "ST" ) {
125 c.addRDN( NID_stateOrProvinceName, a->value );
126 } else if( a->name == "O" ) {
127 c.addRDN( NID_organizationName, a->value );
128 } else if( a->name == "OU" ) {
129 c.addRDN( NID_organizationalUnitName, a->value );
131 throw "unknown AVA-type";
135 c.setIssuerNameFrom( prof.ca );
136 c.setPubkeyFrom( req );
137 long int profile = strtol( cert->profile.c_str(), 0, 10 );
139 if( profile > 0xFFFF || profile < 0 || ( profile == 0 && cert->profile != "0" ) ) {
140 throw "invalid profile id";
143 std::shared_ptr<BIGNUM> ser = nextSerial( profile );
144 c.setSerialNumber( ser.get() );
145 c.setTimes( 0, 60 * 60 * 24 * 10 );
146 signlog << "FINE: Setting extensions." << std::endl;
147 c.setExtensions( prof.ca, cert->SANs );
148 signlog << "FINE: Signed" << std::endl;
149 std::shared_ptr<SignedCertificate> output = c.sign( prof.caKey, cert->md );
150 signlog << "FINE: all went well" << std::endl;
151 output->log = signlog.str();