X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fio%2FrecordHandler.cpp;h=87b11763342f65a4c89b61ae09cb9b49ab4924fa;hb=709700dfbbeb5bf8aee1f5a1966f0192d783ae03;hp=a1d7b47d1e7b0853bd04de66ebc1a807357d26d6;hpb=3c27aae8d1bfbf441c25b273d328d0859022ed60;p=cassiopeia.git diff --git a/src/io/recordHandler.cpp b/src/io/recordHandler.cpp index a1d7b47..87b1176 100644 --- a/src/io/recordHandler.cpp +++ b/src/io/recordHandler.cpp @@ -1,6 +1,7 @@ #include "io/recordHandler.h" #include +#include #include #include #include @@ -24,8 +25,8 @@ extern std::unordered_map> CAs; class RecordHandlerSession { public: - uint32_t sessid; - uint32_t lastCommandCount; + uint32_t sessid = 0; + uint32_t lastCommandCount = 0; std::shared_ptr tbs; std::shared_ptr result; @@ -36,22 +37,18 @@ public: DefaultRecordHandler* parent; std::shared_ptr signer; - std::shared_ptr log; + std::unique_ptr logFile; + //std::stringstream sessionlog; std::vector serials; + logger::logger_set logger; + RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr signer, std::shared_ptr ctx, std::shared_ptr output ) : - sessid( 0 ), - lastCommandCount( 0 ), - tbs( new TBSCertificate() ) { + tbs( std::make_shared() ), + 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_new( ctx.get() ), SSL_free ); std::shared_ptr 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( new OpensslBIOWrapper( bio ) ); + io = std::make_shared( bio ); } void respondCommand( RecordHeader::SignerResult res, std::string payload ) { RecordHeader rh; - rh.command = ( uint16_t ) res; + rh.command = static_cast( 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 buffer( 2048, 0 ); - int res = io->read( buffer.data(), buffer.capacity() ); + std::vector 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( 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( new SAN() ); + auto san = std::make_shared(); 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( new AVA() ); + auto ava = std::make_shared(); 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; std::string date; - std::tie, 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( new RecordHandlerSession( this, signer, ctx, bio ) ); + currentSession = std::make_shared( this, signer, ctx, bio ); } currentSession->work();