X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fcrypto%2FsimpleOpensslSigner.cpp;h=a6d037188fbbf915336db93e305c9a0d71dc165b;hb=HEAD;hp=8576fad1feb088316484343524e53a23f25ec6e2;hpb=d92d96bebe9ad5b4f17e4aef580f7dfd8d34d58b;p=cassiopeia.git diff --git a/src/crypto/simpleOpensslSigner.cpp b/src/crypto/simpleOpensslSigner.cpp index 8576fad..a6d0371 100644 --- a/src/crypto/simpleOpensslSigner.cpp +++ b/src/crypto/simpleOpensslSigner.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -17,6 +18,7 @@ #include "sslUtil.h" extern std::unordered_map profiles; +extern std::unordered_map> CAs; std::shared_ptr SimpleOpensslSigner::lib_ref = ssl_lib_ref; @@ -30,23 +32,23 @@ std::pair, std::string> SimpleOpensslSigner::nextSerial( uint16_t profile = prof.id; std::string res = readFile( ca->path + "/serial" ); - BIGNUM* bn = 0; + BIGNUM *bn = 0; if( res == "" ) { bn = BN_new(); - if( !bn ) { - throw "Initing serial failed"; + if( !bn || !BN_hex2bn( &bn, "1" ) ) { + throw std::runtime_error( "Initing serial failed" ); } } else { if( !BN_hex2bn( &bn, res.c_str() ) ) { - throw "Parsing serial failed."; + throw std::runtime_error( "Parsing serial failed." ); } } std::shared_ptr serial = std::shared_ptr( bn, BN_free ); - std::shared_ptr data = std::shared_ptr( ( unsigned char* ) malloc( BN_num_bytes( serial.get() ) + 20 ), free ); + std::shared_ptr data = std::shared_ptr( ( unsigned char * ) malloc( BN_num_bytes( serial.get() ) + 20 ), free ); int len = BN_bn2bin( serial.get(), data.get() ); data.get()[len] = 0x0; @@ -56,40 +58,59 @@ std::pair, std::string> SimpleOpensslSigner::nextSerial( data.get()[len + 3] = profile & 0xFF; // profile id if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) { - throw "Big number math failed while fetching random data for serial number."; + throw std::runtime_error( "Big number math failed while fetching random data for serial number." ); } - std::shared_ptr serStr = std::shared_ptr( - BN_bn2hex( serial.get() ), - []( char* ref ) { - OPENSSL_free( ref ); - } ); + auto freeMem = []( char *ref ) { + OPENSSL_free( ref ); + }; + std::shared_ptr serStr = std::shared_ptr( BN_bn2hex( serial.get() ), freeMem ); writeFile( ca->path + "/serial", serStr.get() ); - return std::pair, std::string>( std::shared_ptr( BN_bin2bn( data.get(), len + 4 + 16 , 0 ), BN_free ), std::string( serStr.get() ) ); + return std::pair, std::string>( std::shared_ptr( BN_bin2bn( data.get(), len + 4 + 16, 0 ), BN_free ), std::string( serStr.get() ) ); } std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr cert ) { std::stringstream signlog; - logger::logger_set log_set_sign({logger::log_target(signlog, logger::level::debug)}, logger::auto_register::on); + logger::logger_set log_set_sign( {logger::log_target( signlog, logger::level::debug )}, logger::auto_register::on ); - logger::note( "FINE: Profile name is: ", cert->profile ); + std::shared_ptr ca; + Profile *prof; - Profile& prof = profiles.at( cert->profile ); - logger::note( "FINE: Profile ID is: ", prof.id ); + if( cert->ocspCA != "" ) { + auto caIterator = CAs.find( cert->ocspCA ); - std::shared_ptr ca = prof.getCA(); + if( caIterator == CAs.end() ) { + logger::error( "ERROR: Signing CA specified in request for an OCSP cert could not be loaded." ); + throw std::runtime_error( "CA-key for OCSP cert not found" ); + } + + ca = caIterator->second; + logger::note( "Trying to fetch OCSP-profile" ); + prof = &profiles.at( "0100-ocsp" ); + logger::note( "Done with it" ); + } else { + logger::note( "FINE: Profile name is: ", cert->profile ); + + prof = &profiles.at( cert->profile ); + logger::note( "FINE: Profile ID is: ", prof->id ); + ca = prof->getCA(); + } if( !ca ) { logger::error( "ERROR: Signing CA specified in profile could not be loaded." ); - throw "CA-key not found"; + throw std::runtime_error( "CA-key not found" ); + } + + if( !ca->caKey ) { + throw std::runtime_error( "Cannot sign certificate with CA " + ca->name + " because it has no private key." ); } logger::note( "FINE: Key for Signing CA is correctly loaded." ); - logger::note( "INFO: Baseline Key Usage is: ", prof.ku ); - logger::note( "INFO: Extended Key Usage is: ", prof.eku ); + logger::note( "INFO: Baseline Key Usage is: ", prof->ku ); + logger::note( "INFO: Extended Key Usage is: ", prof->eku ); logger::note( "FINE: Signing is wanted by: ", cert->wishFrom ); logger::note( "FINE: Signing is wanted for: ", cert->wishTo ); @@ -102,15 +123,15 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrcsr_content ); } else { logger::errorf( "ERROR: Unknown type (\"%s\") of certification in request.", cert->csr_type ); - throw "Error, unknown REQ rype " + ( cert->csr_type ); //! \fixme: Pointer instead of string, please use proper exception classes + throw std::runtime_error( "Error, unknown REQ rype " + cert->csr_type ); //! \fixme: Pointer instead of string, please use proper exception classe)s } int i = req->verify(); if( i < 0 ) { - throw "Request contains a Signature with problems ... "; + throw std::runtime_error( "Request contains a Signature with problems ... " ); } else if( i == 0 ) { - throw "Request contains a Signature that does not match ..."; + throw std::runtime_error( "Request contains a Signature that does not match ..." ); } else { logger::note( "FINE: Request contains valid self-signature." ); } @@ -123,6 +144,11 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr a : cert->AVAs ) { logger::notef( "INFO: Trying to add RDN: %s: %s", a->name, a->value ); + if( a->value.empty() ) { + logger::notef( "INFO: Removing empty RDN: %s", a->name ); + continue; + } + if( a->name == "CN" ) { c.addRDN( NID_commonName, a->value ); } else if( a->name == "EMAIL" ) { @@ -139,7 +165,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrvalue ); } else { logger::error( "ERROR: Trying to add illegal RDN/AVA type: ", a->name ); - throw "Unhandled/Illegal AVA type"; + throw std::runtime_error( "Unhandled/Illegal AVA type" ); } } @@ -198,7 +224,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrmaxValidity; if( ( to - from > limit ) || ( to - from < 0 ) ) { to = from + limit; @@ -207,29 +233,29 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr timebuf; timeobj = gmtime( &from ); timebuf.resize( 128 ); - timebuf.resize( std::strftime( const_cast( timebuf.data() ), timebuf.size(), "%F %T %Z", timeobj ) ); + timebuf.resize( std::strftime( const_cast( timebuf.data() ), timebuf.size(), "%F %T %Z", timeobj ) ); logger::note( "FINE: - Valid not before: ", std::string( timebuf.cbegin(), timebuf.cend() ) ); timeobj = gmtime( &to ); timebuf.resize( 128 ); - timebuf.resize( std::strftime( const_cast( timebuf.data() ), timebuf.size(), "%F %T %Z", timeobj ) ); + timebuf.resize( std::strftime( const_cast( timebuf.data() ), timebuf.size(), "%F %T %Z", timeobj ) ); logger::note( "FINE: - Valid not after: ", std::string( timebuf.cbegin(), timebuf.cend() ) ); } } logger::note( "INFO: Setting extensions:" ); - c.setExtensions( ca->ca, cert->SANs, prof ); + c.setExtensions( ca->ca, cert->SANs, *prof, ca->crlURL, ca->crtURL ); logger::note( "FINE: Setting extensions successful." ); logger::note( "INFO: Generating next Serial Number ..." ); std::shared_ptr ser; std::string num; - std::tie( ser, num ) = nextSerial( prof, ca ); + std::tie( ser, num ) = nextSerial( *prof, ca ); c.setSerialNumber( ser.get() ); logger::note( "FINE: Certificate Serial Number set to: ", num ); @@ -241,7 +267,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr, std::string> SimpleOpensslSigner::revoke( std::shared_ptr ca, std::vector serials ) { + logger::note( "revoking" ); std::string crlpath = ca->path + "/ca.crl"; - std::shared_ptr crl( new CRL( crlpath ) ); + auto crl = std::make_shared( crlpath ); std::string date = ""; + logger::note( "adding serials" ); + for( std::string serial : serials ) { date = crl->revoke( serial, "" ); } + logger::note( "signing CRL" ); crl->sign( ca ); writeFile( crlpath, crl->toString() ); + logger::note( "wrote CRL" ); return std::pair, std::string>( crl, date ); }