src/corosio/src/tls/detail/context_impl.hpp

100.0% Lines (33/33) 100.0% List of functions (6/6) 70.0% Branches (7/10)
context_impl.hpp
f(x) Functions (6)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #ifndef SRC_TLS_DETAIL_CONTEXT_IMPL_HPP
12 #define SRC_TLS_DETAIL_CONTEXT_IMPL_HPP
13
14 #include <boost/corosio/tls_context.hpp>
15
16 #include <functional>
17 #include <mutex>
18 #include <string>
19 #include <vector>
20
21 namespace boost::corosio {
22
23 namespace detail {
24
25 /** Abstract base for cached native SSL contexts.
26
27 Stored in context::implementation as an intrusive linked list.
28 Each TLS backend derives from this to cache its native
29 context handle ( WOLFSSL_CTX*, SSL_CTX*, etc. ).
30 */
31 2240x class native_context_base
32 {
33 public:
34 2240x native_context_base* next_ = nullptr;
35 2240x void const* service_ = nullptr;
36
37 2240x virtual ~native_context_base() = default;
38 };
39
40 18840x struct tls_context_data
41 {
42 // Credentials
43
44 std::string entity_certificate;
45 3140x tls_file_format entity_cert_format = tls_file_format::pem;
46 std::string certificate_chain;
47 std::string private_key;
48 3140x tls_file_format private_key_format = tls_file_format::pem;
49
50 // PKCS#12 bundle (decoded by the backend into cert/key/chain).
51 std::string pkcs12_data;
52 std::string pkcs12_password;
53
54 // Trust anchors
55
56 std::vector<std::string> ca_certificates;
57 std::vector<std::string> verify_paths;
58 3140x bool use_default_verify_paths = false;
59
60 // Protocol settings
61
62 3140x tls_version min_version = tls_version::tls_1_2;
63 3140x tls_version max_version = tls_version::tls_1_3;
64 std::string ciphersuites; // TLS 1.2 and below (cipher list)
65 std::string ciphersuites_tls13; // TLS 1.3 ciphersuites
66 std::vector<std::string> alpn_protocols;
67
68 // Verification
69
70 3140x tls_verify_mode verification_mode = tls_verify_mode::none;
71 3140x int verify_depth = 100;
72 std::function<bool(bool, verify_context&)> verify_callback;
73
74 // SNI (Server Name Indication)
75
76 std::function<bool(std::string_view)> servername_callback;
77
78 // Revocation
79
80 std::vector<std::string> crls;
81 3140x tls_revocation_policy revocation = tls_revocation_policy::disabled;
82
83 // Password
84
85 std::function<std::string(std::size_t, tls_password_purpose)>
86 password_callback;
87
88 // Cached native contexts (intrusive list)
89
90 mutable std::mutex native_contexts_mutex_;
91 3140x mutable native_context_base* native_contexts_ = nullptr;
92
93 /** Find or insert a cached native context.
94
95 @param service The unique key for the backend.
96 @param create Factory function called if not found.
97
98 @return Pointer to the cached native context.
99 */
100 template<typename Factory>
101 2245x native_context_base* find(void const* service, Factory&& create) const
102 {
103 2245x std::lock_guard<std::mutex> lock(native_contexts_mutex_);
104
105
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2240 times.
2245x for (auto* p = native_contexts_; p; p = p->next_)
106
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5x if (p->service_ == service)
107 5x return p;
108
109 // Not found - create and prepend
110
1/2
✓ Branch 0 taken 2240 times.
✗ Branch 1 not taken.
2240x auto* ctx = create();
111 2240x ctx->service_ = service;
112 2240x ctx->next_ = native_contexts_;
113 2240x native_contexts_ = ctx;
114 2240x return ctx;
115 2245x }
116
117 3140x ~tls_context_data()
118 {
119 // Clean up cached native contexts (no lock needed - destructor)
120
2/2
✓ Branch 0 taken 2240 times.
✓ Branch 1 taken 3140 times.
5380x while (native_contexts_)
121 {
122 2240x auto* next = native_contexts_->next_;
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2240 times.
2240x delete native_contexts_;
124 2240x native_contexts_ = next;
125 }
126 3140x }
127 };
128
129 } // namespace detail
130
131 /** Implementation of tls_context.
132
133 Contains all portable TLS configuration data plus
134 cached native SSL contexts as an intrusive list.
135 */
136 struct tls_context::implementation : detail::tls_context_data
137 {};
138
139 namespace detail {
140
141 /** Return the TLS context data.
142
143 Provides read-only access to the portable configuration
144 stored in the context.
145
146 @param ctx The TLS context.
147
148 @return Reference to the context implementation.
149 */
150 inline tls_context_data const&
151 2278x get_tls_context_data(tls_context const& ctx) noexcept
152 {
153 2278x return *ctx.impl_;
154 }
155
156 } // namespace detail
157
158 } // namespace boost::corosio
159
160 #endif
161