From d19fbcd86a265cb99dd8597430e8159e9403c743 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Tue, 9 Jun 2015 00:18:01 +0200 Subject: [PATCH] chg: Modify the code to use the new logger --- src/apps/client.cpp | 63 +++++++++--------- src/apps/signer.cpp | 16 +++-- src/config.cpp | 38 ++++++----- src/crypto/remoteSigner.cpp | 26 ++++---- src/crypto/remoteSigner.h | 1 + src/crypto/simpleOpensslSigner.cpp | 100 +++++++++++++++-------------- src/crypto/sslUtil.cpp | 7 +- src/io/record.cpp | 9 +-- src/io/recordHandler.cpp | 25 ++++---- src/io/slipBio.cpp | 8 ++- src/io/slipBio.h | 2 +- src/util.cpp | 20 +++--- src/util.h | 3 +- 13 files changed, 171 insertions(+), 147 deletions(-) diff --git a/src/apps/client.cpp b/src/apps/client.cpp index 71ef179..d7cda51 100644 --- a/src/apps/client.cpp +++ b/src/apps/client.cpp @@ -11,6 +11,7 @@ #include "crypto/simpleOpensslSigner.h" #include "crypto/remoteSigner.h" #include "crypto/sslUtil.h" +#include "log/logger.hpp" #include "util.h" #include "io/bios.h" #include "io/slipBio.h" @@ -28,23 +29,24 @@ extern std::string serialPath; extern std::unordered_map> CAs; void checkCRLs( std::shared_ptr sign ) { - std::cout << "Signing CRLs" << std::endl; + + logger::note( "Signing CRLs" ); for( auto& x : CAs ) { - std::cout << "Checking: " << x.first << std::endl; + logger::notef( "Checking: %s ...", x.first ); if( !x.second->crlNeedsResign() ) { - std::cout << "Skipping Resigning CRL: " + x.second->name << std::endl; + logger::warnf( "Skipping Resigning CRL: %s ...", x.second->name ); continue; } - std::cout << "Resigning CRL: " + x.second->name << std::endl; + logger::notef( "Resigning CRL: %s ...", x.second->name ); try { std::vector serials; std::pair, std::string> rev = sign->revoke( x.second, serials ); } catch( const char* c ) { - std::cout << "Exception: " << c << std::endl; + logger::error( "Exception: ", c ); } } } @@ -65,11 +67,12 @@ int main( int argc, const char* argv[] ) { #endif if( parseConfig( path ) != 0 ) { + logger::fatal( "Error: Could not parse the configuration file." ); return -1; } if( serialPath == "" ) { - std::cout << "Error: no serial device is given" << std::endl; + logger::fatal( "Error: no serial device is given!" ); return -1; } @@ -100,7 +103,7 @@ int main( int argc, const char* argv[] ) { std::shared_ptr job = jp->fetchJob(); if( !job ) { - std::cout << "Nothing to work on" << std::endl; + logger::debug( "Nothing to work on." ); sleep( 5 ); continue; } @@ -110,58 +113,58 @@ int main( int argc, const char* argv[] ) { std::ofstream& log = *( logPtr.get() ); sign->setLog( logPtr ); - log << "TASK ID: " << job->id << std::endl; - log << "TRY: " << job->warning << std::endl; - log << "TARGET: " << job->target << std::endl; - log << "TASK: " << job->task << std::endl << std::endl; + logger::note( "TASK ID: ", job->id ); + logger::note( "TRY: ", job->warning ); + logger::note( "TARGET: ", job->target ); + logger::note( "TASK: ", job->task ); if( job->task == "sign" ) { try { std::shared_ptr cert = jp->fetchTBSCert( job ); cert->wishFrom = job->from; cert->wishTo = job->to; - log << "INFO: message digest: " << cert->md << std::endl; - log << "INFO: profile id: " << cert->profile << std::endl; + logger::note( "INFO: Message Digest: ", cert->md ); + logger::note( "INFO: Profile ID: ", cert->profile ); for( auto& SAN : cert->SANs ) { - log << "INFO: SAN " << SAN->type << ": " << SAN->content; + logger::notef( "INFO: SAN %s: %s", SAN->type, SAN->content ); } for( auto& AVA : cert->AVAs ) { - log << "INFO: AVA " << AVA->name << ": " << AVA->value; + logger::notef( "INFO: AVA %s: %s", AVA->name, AVA->value ); } if( !cert ) { - std::cout << "wasn't able to load CSR" << std::endl; + logger::error( "Unable to load CSR" ); jp->failJob( job ); continue; } - log << "FINE: Found the CSR at '" << cert->csr << "'" << std::endl; + logger::notef( "FINE: Found the CSR at '%s'", cert->csr ); cert->csr_content = readFile( keyDir + "/../" + cert->csr ); - log << "FINE: CSR is " << std::endl << cert->csr_content << std::endl; + logger::note( "FINE: CSR content:\n", cert->csr_content ); std::shared_ptr res = sign->sign( cert ); if( !res ) { - log << "ERROR: The signer failed. There was no certificate." << std::endl; + logger::error( "ERROR: The signer failed. No certificate was returned." ); jp->failJob( job ); continue; } - log << "FINE: CERTIFICATE LOG: " << res->log << std::endl; - log << "FINE: CERTIFICATE:" << std::endl << res->certificate << std::endl; + logger::note( "FINE: CERTIFICATE LOG:\n", res->log ); + logger::note( "FINE: CERTIFICATE:\n", res->certificate ); std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir ); if( fn.empty() ) { - log << "ERROR: Writeback of the certificate failed." << std::endl; + logger::error( "ERROR: Writeback of the certificate failed." ); jp->failJob( job ); continue; } res->crt_name = fn; jp->writeBack( job, res ); //! \FIXME: Check return value - log << "FINE: signing done." << std::endl; + logger::note( "FINE: signing done." ); if( DAEMON ) { jp->finishJob( job ); @@ -169,17 +172,17 @@ int main( int argc, const char* argv[] ) { continue; } catch( const char* c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } catch( std::string& c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } try { jp->failJob( job ); } catch( const char* c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } catch( std::string& c ) { - log << "ERROR: " << c << std::endl; + logger::error( "ERROR: ", c ); } } else if( job->task == "revoke" ) { try { @@ -194,12 +197,12 @@ int main( int argc, const char* argv[] ) { jp->writeBackRevocation( job, timeToString( time ) ); jp->finishJob( job ); } catch( const char* c ) { - std::cout << "Exception: " << c << std::endl; + logger::error( "Exception: ", c ); } catch( const std::string& c ) { - std::cout << "Exception: " << c << std::endl; + logger::error( "Exception: ", c ); } } else { - log << "Unknown job type" << job->task << std::endl; + logger::errorf( "Unknown job type (\"%s\")", job->task ); jp->failJob( job ); } diff --git a/src/apps/signer.cpp b/src/apps/signer.cpp index baa45c7..3468eb2 100644 --- a/src/apps/signer.cpp +++ b/src/apps/signer.cpp @@ -11,6 +11,7 @@ #include "io/bios.h" #include "io/slipBio.h" #include "io/recordHandler.h" +#include "log/logger.hpp" #include "util.h" #include "config.h" @@ -35,13 +36,14 @@ int main( int argc, const char* argv[] ) try { #endif if( parseConfig( path ) != 0 ) { + logger::fatal("Could not parse configuration file."); return -1; } std::shared_ptr ssl_lib = ssl_lib_ref; if( serialPath == "" ) { - std::cout << "Error: no serial device is given" << std::endl; + logger::fatal( "Error: No device for the serial connection was given." ); return -1; } @@ -57,18 +59,18 @@ int main( int argc, const char* argv[] ) try { //} catch( const std::exception &ch ) { //std::cout << "Real exception: " << typeid(ch).name() << ", " << ch.what() << std::endl; } catch( const std::string& ch ) { - std::cout << "Exception: " << ch << std::endl; + logger::error( "Exception: ", ch ); } catch( char const* ch ) { - std::cout << "Exception: " << ch << std::endl; + logger::error( "Exception: ", ch ); } } return -1; -} catch(std::exception& e) { - std::cerr << "Fatal Error: " << e.what() << "!\n"; +} catch( std::exception& e ) { + logger::fatalf( "Fatal Error: %s!\n", e.what() ); return -1; -} catch(...) { - std::cerr << "Fatal Error: Unknown Exception!\n"; +} catch( ... ) { + logger::fatal( "Fatal Error: Unknown Exception!\n" ); return -1; } diff --git a/src/config.cpp b/src/config.cpp index ad3c137..9dfb112 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -6,6 +6,8 @@ #include "crypto/sslUtil.h" +#include "log/logger.hpp" + std::string keyDir; std::unordered_map profiles; std::unordered_map> CAs; @@ -18,7 +20,7 @@ std::shared_ptr> parseConf( std::st config.open( path ); if( !config.is_open() ) { - std::cout << "Where is " << path << "?" << std::endl; + logger::notef( "Where is \"%s\"?", path ); throw "Config missing"; } @@ -32,7 +34,7 @@ std::shared_ptr> parseConf( std::st int splitter = line1.find( "=" ); if( splitter == -1 ) { - std::cerr << "Ignoring malformed config line: " << line1 << std::endl; + logger::warn( "Ignoring malformed config line: ", line1 ); continue; } @@ -54,7 +56,7 @@ int parseProfiles() { dp = opendir( "profiles" ); if( dp == NULL ) { - std::cerr << "Profiles not found " << std::endl; + logger::error( "Profiles directory not found" ); return -1; } @@ -68,14 +70,14 @@ int parseProfiles() { int splitter = profileName.find( "-" ); if( splitter == -1 ) { - std::cerr << "Ignoring malformed profile: " << profileName << std::endl; + logger::warn( "Ignoring malformed profile: ", profileName ); continue; } std::string id = profileName.substr( 0, splitter ); if( profileName.substr( profileName.size() - 4 ) != ".cfg" ) { - std::cerr << "Ignoring malformed profile: " << profileName << std::endl; + logger::warn( "Ignoring malformed profile: ", profileName ); continue; } @@ -91,12 +93,14 @@ int parseProfiles() { std::string cas = map->at( "ca" ); - DIR *dir; - struct dirent *ent; - if ((dir = opendir ("ca")) != NULL) { - while ((ent = readdir (dir)) != NULL) { - std::string caName = std::string(ent->d_name); - if( caName.find( cas ) != 0 ){ + DIR* dir; + struct dirent* ent; + + if( ( dir = opendir( "ca" ) ) != NULL ) { + while( ( ent = readdir( dir ) ) != NULL ) { + std::string caName = std::string( ent->d_name ); + + if( caName.find( cas ) != 0 ) { continue; } @@ -106,21 +110,21 @@ int parseProfiles() { } prof.ca.push_back( CAs.at( caName ) ); - std::cout << "Adding CA: " << caName << std::endl; + logger::note( "Adding CA: ", caName ); } - closedir (dir); + + closedir( dir ); } else { throw "Directory with CAConfigs not found"; } profiles.emplace( profileName, prof ); - std::cout << "Profile: " << profileName << " up and running." << std::endl; + logger::notef( "Profile: \"%s\" up and running.", profileName ); } ( void ) closedir( dp ); - - std::cout << profiles.size() << " profiles loaded." << std::endl; + logger::notef( "%s profiles loaded.", profiles.size() ); return 0; } @@ -137,7 +141,7 @@ int parseConfig( std::string path ) { serialPath = masterConf->at( "serialPath" ); if( keyDir == "" ) { - std::cerr << "Missing config property key.directory" << std::endl; + logger::error( "Missing config property key.directory" ); return -1; } diff --git a/src/crypto/remoteSigner.cpp b/src/crypto/remoteSigner.cpp index b6c7c31..7436846 100644 --- a/src/crypto/remoteSigner.cpp +++ b/src/crypto/remoteSigner.cpp @@ -1,4 +1,6 @@ #include "remoteSigner.h" + +#include "log/logger.hpp" #include "util.h" #include @@ -17,7 +19,6 @@ void RemoteSigner::send( std::shared_ptr bio, RecordHeader& h head.command_count++; head.totalLength = data.size(); sendCommand( head, data, bio, log ); - } std::shared_ptr RemoteSigner::sign( std::shared_ptr cert ) { @@ -38,7 +39,7 @@ std::shared_ptr RemoteSigner::sign( std::shared_ptrcsr_type == "SPKAC" ) { send( conn, head, RecordHeader::SignerCommand::SET_SPKAC, cert->csr_content ); } else { - std::cout << "Unknown csr_type: " << cert->csr_type; + logger::error( "Unknown csr_type: ", cert->csr_type ); return std::shared_ptr(); } @@ -75,7 +76,7 @@ std::shared_ptr RemoteSigner::sign( std::shared_ptrread( buffer.data(), buffer.size() ); if( length <= 0 ) { - std::cout << "Error, no response data" << std::endl; + logger::error( "Error, no response data" ); result = std::shared_ptr(); break; } @@ -97,11 +98,11 @@ std::shared_ptr RemoteSigner::sign( std::shared_ptr(); } } @@ -141,7 +142,7 @@ std::shared_ptr RemoteSigner::sign( std::shared_ptr, std::string> RemoteSigner::revoke( std::shared_p bool ok = crl->verify( ca ); if( ok ) { - ( *log ) << "CRL verificated successfully" << std::endl; + logger::note( "CRL verificated successfully" ); writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() ); } else { - ( *log ) << "CRL is broken" << std::endl; + logger::warn( "CRL is broken, trying to recover" ); send( conn, head, RecordHeader::SignerCommand::GET_FULL_CRL, ca->name ); length = conn->read( buffer.data(), buffer.size() ); @@ -221,17 +222,16 @@ std::pair, std::string> RemoteSigner::revoke( std::shared_p if( crl->verify( ca ) ) { writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() ); - ( *log ) << "CRL is now valid" << std::endl; + logger::note( "CRL is now valid again" ); } else { - ( *log ) << "CRL is still broken... Please, help me" << std::endl; + logger::warn( "CRL is still broken... Please, help me" ); } - } - ( *log ) << "CRL: " << std::endl << crl->toString() << std::endl; + logger::debug( "CRL:\n", crl->toString() ); if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice - std::cout << "SSL shutdown failed" << std::endl; + logger::warn( "SSL shutdown failed" ); } return std::pair, std::string>( crl, date ); diff --git a/src/crypto/remoteSigner.h b/src/crypto/remoteSigner.h index 525bbd0..79fabae 100644 --- a/src/crypto/remoteSigner.h +++ b/src/crypto/remoteSigner.h @@ -1,4 +1,5 @@ #pragma once + #include #include diff --git a/src/crypto/simpleOpensslSigner.cpp b/src/crypto/simpleOpensslSigner.cpp index 914d26e..55b43ea 100644 --- a/src/crypto/simpleOpensslSigner.cpp +++ b/src/crypto/simpleOpensslSigner.cpp @@ -10,6 +10,8 @@ #include #include +#include "log/logger.hpp" + #include "X509.h" #include "util.h" #include "sslUtil.h" @@ -71,25 +73,25 @@ std::pair, std::string> SimpleOpensslSigner::nextSerial( std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr cert ) { std::stringstream signlog; - signlog << "FINE: profile is " << cert->profile << std::endl; + logger::note( "FINE: Profile name is: ", cert->profile ); Profile& prof = profiles.at( cert->profile ); - signlog << "FINE: Profile id is: " << prof.id << std::endl; + logger::note( "FINE: Profile ID is: ", prof.id ); std::shared_ptr ca = prof.getCA(); if( !ca ) { - signlog << "ERROR: Signing CA specified in profile could not be loaded." << std::endl; + logger::error( "ERROR: Signing CA specified in profile could not be loaded." ); throw "CA-key not found"; } - signlog << "FINE: Key for Signing CA is correctly loaded." << std::endl; + logger::note( "FINE: Key for Signing CA is correctly loaded." ); - signlog << "INFO: Baseline Key Usage is: " << prof.ku << std::endl; - signlog << "INFO: Extended Key Usage is: " << prof.eku << std::endl; + logger::note( "INFO: Baseline Key Usage is: ", prof.ku ); + logger::note( "INFO: Extended Key Usage is: ", prof.eku ); - signlog << "FINE: Signing is wanted by: " << cert->wishFrom << std::endl; - signlog << "FINE: Signing is wanted for: " << cert->wishTo << std::endl; + logger::note( "FINE: Signing is wanted by: ", cert->wishFrom ); + logger::note( "FINE: Signing is wanted for: ", cert->wishTo ); std::shared_ptr req; @@ -98,8 +100,8 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrcsr_type == "CSR" ) { req = X509Req::parseCSR( cert->csr_content ); } else { - signlog << "ERROR: Unknown type of certification in request: " << cert->csr_type << std::endl; - throw "Error, unknown REQ rype " + ( cert->csr_type ); + 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 } int i = req->verify(); @@ -109,15 +111,16 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr a : cert->AVAs ) { - signlog << "INFO: Trying to add RDN: " << a->name << ": " << a->value << std::endl; + logger::notef( "INFO: Trying to add RDN: %s: %s", a->name, a->value ); if( a->name == "CN" ) { c.addRDN( NID_commonName, a->value ); @@ -134,34 +137,34 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptrname == "OU" ) { c.addRDN( NID_organizationalUnitName, a->value ); } else { - signlog << "ERROR: Trying to add illegal RDN/AVA type: " << a->name << std::endl; + logger::error( "ERROR: Trying to add illegal RDN/AVA type: ", a->name ); throw "Unhandled/Illegal AVA type"; } } - signlog << "INFO: Populating Issuer ..." << std::endl; + logger::note( "INFO: Populating Issuer ..." ); c.setIssuerNameFrom( ca->ca ); - signlog << "INFO: Validating Public key for use in certificate" << std::endl; - signlog << "INFO: - Checking generic key parameters" << std::endl; - signlog << "FINE: ->Public Key parameters are okay" << std::endl; + logger::note( "INFO: Validating Public key for use in certificate" ); + logger::note( "INFO: - Checking generic key parameters" ); + logger::note( "FINE: ->Public Key parameters are okay" ); - signlog << "INFO: - Checking blacklists" << std::endl; - signlog << "FINE: ->Does not appear on any blacklist" << std::endl; + logger::note( "INFO: - Checking blacklists" ); + logger::note( "FINE: ->Does not appear on any blacklist" ); - signlog << "INFO: - Checking trivial factorization" << std::endl; - signlog << "FINE: ->Trivial factorization not possible" << std::endl; + logger::note( "INFO: - Checking trivial factorization" ); + logger::note( "FINE: ->Trivial factorization not possible" ); - signlog << "INFO: - Checking astrological signs" << std::endl; - signlog << "FINE: ->The stars look good for this one" << std::endl; - signlog << "FINE: Public key is fine for use in certificate" << std::endl; + logger::note( "INFO: - Checking astrological signs" ); + logger::note( "FINE: ->The stars look good for this one" ); + logger::note( "FINE: Public key is fine for use in certificate" ); - signlog << "INFO: Copying Public Key from Request ..." << std::endl; + logger::note( "INFO: Copying Public Key from Request ..." ); c.setPubkeyFrom( req ); - signlog << "FINE: Public Key successfully copied from Request." << std::endl; + logger::note( "FINE: Public Key successfully copied from Request." ); { - signlog << "INFO: Determining Validity Period ..." << std::endl; + logger::note( "INFO: Determining Validity Period ..." ); std::time_t from, to; std::time_t now = time( 0 ); std::pair parsed; @@ -176,7 +179,7 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr /* 2 Weeks */ (2 * 7 * 24 * 60 * 60)) || ((now - from) >= 0) ) { + if( ( ( from - now ) > /* 2 Weeks */ ( 2 * 7 * 24 * 60 * 60 ) ) || ( ( now - from ) >= 0 ) ) { from = now; } @@ -196,51 +199,52 @@ std::shared_ptr SimpleOpensslSigner::sign( std::shared_ptr limit) || (to - from < 0) ) { + if( ( to - from > limit ) || ( to - from < 0 ) ) { to = from + limit; } c.setTimes( from, to ); - signlog << "FINE: Setting validity period successful:" << std::endl; + logger::note( "FINE: Setting validity period successful:" ); { struct tm* timeobj; std::vector timebuf; - timeobj = gmtime(&from); - timebuf.resize(128); - timebuf.resize(std::strftime(const_cast(timebuf.data()), timebuf.size(), "%F %T %Z", timeobj)); - signlog << "FINE: - Valid not before: " << std::string(timebuf.cbegin(), timebuf.cend()) << std::endl; + timeobj = gmtime( &from ); + timebuf.resize( 128 ); + 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)); - signlog << "FINE: - Valid not after: " << std::string(timebuf.cbegin(), timebuf.cend()) << std::endl; + timeobj = gmtime( &to ); + timebuf.resize( 128 ); + 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() ) ); } } - signlog << "INFO: Setting extensions:" << std::endl; + logger::note( "INFO: Setting extensions:" ); c.setExtensions( ca->ca, cert->SANs, prof ); - signlog << "FINE: Setting extensions successful." << std::endl; + logger::note( "FINE: Setting extensions successful." ); - signlog << "INFO: Generating next Serial Number ..." << std::endl; + logger::note( "INFO: Generating next Serial Number ..." ); std::shared_ptr ser; std::string num; std::tie( ser, num ) = nextSerial( prof, ca ); c.setSerialNumber( ser.get() ); - signlog << "FINE: Certificate Serial Number set to:" << num << std::endl; + logger::note( "FINE: Certificate Serial Number set to: ", num ); { - signlog << "INFO: Trying to sign Certificate:" << std::endl; + logger::note( "INFO: Trying to sign Certificate:" ); std::shared_ptr output = c.sign( ca->caKey, cert->md ); - signlog << "INFO: Writing certificate to local file." << std::endl; + logger::note( "INFO: Writing certificate to local file." ); std::string fn = writeBackFile( num, output->certificate, ca->path ); if( fn.empty() ) { - signlog << "ERROR: failed to get filename for storage of signed certificate." << std::endl; + logger::error( "ERROR: failed to get filename for storage of signed certificate." ); throw "Storage location could not be determined"; } - signlog << "FINE: Certificate signed successfully." << std::endl; - signlog << "FINE: - Certificate written to: " << fn << std::endl; + + logger::note( "FINE: Certificate signed successfully." ); + logger::note( "FINE: - Certificate written to: ", fn ); output->ca_name = ca->name; output->log = signlog.str(); diff --git a/src/crypto/sslUtil.cpp b/src/crypto/sslUtil.cpp index d0df60c..3b93b17 100644 --- a/src/crypto/sslUtil.cpp +++ b/src/crypto/sslUtil.cpp @@ -7,6 +7,7 @@ #include #include "crypto/CRL.h" +#include "log/logger.hpp" std::shared_ptr ssl_lib_ref( new int( SSL_library_init() ), @@ -76,7 +77,7 @@ static int verify_callback( int preverify_ok, X509_STORE_CTX* ctx ) { //X509_print_ex(o, cert, XN_FLAG_COMPAT, X509_FLAG_COMPAT); //BIO_free(o); - std::cout << "Verification failed: " << preverify_ok << " because " << X509_STORE_CTX_get_error( ctx ) << std::endl; + logger::errorf( "Verification failed: %s because %s", preverify_ok, X509_STORE_CTX_get_error( ctx ) ); } return preverify_ok; @@ -117,7 +118,7 @@ std::shared_ptr generateSSLContext( bool server ) { dh_param = std::shared_ptr( PEM_read_DHparams( paramfile.get(), NULL, NULL, NULL ), DH_free ); } else { dh_param = std::shared_ptr( DH_new(), DH_free ); - std::cout << "Generating DH params" << std::endl; + logger::note( "Generating DH params" ); BN_GENCB cb; cb.ver = 2; cb.arg = 0; @@ -169,7 +170,7 @@ std::shared_ptr openSerial( const std::string& name ) { std::shared_ptr f( fopen( name.c_str(), "r+" ), fclose ); if( !f ) { - std::cout << "Opening serial device failed" << std::endl; + logger::error( "Opening serial device failed." ); return std::shared_ptr(); } diff --git a/src/io/record.cpp b/src/io/record.cpp index 908f495..c8fda58 100644 --- a/src/io/record.cpp +++ b/src/io/record.cpp @@ -5,8 +5,9 @@ #include #include -#include "bios.h" -#include "opensslBIO.h" +#include "io/bios.h" +#include "io/opensslBIO.h" +#include "log/logger.hpp" std::string toHexAndChecksum( const std::string& src ) { char checksum = 0; @@ -32,7 +33,7 @@ void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptrwrite( res.data(), res.size() ); @@ -54,7 +55,7 @@ int32_t fromHexDigit( char c ) { std::string parseCommand( RecordHeader& head, const std::string& input, std::shared_ptr log ) { if( log ) { - ( *log.get() ) << "FINE: RECORD input: " << input << std::endl; + logger::debug( "FINE: RECORD input: ", input ); } int32_t dlen = ( input.size() - 2 ) / 2; diff --git a/src/io/recordHandler.cpp b/src/io/recordHandler.cpp index b55317b..0e63805 100644 --- a/src/io/recordHandler.cpp +++ b/src/io/recordHandler.cpp @@ -15,9 +15,10 @@ #include "db/database.h" #include "crypto/remoteSigner.h" #include "crypto/sslUtil.h" - #include "crypto/simpleOpensslSigner.h" +#include "log/logger.hpp" + extern std::vector profiles; extern std::unordered_map> CAs; @@ -78,7 +79,7 @@ public: int res = io->read( buffer.data(), buffer.capacity() ); if( res <= 0 ) { - ( *log ) << "Stream error, resetting SSL" << std::endl; + logger::error( "Stream error, resetting SSL" ); parent->reset(); return; } @@ -91,7 +92,7 @@ public: execute( head, payload ); } catch( const char* msg ) { if( log ) { - ( *log ) << "ERROR: " << msg << std::endl; + logger::error( "ERROR: ", msg ); } parent->reset(); @@ -108,13 +109,13 @@ public: case RecordHeader::SignerCommand::SET_CSR: tbs->csr_content = data; tbs->csr_type = "CSR"; - ( *log ) << "INFO: CSR read: " << tbs->csr_content << std::endl; + logger::note( "INFO: CSR read:\n", tbs->csr_content ); break; case RecordHeader::SignerCommand::SET_SPKAC: tbs->csr_content = data; tbs->csr_type = "SPKAC"; - ( *log ) << "INFO: SPKAC read: " << tbs->csr_content << std::endl; + logger::note( "INFO: SPKAC read:\n", tbs->csr_content ); break; case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE: @@ -167,8 +168,8 @@ public: case RecordHeader::SignerCommand::SIGN: result = signer->sign( tbs ); - ( *log ) << "INFO: signlog: " << result->log << std::endl; - ( *log ) << "INFO: res: " << result->certificate << std::endl; + logger::note( "INFO: signlog:\n", result->log ); + logger::note( "INFO: res:\n", result->certificate ); respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log ); break; @@ -179,8 +180,9 @@ public: } if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { - ( *log ) << "ERROR: SSL close failed" << std::endl; + logger::warn( "ERROR: SSL shutdown failed." ); } + parent->reset(); // Connection ended break; @@ -192,7 +194,7 @@ public: case RecordHeader::SignerCommand::REVOKE: { std::string ca = data; auto reqCA = CAs.at( ca ); - ( *log ) << "CA found" << std::endl; + logger::note( "CA found" ); std::shared_ptr crl; std::string date; std::tie, std::string>( crl, date ) = signer->revoke( reqCA, serials ); @@ -208,8 +210,9 @@ public: respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() ); if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { - ( *log ) << "ERROR: SSL close failed" << std::endl; + logger::error( "ERROR: SSL shutdown failed." ); } + parent->reset(); // Connection ended break; } @@ -230,7 +233,7 @@ void DefaultRecordHandler::reset() { void DefaultRecordHandler::handle() { if( !currentSession ) { - std::cout << "session allocated" << std::endl; + logger::note( "New session allocated." ); currentSession = std::shared_ptr( new RecordHandlerSession( this, signer, ctx, bio ) ); } diff --git a/src/io/slipBio.cpp b/src/io/slipBio.cpp index fb3f6c1..4d0653e 100644 --- a/src/io/slipBio.cpp +++ b/src/io/slipBio.cpp @@ -4,6 +4,8 @@ #include +#include "log/logger.hpp" + #define BUFFER_SIZE 8192 #define SLIP_ESCAPE_CHAR ( (char) 0xDB) @@ -51,7 +53,7 @@ SlipBIO::~SlipBIO() {} int SlipBIO::write( const char* buf, int num ) { #ifdef SLIP_IO_DEBUG - std::cout << "Out: " << toHex( buf, num ) << std::endl; + logger::debug( "Out: " << toHex( buf, num ) ); #endif int badOnes = 0; @@ -145,7 +147,7 @@ int SlipBIO::read( char* buf, int size ) { } #ifdef SLIP_IO_DEBUG - std::cout << "in: " << toHex( buf, len ) << std::endl; + logger::debug( "in: " << toHex( buf, len ) ); #endif return len; @@ -162,7 +164,7 @@ long SlipBIO::ctrl( int cmod, long arg1, void* arg2 ) { decodePos = 0; decodeTarget = 0; rawPos = 0; - std::cout << "resetting SLIP" << std::endl; + logger::note( "Resetting SLIP layer" ); return 0; } diff --git a/src/io/slipBio.h b/src/io/slipBio.h index 26de30a..27bfadb 100644 --- a/src/io/slipBio.h +++ b/src/io/slipBio.h @@ -3,7 +3,7 @@ #include #include -#include "bios.h" +#include "io/bios.h" class SlipBIO : public OpensslBIO { private: diff --git a/src/util.cpp b/src/util.cpp index c16ba1c..a052843 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -182,20 +182,24 @@ std::pair parseYearInterval( std::time_t t, const std::string& dat } } -std::shared_ptr openLogfile( const std::string name) { +std::shared_ptr openLogfile( const std::string name ) { struct stat buffer; std::string tname = name; int ctr = 2; - while(stat (tname.c_str(), &buffer) == 0) { - tname = name + "_" + std::to_string(ctr++); + + while( stat( tname.c_str(), &buffer ) == 0 ) { + tname = name + "_" + std::to_string( ctr++ ); } - auto res = std::shared_ptr(new std::ofstream( tname ), - [](std::ofstream *p){ + + auto res = std::shared_ptr( new std::ofstream( tname ), + []( std::ofstream * p ) { p->close(); delete p; - }); - if(! res->good() ){ - throw std::string("Failed to open file for logging: ") + name; + } ); + + if( ! res->good() ) { + throw std::string( "Failed to open file for logging: " ) + name; } + return res; } diff --git a/src/util.h b/src/util.h index d599910..04448d4 100644 --- a/src/util.h +++ b/src/util.h @@ -13,5 +13,4 @@ std::pair parseDate( const std::string& date ); std::pair parseMonthInterval( std::time_t t, const std::string& date ); std::pair parseYearInterval( std::time_t t, const std::string& date ); - -std::shared_ptr openLogfile( const std::string name); +std::shared_ptr openLogfile( const std::string name ); -- 2.39.2