]> WPIA git - cassiopeia.git/blobdiff - src/sslUtil.cpp
add: Initial code to implement revocation
[cassiopeia.git] / src / sslUtil.cpp
index 2a9de5999a82f0c49f6681b38221192bc48ddc9d..fd00e8fb85a94db8367299a6d52e676c5cd278bb 100644 (file)
@@ -14,6 +14,48 @@ std::shared_ptr<int> ssl_lib_ref(
         CRYPTO_cleanup_all_ex_data();
     } );
 
+std::shared_ptr<X509> loadX509FromFile( std::string filename ) {
+    FILE* f = fopen( filename.c_str(), "r" );
+
+    if( !f ) {
+        return std::shared_ptr<X509>();
+    }
+
+    X509* key = PEM_read_X509( f, NULL, NULL, 0 );
+    fclose( f );
+
+    if( !key ) {
+        return std::shared_ptr<X509>();
+    }
+
+    return std::shared_ptr<X509>(
+        key,
+        []( X509 * ref ) {
+            X509_free( ref );
+        } );
+}
+
+std::shared_ptr<EVP_PKEY> loadPkeyFromFile( std::string filename ) {
+    FILE* f = fopen( filename.c_str(), "r" );
+
+    if( !f ) {
+        return std::shared_ptr<EVP_PKEY>();
+    }
+
+    EVP_PKEY* key = PEM_read_PrivateKey( f, NULL, NULL, 0 );
+    fclose( f );
+
+    if( !key ) {
+        return std::shared_ptr<EVP_PKEY>();
+    }
+
+    return std::shared_ptr<EVP_PKEY>(
+        key,
+        []( EVP_PKEY * ref ) {
+            EVP_PKEY_free( ref );
+        } );
+}
+
 int gencb( int a, int b, BN_GENCB* g ) {
     ( void ) a;
     ( void ) b;
@@ -103,8 +145,7 @@ void setupSerial( FILE* f ) {
         throw "failed to get attrs";
     }
 
-    attr.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP
-                       | INLCR | IGNCR | ICRNL | IXON );
+    attr.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON );
     attr.c_oflag &= ~OPOST;
     attr.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN );
     attr.c_cflag &= ~( CSIZE | PARENB );
@@ -118,8 +159,8 @@ void setupSerial( FILE* f ) {
     }
 }
 
-std::shared_ptr<BIO> openSerial( const char* name ) {
-    FILE* f = fopen( name, "r+" );
+std::shared_ptr<BIO> openSerial( const std::string name ) {
+    FILE* f = fopen( name.c_str(), "r+" );
 
     if( !f ) {
         std::cout << "Opening serial device failed" << std::endl;
@@ -131,3 +172,10 @@ std::shared_ptr<BIO> openSerial( const char* name ) {
     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;
+    ca = loadX509FromFile( path + "/ca.crt" );
+    caKey = loadPkeyFromFile( path + "/ca.key" );
+}