X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=src%2FsslUtil.cpp;h=fd00e8fb85a94db8367299a6d52e676c5cd278bb;hb=890efd9eb1d32033fe3afd088838bde707f3a2bb;hp=2a9de5999a82f0c49f6681b38221192bc48ddc9d;hpb=d289583508396f1ae84580febc8faa913fd20935;p=cassiopeia.git diff --git a/src/sslUtil.cpp b/src/sslUtil.cpp index 2a9de59..fd00e8f 100644 --- a/src/sslUtil.cpp +++ b/src/sslUtil.cpp @@ -14,6 +14,48 @@ std::shared_ptr ssl_lib_ref( CRYPTO_cleanup_all_ex_data(); } ); +std::shared_ptr loadX509FromFile( std::string filename ) { + FILE* f = fopen( filename.c_str(), "r" ); + + if( !f ) { + return std::shared_ptr(); + } + + X509* key = PEM_read_X509( f, NULL, NULL, 0 ); + fclose( f ); + + if( !key ) { + return std::shared_ptr(); + } + + return std::shared_ptr( + key, + []( X509 * ref ) { + X509_free( ref ); + } ); +} + +std::shared_ptr loadPkeyFromFile( std::string filename ) { + FILE* f = fopen( filename.c_str(), "r" ); + + if( !f ) { + return std::shared_ptr(); + } + + EVP_PKEY* key = PEM_read_PrivateKey( f, NULL, NULL, 0 ); + fclose( f ); + + if( !key ) { + return std::shared_ptr(); + } + + return std::shared_ptr( + 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 openSerial( const char* name ) { - FILE* f = fopen( name, "r+" ); +std::shared_ptr 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 openSerial( const char* name ) { std::shared_ptr 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" ); +}