🚧 Write notes for client
This commit is contained in:
parent
422d0b0bb0
commit
347af49a5d
2 changed files with 44 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue