This question concerns OpenSSL 1.1.0+. In the code example I use std::string_view which implies C++17. This is not required, anything over C++11 is fine, I was just too lazy to have const char* buf and std::size_t len as separate variables.
#include <string_view>
#include <openssl/err.h>
#include <openssl/ssl.h>
void startup()
{
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
ERR_load_crypto_strings();
}
void shutdown()
{
ERR_free_strings();
EVP_cleanup();
}
void thread_shutdown()
{
CRYPTO_cleanup_all_ex_data();
}
void run_per_thread()
{
// intial non SSL stuff
int sockfd = get_connected_socket();
std::string_view hostname = get_hostname();
std::string_view buffer = get_buffer();
// SSL context setup
auto ssl_ctx = SSL_CTX_new(TLS_client_method());
auto ssl_ctx_options = SSL_OP_SINGLE_DH_USE || SSL_OP_NO_SSLv3;
SSL_CTX_set_options(ssl_ctx, ssl_ctx_options);
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, nullptr);
// SSL client setup
auto ssl_client = SSL_new(ssl_ctx);
SSL_set_tlsext_host_name(ssl_client, hostname.data());
// connect and write
auto ssl_err = SSL_connect(ssl_client);
auto result = SSL_write(ssl_client, buf.data(), buf.size());
}
I have these four functions (the last one is more of a pseudo function). startup is run at the beginning of the program and shutdown at the end (both only run once each). thread_shutdown is run at the end of every thread (including the before shutdown in the main thread).
The run_per_thread function is a small example of how I might use SSL with a socket. The function could be run in multiple threads, however the local variables are never shared outside the scope of the function between threads.
Is the way that I'm currently using OpenSSL here thread safe? Or do I need to use CRYPTO locks? (the documentation wasn't clear enough for me). And if I do need to use CRYPTO locks, could you please provide a small example on how to do it?
I have been using these links as a reference guide when writing this:
How to properly uninitialize OpenSSL
https://curl.haxx.se/libcurl/c/threadsafe.html
https://www.openssl.org/docs/man1.1.0/man3/CRYPTO_THREAD_run_once.html#DESCRIPTION