]> WPIA git - cassiopeia.git/blobdiff - src/crypto/remoteSigner.cpp
upd: only throwing exceptions now
[cassiopeia.git] / src / crypto / remoteSigner.cpp
index 6c003c38023fc2ca2986cbf6d53e6a7cfe153a04..1c32ba0ed513f877f13811557865cb7989da49f7 100644 (file)
@@ -73,7 +73,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
     for( int i = 0; i < 3; i++ ) {
         try {
             RecordHeader head;
-            std::string payload = parseCommand( head, conn->readLine() );
+            std::string payload = parseCommandChunked( head, conn );
 
             switch( static_cast<RecordHeader::SignerResult>( head.command )) {
             case RecordHeader::SignerResult::CERTIFICATE:
@@ -92,8 +92,8 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
                 logger::error( "Invalid Message" );
                 break;
             }
-        } catch( const char* msg ) {
-            logger::error( msg );
+        } catch( const std::exception& msg ) {
+            logger::error( msg.what() );
             return std::shared_ptr<SignedCertificate>();
         }
     }
@@ -107,7 +107,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
             int dlen = BIO_write( bios.get(), buf, len );
 
             if( dlen <= 0 ) {
-                throw "Memory error.";
+                throw std::runtime_error("Memory error.");
             }
 
             len -= dlen;
@@ -117,7 +117,7 @@ std::shared_ptr<SignedCertificate> RemoteSigner::sign( std::shared_ptr<TBSCertif
         std::shared_ptr<X509> pem( PEM_read_bio_X509( bios.get(), NULL, 0, NULL ) );
 
         if( !pem ) {
-            throw "Pem was not readable";
+            throw std::runtime_error("Pem was not readable");
         }
 
         std::shared_ptr<BIGNUM> ser( ASN1_INTEGER_to_BN( pem->cert_info->serialNumber, NULL ), BN_free );
@@ -162,14 +162,14 @@ 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 );
 
-    payload = parseCommand( head, conn->readLine() );
+    payload = parseCommandChunked( head, conn );
 
     std::string tgtName = ca->path + std::string( "/ca.crl" );
     auto crl = std::make_shared<CRL>( tgtName );
     std::string date;
 
     if( static_cast<RecordHeader::SignerResult>( head.command ) != RecordHeader::SignerResult::REVOKED ) {
-        throw "Protocol violation";
+        throw std::runtime_error("Protocol violation");
     }
 
     const unsigned char* buffer2 = reinterpret_cast<const unsigned char*>( payload.data() );
@@ -193,10 +193,10 @@ std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_p
         logger::warn( "CRL is broken, trying to recover" );
         send( conn, head, RecordHeader::SignerCommand::GET_FULL_CRL, ca->name );
 
-        payload = parseCommand( head, conn->readLine() );
+        payload = parseCommandChunked( head, conn );
 
         if( static_cast<RecordHeader::SignerResult>( head.command ) != RecordHeader::SignerResult::FULL_CRL ) {
-            throw "Protocol violation";
+            throw std::runtime_error("Protocol violation");
         }
 
         std::string name_bak = ca->path + std::string( "/ca.crl.bak" );
@@ -204,9 +204,8 @@ std::pair<std::shared_ptr<CRL>, std::string> RemoteSigner::revoke( std::shared_p
         crl = std::make_shared<CRL>( name_bak );
 
         if( crl->verify( ca ) ) {
-            writeFile( tgtName, crl->toString() );
-            if( remove( name_bak.c_str() ) != 0 ){
-                logger::warn( "Removing old CRL failed" );
+            if( rename( name_bak.c_str(), tgtName.c_str() ) != 0 ){
+                logger::warn( "Moving new CRL over old CRL failed" );
             }
             logger::note( "CRL is now valid again" );
         } else {