send( conn, head, RecordHeader::SignerCommand::SIGN, "" );
send( conn, head, RecordHeader::SignerCommand::LOG_SAVED, "" );
auto result = std::make_shared<SignedCertificate>();
- std::vector<char> buffer( 2048 * 4 );
for( int i = 0; i < 3; i++ ) {
try {
- int length = conn->read( buffer.data(), buffer.size() );
-
- if( length <= 0 ) {
- logger::error( "Error, no response data" );
- result = nullptr;
- break;
- }
-
RecordHeader head;
- std::string payload = parseCommand( head, std::string( buffer.data(), length ) );
+ std::string payload = parseCommand( head, conn->readLine() );
switch( static_cast<RecordHeader::SignerResult>( head.command )) {
case RecordHeader::SignerResult::CERTIFICATE:
std::string payload = ca->name;
send( conn, head, RecordHeader::SignerCommand::REVOKE, payload );
- std::vector<char> buffer( 2048 * 4 );
- int length = conn->read( buffer.data(), buffer.size() );
-
- if( length <= 0 ) {
- throw "Error, no response data";
- }
-
- payload = parseCommand( head, std::string( buffer.data(), length ) );
+ payload = parseCommand( head, conn->readLine() );
auto crl = std::make_shared<CRL>( ca->path + std::string( "/ca.crl" ) );
std::string date;
} else {
logger::warn( "CRL is broken, trying to recover" );
send( conn, head, RecordHeader::SignerCommand::GET_FULL_CRL, ca->name );
- length = conn->read( buffer.data(), buffer.size() );
-
- if( length <= 0 ) {
- throw "Error, no response data";
- }
- payload = parseCommand( head, std::string( buffer.data(), length ) );
+ payload = parseCommand( head, conn->readLine() );
if( static_cast<RecordHeader::SignerResult>( head.command ) != RecordHeader::SignerResult::FULL_CRL ) {
throw "Protocol violation";
#include "opensslBIO.h"
-OpensslBIOWrapper::OpensslBIOWrapper( std::shared_ptr<BIO> b ) : b( b ) {
+OpensslBIOWrapper::OpensslBIOWrapper( std::shared_ptr<BIO> _b ) : b( _b ), buffer( 2*0xFFFF+20, 0 ), pos(0) {
}
OpensslBIOWrapper::~OpensslBIOWrapper() {
const char* OpensslBIOWrapper::getName() {
return "OpenSSLWrapper";
}
+#include <log/logger.hpp>
+std::string OpensslBIOWrapper::readLine(){
+ int target = 0;
+ while(1){
+ logger::warn("doing data");
+ while(target < pos){
+ if(buffer[target] == '\n'){
+ target++;
+ std::string res(buffer.data(), 0, target);
+ std::copy(buffer.data() + target, buffer.data() + pos, buffer.data() );
+ pos -= target;
+ logger::warn("emit");
+ return res;
+ }
+ target++;
+ }
+ std::stringstream ss;
+ ss << "target: " << target << ", pos:" << pos;
+ logger::warn(ss.str());
+ int dlen = read(buffer.data() + pos, buffer.size() - pos);
+ if ( dlen <= 0 ){
+ logger::warn(" error! ");
+ throw EOFException();
+ }
+ std::stringstream ss2;
+ ss2 << "done: " << dlen;
+ logger::warn(ss2.str());
+ pos += dlen;
+ }
+}
#include <memory>
#include "bios.h"
+#include <vector>
+#include <exception>
class OpensslBIOWrapper : public OpensslBIO {
private:
std::shared_ptr<BIO> b;
+ std::vector<char> buffer;
+ int pos = 0;
public:
OpensslBIOWrapper( std::shared_ptr<BIO> b );
virtual ~OpensslBIOWrapper();
int gets( char* str, int size );
static const char* getName();
+
+ std::string readLine();
+};
+
+class EOFException : public std::exception{
};
}
void sendCommand( RecordHeader& head, const std::string& data, std::shared_ptr<OpensslBIO> bio ) {
+ std::stringstream ss;
+ ss << data.size();
+ logger::debugf( "Record payload length: %s", ss.str() );
+ if(data.size() > 0xFFFF){
+ logger::warn( "Data too big, need chunking" );
+ }
head.payloadLength = data.size();
std::string s;
s += head.packToString();
uint32_t expectedTotalLength = ( RECORD_HEADER_SIZE + len + 1 /*checksum*/ ) * 2 + 2;
std::string data = str.substr( RECORD_HEADER_SIZE, str.size() - RECORD_HEADER_SIZE );
- if( checksum != -1 || expectedTotalLength != input.size() || error || dlen < RECORD_HEADER_SIZE ) {
+ if( expectedTotalLength != input.size() ) {
+ std::stringstream ss;
+ ss << "Expected: " << expectedTotalLength << ", Got: " << input.size();
+ logger::error( ss.str() );
+ throw "Error, invalid length";
+ }
+ if( checksum != -1 || error || dlen < RECORD_HEADER_SIZE ) {
throw "Error, invalid checksum";
}
}
void work() {
- std::vector<char> 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 );
+ std::string content = io->readLine();
try {
RecordHeader head;
logger::note( "New session allocated." );
currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
}
-
- currentSession->work();
+ try {
+ currentSession->work();
+ } catch( EOFException e ){
+ reset();
+ }
}
#include "log/logger.hpp"
-static constexpr std::size_t buffer_size = 8192;
+static constexpr std::size_t buffer_size = 2 * 0xFFFF + 20;//8192;
#define SLIP_ESCAPE_CHAR ( (char) 0xDB)
#define SLIP_CONNECTION ( (char) 0xC0)
return data;
}
-SlipBIO::SlipBIO() : buffer( std::vector<char>( buffer_size ) ), decodeTarget( 0 ), decodePos( 0 ), rawPos( 0 ) {
+SlipBIO::SlipBIO() : buffer( buffer_size ), decodeTarget( 0 ), decodePos( 0 ), rawPos( 0 ) {
}
void SlipBIO::setTarget( std::shared_ptr<OpensslBIO> target, bool server ) {