Skip to content

Commit

Permalink
Add to test for testing TLS client & server
Browse files Browse the repository at this point in the history
  • Loading branch information
Iandiehard committed Dec 1, 2023
1 parent f90cf48 commit 2007bdf
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 51 deletions.
2 changes: 1 addition & 1 deletion diag-client-lib/appl/include/create_diagnostic_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DiagClient;
* Unique pointer to diag client object
* @implements DiagClientLib-Library-Support, DiagClientLib-ComParam-Settings
*/
std::unique_ptr<diag::client::DiagClient> CreateDiagnosticClient(std::string_view diag_client_config_path);
std::unique_ptr<DiagClient> CreateDiagnosticClient(std::string_view diag_client_config_path);

} // namespace client
} // namespace diag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CreateTcpServerSocket::CreateTcpServerSocket(std::string_view local_ip_address,
msg << "Tcp Socket Accepter created at "
<< "<" << local_ip_address << "," << local_port_num << ">";
});
tcp_accepter_->listen();
}

CreateTcpServerSocket::TcpServerConnection CreateTcpServerSocket::GetTcpServerConnection(
Expand Down
8 changes: 6 additions & 2 deletions diag-client-lib/lib/boost-support/socket/tcp/tls_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace socket {
namespace tcp {

TlsClientSocket::TlsClientSocket(std::string_view local_ip_address, std::uint16_t local_port_num,
TcpHandlerRead tcp_handler_read)
TcpHandlerRead tcp_handler_read, std::string_view ca_certification_path)
: local_ip_address_{local_ip_address},
local_port_num_{local_port_num},
io_service{},
Expand All @@ -27,12 +27,16 @@ TlsClientSocket::TlsClientSocket(std::string_view local_ip_address, std::uint16_
running_{false},
cond_var_{},
mutex_{},
thread_{},
tcp_handler_read_{std::move(tcp_handler_read)} {
// Set verification mode
tls_socket_.set_verify_mode(boost::asio::ssl::verify_peer);
// Set the verification callback
tls_socket_.set_verify_callback(
[](bool pre_verified, boost::asio::ssl::verify_context &ctx) noexcept -> bool { return true; });
// Load the root CA certificates
io_ssl_context_.load_verify_file(std::string{ca_certification_path});

// Start thread to receive messages
thread_ = std::thread([this]() {
std::unique_lock<std::mutex> lck(mutex_);
Expand Down Expand Up @@ -65,7 +69,7 @@ core_type::Result<void, TlsClientSocket::TlsErrorCode> TlsClientSocket::Open() {
// Open the socket
GetNativeTcpSocket().open(Tcp::v4(), ec);
if (ec.value() == boost::system::errc::success) {
// reuse address
// Re-use address
GetNativeTcpSocket().set_option(boost::asio::socket_base::reuse_address{true});
// Set socket to non blocking
GetNativeTcpSocket().non_blocking(false);
Expand Down
11 changes: 6 additions & 5 deletions diag-client-lib/lib/boost-support/socket/tcp/tls_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class TlsClientSocket final {
* @param[in] tcp_handler_read
* The handler to send received data to user
*/
TlsClientSocket(std::string_view local_ip_address, std::uint16_t local_port_num, TcpHandlerRead tcp_handler_read);
TlsClientSocket(std::string_view local_ip_address, std::uint16_t local_port_num, TcpHandlerRead tcp_handler_read,
std::string_view ca_certification_path);

/**
* @brief Destruct an instance of TcpClientSocket
Expand Down Expand Up @@ -162,14 +163,14 @@ class TlsClientSocket final {
std::condition_variable cond_var_;

/**
* @brief The thread itself
* @brief mutex to lock critical section
*/
std::thread thread_;
std::mutex mutex_;

/**
* @brief mutex to lock critical section
* @brief The thread itself
*/
std::mutex mutex_;
std::thread thread_;

/**
* @brief Store the handler
Expand Down
89 changes: 51 additions & 38 deletions diag-client-lib/lib/boost-support/socket/tcp/tls_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,21 @@ std::optional<TcpServerConnection> TlsServerSocket::GetTcpServerConnection(TcpHa
}

TcpServerConnection::TcpServerConnection(boost::asio::io_context &io_context, TcpHandlerRead tcp_handler_read)
: io_ssl_context_{boost::asio::ssl::context::tlsv12_client},
: io_ssl_context_{boost::asio::ssl::context::tlsv12_server},
tls_socket_{io_context, io_ssl_context_},
tcp_handler_read_{std::move(tcp_handler_read)} {}
tcp_handler_read_{std::move(tcp_handler_read)} {
io_ssl_context_.use_certificate_chain_file("../../../tools/openssl/DiagClientLib.crt");
io_ssl_context_.use_private_key_file("../../../tools/openssl/DiagClientLib.key", boost::asio::ssl::context::pem);
}

TcpServerConnection::TlsStream::lowest_layer_type &TcpServerConnection::GetSocket() {
return tls_socket_.lowest_layer();
}

core_type::Result<void, TcpServerConnection::TcpErrorCode> TcpServerConnection::Transmit(
TcpMessageConstPtr tcp_tx_message) {
core_type::Result<void, TcpErrorCode> result{TcpErrorCode::kGenericError};
TcpErrorCodeType ec{};
core_type::Result<void, TcpErrorCode> result{TcpErrorCode::kGenericError};

boost::asio::write(
tls_socket_,
Expand All @@ -88,47 +91,57 @@ core_type::Result<void, TcpServerConnection::TcpErrorCode> TcpServerConnection::
}

bool TcpServerConnection::ReceivedMessage() {
TcpErrorCodeType ec;
TcpErrorCodeType ec{};
bool connection_closed{false};

// create and reserve the buffer
TcpMessage::BufferType rx_buffer{};
rx_buffer.resize(kDoipheadrSize);
// start blocking read to read Header first
boost::asio::read(tls_socket_, boost::asio::buffer(&rx_buffer[0], kDoipheadrSize), ec);
// Check for error
// Perform TLS handshake
tls_socket_.handshake(boost::asio::ssl::stream_base::server, ec);

if (ec.value() == boost::system::errc::success) {
// read the next bytes to read
std::uint32_t const read_next_bytes = [&rx_buffer]() noexcept -> std::uint32_t {
return static_cast<std::uint32_t>((static_cast<std::uint32_t>(rx_buffer[4u] << 24u) & 0xFF000000) |
(static_cast<std::uint32_t>(rx_buffer[5u] << 16u) & 0x00FF0000) |
(static_cast<std::uint32_t>(rx_buffer[6u] << 8u) & 0x0000FF00) |
(static_cast<std::uint32_t>(rx_buffer[7u] & 0x000000FF)));
}();
// reserve the buffer
rx_buffer.resize(kDoipheadrSize + std::size_t(read_next_bytes));
boost::asio::read(tls_socket_, boost::asio::buffer(&rx_buffer[kDoipheadrSize], read_next_bytes), ec);

// all message received, transfer to upper layer
Tcp::endpoint endpoint_{GetSocket().remote_endpoint()};
TcpMessagePtr tcp_rx_message{
std::make_unique<TcpMessage>(endpoint_.address().to_string(), endpoint_.port(), std::move(rx_buffer))};
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug(
__FILE__, __LINE__, __func__, [endpoint_](std::stringstream &msg) {
msg << "Tcp Message received from "
<< "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">";
});
// send data to upper layer
tcp_handler_read_(std::move(tcp_rx_message));
} else if (ec.value() == boost::asio::error::eof) {
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug(
__FILE__, __LINE__, __func__,
[ec](std::stringstream &msg) { msg << "Remote Disconnected with: " << ec.message(); });
connection_closed = true;
// Create and reserve the buffer
TcpMessage::BufferType rx_buffer{};
rx_buffer.resize(kDoipheadrSize);
// Start blocking read to read Header first
boost::asio::read(tls_socket_, boost::asio::buffer(&rx_buffer[0], kDoipheadrSize), ec);
// Check for error
if (ec.value() == boost::system::errc::success) {
// Read the next bytes to read
std::uint32_t const read_next_bytes = [&rx_buffer]() noexcept -> std::uint32_t {
return static_cast<std::uint32_t>((static_cast<std::uint32_t>(rx_buffer[4u] << 24u) & 0xFF000000) |
(static_cast<std::uint32_t>(rx_buffer[5u] << 16u) & 0x00FF0000) |
(static_cast<std::uint32_t>(rx_buffer[6u] << 8u) & 0x0000FF00) |
(static_cast<std::uint32_t>(rx_buffer[7u] & 0x000000FF)));
}();
// reserve the buffer
rx_buffer.resize(kDoipheadrSize + std::size_t(read_next_bytes));
boost::asio::read(tls_socket_, boost::asio::buffer(&rx_buffer[kDoipheadrSize], read_next_bytes), ec);

// all message received, transfer to upper layer
Tcp::endpoint endpoint_{GetSocket().remote_endpoint()};
TcpMessagePtr tcp_rx_message{
std::make_unique<TcpMessage>(endpoint_.address().to_string(), endpoint_.port(), std::move(rx_buffer))};
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug(
__FILE__, __LINE__, __func__, [endpoint_](std::stringstream &msg) {
msg << "Tcp Message received from "
<< "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">";
});
// send data to upper layer
tcp_handler_read_(std::move(tcp_rx_message));
} else if (ec.value() == boost::asio::error::eof) {
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug(
__FILE__, __LINE__, __func__,
[ec](std::stringstream &msg) { msg << "Remote Disconnected with: " << ec.message(); });
connection_closed = true;
} else {
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogError(
__FILE__, __LINE__, __func__,
[ec](std::stringstream &msg) { msg << "Remote Disconnected with undefined error: " << ec.message(); });
connection_closed = true;
}
} else {
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogError(
__FILE__, __LINE__, __func__,
[ec](std::stringstream &msg) { msg << "Remote Disconnected with undefined error: " << ec.message(); });
[ec](std::stringstream &msg) { msg << "Tls server handshake with host failed with error: " << ec.message(); });
connection_closed = true;
}
return connection_closed;
Expand Down
Loading

0 comments on commit 2007bdf

Please sign in to comment.