From a631bfac392e4c205596b2f1a0a0ae8ce4bf25c8 Mon Sep 17 00:00:00 2001 From: "Simon V. Lejel" Date: Mon, 12 Feb 2024 17:21:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Make=20method=20for=20getting=20?= =?UTF-8?q?OpenSSL=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/main.cc | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) 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;