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();
112 std::shared_ptr<X509> retsh = std::shared_ptr<X509>( X509_new(), X509_free );
113 X509* ret = retsh.get();
116 throw "Creating X509 failed.";
119 X509_NAME* subjectP = X509_NAME_new();
122 throw "malloc failure";
125 for( std::shared_ptr<AVA> a : cert->AVAs ) {
126 signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
128 if( a->name == "CN" ) {
129 c.addRDN( NID_commonName, a->value );
130 } else if( a->name == "EMAIL" ) {
131 c.addRDN( NID_pkcs9_emailAddress, a->value );
132 } else if( a->name == "C" ) {
133 c.addRDN( NID_countryName, a->value );
134 } else if( a->name == "L" ) {
135 c.addRDN( NID_localityName, a->value );
136 } else if( a->name == "ST" ) {
137 c.addRDN( NID_stateOrProvinceName, a->value );
138 } else if( a->name == "O" ) {
139 c.addRDN( NID_organizationName, a->value );
140 } else if( a->name == "OU" ) {
141 c.addRDN( NID_organizationalUnitName, a->value );
143 throw "unknown AVA-type";
147 c.setIssuerNameFrom( ca->ca );
148 c.setPubkeyFrom( req );
150 std::shared_ptr<BIGNUM> ser;
152 std::tie( ser, num ) = nextSerial( prof, ca );
153 c.setSerialNumber( ser.get() );
155 std::time_t from, to;
156 std::time_t now = time( 0 );
157 std::pair<bool, std::time_t> parsed;
159 if( ( parsed = parseDate( cert->wishFrom ) ).first /* is of yyyy-mm-dd */ ) {
160 if( parsed.second > now ) {
161 from = parsed.second;
169 if( from - now > /* 2 Weeks */ 2 * 7 * 24 * 60 * 60 || now - from >= 0 ) {
173 if( ( parsed = parseDate( cert->wishTo ) ).first /*is of yyyy-mm-dd */ ) {
174 if( parsed.second > from ) {
177 to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
179 } else if( ( parsed = parseYearInterval( from, cert->wishTo ) ).first /*is of [0-9]+y */ ) {
181 } else if( ( parsed = parseMonthInterval( from, cert->wishTo ) ).first /*is of [0-9]+m */ ) {
184 to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
187 time_t limit = /*2 Years (max possible) */ 2 * 366 * 24 * 60 * 60;
189 if( to - from > limit || to - from < 0 ) {
193 c.setTimes( from, to );
194 signlog << "FINE: Setting extensions." << std::endl;
195 c.setExtensions( ca->ca, cert->SANs, prof );
196 signlog << "FINE: Signed" << std::endl;
197 std::shared_ptr<SignedCertificate> output = c.sign( ca->caKey, cert->md );
198 signlog << "FINE: all went well" << std::endl;
199 signlog << "FINE: crt went to: " << writeBackFile( num, output->certificate, ca->path ) << std::endl;
200 output->ca_name = ca->name;
201 output->log = signlog.str();
205 std::pair<std::shared_ptr<CRL>, std::string> SimpleOpensslSigner::revoke( std::shared_ptr<CAConfig> ca, std::vector<std::string> serials ) {
206 std::string crlpath = ca->path + "/ca.crl";
208 std::shared_ptr<CRL> crl( new CRL( crlpath ) );
209 std::string date = "";
211 for( std::string serial : serials ) {
212 date = crl->revoke( serial, "" );
216 writeFile( crlpath, crl->toString() );
217 return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );