RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
tbs( std::make_shared<TBSCertificate>() ),
- logFile(openLogfile( "logs/log_" + timestamp() ) ),
+ logFile( openLogfile( "logs/log_" + timestamp() ) ),
logger{ std::cout, *logFile } {
this->parent = parent;
this->signer = signer;
rh.command = static_cast<uint16_t>( res );
rh.flags = 0;
rh.command_count = 0; // TODO i++
- rh.totalLength = payload.size();
sendCommand( rh, payload, io );
}
void work() {
- std::string content = io->readLine();
-
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<RecordHeader::SignerCommand>( 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<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";
}
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" );
break;
default:
- throw "Unimplemented";
+ throw std::runtime_error( "Unimplemented" );
}
}
};
void DefaultRecordHandler::handle() {
if( !currentSession ) {
- (void) BIO_reset( bio.get() );
+ ( void ) BIO_reset( bio.get() );
logger::note( "New session allocated." );
currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
}
+
try {
currentSession->work();
- } catch( EOFException e ){
+ } catch( eof_exception e ) {
reset();
}
}