]> WPIA git - cassiopeia.git/blobdiff - src/crypto/sslUtil.cpp
upd: clean up valgrind stuff
[cassiopeia.git] / src / crypto / sslUtil.cpp
index d4f55da86b33247c6d9a228f7ab04f416bd4d291..a3432ea5253c87026814d3eaf1cfa96dc231d858 100644 (file)
@@ -91,13 +91,13 @@ static std::shared_ptr<DH> dh_param;
 
 std::shared_ptr<SSL_CTX> generateSSLContext( bool server ) {
     std::shared_ptr<SSL_CTX> ctx = std::shared_ptr<SSL_CTX>(
-        SSL_CTX_new( TLSv1_2_method() ),
+        SSL_CTX_new( TLS_method() ),
         []( SSL_CTX* p ) {
             SSL_CTX_free( p );
         } );
 
     if( !SSL_CTX_set_cipher_list( ctx.get(), "HIGH:+CAMELLIA256:!eNull:!aNULL:!ADH:!MD5:-RSA+AES+SHA1:!RC4:!DES:!3DES:!SEED:!EXP:!AES128:!CAMELLIA128" ) ) {
-        throw "Cannot set cipher list. Your source is broken.";
+        throw std::runtime_error("Cannot set cipher list. Your source is broken.");
     }
 
     SSL_CTX_set_verify( ctx.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback );
@@ -105,7 +105,7 @@ std::shared_ptr<SSL_CTX> generateSSLContext( bool server ) {
     SSL_CTX_use_PrivateKey_file( ctx.get(), server ? "keys/signer_server.key" : "keys/signer_client.key", SSL_FILETYPE_PEM );
 
     if( 1 != SSL_CTX_load_verify_locations( ctx.get(), "keys/ca.crt", 0 ) ) {
-        throw "Cannot load CA store for certificate validation.";
+        throw std::runtime_error("Cannot load CA store for certificate validation.");
     }
 
     if( server ) {
@@ -125,14 +125,13 @@ std::shared_ptr<SSL_CTX> generateSSLContext( bool server ) {
             } else {
                 dh_param = std::shared_ptr<DH>( DH_new(), DH_free );
                 logger::note( "Generating DH params" );
-                BN_GENCB cb;
-                cb.ver = 2;
-                cb.arg = 0;
-                cb.cb.cb_2 = gencb;
+                BN_GENCB *cb = BN_GENCB_new();
+               BN_GENCB_set(cb, gencb, NULL);
 
-                if( !DH_generate_parameters_ex( dh_param.get(), 2048, 5, &cb ) ) {
-                    throw "DH generation failed";
+                if( !DH_generate_parameters_ex( dh_param.get(), 2048, 5, cb ) ) {
+                    throw std::runtime_error("DH generation failed");
                 }
+               BN_GENCB_free(cb);
 
                 std::cout << std::endl;
                 paramfile = std::shared_ptr<FILE>( fopen( "dh_param.pem", "w" ), fclose );
@@ -144,7 +143,7 @@ std::shared_ptr<SSL_CTX> generateSSLContext( bool server ) {
         }
 
         if( !SSL_CTX_set_tmp_dh( ctx.get(), dh_param.get() ) ) {
-            throw "Cannot set tmp dh.";
+            throw std::runtime_error("Cannot set tmp dh.");
         }
     }
 
@@ -155,7 +154,7 @@ void setupSerial( std::shared_ptr<FILE> f ) {
     struct termios attr;
 
     if( tcgetattr( fileno( f.get() ), &attr ) ) {
-        throw "failed to get attrs";
+        throw std::runtime_error("failed to get attrs");
     }
 
     attr.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON );
@@ -168,7 +167,7 @@ void setupSerial( std::shared_ptr<FILE> f ) {
     cfsetospeed( &attr, B115200 );
 
     if( tcsetattr( fileno( f.get() ), TCSANOW, &attr ) ) {
-        throw "failed to get attrs";
+        throw std::runtime_error("failed to get attrs");
     }
 }
 
@@ -188,19 +187,34 @@ std::shared_ptr<BIO> openSerial( const std::string& name ) {
         } );
 }
 
+extern std::string crlPrefix;
+extern std::string crtPrefix;
+
 CAConfig::CAConfig( const std::string& name ) : path( "ca/" + name ), name( name ) {
     ca = loadX509FromFile( path + "/ca.crt" );
     caKey = loadPkeyFromFile( path + "/ca.key" );
-    ASN1_TIME* tm = X509_get_notBefore( ca );
-    notBefore = std::shared_ptr<ASN1_TIME>( tm, ASN1_TIME_free );
+    ASN1_TIME* tm = X509_get_notBefore( ca.get() );
+    auto ca0 = ca;
+    notBefore = std::shared_ptr<ASN1_TIME>( tm, [ca0](auto p){(void)p;} );
+    std::size_t pos = name.find("_");
+    if (pos == std::string::npos) {
+        throw new std::invalid_argument("ca name: " + name + " is malformed.");
+    }
+    std::size_t pos2 = name.find("_", pos + 1);
+    if (pos2 == std::string::npos) {
+        throw new std::invalid_argument("ca name: " + name + " is malformed.");
+    }
+    crlURL = crlPrefix + "/g2/" + name.substr(pos+1, pos2-pos - 1) + "/" + name.substr(0,pos) + "-" + name.substr(pos2+1) + ".crl";
+    crtURL = crtPrefix + "/g2/" + name.substr(pos+1, pos2-pos - 1) + "/" + name.substr(0,pos) + "-" + name.substr(pos2+1) + ".crt";
 }
 
 std::string timeToString( std::shared_ptr<ASN1_TIME> time ) {
     std::shared_ptr<ASN1_GENERALIZEDTIME> gtime( ASN1_TIME_to_generalizedtime( time.get(), 0 ) );
-    std::string strdate( ( char* ) ASN1_STRING_data( gtime.get() ), ASN1_STRING_length( gtime.get() ) );
+    std::string strdate( ( char* ) ASN1_STRING_get0_data( gtime.get() ), ASN1_STRING_length( gtime.get() ) );
 
+    logger::notef("openssl formatted me a date: %s", strdate);
     if( strdate[strdate.size() - 1] != 'Z' ) {
-        throw "Got invalid date?";
+        throw std::runtime_error("Got invalid date?");
     }
 
     return strdate.substr( 0, strdate.size() - 1 );
@@ -212,6 +226,6 @@ void extractTimes( std::shared_ptr<X509> target,  std::shared_ptr<SignedCertific
 }
 
 bool CAConfig::crlNeedsResign() {
-    std::shared_ptr<CRL> crl( new CRL( path + "/ca.crl" ) );
+    auto crl = std::make_shared<CRL>( path + "/ca.crl" );
     return crl->needsResign();
 }