]> WPIA git - cassiopeia.git/commitdiff
upd: use central position to do line buffering
authorFelix Dörre <felix@dogcraft.de>
Wed, 11 Nov 2015 15:28:42 +0000 (16:28 +0100)
committerFelix Dörre <felix@dogcraft.de>
Wed, 11 Nov 2015 15:28:42 +0000 (16:28 +0100)
src/crypto/remoteSigner.cpp
src/io/opensslBIO.cpp
src/io/opensslBIO.h
src/io/record.cpp
src/io/recordHandler.cpp
src/io/slipBio.cpp

index 6db3973a7d7d48b378936c32ce239b683f2ecfa6..f4680d650933248c0bb9584f63cc8b892583b549 100644 (file)
@@ -69,20 +69,11 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
     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:
@@ -171,14 +162,7 @@ std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_p
     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;
@@ -207,13 +191,8 @@ std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_p
     } 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";
index 03122db2ce738ec04c1df0f5660fc99ac5c26ecd..056c2911f657085a844fe557ddc211029f525187 100644 (file)
@@ -1,6 +1,6 @@
 #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() {
@@ -29,3 +29,33 @@ int OpensslBIOWrapper::gets( char* str, int size ) {
 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;
+    }
+}
index a3d7188c02e74800808994edaf98793007e0763c..092fa9cf3a9ff36205b4b15c0d996e1097baab5f 100644 (file)
@@ -2,10 +2,14 @@
 
 #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();
@@ -18,4 +22,9 @@ public:
     int gets( char* str, int size );
 
     static const char* getName();
+
+    std::string readLine();
+};
+
+class EOFException : public std::exception{
 };
index 1332a0ffce437625691db71a656ea535aac8e38c..97e601549428b26a4de512e3deba1e7d711a2926 100644 (file)
@@ -25,6 +25,12 @@ std::string toHexAndChecksum( const std::string& src ) {
 }
 
 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();
@@ -79,7 +85,13 @@ std::string parseCommand( RecordHeader& head, const std::string& input) {
     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";
     }
 
index b2fe82c2973783a73f863370df590ad3b6ee0878..2d58be0b7d1e195e62350ebaadd4cc9191074b4f 100644 (file)
@@ -72,16 +72,7 @@ public:
     }
 
     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;
@@ -242,6 +233,9 @@ void DefaultRecordHandler::handle() {
         logger::note( "New session allocated." );
         currentSession = std::make_shared<RecordHandlerSession>( this, signer, ctx, bio );
     }
-
-    currentSession->work();
+    try {
+        currentSession->work();
+    } catch( EOFException e ){
+        reset();
+    }
 }
index f3db8204c1eef6272e870c3b22827d6967a013bf..a9100b31c88a5e9e44a119ec4f1bf3bb30f2f45f 100644 (file)
@@ -6,7 +6,7 @@
 
 #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)
@@ -44,7 +44,7 @@ std::string toHex( const char* buf, int len ) {
     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 ) {