1 #include "simpleOpensslSigner.h"
4 #include <unordered_map>
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::unordered_map<std::string, Profile> profiles;
19 std::shared_ptr<int> SimpleOpensslSigner::lib_ref = ssl_lib_ref;
21 SimpleOpensslSigner::SimpleOpensslSigner() {
24 SimpleOpensslSigner::~SimpleOpensslSigner() {
27 std::pair<std::shared_ptr<BIGNUM>, std::string> SimpleOpensslSigner::nextSerial( Profile& prof, std::shared_ptr<CAConfig> ca ) {
28 uint16_t profile = prof.id;
29 std::string res = readFile( ca->path + "/serial" );
37 throw "Initing serial failed";
40 if( !BN_hex2bn( &bn, res.c_str() ) ) {
41 throw "Parsing serial failed.";
45 std::shared_ptr<BIGNUM> serial = std::shared_ptr<BIGNUM>( bn, BN_free );
47 std::shared_ptr<unsigned char> data = std::shared_ptr<unsigned char>( ( unsigned char* ) malloc( BN_num_bytes( serial.get() ) + 20 ), free );
48 int len = BN_bn2bin( serial.get(), data.get() );
50 data.get()[len] = 0x0;
51 data.get()[len + 1] = 0x0; // signer id
53 data.get()[len + 2] = profile >> 8;
54 data.get()[len + 3] = profile & 0xFF; // profile id
56 if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
57 throw "Big number math failed while calcing serials.";
60 std::shared_ptr<char> serStr = std::shared_ptr<char>(
61 BN_bn2hex( serial.get() ),
66 writeFile( ca->path + "/serial", serStr.get() );
68 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() ) );
71 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
72 std::stringstream signlog;
74 signlog << "FINE: profile is " << cert->profile << std::endl;
76 Profile& prof = profiles.at( cert->profile );
77 std::shared_ptr<CAConfig> ca = prof.getCA();
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;
87 signlog << "FINE: Signing is wanted from: " << cert->wishFrom << std::endl;
88 signlog << "FINE: Signing is wanted to: " << cert->wishTo << std::endl;
90 std::shared_ptr<X509Req> req;
92 if( cert->csr_type == "SPKAC" ) {
93 req = X509Req::parseSPKAC( cert->csr_content );
94 } else if( cert->csr_type == "CSR" ) {
95 req = X509Req::parseCSR( cert->csr_content );
97 throw "Error, unknown REQ rype " + ( cert->csr_type );
100 int i = req->verify();
103 throw "Signature problems ... ";
104 } else if( i == 0 ) {
105 throw "Signature did not match";
107 signlog << "FINE: Signature ok" << std::endl;
110 // Construct the Certificate
111 X509Cert c = X509Cert();
113 X509_NAME* subjectP = X509_NAME_new();
116 throw "malloc failure";
119 for( std::shared_ptr<AVA> a : cert->AVAs ) {
120 signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
122 if( a->name == "CN" ) {
123 c.addRDN( NID_commonName, a->value );
124 } else if( a->name == "EMAIL" ) {
125 c.addRDN( NID_pkcs9_emailAddress, a->value );
126 } else if( a->name == "C" ) {
127 c.addRDN( NID_countryName, a->value );
128 } else if( a->name == "L" ) {
129 c.addRDN( NID_localityName, a->value );
130 } else if( a->name == "ST" ) {
131 c.addRDN( NID_stateOrProvinceName, a->value );
132 } else if( a->name == "O" ) {
133 c.addRDN( NID_organizationName, a->value );
134 } else if( a->name == "OU" ) {
135 c.addRDN( NID_organizationalUnitName, a->value );
137 throw "unknown AVA-type";
141 c.setIssuerNameFrom( ca->ca );
142 c.setPubkeyFrom( req );
144 std::shared_ptr<BIGNUM> ser;
146 std::tie( ser, num ) = nextSerial( prof, ca );
147 c.setSerialNumber( ser.get() );
149 std::time_t from, to;
150 std::time_t now = time( 0 );
151 std::pair<bool, std::time_t> parsed;
153 if( ( parsed = parseDate( cert->wishFrom ) ).first /* is of yyyy-mm-dd */ ) {
154 if( parsed.second > now ) {
155 from = parsed.second;
163 if( from - now > /* 2 Weeks */ 2 * 7 * 24 * 60 * 60 || now - from >= 0 ) {
167 if( ( parsed = parseDate( cert->wishTo ) ).first /*is of yyyy-mm-dd */ ) {
168 if( parsed.second > from ) {
171 to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
173 } else if( ( parsed = parseYearInterval( from, cert->wishTo ) ).first /*is of [0-9]+y */ ) {
175 } else if( ( parsed = parseMonthInterval( from, cert->wishTo ) ).first /*is of [0-9]+m */ ) {
178 to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
181 time_t limit = prof.maxValidity;
183 if( to - from > limit || to - from < 0 ) {
187 c.setTimes( from, to );
188 signlog << "FINE: Setting extensions." << std::endl;
189 c.setExtensions( ca->ca, cert->SANs, prof );
190 signlog << "FINE: Signed" << std::endl;
191 std::shared_ptr<SignedCertificate> output = c.sign( ca->caKey, cert->md );
192 signlog << "FINE: all went well" << std::endl;
193 std::string fn = writeBackFile( num, output->certificate, ca->path );
196 signlog << "ERROR: failed to get filename for storage of signed certificate." << std::endl;
197 throw "Storage location could not be determined";
200 signlog << "FINE: crt went to: " << fn << std::endl;
201 output->ca_name = ca->name;
202 output->log = signlog.str();
206 std::pair<std::shared_ptr<CRL>, std::string> SimpleOpensslSigner::revoke( std::shared_ptr<CAConfig> ca, std::vector<std::string> serials ) {
207 std::string crlpath = ca->path + "/ca.crl";
209 std::shared_ptr<CRL> crl( new CRL( crlpath ) );
210 std::string date = "";
212 for( std::string serial : serials ) {
213 date = crl->revoke( serial, "" );
217 writeFile( crlpath, crl->toString() );
218 return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );