]> WPIA git - cassiopeia.git/blobdiff - src/io/recordHandler.cpp
chg: Modify the code to use the new logger
[cassiopeia.git] / src / io / recordHandler.cpp
index abac9a53b7ec8034f29424f0af7969c749e8110d..0e63805e98f8e5e57eb2cfd06913421395ec147b 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <openssl/ssl.h>
 
+#include "util.h"
 #include "io/record.h"
 #include "io/opensslBIO.h"
 #include "io/slipBio.h"
 #include "db/database.h"
 #include "crypto/remoteSigner.h"
 #include "crypto/sslUtil.h"
-
 #include "crypto/simpleOpensslSigner.h"
 
+#include "log/logger.hpp"
+
 extern std::vector<Profile> profiles;
 extern std::unordered_map<std::string, std::shared_ptr<CAConfig>> CAs;
 
@@ -35,8 +37,11 @@ public:
     std::shared_ptr<Signer> signer;
 
     std::shared_ptr<std::ofstream> log;
+    std::vector<std::string> serials;
 
     RecordHandlerSession( DefaultRecordHandler* parent, std::shared_ptr<Signer> signer, std::shared_ptr<SSL_CTX> ctx, std::shared_ptr<BIO> output ) :
+        sessid( 0 ),
+        lastCommandCount( 0 ),
         tbs( new TBSCertificate() ) {
         this->parent = parent;
         this->signer = signer;
@@ -46,12 +51,7 @@ public:
             throw "Error while fetching time?";
         }
 
-        log = std::shared_ptr<std::ofstream>(
-            new std::ofstream( std::string( "logs/log_" ) + std::to_string( c_time ) ),
-            []( std::ofstream * ptr ) {
-                ptr->close();
-                delete ptr;
-            } );
+        log = openLogfile( std::string( "logs/log_" ) + std::to_string( c_time ) );
 
         ssl = std::shared_ptr<SSL>( SSL_new( ctx.get() ), SSL_free );
         std::shared_ptr<BIO> bio(
@@ -79,7 +79,7 @@ public:
         int res = io->read( buffer.data(), buffer.capacity() );
 
         if( res <= 0 ) {
-            ( *log ) << "Stream error, resetting SSL" << std::endl;
+            logger::error( "Stream error, resetting SSL" );
             parent->reset();
             return;
         }
@@ -92,7 +92,7 @@ public:
             execute( head, payload );
         } catch( const char* msg ) {
             if( log ) {
-                ( *log ) << "ERROR: " << msg << std::endl;
+                logger::error( "ERROR: ", msg );
             }
 
             parent->reset();
@@ -109,13 +109,13 @@ public:
         case RecordHeader::SignerCommand::SET_CSR:
             tbs->csr_content = data;
             tbs->csr_type = "CSR";
-            ( *log ) << "INFO: CSR read:" << std::endl << tbs->csr_content;
+            logger::note( "INFO: CSR read:\n", tbs->csr_content );
             break;
 
         case RecordHeader::SignerCommand::SET_SPKAC:
             tbs->csr_content = data;
             tbs->csr_type = "SPKAC";
-            ( *log ) << "INFO: SPKAC read:" << std::endl << tbs->csr_content;
+            logger::note( "INFO: SPKAC read:\n", tbs->csr_content );
             break;
 
         case RecordHeader::SignerCommand::SET_SIGNATURE_TYPE:
@@ -127,6 +127,14 @@ public:
             tbs->profile = data;
             break;
 
+        case RecordHeader::SignerCommand::SET_WISH_FROM:
+            tbs->wishFrom = data;
+            break;
+
+        case RecordHeader::SignerCommand::SET_WISH_TO:
+            tbs->wishTo = data;
+            break;
+
         case RecordHeader::SignerCommand::ADD_SAN: {
             size_t pos = data.find( "," );
 
@@ -160,55 +168,52 @@ public:
 
         case RecordHeader::SignerCommand::SIGN:
             result = signer->sign( tbs );
-            ( *log ) << "INFO: signlog: " << result->log << std::endl;
-            ( *log ) << "INFO: res: " << result->certificate << std::endl;
+            logger::note( "INFO: signlog:\n", result->log );
+            logger::note( "INFO: res:\n", result->certificate );
             respondCommand( RecordHeader::SignerResult::SAVE_LOG, result->log );
             break;
 
         case RecordHeader::SignerCommand::LOG_SAVED:
             if( result ) {
+                respondCommand( RecordHeader::SignerResult::SIGNING_CA, result->ca_name );
                 respondCommand( RecordHeader::SignerResult::CERTIFICATE, result->certificate );
             }
 
             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
-                ( *log ) << "ERROR: SSL close failed" << std::endl;
+                logger::warn( "ERROR: SSL shutdown failed." );
             }
 
-            break;
-
-        case RecordHeader::SignerCommand::REVOKE: {
-            ( *log ) << "got revoking command: " << data.size() << std::endl;
-            std::string nullstr( "\0", 1 );
-            size_t t = data.find( nullstr );
-
-            if( t == std::string::npos ) {
-                // error
-                ( *log ) << "error while parsing revoking command." << data << std::endl;
-                break;
-            }
+            parent->reset(); // Connection ended
 
-            std::string ca = data.substr( 0, t );
-            std::string serial = data.substr( t + 1 );
-            ( *log ) << "revoking " << ca << "<->" << serial << std::endl;
+            break;
 
-            ( *log ) << "[";
+        case RecordHeader::SignerCommand::ADD_SERIAL:
+            serials.push_back( data );
+            break;
 
-            for( auto x : CAs ) {
-                ( *log ) << x.first << ", ";
-            }
+        case RecordHeader::SignerCommand::REVOKE: {
+            std::string ca = data;
+            auto reqCA = CAs.at( ca );
+            logger::note( "CA found" );
+            std::shared_ptr<CRL> crl;
+            std::string date;
+            std::tie<std::shared_ptr<CRL>, std::string>( crl, date ) = signer->revoke( reqCA, serials );
 
-            ( *log ) << "]" << std::endl;
+            respondCommand( RecordHeader::SignerResult::REVOKED, date + crl->getSignature() );
 
-            auto reqCA = CAs.at( ca );
-            ( *log ) << "CA found" << std::endl;
-            std::shared_ptr<CRL> crl = signer->revoke( reqCA, serial );
+            break;
+        }
 
-            respondCommand( RecordHeader::SignerResult::REVOKED, crl->toString() );
+        case RecordHeader::SignerCommand::GET_FULL_CRL: {
+            auto ca = CAs.at( data );
+            CRL c( ca->path + "/ca.crl" );
+            respondCommand( RecordHeader::SignerResult::FULL_CRL, c.toString() );
 
             if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) {
-                ( *log ) << "ERROR: SSL close failed" << std::endl;
+                logger::error( "ERROR: SSL shutdown failed." );
             }
 
+            parent->reset(); // Connection ended
             break;
         }
 
@@ -218,14 +223,8 @@ public:
     }
 };
 
-DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio ) :
-    currentSession() {
-
-    this->signer = signer;
-
-    ctx = generateSSLContext( true );
-
-    this->bio = bio;
+DefaultRecordHandler::DefaultRecordHandler( std::shared_ptr<Signer> signer, std::shared_ptr<BIO> bio )
+    : bio( bio ), ctx( generateSSLContext( true ) ), signer( signer ), currentSession() {
 }
 
 void DefaultRecordHandler::reset() {
@@ -234,7 +233,7 @@ void DefaultRecordHandler::reset() {
 
 void DefaultRecordHandler::handle() {
     if( !currentSession ) {
-        std::cout << "session allocated" << std::endl;
+        logger::note( "New session allocated." );
         currentSession = std::shared_ptr<RecordHandlerSession>( new RecordHandlerSession( this, signer, ctx, bio ) );
     }