]> WPIA git - cassiopeia.git/blobdiff - src/crypto/CRL.cpp
chg: rename package name and all references to it
[cassiopeia.git] / src / crypto / CRL.cpp
index 41fbe8b6472464af1dbcfab4ed5aec66b7cf44bd..01f8af5320b73def166310920010e24362458b1a 100644 (file)
@@ -42,11 +42,9 @@ std::string CRL::revoke( std::string serial, std::string time ) {
     X509_REVOKED_set_serialNumber( rev, ser.get() );
 
     if( time != "" ) {
-        const unsigned char* data = ( unsigned char* )( time.data() );
-        d2i_ASN1_TIME( &rev->revocationDate, &data, time.size() );
-    } else {
-        X509_REVOKED_set_revocationDate( rev, tmptm.get() );
+      ASN1_TIME_set_string( tmptm.get(), time.data() );
     }
+    X509_REVOKED_set_revocationDate( rev, tmptm.get() );
 
     X509_CRL_add0_revoked( crl.get(), rev );
 
@@ -60,6 +58,10 @@ std::string CRL::revoke( std::string serial, std::string time ) {
 }
 
 void CRL::sign( std::shared_ptr<CAConfig> ca ) {
+    if(!ca->caKey){
+        throw new std::invalid_argument("Cannot sign CRL with CA " + ca->name + " because it has no private key.");
+    }
+
     // Updating necessary CRL props
     std::shared_ptr<ASN1_TIME> tmptm( ASN1_TIME_new(), ASN1_TIME_free );
 
@@ -107,17 +109,21 @@ std::string CRL::toString() {
 }
 
 std::string CRL::getSignature() {
-    int len = i2d_X509_ALGOR( crl->sig_alg, NULL );
-    len += i2d_ASN1_BIT_STRING( crl->signature, NULL );
-    len += i2d_ASN1_TIME( crl->crl->lastUpdate, NULL );
-    len += i2d_ASN1_TIME( crl->crl->nextUpdate, NULL );
+    const X509_ALGOR *palg;
+    const ASN1_BIT_STRING *psig;
+
+    X509_CRL_get0_signature(crl.get(), &psig, &palg);
+    int len = i2d_X509_ALGOR( const_cast<X509_ALGOR*>(palg), NULL );
+    len += i2d_ASN1_BIT_STRING( const_cast<ASN1_BIT_STRING*>(psig), NULL );
+    len += i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_lastUpdate(crl.get())), NULL );
+    len += i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_nextUpdate(crl.get())), NULL );
 
     unsigned char* buffer = ( unsigned char* ) OPENSSL_malloc( len );
     unsigned char* pos = buffer;
-    i2d_X509_ALGOR( crl->sig_alg, &pos );
-    i2d_ASN1_BIT_STRING( crl->signature, &pos );
-    i2d_ASN1_TIME( crl->crl->lastUpdate, &pos );
-    i2d_ASN1_TIME( crl->crl->nextUpdate, &pos );
+    i2d_X509_ALGOR( const_cast<X509_ALGOR*>(palg), &pos );
+    i2d_ASN1_BIT_STRING( const_cast<ASN1_BIT_STRING*>(psig), &pos );
+    i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_lastUpdate(crl.get())), &pos );
+    i2d_ASN1_TIME( const_cast<ASN1_TIME*>(X509_CRL_get0_nextUpdate(crl.get())), &pos );
     std::string res = std::string( ( char* ) buffer, len );
     OPENSSL_free( buffer );
 
@@ -126,19 +132,33 @@ std::string CRL::getSignature() {
 
 void CRL::setSignature( std::string signature ) {
     X509_CRL_sort( crl.get() );
+    X509_ALGOR *palg;
+    ASN1_BIT_STRING *psig;
+    // this is not intended use of the OPENSSL-API but API-limitations leave us with no other options.
+    X509_CRL_get0_signature(crl.get(), const_cast<const ASN1_BIT_STRING **>(&psig), const_cast<const X509_ALGOR**>(&palg));
+
     const unsigned char* data = ( unsigned char* )( signature.data() );
     const unsigned char* buffer = data;
-    d2i_X509_ALGOR( &crl->sig_alg, &buffer, signature.size() );
-    d2i_ASN1_BIT_STRING( &crl->signature, &buffer, signature.size() + data - buffer );
-    d2i_ASN1_TIME( &crl->crl->lastUpdate, &buffer, signature.size() + data - buffer );
-    d2i_ASN1_TIME( &crl->crl->nextUpdate, &buffer, signature.size() + data - buffer );
+    X509_ALGOR *alg = d2i_X509_ALGOR( NULL, &buffer, signature.size() );
+    ASN1_BIT_STRING *sig = d2i_ASN1_BIT_STRING( NULL, &buffer, signature.size() + data - buffer );
+    ASN1_TIME *a1 = d2i_ASN1_TIME( NULL, &buffer, signature.size() + data - buffer );
+    ASN1_TIME *a2 = d2i_ASN1_TIME( NULL, &buffer, signature.size() + data - buffer );
+    std::swap(*palg, *alg);
+    std::swap(*psig, *sig);
+    X509_CRL_set1_lastUpdate( crl.get(), a1);
+    X509_CRL_set1_nextUpdate( crl.get(), a2);
+
+    X509_ALGOR_free(alg);
+    ASN1_BIT_STRING_free(sig);
+    ASN1_TIME_free(a1);
+    ASN1_TIME_free(a2);
 }
 
 bool CRL::needsResign() {
     time_t current;
     time( &current );
     current += 60 * 60;// 1 hour
-    auto time = X509_CRL_get_nextUpdate( crl.get() );
+    auto time = X509_CRL_get0_nextUpdate( crl.get() );
 
     if( !time ) {
         return true;