diff --git a/src/server/main.cc b/src/server/main.cc index 992effc..0d5ef6a 100644 --- a/src/server/main.cc +++ b/src/server/main.cc @@ -13,6 +13,24 @@ using std::cout, std::endl, std::string; * Remember to build with the flags "-L/usr/lib -lssl -lcrypto" */ +/** + * Gets the latest error from OpenSSL and returns it as an string. + * This makes it easier to log any errors that OpenSSL throws. + */ +string getCtxError() { + + // Get the latest error code + unsigned long errCode = ERR_get_error(); + + // Define a buffer of 128 bytes to hold the error message + char errBuffer[128]; + + // Get a human readable description of the error from OpenSSL and write it + // to the buffer + ERR_error_string_n(errCode, errBuffer, sizeof(errBuffer)); + return errBuffer; +} + int main() { SSL_library_init(); @@ -26,19 +44,13 @@ int main() { // Load certificate if (SSL_CTX_use_certificate_file(ctx, "./ssl/server.crt", SSL_FILETYPE_PEM) <= 0) { - unsigned long errCode = ERR_get_error(); - char errBuffer[128]; - ERR_error_string_n(errCode, errBuffer, sizeof(errBuffer)); - cout << "Certificate load failed: " << errBuffer << endl; + cout << "Certificate load failed: " << getCtxError() << endl; } // Load certificate private key if (SSL_CTX_use_PrivateKey_file(ctx, "./ssl/server.key", SSL_FILETYPE_PEM) <= 0) { - unsigned long errCode = ERR_get_error(); - char errBuffer[128]; - ERR_error_string_n(errCode, errBuffer, sizeof(errBuffer)); - cout << "Private key load failed: " << errBuffer << endl; + cout << "Private key load failed: " << getCtxError() << endl; } cout << "Initializing server" << endl;