Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
ssl.cppm
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
25module;
26
27// =============================================================================
28// Global Module Fragment - Standard Library Headers
29// =============================================================================
30#include <atomic>
31#include <chrono>
32#include <filesystem>
33#include <functional>
34#include <memory>
35#include <mutex>
36#include <optional>
37#include <span>
38#include <string>
39#include <string_view>
40#include <vector>
41
42// Third-party headers
43#include <asio.hpp>
44#include <asio/ssl.hpp>
45
46export module kcenon.network:ssl;
47
48import :core;
49
50// =============================================================================
51// SSL Configuration Types
52// =============================================================================
53
54export namespace kcenon::network::core {
55
59enum class ssl_protocol {
60 tls_1_2,
61 tls_1_3,
62 dtls_1_2,
64};
65
69enum class ssl_verify_mode {
70 none,
71 peer,
74};
75
79struct ssl_config {
81 std::filesystem::path certificate_path;
82
84 std::filesystem::path private_key_path;
85
87 std::filesystem::path ca_certificate_path;
88
91
94
97
99 std::string server_name;
100
102 std::string cipher_list;
103
106
109};
110
111// =============================================================================
112// Secure TCP Client
113// =============================================================================
114
145public:
151 explicit secure_messaging_client(std::string_view client_id, const ssl_config& config = {});
152
156 virtual ~secure_messaging_client() noexcept;
157
158 // Non-copyable, movable
163
170 bool start_client(std::string_view host, uint16_t port);
171
176
182 bool wait_for_stop(std::chrono::milliseconds timeout = std::chrono::milliseconds(5000));
183
189 bool send_packet(std::span<const uint8_t> data);
190
196 bool send_packet(std::string_view data);
197
202 bool is_connected() const noexcept;
203
208 bool is_running() const noexcept;
209
214 connection_state get_state() const noexcept;
215
220 std::string_view client_id() const noexcept;
221
227
233
239
245
250 std::optional<std::string> get_peer_certificate_subject() const;
251
256 std::string get_protocol_version() const;
257
262 std::string get_cipher_suite() const;
263
264private:
265 struct impl;
266 std::unique_ptr<impl> pimpl_;
267};
268
269// =============================================================================
270// Secure TCP Server
271// =============================================================================
272
303public:
307 using client_connected_callback = std::function<void(const std::string& session_id)>;
308
312 using client_disconnected_callback = std::function<void(const std::string& session_id)>;
313
317 using client_data_callback = std::function<void(
318 const std::string& session_id,
319 std::span<const uint8_t> data)>;
320
326 explicit secure_messaging_server(std::string_view server_id, const ssl_config& config);
327
331 virtual ~secure_messaging_server() noexcept;
332
333 // Non-copyable, movable
338
345 bool start_server(uint16_t port, int backlog = 128);
346
354 bool start_server(std::string_view address, uint16_t port, int backlog = 128);
355
359 void stop_server();
360
366 bool wait_for_stop(std::chrono::milliseconds timeout = std::chrono::milliseconds(5000));
367
374 bool send_to(std::string_view session_id, std::span<const uint8_t> data);
375
380 void broadcast(std::span<const uint8_t> data);
381
386 void disconnect(std::string_view session_id);
387
392 bool is_running() const noexcept;
393
398 size_t client_count() const noexcept;
399
404 std::string_view server_id() const noexcept;
405
410 void set_client_connected_callback(client_connected_callback callback);
411
416 void set_client_disconnected_callback(client_disconnected_callback callback);
417
422 void set_client_data_callback(client_data_callback callback);
423
429
435 std::optional<std::string> get_peer_certificate_subject(std::string_view session_id) const;
436
437private:
438 struct impl;
439 std::unique_ptr<impl> pimpl_;
440};
441
442// =============================================================================
443// Secure UDP Client (DTLS)
444// =============================================================================
445
472public:
478 explicit secure_messaging_udp_client(std::string_view client_id, const ssl_config& config = {});
479
484
485 // Non-copyable, movable
490
497 bool start_client(std::string_view remote_host, uint16_t remote_port);
498
503
509 bool send_packet(std::span<const uint8_t> data);
510
515 bool is_connected() const noexcept;
516
521 bool is_running() const noexcept;
522
527 std::string_view client_id() const noexcept;
528
534
540
546
552
553private:
554 struct impl;
555 std::unique_ptr<impl> pimpl_;
556};
557
558// =============================================================================
559// Secure UDP Server (DTLS)
560// =============================================================================
561
584public:
588 using dtls_data_callback = std::function<void(
589 const std::string& session_id,
590 std::span<const uint8_t> data)>;
591
597 explicit secure_messaging_udp_server(std::string_view server_id, const ssl_config& config);
598
603
604 // Non-copyable, movable
609
615 bool start_server(uint16_t port);
616
623 bool start_server(std::string_view address, uint16_t port);
624
628 void stop_server();
629
636 bool send_to(std::string_view session_id, std::span<const uint8_t> data);
637
642 bool is_running() const noexcept;
643
648 size_t session_count() const noexcept;
649
654 std::string_view server_id() const noexcept;
655
660 void set_session_connected_callback(std::function<void(const std::string&)> callback);
661
666 void set_session_disconnected_callback(std::function<void(const std::string&)> callback);
667
673
679
680private:
681 struct impl;
682 std::unique_ptr<impl> pimpl_;
683};
684
685} // namespace kcenon::network::core
A secure client for establishing TLS/SSL encrypted TCP connections to a server.
Definition ssl.cppm:144
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends data to the connected server.
auto client_id() const -> const std::string &
Returns the client identifier.
connection_state get_state() const noexcept
Get the current connection state.
auto stop_client() -> VoidResult
Stops the client and disconnects from the server.
std::string get_cipher_suite() const
Get the negotiated cipher suite.
auto wait_for_stop() -> void
Blocks until stop_client() is called.
void set_disconnection_callback(disconnection_callback callback)
Set disconnection callback.
void set_connection_callback(connection_callback callback)
Set connection callback (called after TLS handshake)
secure_messaging_client(std::string_view client_id, const ssl_config &config={})
Construct a secure client with SSL configuration.
auto is_running() const noexcept -> bool
Checks if the client is currently running.
std::string get_protocol_version() const
Get the negotiated TLS protocol version.
auto set_error_callback(error_callback_t callback) -> void
Sets the callback for errors.
virtual ~secure_messaging_client() noexcept
Destructor.
auto start_client(std::string_view host, unsigned short port) -> VoidResult
Starts the client and connects to the specified host and port.
void set_data_callback(data_callback callback)
Set data received callback.
auto is_connected() const noexcept -> bool
Checks if the client is connected to the server.
std::optional< std::string > get_peer_certificate_subject() const
Get peer certificate information.
A secure server class that manages incoming TLS/SSL encrypted TCP connections, creating secure_sessio...
Definition ssl.cppm:302
secure_messaging_server(std::string_view server_id, const ssl_config &config)
Construct a secure server with SSL configuration.
std::function< void(const std::string &session_id)> client_disconnected_callback
Callback type for client disconnection events.
Definition ssl.cppm:312
std::function< void( const std::string &session_id, std::span< const uint8_t > data)> client_data_callback
Callback type for client data events.
Definition ssl.cppm:317
std::function< void(const std::string &session_id)> client_connected_callback
Callback type for secure client connection events.
Definition ssl.cppm:307
virtual ~secure_messaging_server() noexcept
Destructor.
A secure UDP client using DTLS (Datagram TLS) for encrypted communication.
Definition ssl.cppm:471
virtual ~secure_messaging_udp_client() noexcept
Destructor.
secure_messaging_udp_client(std::string_view client_id, const ssl_config &config={})
Construct a secure UDP client with DTLS configuration.
A secure UDP server using DTLS (Datagram TLS) for encrypted communication.
Definition ssl.cppm:583
secure_messaging_udp_server(std::string_view server_id, const ssl_config &config)
Construct a secure UDP server with DTLS configuration.
std::function< void( const std::string &session_id, std::span< const uint8_t > data)> dtls_data_callback
Callback type for DTLS data reception.
Definition ssl.cppm:588
virtual ~secure_messaging_udp_server() noexcept
Destructor.
ssl_protocol
SSL/TLS protocol version enumeration.
Definition ssl.cppm:59
@ tls_1_3
TLS 1.3 (recommended)
std::function< void()> disconnection_callback
Definition core.cppm:411
std::function< void()> connection_callback
Callback type aliases for messaging.
Definition core.cppm:410
ssl_verify_mode
SSL verification mode.
Definition ssl.cppm:69
@ client_once
Request client certificate once.
@ fail_if_no_peer_cert
Fail if no peer certificate.
@ peer
Verify peer certificate.
std::function< void(const std::string &)> error_callback
Definition core.cppm:412
connection_state
Connection state enumeration.
Definition core.cppm:385
std::function< void(std::span< const uint8_t >)> data_callback
Definition core.cppm:413
SSL/TLS configuration structure.
Definition ssl.cppm:79
ssl_verify_mode verify_mode
Verification mode.
Definition ssl.cppm:96
std::filesystem::path private_key_path
Path to private key file (PEM format)
Definition ssl.cppm:84
std::string server_name
Server name for SNI (Server Name Indication)
Definition ssl.cppm:99
bool enable_ocsp_stapling
Enable OCSP stapling.
Definition ssl.cppm:108
bool enable_session_resumption
Enable session resumption.
Definition ssl.cppm:105
std::filesystem::path certificate_path
Path to certificate file (PEM format)
Definition ssl.cppm:81
std::string cipher_list
Cipher list (OpenSSL format, empty for default)
Definition ssl.cppm:102
ssl_protocol protocol
SSL protocol version.
Definition ssl.cppm:93
std::string private_key_password
Password for encrypted private key (empty if not encrypted)
Definition ssl.cppm:90
std::filesystem::path ca_certificate_path
Path to CA certificate file for verification (PEM format)
Definition ssl.cppm:87