From 347af49a5d70d33bcc1f5fec1573ef336b21642a Mon Sep 17 00:00:00 2001 From: "Simon V. Lejel" Date: Wed, 14 Feb 2024 21:41:01 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Write=20notes=20for=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/main.cc | 52 ++++++++++++++++++++++++++++++++++++++-------- src/server/main.cc | 1 + 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/client/main.cc b/src/client/main.cc index 05be09f..b5c3ea6 100644 --- a/src/client/main.cc +++ b/src/client/main.cc @@ -13,8 +13,28 @@ using std::cout, std::cin, 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() { + // Define a structure to contains the content of a message and the user who + // sent it struct Message { string content; string username; @@ -24,20 +44,33 @@ int main() { } }; - // Initialize OpenSSL - SSL_library_init(); - SSL_load_error_strings(); + // Initializes the SSL library + if (OpenSSL_version_num() < 0x10100000L) { + // Old version, deprecated as of version 1.1.0 + SSL_library_init(); - // Create new SSL context + // Loads human readable error strings + // Automatically initialized in newer versions + SSL_load_error_strings(); + } else { + // New version + // Initializes both SSL and crypto and is generally better + OPENSSL_init_ssl(0, nullptr); + } + + // Create new SSL context with client side connections and save it to a + // pointer SSL_CTX *ctx = SSL_CTX_new(TLS_client_method()); if (!ctx) { - cout << "Creation of SSL context failed" << endl; + cout << "Creation of SSL context failed: " << getCtxError() << endl; + return -1; } // Load the server's certificate into context - if (SSL_CTX_load_verify_locations( - ctx, "./ssl/server.crt", nullptr) <= 0) { - cout << "SSL load failed" << endl; + // Returns 1 on success, otherwise check error stack + if (SSL_CTX_load_verify_locations(ctx, "./ssl/server.crt", nullptr) <= 0) { + cout << "Certificate load failed: " << getCtxError() << endl; + return -1; } // Create SSL object @@ -81,7 +114,8 @@ int main() { cout << endl; message.username = "Client 1"; - SSL_write(ssl, message.toString().data(), strlen(message.toString().data())); + SSL_write(ssl, message.toString().data(), + strlen(message.toString().data())); } // Clean up diff --git a/src/server/main.cc b/src/server/main.cc index fca2726..f997153 100644 --- a/src/server/main.cc +++ b/src/server/main.cc @@ -57,6 +57,7 @@ int main() { // If it's null, then an error occurred if (!ctx) { cout << "Creation of SSL context failed: " << getCtxError() << endl; + return -1; } // Load certificate into the context