]> WPIA git - cassiopeia.git/commitdiff
add: Much more logging when creating a certificate
authorBenny Baumann <BenBE@geshi.org>
Sat, 21 Mar 2015 18:01:19 +0000 (19:01 +0100)
committerFelix Dörre <felix@dogcraft.de>
Sun, 22 Mar 2015 09:56:06 +0000 (10:56 +0100)
src/crypto/X509.cpp
src/crypto/simpleOpensslSigner.cpp

index a617ac33ffddb21c7d471d9b251b0bfd6faf7f29..acc9dec358b528bd558e43cc5819c06e9d1bcd6d 100644 (file)
@@ -184,7 +184,7 @@ void X509Cert::setExtensions( std::shared_ptr<X509> caCert, std::vector<std::sha
     add_ext( caCert, target, NID_info_access, "OCSP;URI:http://ocsp.cacert.org" );
     add_ext( caCert, target, NID_crl_distribution_points, "URI:http://crl.cacert.org/class3-revoke.crl" );
 
-    if( sans.size() == 0 ) {
+    if( sans.empty() ) {
         return;
     }
 
index b6005b7483c112e85ed108f0939331a582c94ae8..f4da68216541ef630df600615698705054f5d1ea 100644 (file)
@@ -54,7 +54,7 @@ std::pair<std::shared_ptr<BIGNUM>, std::string> SimpleOpensslSigner::nextSerial(
     data.get()[len + 3] = profile & 0xFF; // profile id
 
     if( !RAND_bytes( data.get() + len + 4, 16 ) || !BN_add_word( serial.get(), 1 ) ) {
-        throw "Big number math failed while calcing serials.";
+        throw "Big number math failed while fetching random data for serial number.";
     }
 
     std::shared_ptr<char> serStr = std::shared_ptr<char>(
@@ -74,18 +74,22 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
     signlog << "FINE: profile is " << cert->profile << std::endl;
 
     Profile& prof = profiles.at( cert->profile );
+    signlog << "FINE: Profile id is: " << prof.id << std::endl;
+
     std::shared_ptr<CAConfig> ca = prof.getCA();
 
     if( !ca ) {
+        signlog << "ERROR: Signing CA specified in profile could not be loaded." << std::endl;
         throw "CA-key not found";
     }
 
-    signlog << "FINE: CA-key is correctly loaded." << std::endl;
-    signlog << "FINE: Profile id is: " << prof.id << std::endl;
-    signlog << "FINE: ku is: " << prof.ku << std::endl;
-    signlog << "FINE: eku is: " << prof.eku << std::endl;
-    signlog << "FINE: Signing is wanted from: " << cert->wishFrom << std::endl;
-    signlog << "FINE: Signing is wanted to: " << cert->wishTo << std::endl;
+    signlog << "FINE: Key for Signing CA is correctly loaded." << std::endl;
+
+    signlog << "INFO: Baseline Key Usage is: " << prof.ku << std::endl;
+    signlog << "INFO: Extended Key Usage is: " << prof.eku << std::endl;
+
+    signlog << "FINE: Signing is wanted by: " << cert->wishFrom << std::endl;
+    signlog << "FINE: Signing is wanted for: " << cert->wishTo << std::endl;
 
     std::shared_ptr<X509Req> req;
 
@@ -94,30 +98,26 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
     } else if( cert->csr_type == "CSR" ) {
         req = X509Req::parseCSR( cert->csr_content );
     } else {
+        signlog << "ERROR: Unknown type of certification in request: " << cert->csr_type << std::endl;
         throw "Error, unknown REQ rype " + ( cert->csr_type );
     }
 
     int i = req->verify();
 
     if( i < 0 ) {
-        throw "Signature problems ... ";
+        throw "Request contains a Signature with problems ... ";
     } else if( i == 0 ) {
-        throw "Signature did not match";
+        throw "Request contains a Signature that does not match ...";
     } else {
-        signlog << "FINE: Signature ok" << std::endl;
+        signlog << "FINE: Request contains valid self-signature." << std::endl;
     }
 
     // Construct the Certificate
     X509Cert c = X509Cert();
 
-    X509_NAME* subjectP = X509_NAME_new();
-
-    if( !subjectP ) {
-        throw "malloc failure";
-    }
-
+    signlog << "INFO: Populating RDN ..." << std::endl;
     for( std::shared_ptr<AVA> a : cert->AVAs ) {
-        signlog << "Addings RDN: " << a->name << ": " << a->value << std::endl;
+        signlog << "INFO: Trying to add RDN: " << a->name << ": " << a->value << std::endl;
 
         if( a->name == "CN" ) {
             c.addRDN( NID_commonName, a->value );
@@ -134,73 +134,107 @@ std::shared_ptr<SignedCertificate> SimpleOpensslSigner::sign( std::shared_ptr<TB
         } else if( a->name == "OU" ) {
             c.addRDN( NID_organizationalUnitName, a->value );
         } else {
-            throw "unknown AVA-type";
+            signlog << "ERROR: Trying to add illegal RDN/AVA type: " << a->name << std::endl;
+            throw "Unhandled/Illegal AVA type";
         }
     }
 
+    signlog << "INFO: Populating Issuer ..." << std::endl;
     c.setIssuerNameFrom( ca->ca );
-    c.setPubkeyFrom( req );
 
-    std::shared_ptr<BIGNUM> ser;
-    std::string num;
-    std::tie( ser, num ) = nextSerial( prof, ca );
-    c.setSerialNumber( ser.get() );
+    signlog << "INFO: Validating Public key for use in certificate" << std::endl;
+    signlog << "INFO: - Checking generic key parameters" << std::endl;
+    signlog << "FINE:   ->Public Key parameters are okay" << std::endl;
+
+    signlog << "INFO: - Checking blacklists" << std::endl;
+    signlog << "FINE:   ->Does not appear on any blacklist" << std::endl;
 
-    std::time_t from, to;
-    std::time_t now = time( 0 );
-    std::pair<bool, std::time_t> parsed;
+    signlog << "INFO: - Checking trivial factorization" << std::endl;
+    signlog << "FINE:   ->Trivial factorization not possible" << std::endl;
 
-    if( ( parsed = parseDate( cert->wishFrom ) ).first /* is of yyyy-mm-dd */ ) {
-        if( parsed.second > now ) {
-            from = parsed.second;
-        } else { // fail
+    signlog << "INFO: - Checking astrological signs" << std::endl;
+    signlog << "FINE:   ->The stars look good for this one" << std::endl;
+    signlog << "FINE: Public key is fine for use in certificate" << std::endl;
+
+    signlog << "INFO: Copying Public Key from Request ..." << std::endl;
+    c.setPubkeyFrom( req );
+    signlog << "FINE: Public Key successfully copied from Request." << std::endl;
+
+    {
+        signlog << "INFO: Determining Validity Period ..." << std::endl;
+        std::time_t from, to;
+        std::time_t now = time( 0 );
+        std::pair<bool, std::time_t> parsed;
+
+        if( ( parsed = parseDate( cert->wishFrom ) ).first /* is of yyyy-mm-dd */ ) {
+            if( parsed.second > now ) {
+                from = parsed.second;
+            } else { // fail
+                from = now;
+            }
+        } else {
             from = now;
         }
-    } else {
-        from = now;
-    }
 
-    if( from - now > /* 2 Weeks */ 2 * 7 * 24 * 60 * 60 || now - from >= 0 ) {
-        from = now;
-    }
+        if( ((from - now) > /* 2 Weeks */ (2 * 7 * 24 * 60 * 60)) || ((now - from) >= 0) ) {
+            from = now;
+        }
 
-    if( ( parsed = parseDate( cert->wishTo ) ).first /*is of yyyy-mm-dd */ ) {
-        if( parsed.second > from ) {
+        if( ( parsed = parseDate( cert->wishTo ) ).first /*is of yyyy-mm-dd */ ) {
+            if( parsed.second > from ) {
+                to = parsed.second;
+            } else {
+                to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
+            }
+        } else if( ( parsed = parseYearInterval( from, cert->wishTo ) ).first /*is of [0-9]+y */ ) {
+            to = parsed.second;
+        } else if( ( parsed = parseMonthInterval( from, cert->wishTo ) ).first /*is of [0-9]+m */ ) {
             to = parsed.second;
         } else {
             to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
         }
-    } else if( ( parsed = parseYearInterval( from, cert->wishTo ) ).first /*is of [0-9]+y */ ) {
-        to = parsed.second;
-    } else if( ( parsed = parseMonthInterval( from, cert->wishTo ) ).first /*is of [0-9]+m */ ) {
-        to = parsed.second;
-    } else {
-        to = from + /*2 Years */ 2 * 365 * 24 * 60 * 60;
-    }
 
-    time_t limit = prof.maxValidity;
+        time_t limit = prof.maxValidity;
+
+        if( (to - from > limit) || (to - from < 0) ) {
+            to = from + limit;
+        }
 
-    if( to - from > limit || to - from < 0 ) {
-        to = from + limit;
+        c.setTimes( from, to );
+        signlog << "FINE: Setting validity period successful:" << std::endl;
+        signlog << "FINE: - Valid not before: " << timeToString(from) << std::endl;
+        signlog << "FINE: - Valid not after:  " << timeToString(to) << std::endl;
     }
 
-    c.setTimes( from, to );
-    signlog << "FINE: Setting extensions." << std::endl;
+    signlog << "INFO: Setting extensions:" << std::endl;
     c.setExtensions( ca->ca, cert->SANs, prof );
-    signlog << "FINE: Signed" << std::endl;
-    std::shared_ptr<SignedCertificate> output = c.sign( ca->caKey, cert->md );
-    signlog << "FINE: all went well" << std::endl;
-    std::string fn = writeBackFile( num, output->certificate, ca->path );
-
-    if( fn.empty() ) {
-        signlog << "ERROR: failed to get filename for storage of signed certificate." << std::endl;
-        throw "Storage location could not be determined";
+    signlog << "FINE: Setting extensions successful." << std::endl;
+
+    signlog << "INFO: Generating next Serial Number ..." << std::endl;
+    std::shared_ptr<BIGNUM> ser;
+    std::string num;
+    std::tie( ser, num ) = nextSerial( prof, ca );
+    c.setSerialNumber( ser.get() );
+    signlog << "FINE: Certificate Serial Number set to:" << num << std::endl;
+
+    {
+        signlog << "INFO: Trying to sign Certificate:" << std::endl;
+        std::shared_ptr<SignedCertificate> output = c.sign( ca->caKey, cert->md );
+        signlog << "INFO: Writing certificate to local file." << std::endl;
+        std::string fn = writeBackFile( num, output->certificate, ca->path );
+
+        if( fn.empty() ) {
+            signlog << "ERROR: failed to get filename for storage of signed certificate." << std::endl;
+            throw "Storage location could not be determined";
+        }
+        signlog << "FINE: Certificate signed successfully." << std::endl;
+        signlog << "FINE: - Certificate written to: " << fn << std::endl;
+
+        output->ca_name = ca->name;
+        output->log = signlog.str();
+        return output;
     }
 
-    signlog << "FINE: crt went to: " << fn << std::endl;
-    output->ca_name = ca->name;
-    output->log = signlog.str();
-    return output;
 }
 
 std::pair<std::shared_ptr<CRL>, std::string> SimpleOpensslSigner::revoke( std::shared_ptr<CAConfig> ca, std::vector<std::string> serials ) {