]> WPIA git - cassiopeia.git/blobdiff - src/crypto/remoteSigner.cpp
Merge remote-tracking branch 'benbe/toCoverity'
[cassiopeia.git] / src / crypto / remoteSigner.cpp
index 6259710f8868773a9d037d5fb6a8e163df1bb0ae..b6c7c317d30047cf703c4c1824bb9c8836031ba2 100644 (file)
@@ -6,9 +6,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() {
@@ -46,6 +44,8 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
 
     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 ) {
@@ -134,6 +134,9 @@ 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() );
     }
 
@@ -144,7 +147,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
     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,15 +161,18 @@ 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 );
@@ -174,52 +180,55 @@ std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_p
     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_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 );
+    if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::REVOKED ) {
+        throw "Protocol violation";
+    }
+
+    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 );
+
+    for( std::string serial : serials ) {
         crl->revoke( serial, date );
-        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() );
+    crl->setSignature( rest );
+    bool ok = crl->verify( ca );
 
-            if( length <= 0 ) {
-                ( *log ) << "Error, no response data" << std::endl;
-                return std::pair<std::shared_ptr<CRL>, std::string>( std::shared_ptr<CRL>(), "" );
-            }
+    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() );
 
-            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" ) ) );
+        if( length <= 0 ) {
+            throw "Error, no response data";
+        }
 
-            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;
-            }
+        payload = parseCommand( head, std::string( buffer.data(), length ), log );
 
+        if( ( RecordHeader::SignerResult ) head.command != RecordHeader::SignerResult::FULL_CRL ) {
+            throw "Protocol violation";
         }
 
-        ( *log ) << "CRL: " << std::endl << crl->toString() << std::endl;
-        break;
-    }
+        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() );
+            ( *log ) << "CRL is now valid" << std::endl;
+        } else {
+            ( *log ) << "CRL is still broken... Please, help me" << std::endl;
+        }
 
-    default:
-        throw "Invalid response command.";
     }
 
+    ( *log ) << "CRL: " << std::endl << crl->toString() << std::endl;
 
     if( !SSL_shutdown( ssl.get() ) && !SSL_shutdown( ssl.get() ) ) { // need to close the connection twice
         std::cout << "SSL shutdown failed" << std::endl;