Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/dpp/ssl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ wrapped_ssl_ctx* generate_ssl_context(uint16_t port = 0, const std::string &priv
*/
void release_ssl_context(uint16_t port = 0);

};
};
20 changes: 18 additions & 2 deletions include/dpp/wrapped_ssl_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
************************************************************************************/
#include <dpp/exception.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string>
#pragma once

namespace dpp::detail {
Expand All @@ -36,14 +38,28 @@ struct wrapped_ssl_ctx {
*/
SSL_CTX *context{nullptr};

/**
* @brief Get last SSL error message
* @return SSL error message
*/
std::string get_ssl_error() {
unsigned long error_code = ERR_get_error();
if (error_code == 0) {
return "No error";
}
char error_buffer[1024]{0};
ERR_error_string_n(error_code, error_buffer, sizeof(error_buffer));
return std::string(error_buffer);
}

/**
* @brief Create a wrapped SSL context
* @param is_server true to create a server context, false to create a client context
* @throws dpp::connection_exception if context could not be created
*/
explicit wrapped_ssl_ctx(bool is_server = false) : context(SSL_CTX_new(is_server ? TLS_server_method() : TLS_client_method())) {
if (context == nullptr) {
throw dpp::connection_exception(err_ssl_context, "Failed to create SSL client context!");
throw dpp::connection_exception(err_ssl_context, "Failed to create SSL client context: " + get_ssl_error());
}
}

Expand Down Expand Up @@ -87,4 +103,4 @@ struct wrapped_ssl_ctx {
}
};

};
};
2 changes: 1 addition & 1 deletion src/dpp/ssl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ wrapped_ssl_ctx* generate_ssl_context(uint16_t port, const std::string &private_
return contexts.back().second.get();
}

}
}
Loading