X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fio%2FrecordHandler.cpp;h=2eb8358a567c55f19c117c231f6fc8ca8c1c28c3;hb=dc63575cbbef92d3ee7f0f6c22dae7e42f579651;hp=a1d7b47d1e7b0853bd04de66ebc1a807357d26d6;hpb=3d3967b16610579e977b5d8685f3eedb10cbc23a;p=cassiopeia.git diff --git a/src/io/recordHandler.cpp b/src/io/recordHandler.cpp index a1d7b47..2eb8358 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,50 +59,31 @@ 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() ); - - if( res <= 0 ) { - logger::error( "Stream error, resetting SSL" ); - parent->reset(); - return; - } - - std::string content( buffer.data(), res ); - try { RecordHeader head; - std::string payload = parseCommand( head, content, log ); - execute( head, payload ); - } catch( const char* msg ) { - if( log ) { - logger::error( "ERROR: ", msg ); - } - + std::string all = parseCommandChunked( head, io ); + execute( static_cast( head.command ), all ); + } catch( const std::exception& msg ) { + logger::error( "ERROR: ", msg.what() ); parent->reset(); return; } } - void execute( RecordHeader& head, std::string data ) { - if( head.totalLength != head.payloadLength || head.offset != 0 ) { - throw "Error, chunking not supported yet"; - } - - switch( ( RecordHeader::SignerCommand ) head.command ) { + void execute( RecordHeader::SignerCommand command, std::string data ) { + switch( command ) { case RecordHeader::SignerCommand::SET_CSR: tbs->csr_content = data; tbs->csr_type = "CSR"; @@ -142,7 +120,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 +135,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 ); @@ -181,10 +159,15 @@ public: respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate ); } + logger::note( "Shutting down SSL" ); + if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { logger::warn( "ERROR: SSL shutdown failed." ); } + io->ctrl( BIO_CTRL_FLUSH, 0, NULL ); + logger::note( "Shutted down SSL" ); + parent->reset(); // Connection ended break; @@ -195,12 +178,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,20 +192,24 @@ 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() ); - + + logger::note( "Shutting down SSL" ); if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { logger::error( "ERROR: SSL shutdown failed." ); } + io->ctrl( BIO_CTRL_FLUSH, 0, NULL ); + logger::note( "Shutted down SSL" ); parent->reset(); // Connection ended } break; default: - throw "Unimplemented"; + throw std::runtime_error( "Unimplemented" ); } } }; @@ -236,9 +224,14 @@ void DefaultRecordHandler::reset() { void DefaultRecordHandler::handle() { if( !currentSession ) { + ( void ) BIO_reset( bio.get() ); 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(); + try { + currentSession->work(); + } catch( eof_exception e ) { + reset(); + } }