]> WPIA git - cassiopeia.git/commitdiff
chg: Modify the code to use the new logger
authorBenny Baumann <BenBE@geshi.org>
Mon, 8 Jun 2015 22:18:01 +0000 (00:18 +0200)
committerBenny Baumann <BenBE@geshi.org>
Sun, 19 Jul 2015 16:54:17 +0000 (18:54 +0200)
13 files changed:
src/apps/client.cpp
src/apps/signer.cpp
src/config.cpp
src/crypto/remoteSigner.cpp
src/crypto/remoteSigner.h
src/crypto/simpleOpensslSigner.cpp
src/crypto/sslUtil.cpp
src/io/record.cpp
src/io/recordHandler.cpp
src/io/slipBio.cpp
src/io/slipBio.h
src/util.cpp
src/util.h

index 71ef1790cd70cbb65d56b22c586614ffb195b4e6..d7cda514b5bcaa8830c75156bf284b77089f96d0 100644 (file)
@@ -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<std::string, std::shared_ptr<CAConfig>> CAs;
 
 void checkCRLs( std::shared_ptr<Signer> 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<std::string> serials;
             std::pair<std::shared_ptr<CRL>, 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> 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<TBSCertificate> 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<SignedCertificate> 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 );
         }
 
index baa45c72fef8865e170326b7ab5b3f1a755db8c0..3468eb274d6697c6cf399aefdcc81df182c98bd9 100644 (file)
@@ -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<int> 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;
 }
index ad3c1375963a16602d07611b62bc7ff502e31606..9dfb112a5107836cf43fe2755328fc999794fa6d 100644 (file)
@@ -6,6 +6,8 @@
 
 #include "crypto/sslUtil.h"
 
+#include "log/logger.hpp"
+
 std::string keyDir;
 std::unordered_map<std::string, Profile> profiles;
 std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
@@ -18,7 +20,7 @@ std::shared_ptr<std::unordered_map<std::string, std::string>> 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<std::unordered_map<std::string, std::string>> 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;
     }
 
index b6c7c317d30047cf703c4c1824bb9c8836031ba2..74368467540de8af841e24a16ec6a9b618f571fc 100644 (file)
@@ -1,4 +1,6 @@
 #include "remoteSigner.h"
+
+#include "log/logger.hpp"
 #include "util.h"
 
 #include <iostream>
@@ -17,7 +19,6 @@ void RemoteSigner::send( std::shared_ptr<OpensslBIOWrapper> bio, RecordHeader& h
     head.command_count++;
     head.totalLength = data.size();
     sendCommand( head, data, bio, log );
-
 }
 
 std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
@@ -38,7 +39,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
     } else if( cert->csr_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<SignedCertificate>();
     }
 
@@ -75,7 +76,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
             int length = conn->read( 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<SignedCertificate>();
                 break;
             }
@@ -97,11 +98,11 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
                 break;
 
             default:
-                std::cout << "Invalid Message" << std::endl;
+                logger::error( "Invalid Message" );
                 break;
             }
         } catch( const char* msg ) {
-            std::cout << msg << std::endl;
+            logger::error( msg );
             return std::shared_ptr<SignedCertificate>();
         }
     }
@@ -141,7 +142,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
     }
 
     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 result;
@@ -199,10 +200,10 @@ std::pair<std::shared_ptr<CRL>, 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::shared_ptr<CRL>, 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::shared_ptr<CRL>, std::string>( crl, date );
index 525bbd0123632e3eddbd48a98204a21d52dd770c..79fabae4c1bc156909fe00fbd31a66596c988d89 100644 (file)
@@ -1,4 +1,5 @@
 #pragma once
+
 #include <memory>
 #include <openssl/ssl.h>
 
index 914d26e459fff0b9c8b994d2044c4d2392e31834..55b43ea5757c8276dd9accb34119f14d7187c46a 100644 (file)
@@ -10,6 +10,8 @@
 #include <openssl/engine.h>
 #include <openssl/x509v3.h>
 
+#include "log/logger.hpp"
+
 #include "X509.h"
 #include "util.h"
 #include "sslUtil.h"
@@ -71,25 +73,25 @@ std::pair<std::shared_ptr<BIGNUM>, std::string> SimpleOpensslSigner::nextSerial(
 std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TBSCertificate> 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<CAConfig> 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<X509Req> req;
 
@@ -98,8 +100,8 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
     } else if( cert->csr_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<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
     } else if( i == 0 ) {
         throw "Request contains a Signature that does not match ...";
     } else {
-        signlog << "FINE: Request contains valid self-signature." << std::endl;
+        logger::note( "FINE: Request contains valid self-signature." );
     }
 
     // Construct the Certificate
     X509Cert c = X509Cert();
 
-    signlog << "INFO: Populating RDN ..." << std::endl;
+    logger::note( "INFO: Populating RDN ..." );
+
     for( std::shared_ptr<AVA> 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<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
         } else if( a->name == "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<bool, std::time_t> parsed;
@@ -176,7 +179,7 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
             from = now;
         }
 
-        if( ((from - now) > /* 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<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
 
         time_t limit = prof.maxValidity;
 
-        if( (to - from > 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<char> timebuf;
 
-            timeobj = gmtime(&from);
-            timebuf.resize(128);
-            timebuf.resize(std::strftime(const_cast<char *>(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<char*>( 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<char *>(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<char*>( 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<BIGNUM> 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<SignedCertificate> 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();
index d0df60c9d7a3f5171b093e8bf1421ede90a6bee9..3b93b17be21d4fb3afbaef3bb53c48aeae948de5 100644 (file)
@@ -7,6 +7,7 @@
 #include <iostream>
 
 #include "crypto/CRL.h"
+#include "log/logger.hpp"
 
 std::shared_ptr<int> 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<SSL_CTX> generateSSLContext( bool server ) {
                 dh_param = std::shared_ptr<DH>( PEM_read_DHparams( paramfile.get(), NULL, NULL, NULL ), DH_free );
             } else {
                 dh_param = std::shared_ptr<DH>( 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<BIO> openSerial( const std::string& name ) {
     std::shared_ptr<FILE> 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<BIO>();
     }
 
index 908f495e6d0383f4a580f199e5506d23fa853f8b..c8fda58c98dd942c6e7d41b11efb97cf13c28b26 100644 (file)
@@ -5,8 +5,9 @@
 #include <memory>
 #include <sstream>
 
-#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_ptr<O
     std::string res = toHexAndChecksum( s );
 
     if( log ) {
-        ( *log.get() ) << "FINE: RECORD output: " << res << std::endl;
+        logger::debug( "FINE: RECORD output: ", res );
     }
 
     bio->write( 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<std::ostream> log ) {
     if( log ) {
-        ( *log.get() ) << "FINE: RECORD input: " << input << std::endl;
+        logger::debug( "FINE: RECORD input: ", input );
     }
 
     int32_t dlen = ( input.size() - 2 ) / 2;
index b55317b1d26b8b6a7f2f89159195c4dbc65e51ab..0e63805e98f8e5e57eb2cfd06913421395ec147b 100644 (file)
 #include "db/database.h"
 #include "crypto/remoteSigner.h"
 #include "crypto/sslUtil.h"
-
 #include "crypto/simpleOpensslSigner.h"
 
+#include "log/logger.hpp"
+
 extern std::vector<Profile> profiles;
 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> 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> crl;
             std::string date;
             std::tie<std::shared_ptr<CRL>, 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<RecordHandlerSession>( new RecordHandlerSession( this, signer, ctx, bio ) );
     }
 
index fb3f6c1925cbde5cb499008b1edfcb656fe1883d..4d0653e27b14f221cb25ff9d007cdd28721f6da4 100644 (file)
@@ -4,6 +4,8 @@
 
 #include <iostream>
 
+#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;
     }
 
index 26de30aefa75f6dad29b3fa1d91d80ba98e05dda..27bfadb7950d2e452146a33614ddc59860759667 100644 (file)
@@ -3,7 +3,7 @@
 #include <memory>
 #include <vector>
 
-#include "bios.h"
+#include "io/bios.h"
 
 class SlipBIO : public OpensslBIO {
 private:
index c16ba1c6c8d541164fc5ff7fd540aadb74d1e7fa..a052843561302edcf1869df1250bcf11dca04a80 100644 (file)
@@ -182,20 +182,24 @@ std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& dat
     }
 }
 
-std::shared_ptr<std::ofstream> openLogfile( const std::string name) {
+std::shared_ptr<std::ofstream> 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<std::ofstream>(new std::ofstream( tname ),
-        [](std::ofstream *p){
+
+    auto res = std::shared_ptr<std::ofstream>( 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;
 }
index d59991042c1c00ed1fedf63c43073732581a2997..04448d402d6ccfb6bd87b721d11c9962931e0ecc 100644 (file)
@@ -13,5 +13,4 @@ std::pair<bool, std::time_t> parseDate( const std::string& date );
 std::pair<bool, std::time_t> parseMonthInterval( std::time_t t, const std::string& date );
 std::pair<bool, std::time_t> parseYearInterval( std::time_t t, const std::string& date );
 
-
-std::shared_ptr<std::ofstream> openLogfile( const std::string name);
+std::shared_ptr<std::ofstream> openLogfile( const std::string name );