X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fio%2FrecordHandler.cpp;h=2eb8358a567c55f19c117c231f6fc8ca8c1c28c3;hb=dc63575cbbef92d3ee7f0f6c22dae7e42f579651;hp=b2fe82c2973783a73f863370df590ad3b6ee0878;hpb=e8abb7ca28c05c91fef58d23c40d3f2b1d5322ff;p=cassiopeia.git diff --git a/src/io/recordHandler.cpp b/src/io/recordHandler.cpp index b2fe82c..2eb8358 100644 --- a/src/io/recordHandler.cpp +++ b/src/io/recordHandler.cpp @@ -45,7 +45,7 @@ public: RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr signer, std::shared_ptr ctx, std::shared_ptr output ) : tbs( std::make_shared() ), - logFile(openLogfile( "logs/log_" + timestamp() ) ), + logFile( openLogfile( "logs/log_" + timestamp() ) ), logger{ std::cout, *logFile } { this->parent = parent; this->signer = signer; @@ -67,39 +67,23 @@ public: rh.command = static_cast( res ); rh.flags = 0; rh.command_count = 0; // TODO i++ - rh.totalLength = payload.size(); sendCommand( rh, payload, io ); } void work() { - std::vector buffer( 2048 ); - int res = io->read( buffer.data(), buffer.size() ); - - 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 ); - execute( head, payload ); - } catch( const char* msg ) { - 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( static_cast( 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"; @@ -176,9 +160,11 @@ public: } 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" ); @@ -223,7 +209,7 @@ public: break; default: - throw "Unimplemented"; + throw std::runtime_error( "Unimplemented" ); } } }; @@ -238,10 +224,14 @@ void DefaultRecordHandler::reset() { void DefaultRecordHandler::handle() { if( !currentSession ) { - (void) BIO_reset( bio.get() ); + ( void ) BIO_reset( bio.get() ); logger::note( "New session allocated." ); currentSession = std::make_shared( this, signer, ctx, bio ); } - currentSession->work(); + try { + currentSession->work(); + } catch( eof_exception e ) { + reset(); + } }