qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1
24
25#include <filesystem>
26#include <openssl/ssl.h>
27#include "../socket.h"
28
29#ifndef QB_IO_TCP_SSL_SOCKET_H_
30#define QB_IO_TCP_SSL_SOCKET_H_
31
32namespace qb::io::ssl {
33
41 std::string subject;
42 std::string issuer;
43 int64_t version;
44 std::string serial_number;
45 int64_t not_before;
46 int64_t not_after;
47 std::string signature_algorithm;
48 std::vector<std::string> subject_alternative_names;
49};
50
60
68SSL_CTX *create_client_context(const SSL_METHOD *method);
69
79SSL_CTX *create_server_context(const SSL_METHOD *method, std::filesystem::path cert_path,
80 std::filesystem::path key_path);
81
89bool load_ca_certificates(SSL_CTX *ctx, const std::string &ca_file_path);
90
99bool load_ca_directory(SSL_CTX *ctx, const std::string &ca_dir_path);
100
108bool set_cipher_list(SSL_CTX *ctx, const std::string &ciphers);
109
117bool set_ciphersuites_tls13(SSL_CTX *ctx, const std::string &ciphersuites);
118
127bool set_tls_protocol_versions(SSL_CTX *ctx, int min_version, int max_version);
128
139bool configure_mtls_server_context(SSL_CTX *ctx, const std::string &client_ca_file_path, int verification_mode = SSL_VERIFY_PEER);
140
149bool configure_client_certificate(SSL_CTX *ctx, const std::string &client_cert_path, const std::string &client_key_path);
150
158bool set_alpn_protos_client(SSL_CTX *ctx, const std::vector<std::string>& protocols);
159
169bool set_alpn_selection_callback_server(SSL_CTX *ctx, SSL_CTX_alpn_select_cb_func callback, void *arg);
170
181bool enable_server_session_caching(SSL_CTX *ctx, long cache_size);
182
191
203bool set_custom_verify_callback(SSL_CTX *ctx, int (*callback)(int, X509_STORE_CTX *), int verification_mode);
204
215bool set_ocsp_stapling_client_callback(SSL_CTX *ctx, int (*callback)(SSL *s, void *arg), void *arg);
216
227bool set_ocsp_stapling_responder_server(SSL_CTX *ctx, int (*callback)(SSL *s, void *arg), void *arg);
228
239bool set_sni_hostname_selection_callback_server(SSL_CTX *ctx, int (*callback)(SSL *s, int *al, void *arg), void *arg);
240
249bool set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func callback);
250
259bool configure_dh_parameters_server(SSL_CTX* ctx, const std::string& dh_param_file_path);
260
270bool configure_ecdh_curves_server(SSL_CTX* ctx, const std::string& curve_names_list);
271
279struct Session {
280 SSL_SESSION *_session_handle = nullptr;
281 // Add any other metadata if needed, e.g., creation time, peer identifier
282
284 [[nodiscard]] bool is_valid() const { return _session_handle != nullptr; }
285};
286
292void free_session(Session& session);
293
304
305} // namespace qb::io::ssl
306namespace qb::io::tcp::ssl {
307
308// class listener;
309
320class QB_API socket : public tcp::socket {
321 std::unique_ptr<SSL, void (*)(SSL *)> _ssl_handle;
322 bool _connected;
323
331 int handCheck() noexcept;
332
341 int connect_in(int af, std::string const &host, uint16_t port) noexcept;
342
352 int n_connect_in(int af, std::string const &host, uint16_t port) noexcept;
353
354public:
356 constexpr static bool is_secure() noexcept { return true; }
362 ~socket() noexcept;
363
368 socket() noexcept;
369
378 socket(SSL *ssl_ptr, tcp::socket &sock) noexcept;
379
383 socket(socket const &rhs) = delete;
384
388 socket(socket &&rhs) = default;
389
394 socket &operator=(socket &&rhs) = default;
395
403 void init(SSL *handle = nullptr) noexcept;
404
412 int connect(endpoint const &ep, std::string const &hostname = "") noexcept;
413
419 int connect(uri const &u) noexcept;
420
427 int connect_v4(std::string const &host, uint16_t port) noexcept;
428
435 int connect_v6(std::string const &host, uint16_t port) noexcept;
436
443 int connect_un(std::string const &path) noexcept;
444
454 int n_connect(qb::io::endpoint const &ep, std::string const &hostname = "") noexcept;
455
465 int connected() noexcept;
466
472 int n_connect(uri const &u) noexcept;
473
480 int n_connect_v4(std::string const &host, uint16_t port) noexcept;
481
488 int n_connect_v6(std::string const &host, uint16_t port) noexcept;
489
495 int n_connect_un(std::string const &path) noexcept;
496
502 int disconnect() noexcept;
503
513 int read(void *data, std::size_t size) noexcept;
514
524 int write(const void *data, std::size_t size) noexcept;
525
531 [[nodiscard]] SSL *ssl_handle() const noexcept;
532
537 qb::io::ssl::Certificate get_peer_certificate_details() const noexcept;
538
543 std::string get_negotiated_cipher_suite() const noexcept;
544
549 std::string get_negotiated_tls_version() const noexcept;
550
555 std::string get_alpn_selected_protocol() const noexcept;
556
562 std::string get_last_ssl_error_string() const noexcept;
563
572
581 bool request_ocsp_stapling(bool enable = true) noexcept;
582
590 std::vector<qb::io::ssl::Certificate> get_peer_certificate_chain() const noexcept;
591
602 qb::io::ssl::Session get_session() const noexcept;
603
613 bool set_session(qb::io::ssl::Session& session) noexcept;
614
625
633 bool set_sni_hostname(const std::string& hostname) noexcept;
634
641 bool set_alpn_protocols(const std::vector<std::string>& protocols) noexcept;
642
651 bool set_verify_callback(int (*callback)(int, X509_STORE_CTX *), int verification_mode) noexcept;
652
659 bool set_verify_depth(int depth) noexcept;
660
661 inline int do_handshake() noexcept {
662 return handCheck();
663 }
664
665private:
666 // friend class ssl::listener; // If listener needs to call private methods for accept
667};
668
669} // namespace qb::io::tcp::ssl
670
671#endif // QB_IO_TCP_SSL_SOCKET_H_
Class implementing TCP socket functionality for reliable, stream-oriented communication.
Definition socket.h:43
int n_connect_v6(std::string const &host, uint16_t port) noexcept
Initiate a non-blocking SSL/TLS connection to an IPv6 server.
std::string get_alpn_selected_protocol() const noexcept
Get the ALPN protocol selected by the peer (typically for clients) or by this endpoint (for servers).
bool set_verify_callback(int(*callback)(int, X509_STORE_CTX *), int verification_mode) noexcept
Set a custom X.509 certificate verification callback and mode for this SSL connection.
bool request_client_post_handshake_auth() noexcept
Request Post-Handshake Authentication from the server (client-side, TLS 1.3+).
std::string get_last_ssl_error_string() const noexcept
Get the last OpenSSL error string for the current SSL handle.
int disconnect() noexcept
Gracefully shut down the SSL/TLS connection and close the underlying socket.
int connect(endpoint const &ep, std::string const &hostname="") noexcept
Establish a blocking SSL/TLS connection to a remote endpoint.
int connected() noexcept
Finalizes a non-blocking SSL connection after the underlying TCP socket is connected.
bool set_sni_hostname(const std::string &hostname) noexcept
Set the Server Name Indication (SNI) hostname for this SSL connection.
~socket() noexcept
Destructor.
bool request_ocsp_stapling(bool enable=true) noexcept
Request OCSP stapling from the server for this connection (client-side).
int write(const void *data, std::size_t size) noexcept
Write data to be encrypted and sent over the SSL/TLS socket.
int n_connect_v4(std::string const &host, uint16_t port) noexcept
Initiate a non-blocking SSL/TLS connection to an IPv4 server.
static constexpr bool is_secure() noexcept
Indicates that this socket implementation is secure.
Definition socket.h:356
SSL * ssl_handle() const noexcept
Get the underlying OpenSSL SSL handle.
int n_connect_un(std::string const &path) noexcept
Initiate a non-blocking SSL/TLS connection over a Unix domain socket.
std::vector< qb::io::ssl::Certificate > get_peer_certificate_chain() const noexcept
Get the peer's full certificate chain.
bool disable_session_resumption() noexcept
Disable SSL/TLS session resumption for this specific connection (client-side).
int read(void *data, std::size_t size) noexcept
Read decrypted data from the secure SSL/TLS socket.
int connect_v6(std::string const &host, uint16_t port) noexcept
Establish a blocking SSL/TLS connection to an IPv6 server.
void init(SSL *handle=nullptr) noexcept
Initialize the SSL socket with an OpenSSL SSL handle.
int connect_un(std::string const &path) noexcept
Establish a blocking SSL/TLS connection over a Unix domain socket (conceptual, as SSL is typically ov...
qb::io::ssl::Certificate get_peer_certificate_details() const noexcept
Get details of the peer's certificate, if available.
int connect_v4(std::string const &host, uint16_t port) noexcept
Establish a blocking SSL/TLS connection to an IPv4 server.
qb::io::ssl::Session get_session() const noexcept
Retrieves the current SSL session from this connection.
std::string get_negotiated_cipher_suite() const noexcept
Get the negotiated cipher suite string.
int n_connect(qb::io::endpoint const &ep, std::string const &hostname="") noexcept
Initiate a non-blocking SSL/TLS connection to a remote endpoint.
socket() noexcept
Default constructor.
bool set_session(qb::io::ssl::Session &session) noexcept
Sets an SSL session to be used for resumption on this connection (client-side).
bool set_alpn_protocols(const std::vector< std::string > &protocols) noexcept
Set the ALPN protocols to offer for this specific SSL connection (client-side).
std::string get_negotiated_tls_version() const noexcept
Get the negotiated TLS protocol version string.
bool set_verify_depth(int depth) noexcept
Set the maximum verification depth for the peer certificate chain for this SSL connection.
Class for parsing, manipulating, and representing URIs.
Definition uri.h:181
bool disable_client_session_cache(SSL_CTX *ctx)
Disable client-side SSL session caching for an SSL_CTX.
SSL_CTX * create_client_context(const SSL_METHOD *method)
Create an SSL context (SSL_CTX) configured for client-side SSL/TLS operations.
bool set_ocsp_stapling_client_callback(SSL_CTX *ctx, int(*callback)(SSL *s, void *arg), void *arg)
Set a callback for the client to handle stapled OCSP responses from the server.
bool enable_post_handshake_auth_server(SSL_CTX *ctx)
Enable server-side support for TLS 1.3 Post-Handshake Authentication (PHA).
bool configure_dh_parameters_server(SSL_CTX *ctx, const std::string &dh_param_file_path)
Configure Diffie-Hellman parameters for a server SSL_CTX.
bool set_sni_hostname_selection_callback_server(SSL_CTX *ctx, int(*callback)(SSL *s, int *al, void *arg), void *arg)
Set a callback for server-side SNI (Server Name Indication) handling.
bool set_custom_verify_callback(SSL_CTX *ctx, int(*callback)(int, X509_STORE_CTX *), int verification_mode)
Set a custom callback for X.509 certificate verification.
bool configure_ecdh_curves_server(SSL_CTX *ctx, const std::string &curve_names_list)
Configure preferred ECDH curves for a server SSL_CTX.
bool set_tls_protocol_versions(SSL_CTX *ctx, int min_version, int max_version)
Set the minimum and maximum TLS protocol versions.
bool load_ca_directory(SSL_CTX *ctx, const std::string &ca_dir_path)
Load CA certificates from a directory for peer verification.
bool load_ca_certificates(SSL_CTX *ctx, const std::string &ca_file_path)
Load CA certificates from a file for peer verification.
bool set_ciphersuites_tls13(SSL_CTX *ctx, const std::string &ciphersuites)
Set the preferred cipher suites for TLS 1.3.
SSL_CTX * create_server_context(const SSL_METHOD *method, std::filesystem::path cert_path, std::filesystem::path key_path)
Create an SSL context (SSL_CTX) configured for server-side SSL/TLS operations.
bool configure_mtls_server_context(SSL_CTX *ctx, const std::string &client_ca_file_path, int verification_mode=SSL_VERIFY_PEER)
Configure client certificate authentication (mTLS) for a server SSL_CTX.
bool set_alpn_protos_client(SSL_CTX *ctx, const std::vector< std::string > &protocols)
Set the ALPN protocols for a client SSL_CTX to offer during handshake.
bool set_cipher_list(SSL_CTX *ctx, const std::string &ciphers)
Set the preferred cipher suites for TLS 1.2 and earlier.
bool configure_client_certificate(SSL_CTX *ctx, const std::string &client_cert_path, const std::string &client_key_path)
Configure a client SSL_CTX to use a specific client certificate and private key.
bool set_ocsp_stapling_responder_server(SSL_CTX *ctx, int(*callback)(SSL *s, void *arg), void *arg)
Set a callback for the server to provide an OCSP response to be stapled.
void free_session(Session &session)
Frees an SSL_SESSION object held by qb::io::ssl::Session.
bool set_alpn_selection_callback_server(SSL_CTX *ctx, SSL_CTX_alpn_select_cb_func callback, void *arg)
Set the ALPN selection callback for a server SSL_CTX.
Certificate get_certificate(SSL *ssl)
Extract certificate information from an active SSL connection.
bool set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func callback)
Set the SSL/TLS key log callback function.
bool enable_server_session_caching(SSL_CTX *ctx, long cache_size)
Enable and configure server-side SSL session caching.
Structure to hold essential SSL certificate information.
Definition socket.h:40
std::string subject
The subject name of the certificate.
Definition socket.h:41
int64_t version
The version number of the certificate.
Definition socket.h:43
std::string issuer
The issuer name of the certificate.
Definition socket.h:42
std::vector< std::string > subject_alternative_names
List of Subject Alternative Names (DNS, IP, etc.).
Definition socket.h:48
int64_t not_after
Certificate validity end date (Unix timestamp).
Definition socket.h:46
int64_t not_before
Certificate validity start date (Unix timestamp).
Definition socket.h:45
std::string signature_algorithm
The signature algorithm used in the certificate.
Definition socket.h:47
std::string serial_number
The serial number of the certificate as a hex string.
Definition socket.h:44
Opaque wrapper for an OpenSSL SSL_SESSION object.
Definition socket.h:279
bool is_valid() const
Checks if the session handle is valid (not null).
Definition socket.h:284
Implementation of TCP sockets for the QB IO library.