]> WPIA git - cassiopeia.git/blobdiff - src/crypto/remoteSigner.cpp
upd: various logging updates
[cassiopeia.git] / src / crypto / remoteSigner.cpp
index bad2065b2c435bc39516e57026183c0c5136a461..d461eda2508ea15b308f4d932a422e6daed4b5cf 100644 (file)
@@ -1,4 +1,6 @@
 #include "remoteSigner.h"
+
+#include "log/logger.hpp"
 #include "util.h"
 
 #include <iostream>
@@ -6,9 +8,7 @@
 #include <openssl/ssl.h>
 #include <openssl/bn.h>
 
-RemoteSigner::RemoteSigner( std::shared_ptr<BIO> target, std::shared_ptr<SSL_CTX> ctx ) {
-    this->target = target;
-    this->ctx = ctx;
+RemoteSigner::RemoteSigner( std::shared_ptr<BIO> target, std::shared_ptr<SSL_CTX> ctx ) : target( target ), ctx( ctx ) {
 }
 
 RemoteSigner::~RemoteSigner() {
@@ -18,8 +18,7 @@ void RemoteSigner::send( std::shared_ptr<OpensslBIOWrapper> bio, RecordHeader& h
     head.command = ( uint16_t ) cmd;
     head.command_count++;
     head.totalLength = data.size();
-    sendCommand( head, data, bio, log );
-
+    sendCommand( head, data, bio );
 }
 
 std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertificate> cert ) {
@@ -40,12 +39,14 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
     } else if( cert->csr_type == "SPKAC" ) {
         send( conn, head, RecordHeader::SignerCommand::SET_SPKAC, cert->csr_content );
     } else {
-        std::cout << "Unknown csr_type: " << cert->csr_type;
+        logger::error( "Unknown csr_type: ", cert->csr_type );
         return std::shared_ptr<SignedCertificate>();
     }
 
     send( conn, head, RecordHeader::SignerCommand::SET_SIGNATURE_TYPE, cert->md );
     send( conn, head, RecordHeader::SignerCommand::SET_PROFILE, cert->profile );
+    send( conn, head, RecordHeader::SignerCommand::SET_WISH_FROM, cert->wishFrom );
+    send( conn, head, RecordHeader::SignerCommand::SET_WISH_TO, cert->wishTo );
 
     for( auto ava : cert->AVAs ) {
         if( ava->name.find( "," ) != std::string::npos ) {
@@ -75,13 +76,13 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
             int length = conn->read( buffer.data(), buffer.size() );
 
             if( length <= 0 ) {
-                std::cout << "Error, no response data" << std::endl;
+                logger::error( "Error, no response data" );
                 result = std::shared_ptr<SignedCertificate>();
                 break;
             }
 
             RecordHeader head;
-            std::string payload = parseCommand( head, std::string( buffer.data(), length ), log );
+            std::string payload = parseCommand( head, std::string( buffer.data(), length ) );
 
             switch( ( RecordHeader::SignerResult ) head.command ) {
             case RecordHeader::SignerResult::CERTIFICATE:
@@ -97,11 +98,11 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
                 break;
 
             default:
-                std::cout << "Invalid Message" << std::endl;
+                logger::error( "Invalid Message" );
                 break;
             }
         } catch( const char* msg ) {
-            std::cout << msg << std::endl;
+            logger::error( msg );
             return std::shared_ptr<SignedCertificate>();
         }
     }
@@ -134,17 +135,20 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
             []( char* p ) {
                 OPENSSL_free( p );
             } ); // OPENSSL_free is a macro...
+
+        extractTimes( pem, result );
+
         result->serial = std::string( serStr.get() );
     }
 
     if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
-        std::cout << "SSL shutdown failed" << std::endl;
+        logger::warn( "SSL shutdown failed" );
     }
 
     return result;
 }
 
-std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_ptr<CAConfig> ca, std::string serial ) {
+std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_ptr<CAConfig> ca, std::vector<std::string> serials ) {
     ( void )BIO_reset( target.get() );
 
     std::shared_ptr<SSL> ssl( SSL_new( ctx.get() ), SSL_free );
@@ -158,72 +162,79 @@ std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_p
     head.flags = 0;
     head.sessid = 13;
 
-    std::string payload = ca->name + std::string( "\0", 1 ) + serial;
+    for( std::string serial : serials ) {
+        send( conn, head, RecordHeader::SignerCommand::ADD_SERIAL, serial );
+    }
+
+    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 ) {
-        std::cout << "Error, no response data" << std::endl;
-        return std::pair<std::shared_ptr<CRL>, std::string>( std::shared_ptr<CRL>(), "" );
+        throw "Error, no response data";
     }
 
-    payload = parseCommand( head, std::string( buffer.data(), length ), log );
+    payload = parseCommand( head, std::string( buffer.data(), length ) );
 
     std::shared_ptr<CRL> crl( new CRL( ca->path + std::string( "/ca.crl" ) ) );
+    std::string date;
 
-    switch( ( RecordHeader::SignerResult ) head.command ) {
-    case RecordHeader::SignerResult::REVOKED: {
-        const unsigned char* buffer2 = ( const unsigned char* ) payload.data();
-        const unsigned char* pos = buffer2;
-        ASN1_UTCTIME* time = d2i_ASN1_UTCTIME( NULL, &pos, payload.size() );
-        ASN1_UTCTIME_free( time );
-        std::string rest = payload.substr( pos - buffer2 );
-        crl->revoke( serial, payload.substr( 0, pos - buffer2 ) );
-        crl->setSignature( rest );
-        bool ok = crl->verify( ca );
-
-        if( ok ) {
-            ( *log ) << "CRL verificated successfully" << std::endl;
-            writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
-        } else {
-            ( *log ) << "CRL is broken" << std::endl;
-            send( conn, head, RecordHeader::SignerCommand::GET_FULL_CRL, ca->name );
-            length = conn->read( buffer.data(), buffer.size() );
+    if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::REVOKED ) {
+        throw "Protocol violation";
+    }
 
-            if( length <= 0 ) {
-                ( *log ) << "Error, no response data" << std::endl;
-                return std::pair<std::shared_ptr<CRL>, std::string>( std::shared_ptr<CRL>(), "" );
-            }
+    const unsigned char* buffer2 = ( const unsigned char* ) payload.data();
+    const unsigned char* pos = buffer2;
+    ASN1_TIME* time = d2i_ASN1_TIME( NULL, &pos, payload.size() );
+    ASN1_TIME_free( time );
+    date = payload.substr( 0, pos - buffer2 );
+    std::string rest = payload.substr( pos - buffer2 );
 
-            payload = parseCommand( head, std::string( buffer.data(), length ), log );
-            writeFile( ca->path + std::string( "/ca.crl.bak" ), payload );
-            crl = std::shared_ptr<CRL>( new CRL( ca->path + std::string( "/ca.crl.bak" ) ) );
+    for( std::string serial : serials ) {
+        crl->revoke( serial, date );
+    }
 
-            if( crl->verify( ca ) ) {
-                writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
-                ( *log ) << "CRL is now valid" << std::endl;
-            } else {
-                ( *log ) << "CRL is still broken... Please, help me" << std::endl;
-            }
+    crl->setSignature( rest );
+    bool ok = crl->verify( ca );
+
+    if( ok ) {
+        logger::note( "CRL verificated successfully" );
+        writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
+    } 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";
         }
 
-        ( *log ) << "CRL: " << std::endl << crl->toString() << std::endl;
-        break;
-    }
+        payload = parseCommand( head, std::string( buffer.data(), length ) );
 
-    default:
-        throw "Invalid response command.";
+        if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::FULL_CRL ) {
+            throw "Protocol violation";
+        }
+
+        writeFile( ca->path + std::string( "/ca.crl.bak" ), payload );
+        crl = std::shared_ptr<CRL>( new CRL( ca->path + std::string( "/ca.crl.bak" ) ) );
+
+        if( crl->verify( ca ) ) {
+            writeFile( ca->path + std::string( "/ca.crl" ), crl->toString() );
+            logger::note( "CRL is now valid again" );
+        } else {
+            logger::warn( "CRL is still broken... Please, help me" );
+        }
     }
 
+    logger::debug( "CRL:\n", crl->toString() );
 
     if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
-        std::cout << "SSL shutdown failed" << std::endl;
+        logger::warn( "SSL shutdown failed" );
     }
 
-    return std::pair<std::shared_ptr<CRL>, std::string>( std::shared_ptr<CRL>(), "" );
+    return std::pair<std::shared_ptr<CRL>, std::string>( crl, date );
 }
 
 void RemoteSigner::setLog( std::shared_ptr<std::ostream> target ) {