Archived
1
0
Fork 0

🚸 Make method for getting OpenSSL errors

This commit is contained in:
Simon V. Lejel 2024-02-12 17:21:38 +01:00
parent cba0ae1da3
commit a631bfac39
Signed by: sl
GPG key ID: 6544A0430A2CFFAD

View file

@ -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;