]> WPIA git - cassiopeia.git/blobdiff - src/io/recordHandler.cpp
upd: remarks from FJW
[cassiopeia.git] / src / io / recordHandler.cpp
index a1d7b47d1e7b0853bd04de66ebc1a807357d26d6..87b11763342f65a4c89b61ae09cb9b49ab4924fa 100644 (file)
@@ -1,6 +1,7 @@
 #include "io/recordHandler.h"
 
 #include <iostream>
+#include <sstream>
 #include <fstream>
 #include <ctime>
 #include <unordered_map>
@@ -24,8 +25,8 @@ extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
 
 class RecordHandlerSession {
 public:
-    uint32_t sessid;
-    uint32_t lastCommandCount;
+    uint32_t sessid = 0;
+    uint32_t lastCommandCount = 0;
 
     std::shared_ptr<TBSCertificate> tbs;
     std::shared_ptr<SignedCertificate> result;
@@ -36,22 +37,18 @@ public:
     DefaultRecordHandler* parent;
     std::shared_ptr<Signer> signer;
 
-    std::shared_ptr<std::ofstream> log;
+    std::unique_ptr<std::ofstream> logFile;
+    //std::stringstream sessionlog;
     std::vector<std::string> serials;
+    logger::logger_set logger;
+
 
     RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
-        sessid( 0 ),
-        lastCommandCount( 0 ),
-        tbs( new TBSCertificate() ) {
+        tbs( std::make_shared<TBSCertificate>() ),
+        logFile(openLogfile( "logs/log_" + timestamp() ) ),
+        logger{ std::cout, *logFile } {
         this->parent = parent;
         this->signer = signer;
-        time_t c_time;
-
-        if( time( &c_time ) == -1 ) {
-            throw "Error while fetching time?";
-        }
-
-        log = openLogfile( std::string( "logs/log_" ) + std::to_string( c_time ) );
 
         ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
         std::shared_ptr<BIO> bio(
@@ -62,21 +59,21 @@ public:
         SSL_set_accept_state( ssl.get() );
         SSL_set_bio( ssl.get(), output.get(), output.get() );
         BIO_set_ssl( bio.get(), ssl.get(), BIO_NOCLOSE );
-        io = std::shared_ptr<OpensslBIOWrapper>( new OpensslBIOWrapper( bio ) );
+        io = std::make_shared<OpensslBIOWrapper>( bio );
     }
 
     void respondCommand( RecordHeader::SignerResult res, std::string payload ) {
         RecordHeader rh;
-        rh.command = ( uint16_t ) res;
+        rh.command = static_cast<uint16_t>( res );
         rh.flags = 0;
         rh.command_count = 0; // TODO i++
         rh.totalLength = payload.size();
-        sendCommand( rh, payload, io, log );
+        sendCommand( rh, payload, io );
     }
 
     void work() {
-        std::vector<char> buffer( 2048, 0 );
-        int res = io->read( buffer.data(), buffer.capacity() );
+        std::vector<char> buffer( 2048 );
+        int res = io->read( buffer.data(), buffer.size() );
 
         if( res <= 0 ) {
             logger::error( "Stream error, resetting SSL" );
@@ -88,13 +85,10 @@ public:
 
         try {
             RecordHeader head;
-            std::string payload = parseCommand( head, content, log );
+            std::string payload = parseCommand( head, content );
             execute( head, payload );
         } catch( const char* msg ) {
-            if( log ) {
-                logger::error( "ERROR: ", msg );
-            }
-
+            logger::error( "ERROR: ", msg );
             parent->reset();
             return;
         }
@@ -105,7 +99,7 @@ public:
             throw "Error, chunking not supported yet";
         }
 
-        switch( ( RecordHeader::SignerCommand ) head.command ) {
+        switch( static_cast<RecordHeader::SignerCommand>( head.command )) {
         case RecordHeader::SignerCommand::SET_CSR:
             tbs->csr_content = data;
             tbs->csr_type = "CSR";
@@ -142,7 +136,7 @@ public:
                 if( pos == std::string::npos ) {
                     // error
                 } else {
-                    std::shared_ptr<SAN> san( new SAN() );
+                    auto san = std::make_shared<SAN>();
                     san->type = data.substr( 0, pos );
                     san->content = data.substr( pos + 1 );
                     tbs->SANs.push_back( san );
@@ -157,7 +151,7 @@ public:
                 if( pos == std::string::npos ) {
                     // error
                 } else {
-                    std::shared_ptr<AVA> ava( new AVA() );
+                    auto ava = std::make_shared<AVA>();
                     ava->name = data.substr( 0, pos );
                     ava->value = data.substr( pos + 1 );
                     tbs->AVAs.push_back( ava );
@@ -195,12 +189,13 @@ public:
 
         case RecordHeader::SignerCommand::REVOKE:
             {
+                logger::note("Revoking: ", data);
                 std::string ca = data;
                 auto reqCA = CAs.at( ca );
-                logger::note( "CA found" );
+                logger::note( "CA found in recordHandler" );
                 std::shared_ptr<CRL> crl;
                 std::string date;
-                std::tie<std::shared_ptr<CRL>, std::string>( crl, date ) = signer->revoke( reqCA, serials );
+                std::tie( crl, date ) = signer->revoke( reqCA, serials );
 
                 respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
             }
@@ -208,6 +203,7 @@ public:
 
         case RecordHeader::SignerCommand::GET_FULL_CRL:
             {
+                logger::note("Requesting full CRL: ", data);
                 auto ca = CAs.at( data );
                 CRL c( ca->path + "/ca.crl" );
                 respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
@@ -237,7 +233,7 @@ void DefaultRecordHandler::reset() {
 void DefaultRecordHandler::handle() {
     if( !currentSession ) {
         logger::note( "New session allocated." );
-        currentSession = std::shared_ptr<RecordHandlerSession>( new RecordHandlerSession( this, signer, ctx, bio ) );
+        currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
     }
 
     currentSession->work();