]> WPIA git - cassiopeia.git/blobdiff - src/crypto/CRL.cpp
upd: only throwing exceptions now
[cassiopeia.git] / src / crypto / CRL.cpp
index 7c3eb866dfff7be87521bb1ec349a4c0bd471104..41fbe8b6472464af1dbcfab4ed5aec66b7cf44bd 100644 (file)
@@ -1,9 +1,11 @@
 #include "CRL.h"
 
 #include <openssl/ssl.h>
+#include <log/logger.hpp>
+#include <exception>
 
 CRL::CRL( std::string path ) {
-    std::shared_ptr<BIO> bio( BIO_new_file( path.c_str(), "r" ), free );
+    std::shared_ptr<BIO> bio( BIO_new_file( path.c_str(), "r" ), BIO_free );
     crl = std::shared_ptr<X509_CRL>( PEM_read_bio_X509_CRL( bio.get(), 0, NULL, 0 ), X509_CRL_free );
 
     if( !crl ) {
@@ -14,25 +16,28 @@ CRL::CRL( std::string path ) {
 std::string CRL::revoke( std::string serial, std::string time ) {
     BIGNUM* serBN = 0;
 
+    logger::note("parsing serial");
     if( ! BN_hex2bn( &serBN, serial.c_str() ) ) {
-        throw "hex2bn malloc fail";
+        throw std::runtime_error("hex2bn malloc fail");
     }
 
     std::shared_ptr<BIGNUM> serBNP( serBN, BN_free );
     std::shared_ptr<ASN1_INTEGER> ser( BN_to_ASN1_INTEGER( serBN, NULL ), ASN1_INTEGER_free );
 
     if( !ser ) {
-        throw "BN Malloc fail";
+        throw std::runtime_error("BN Malloc fail");
     }
 
+    logger::note("building current time");
     std::shared_ptr<ASN1_TIME> tmptm( ASN1_TIME_new(), ASN1_TIME_free );
 
     if( !tmptm ) {
-        throw "ASN1-Time Malloc fail";
+        throw std::runtime_error("ASN1-Time Malloc fail");
     }
 
     X509_gmtime_adj( tmptm.get(), 0 );
 
+    logger::note("creating entry");
     X509_REVOKED* rev = X509_REVOKED_new();
     X509_REVOKED_set_serialNumber( rev, ser.get() );
 
@@ -59,25 +64,30 @@ void CRL::sign( std::shared_ptr<CAConfig> ca ) {
     std::shared_ptr<ASN1_TIME> tmptm( ASN1_TIME_new(), ASN1_TIME_free );
 
     if( !tmptm ) {
-        throw "ASN1-Time Malloc fail";
+        throw std::runtime_error("ASN1-Time Malloc fail");
     }
 
     X509_gmtime_adj( tmptm.get(), 0 );
 
+    logger::note("setting issuer");
     if( !X509_CRL_set_issuer_name( crl.get(), X509_get_subject_name( ca->ca.get() ) ) ) {
-        throw "Setting issuer failed";
+        throw std::runtime_error("Setting issuer failed");
     }
 
+    logger::note("setting update");
     X509_CRL_set_lastUpdate( crl.get(), tmptm.get() );
 
     if( !X509_time_adj_ex( tmptm.get(), 1, 10, NULL ) ) {
-        throw "Updating time failed";
+        throw std::runtime_error("Updating time failed");
     }
 
+    logger::note("setting next update");
     X509_CRL_set_nextUpdate( crl.get(), tmptm.get() );
 
+    logger::note("sorting");
     // Sorting and signing
     X509_CRL_sort( crl.get() );
+    logger::note("signing");
     X509_CRL_sign( crl.get(), ca->caKey.get(), EVP_sha256() );
 }
 
@@ -115,6 +125,7 @@ std::string CRL::getSignature() {
 }
 
 void CRL::setSignature( std::string signature ) {
+    X509_CRL_sort( crl.get() );
     const unsigned char* data = ( unsigned char* )( signature.data() );
     const unsigned char* buffer = data;
     d2i_X509_ALGOR( &crl->sig_alg, &buffer, signature.size() );
@@ -122,3 +133,17 @@ void CRL::setSignature( std::string signature ) {
     d2i_ASN1_TIME( &crl->crl->lastUpdate, &buffer, signature.size() + data - buffer );
     d2i_ASN1_TIME( &crl->crl->nextUpdate, &buffer, signature.size() + data - buffer );
 }
+
+bool CRL::needsResign() {
+    time_t current;
+    time( &current );
+    current += 60 * 60;// 1 hour
+    auto time = X509_CRL_get_nextUpdate( crl.get() );
+
+    if( !time ) {
+        return true;
+    }
+
+    int cmp =  X509_cmp_time( time, &current );
+    return cmp < 0;
+}