Get the access to native handle behind Asio's ssl_stream.
struct openssl_free_t {
void operator()(void * ptr) const noexcept
{
OPENSSL_free( ptr );
}
};
std::string extract_user_name_from_client_certificate(
{
std::unique_ptr<X509, decltype(&X509_free)> client_cert{
SSL_get_peer_certificate(nhandle),
X509_free
};
if( !client_cert )
throw std::runtime_error( "Unable to get client certificate!" );
X509_NAME * subject_name = X509_get_subject_name( client_cert.get() );
int last_pos = -1;
last_pos = X509_NAME_get_index_by_NID(
subject_name,
NID_commonName,
last_pos );
if( last_pos < 0 )
throw std::runtime_error( "commonName is not found!" );
unsigned char * common_name_utf8{};
if( ASN1_STRING_to_UTF8(
&common_name_utf8,
X509_NAME_ENTRY_get_data(
X509_NAME_get_entry( subject_name, last_pos ) ) ) < 0 )
throw std::runtime_error( "ASN1_STRING_to_UTF8 failed!" );
std::unique_ptr<unsigned char, openssl_free_t > common_name_deleter{
common_name_utf8
};
return { reinterpret_cast<char *>(common_name_utf8) };
}
Accessor to TLS-specific information related to a connection.
RESTINIO_NODISCARD auto native_handle() const noexcept
Get the access to native handle behind Asio's ssl_stream.