]> WPIA git - cassiopeia.git/blobdiff - src/crypto/sslUtil.cpp
fix: Properly check for success to create the necessary directories
[cassiopeia.git] / src / crypto / sslUtil.cpp
index fd00e8fb85a94db8367299a6d52e676c5cd278bb..5855e6670437f230fc5cab63ebf351ec0b3f745c 100644 (file)
@@ -3,8 +3,11 @@
 #include <sys/types.h>
 #include <termios.h>
 #include <unistd.h>
+
 #include <iostream>
 
+#include "crypto/CRL.h"
+
 std::shared_ptr<int> ssl_lib_ref(
     new int( SSL_library_init() ),
     []( int* ref ) {
@@ -14,7 +17,7 @@ std::shared_ptr<int> ssl_lib_ref(
         CRYPTO_cleanup_all_ex_data();
     } );
 
-std::shared_ptr<X509> loadX509FromFile( std::string filename ) {
+std::shared_ptr<X509> loadX509FromFile( const std::string& filename ) {
     FILE* f = fopen( filename.c_str(), "r" );
 
     if( !f ) {
@@ -35,7 +38,7 @@ std::shared_ptr<X509> loadX509FromFile( std::string filename ) {
         } );
 }
 
-std::shared_ptr<EVP_PKEY> loadPkeyFromFile( std::string filename ) {
+std::shared_ptr<EVP_PKEY> loadPkeyFromFile( const std::string& filename ) {
     FILE* f = fopen( filename.c_str(), "r" );
 
     if( !f ) {
@@ -91,7 +94,9 @@ std::shared_ptr<SSL_CTX> generateSSLContext( bool server ) {
     SSL_CTX_set_verify( ctx.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_callback );
     SSL_CTX_use_certificate_file( ctx.get(), server ? "keys/signer_server.crt" : "keys/signer_client.crt", SSL_FILETYPE_PEM );
     SSL_CTX_use_PrivateKey_file( ctx.get(), server ? "keys/signer_server.key" : "keys/signer_client.key", SSL_FILETYPE_PEM );
-    SSL_CTX_load_verify_locations( ctx.get(), "keys/ca.crt", 0 );
+    if( 1 != SSL_CTX_load_verify_locations( ctx.get(), "keys/ca.crt", 0 ) ) {
+        throw "Cannot load CA store for certificate validation.";
+    }
 
     if( server ) {
         STACK_OF( X509_NAME ) *names = SSL_load_client_CA_file( "keys/env.crt" );
@@ -159,7 +164,7 @@ void setupSerial( FILE* f ) {
     }
 }
 
-std::shared_ptr<BIO> openSerial( const std::string name ) {
+std::shared_ptr<BIO> openSerial( const std::string& name ) {
     FILE* f = fopen( name.c_str(), "r+" );
 
     if( !f ) {
@@ -168,14 +173,34 @@ std::shared_ptr<BIO> openSerial( const std::string name ) {
     }
 
     setupSerial( f );
-
     std::shared_ptr<BIO> b( BIO_new_fd( fileno( f ), 0 ), BIO_free );
     return b;
 }
 
-CAConfig::CAConfig( std::string name ) {
-    this->name = name;
-    this->path = "ca/" + name;
+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 );
+}
+
+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() ) );
+
+    if( strdate[strdate.size() - 1] != 'Z' ) {
+        throw "Got invalid date?";
+    }
+
+    return strdate.substr( 0, strdate.size() - 1 );
+}
+
+void extractTimes( std::shared_ptr<X509> target,  std::shared_ptr<SignedCertificate> cert ) {
+    cert->before = timeToString( std::shared_ptr<ASN1_TIME>( X509_get_notBefore( target.get() ), ASN1_TIME_free ) );
+    cert->after = timeToString( std::shared_ptr<ASN1_TIME>( X509_get_notAfter( target.get() ), ASN1_TIME_free ) );
+}
+
+bool CAConfig::crlNeedsResign() {
+    std::shared_ptr<CRL> crl( new CRL( path + "/ca.crl" ) );
+    return crl->needsResign();
 }