1 #include "simpleOpensslSigner.h"
5 #include <unordered_map>
7 #include <openssl/ssl.h>
8 #include <openssl/err.h>
9 #include <openssl/bio.h>
10 #include <openssl/bn.h>
11 #include <openssl/engine.h>
12 #include <openssl/x509v3.h>
18 extern std::unordered_map<std::string, Profile> profiles;
20 std::shared_ptr<int> SimpleOpensslSigner::lib_ref = ssl_lib_ref;
22 SimpleOpensslSigner::SimpleOpensslSigner() {
25 SimpleOpensslSigner::~SimpleOpensslSigner() {
28 std::pair<std::shared_ptr<BIGNUM>, std::string> SimpleOpensslSigner::nextSerial( Profile& prof ) {
29 uint16_t profile = prof.id;
30 std::string res = readFile( prof.ca->path + "/serial" );
38 throw "Initing serial failed";
41 if( !BN_hex2bn( &bn, res.c_str() + 1 ) ) {
42 throw "Parsing serial failed.";
46 std::shared_ptr<BIGNUM> serial = std::shared_ptr<BIGNUM>( bn, BN_free );
48 std::shared_ptr<unsigned char> data = std::shared_ptr<unsigned char>( ( unsigned char* ) malloc( BN_num_bytes( serial.get() ) + 20 ), free );
49 int len = BN_bn2bin( serial.get(), data.get() );
51 data.get()[len] = 0x0;
52 data.get()[len + 1] = 0x0; // signer id
54 data.get()[len + 2] = profile >> 8;
55 data.get()[len + 3] = profile & 0xFF; // profile id
57 if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
58 throw "Big number math failed while calcing serials.";
61 std::shared_ptr<char> serStr = std::shared_ptr<char>(
62 BN_bn2hex( serial.get() ),
67 writeFile( prof.ca->path + "/serial", serStr.get() );
69 return std::pair<std::shared_ptr<BIGNUM>, std::string>( std::shared_ptr<BIGNUM>( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free ), std::string( serStr.get() ) );
72 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
73 std::stringstream signlog;
75 signlog << "FINE: profile is " << cert->profile << std::endl;
77 Profile& prof = profiles.at( cert->profile );
80 throw "CA-key not found";
83 signlog << "FINE: CA-key is correctly loaded." << std::endl;
84 signlog << "FINE: Profile id is: " << prof.id << std::endl;
85 signlog << "FINE: ku is: " << prof.ku << std::endl;
86 signlog << "FINE: eku is: " << prof.eku << std::endl;
88 std::shared_ptr<X509Req> req;
90 if( cert->csr_type == "SPKAC" ) {
91 req = X509Req::parseSPKAC( cert->csr_content );
92 } else if( cert->csr_type == "CSR" ) {
93 req = X509Req::parseCSR( cert->csr_content );
95 throw "Error, unknown REQ rype " + ( cert->csr_type );
98 int i = req->verify();
101 throw "Signature problems ... ";
102 } else if( i == 0 ) {
103 throw "Signature did not match";
105 signlog << "FINE: Signature ok" << std::endl;
108 // Construct the Certificate
109 X509Cert c = X509Cert();
110 std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
111 X509* ret = retsh.get();
114 throw "Creating X509 failed.";
117 X509_NAME* subjectP = X509_NAME_new();
120 throw "malloc failure";
123 for( std::shared_ptr<AVA> a : cert->AVAs ) {
124 signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
126 if( a->name == "CN" ) {
127 c.addRDN( NID_commonName, a->value );
128 } else if( a->name == "EMAIL" ) {
129 c.addRDN( NID_pkcs9_emailAddress, a->value );
130 } else if( a->name == "C" ) {
131 c.addRDN( NID_countryName, a->value );
132 } else if( a->name == "L" ) {
133 c.addRDN( NID_localityName, a->value );
134 } else if( a->name == "ST" ) {
135 c.addRDN( NID_stateOrProvinceName, a->value );
136 } else if( a->name == "O" ) {
137 c.addRDN( NID_organizationName, a->value );
138 } else if( a->name == "OU" ) {
139 c.addRDN( NID_organizationalUnitName, a->value );
141 throw "unknown AVA-type";
145 c.setIssuerNameFrom( prof.ca->ca );
146 c.setPubkeyFrom( req );
148 std::shared_ptr<BIGNUM> ser;
150 std::tie( ser, num ) = nextSerial( prof );
151 c.setSerialNumber( ser.get() );
152 c.setTimes( 0, 60 * 60 * 24 * 10 );
153 signlog << "FINE: Setting extensions." << std::endl;
154 c.setExtensions( prof.ca->ca, cert->SANs, prof );
155 signlog << "FINE: Signed" << std::endl;
156 std::shared_ptr<SignedCertificate> output = c.sign( prof.ca->caKey, cert->md );
157 signlog << "FINE: all went well" << std::endl;
158 signlog << "FINE: crt went to: " << writeBackFile( num, output->certificate, prof.ca->path ) << std::endl;
159 output->log = signlog.str();
163 std::pair<std::shared_ptr<CRL>, std::string> SimpleOpensslSigner::revoke( std::shared_ptr<CAConfig> ca, std::string serial ) {
164 std::string crlpath = ca->path + "/ca.crl";
166 std::shared_ptr<CRL> crl( new CRL( crlpath ) );
167 std::string date = crl->revoke( serial, "" );
169 writeFile( crlpath, crl->toString() );
170 return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );