]> WPIA git - cassiopeia.git/blobdiff - src/apps/client.cpp
chg: make cassiopeia conform to db schema version 33
[cassiopeia.git] / src / apps / client.cpp
index 9a7add6badb44fcb0a07817e50b070afa7057cc9..4f2d81af6ec81f0747b7f5822fa783188e60d4d7 100644 (file)
@@ -16,6 +16,9 @@
 #include "io/bios.h"
 #include "io/slipBio.h"
 #include "config.h"
+#include <internal/bio.h>
+#include <dirent.h>
+#include <crypto/X509.h>
 
 #ifdef NO_DAEMON
 #define DAEMON false
@@ -33,10 +36,7 @@ void checkCRLs( std::shared_ptr<Signer> sign ) {
     logger::note( "Signing CRLs" );
 
     for( auto& x : CAs ) {
-        logger::notef( "Checking: %s ...", x.first );
-
         if( !x.second->crlNeedsResign() ) {
-            logger::warnf( "Skipping Resigning CRL: %s ...", x.second->name );
             continue;
         }
 
@@ -45,19 +45,105 @@ void checkCRLs( std::shared_ptr<Signer> sign ) {
         try {
             std::vector<std::string> serials;
             std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( x.second, serials );
-        } catch( const std::exception &e ) {
+        } catch( const std::exceptione ) {
             logger::error( "Exception: ", e.what() );
         }
     }
 }
 
-int main( int argc, const char* argv[] ) {
+bool pathExists( const std::string& name ) {
+    struct stat buffer;
+    return stat( name.c_str(), &buffer ) == 0;
+}
+
+void signOCSP( std::shared_ptr<Signer> sign, std::string profileName, std::string req, std::string crtName, std::string failName ) {
+    auto cert = std::make_shared<TBSCertificate>();
+    cert->ocspCA = profileName;
+    cert->wishFrom = "now";
+    cert->wishTo = "1y";
+    cert->md = "sha512";
+
+    logger::note( "INFO: Message Digest: ", cert->md );
+
+    cert->csr_content = req;
+    cert->csr_type = "CSR";
+    auto nAVA = std::make_shared<AVA>();
+    nAVA->name = "CN";
+    nAVA->value = "OCSP Responder";
+    cert->AVAs.push_back( nAVA );
+
+    std::shared_ptr<SignedCertificate> res = sign->sign( cert );
+
+    if( !res ) {
+        writeFile( failName, "failed" );
+        logger::error( "OCSP Cert signing failed." );
+        return;
+    }
+
+    writeFile( crtName, res->certificate );
+    logger::notef( "Cert log: %s", res->log );
+}
+
+void checkOCSP( std::shared_ptr<Signer> sign ) {
+    std::unique_ptr<DIR, std::function<void( DIR * )>> dp( opendir( "ca" ), []( DIR * d ) {
+        closedir( d );
+    } );
+
+    // When opendir fails and returns 0 the unique_ptr will be considered unintialized and will not call closedir.
+    // Even if closedir would be called, according to POSIX it MAY handle nullptr properly (for glibc it does).
+    if( !dp ) {
+        logger::error( "CA directory not found" );
+        return;
+    }
+
+    struct dirent *ep;
+
+    while( ( ep = readdir( dp.get() ) ) ) {
+        if( ep->d_name[0] == '.' ) {
+            continue;
+        }
+
+        std::string profileName( ep->d_name );
+        std::string csr = "ca/" + profileName + "/ocsp.csr";
+
+        if( ! pathExists( csr ) ) {
+            continue;
+        }
+
+        std::string crtName = "ca/" + profileName + "/ocsp.crt";
+
+        if( pathExists( crtName ) ) {
+            continue;
+        }
+
+        std::string failName = "ca/" + profileName + "/ocsp.fail";
+
+        if( pathExists( failName ) ) {
+            continue;
+        }
+
+        logger::notef( "Discovered OCSP CSR that needs action: %s", csr );
+        std::string req = readFile( csr );
+        std::shared_ptr<X509Req> parsed = X509Req::parseCSR( req );
+
+        if( parsed->verify() <= 0 ) {
+            logger::errorf( "Invalid CSR for %s", profileName );
+            continue;
+        }
+
+        signOCSP( sign, profileName, req, crtName, failName );
+    }
+}
+
+
+int main( int argc, const char *argv[] ) {
     bool once = false;
     bool resetOnly = false;
 
     if( argc == 2 && std::string( "--once" ) == argv[1] ) {
         once = true;
     }
+
     if( argc == 2 && std::string( "--reset" ) == argv[1] ) {
         resetOnly = true;
     }
@@ -65,7 +151,7 @@ int main( int argc, const char* argv[] ) {
     std::string path;
 
 #ifdef NDEBUG
-    path = "/etc/cacert/cassiopeia/cassiopeia.conf";
+    path = "/etc/wpia/cassiopeia/cassiopeia.conf";
 #else
     path = "config.txt";
 #endif
@@ -79,10 +165,12 @@ int main( int argc, const char* argv[] ) {
         logger::fatal( "Error: no serial device is given!" );
         return -1;
     }
+
     std::shared_ptr<JobProvider> jp = std::make_shared<PostgresJobProvider>( sqlHost, sqlUser, sqlPass, sqlDB );
     std::shared_ptr<BIO> b = openSerial( serialPath );
-    std::shared_ptr<BIO> slip1( BIO_new( toBio<SlipBIO>() ), BIO_free );
-    static_cast<SlipBIO*>( slip1->ptr )->setTarget( std::make_shared<OpensslBIOWrapper>( b ), false );
+    std::shared_ptr<BIO_METHOD> m( toBio<SlipBIO>(), BIO_meth_free );
+    std::shared_ptr<BIO> slip1( BIO_new( m.get() ), BIO_free );
+    static_cast<SlipBIO *>( slip1->ptr )->setTarget( std::make_shared<OpensslBIOWrapper>( b ), false );
     auto sign = std::make_shared<RemoteSigner>( slip1, generateSSLContext( false ) );
     // std::shared_ptr<Signer> sign( new SimpleOpensslSigner() );
 
@@ -96,36 +184,38 @@ int main( int argc, const char* argv[] ) {
     time_t lastCRLCheck = 0;
 
     while( true ) {
-       try {
+        try {
             time_t current;
             time( &current );
 
             if( lastCRLCheck + 30 * 60 < current ) {
                 // todo set good log TODO FIXME
-                sign->setLog( std::shared_ptr<std::ostream>(
-                    &std::cout,
-                    []( std::ostream* o ) {
-                        ( void ) o;
-                    } ) );
+                auto ostreamFree = []( std::ostream * o ) {
+                    ( void ) o;
+                };
+                sign->setLog( std::shared_ptr<std::ostream>( &std::cout, ostreamFree ) );
                 checkCRLs( sign );
                 lastCRLCheck = current;
             }
 
+            checkOCSP( sign );
+
             std::shared_ptr<Job> job;
+
             try {
                 job = jp->fetchJob();
-            } catch ( std::exception &e ){
-                logger::errorf ( "Exception while fetchJob: %s", e.what() );
-           }
+            } catch( std::exception& e ) {
+                logger::errorf( "Exception while fetchJob: %s", e.what() );
+            }
+
             if( !job ) {
-                logger::note( "Nothing to work on." );
                 sleep( 5 );
                 continue;
             }
 
             std::shared_ptr<std::ofstream> logPtr = openLogfile( std::string( "logs/" ) + job->id + std::string( "_" ) + job->warning + std::string( ".log" ) );
 
-            logger::logger_set log_set({logger::log_target(*logPtr, logger::level::debug)}, logger::auto_register::on);
+            logger::logger_set log_set( {logger::log_target( *logPtr, logger::level::debug )}, logger::auto_register::on );
 
             logger::note( "TASK ID: ", job->id );
             logger::note( "TRY:     ", job->warning );
@@ -135,6 +225,13 @@ int main( int argc, const char* argv[] ) {
             if( job->task == "sign" ) {
                 try {
                     std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
+
+                    if( !cert ) {
+                        logger::error( "Unable to load CSR" );
+                        jp->failJob( job );
+                        continue;
+                    }
+
                     cert->wishFrom = job->from;
                     cert->wishTo = job->to;
                     logger::note( "INFO: Message Digest: ", cert->md );
@@ -148,14 +245,6 @@ int main( int argc, const char* argv[] ) {
                         logger::notef( "INFO: AVA %s: %s", AVA->name, AVA->value );
                     }
 
-                    if( !cert ) {
-                        logger::error( "Unable to load CSR" );
-                        jp->failJob( job );
-                        continue;
-                    }
-
-                    logger::notef( "FINE: Found the CSR at '%s'", cert->csr );
-                    cert->csr_content = readFile( keyDir + "/../" + cert->csr );
                     logger::note( "FINE: CSR content:\n", cert->csr_content );
 
                     std::shared_ptr<SignedCertificate> res = sign->sign( cert );
@@ -167,17 +256,8 @@ int main( int argc, const char* argv[] ) {
                     }
 
                     logger::note( "FINE: CERTIFICATE LOG:\n", res->log,
-                                 "FINE: CERTIFICATE:\n", res->certificate );
-
-                    std::string fn = writeBackFile( job->target.c_str(), res->certificate, keyDir );
-
-                    if( fn.empty() ) {
-                        logger::error( "ERROR: Writeback of the certificate failed." );
-                        jp->failJob( job );
-                        continue;
-                    }
+                                  "FINE: CERTIFICATE:\n", res->certificate );
 
-                    res->crt_name = fn;
                     jp->writeBack( job, res ); //! \FIXME: Check return value
                     logger::note( "FINE: signing done." );
 
@@ -204,7 +284,7 @@ int main( int argc, const char* argv[] ) {
                     logger::note( "revoking" );
                     std::pair<std::shared_ptr<CRL>, std::string> rev = sign->revoke( CAs.at( data.second ), serials );
                     std::string date = rev.second;
-                    const unsigned char* pos = ( const unsigned char* ) date.data();
+                    const unsigned char *pos = ( const unsigned char * ) date.data();
                     std::shared_ptr<ASN1_TIME> time( d2i_ASN1_TIME( NULL, &pos, date.size() ), ASN1_TIME_free );
 
                     jp->writeBackRevocation( job, timeToString( time ) );
@@ -220,8 +300,8 @@ int main( int argc, const char* argv[] ) {
             if( !DAEMON || once ) {
                 return 0;
             }
-        } catch ( std::exception &e ){
-            logger::errorf ( "std::exception in mainloop: %s", e.what() );
+        } catch( std::exception& e ) {
+            logger::errorf( "std::exception in mainloop: %s", e.what() );
         }
 
     }