src/corosio/src/tls/detail/context_impl.hpp
100.0% Lines (20/20)
100.0% List of functions (4/4)
80.0% Branches (8/10)
Functions (4)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::native_context_base::~native_context_base()
:37
2188x
0.0%
–
100.0%
boost::corosio::detail::native_context_base* boost::corosio::detail::tls_context_data::find<boost::corosio::detail::get_openssl_native_context(boost::corosio::detail::tls_context_data const&)::{lambda()#1}>(void const*, boost::corosio::detail::get_openssl_native_context(boost::corosio::detail::tls_context_data const&)::{lambda()#1}&&) const
:102
3712x
100.0%
83.3%
78.6%
boost::corosio::detail::tls_context_data::~tls_context_data()
:118
2220x
100.0%
75.0%
100.0%
boost::corosio::detail::get_tls_context_data(boost::corosio::tls_context const&)
:152
3768x
100.0%
–
100.0%
| 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::impl 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 | class native_context_base | |||
| 32 | { | |||
| 33 | public: | |||
| 34 | native_context_base* next_ = nullptr; | |||
| 35 | void const* service_ = nullptr; | |||
| 36 | ||||
| 37 | 2188x | virtual ~native_context_base() = default; | ||
| 38 | }; | |||
| 39 | ||||
| 40 | struct tls_context_data | |||
| 41 | { | |||
| 42 | // Credentials | |||
| 43 | ||||
| 44 | std::string entity_certificate; | |||
| 45 | tls_file_format entity_cert_format = tls_file_format::pem; | |||
| 46 | std::string certificate_chain; | |||
| 47 | std::string private_key; | |||
| 48 | 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 | bool use_default_verify_paths = false; | |||
| 59 | ||||
| 60 | // Protocol settings | |||
| 61 | ||||
| 62 | tls_version min_version = tls_version::tls_1_2; | |||
| 63 | 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 | tls_verify_mode verification_mode = tls_verify_mode::none; | |||
| 71 | int verify_depth = 100; | |||
| 72 | std::string hostname; | |||
| 73 | std::function<bool(bool, verify_context&)> verify_callback; | |||
| 74 | ||||
| 75 | // SNI (Server Name Indication) | |||
| 76 | ||||
| 77 | std::function<bool(std::string_view)> servername_callback; | |||
| 78 | ||||
| 79 | // Revocation | |||
| 80 | ||||
| 81 | std::vector<std::string> crls; | |||
| 82 | tls_revocation_policy revocation = tls_revocation_policy::disabled; | |||
| 83 | ||||
| 84 | // Password | |||
| 85 | ||||
| 86 | std::function<std::string(std::size_t, tls_password_purpose)> | |||
| 87 | password_callback; | |||
| 88 | ||||
| 89 | // Cached native contexts (intrusive list) | |||
| 90 | ||||
| 91 | mutable std::mutex native_contexts_mutex_; | |||
| 92 | mutable native_context_base* native_contexts_ = nullptr; | |||
| 93 | ||||
| 94 | /** Find or insert a cached native context. | |||
| 95 | ||||
| 96 | @param service The unique key for the backend. | |||
| 97 | @param create Factory function called if not found. | |||
| 98 | ||||
| 99 | @return Pointer to the cached native context. | |||
| 100 | */ | |||
| 101 | template<typename Factory> | |||
| 102 | 3712x | native_context_base* find(void const* service, Factory&& create) const | ||
| 103 | { | |||
| 104 |
1/1✓ Branch 2 → 3 taken 3712 times.
|
3712x | std::lock_guard<std::mutex> lock(native_contexts_mutex_); | |
| 105 | ||||
| 106 |
2/2✓ Branch 7 → 4 taken 1524 times.
✓ Branch 7 → 8 taken 2188 times.
|
3712x | for (auto* p = native_contexts_; p; p = p->next_) | |
| 107 |
1/2✓ Branch 4 → 5 taken 1524 times.
✗ Branch 4 → 6 not taken.
|
1524x | if (p->service_ == service) | |
| 108 | 1524x | return p; | ||
| 109 | ||||
| 110 | // Not found - create and prepend | |||
| 111 |
1/1✓ Branch 8 → 9 taken 2188 times.
|
2188x | auto* ctx = create(); | |
| 112 | 2188x | ctx->service_ = service; | ||
| 113 | 2188x | ctx->next_ = native_contexts_; | ||
| 114 | 2188x | native_contexts_ = ctx; | ||
| 115 | 2188x | return ctx; | ||
| 116 | 3712x | } | ||
| 117 | ||||
| 118 | 2220x | ~tls_context_data() | ||
| 119 | { | |||
| 120 | // Clean up cached native contexts (no lock needed - destructor) | |||
| 121 |
2/2✓ Branch 6 → 3 taken 2188 times.
✓ Branch 6 → 7 taken 2220 times.
|
4408x | while (native_contexts_) | |
| 122 | { | |||
| 123 | 2188x | auto* next = native_contexts_->next_; | ||
| 124 |
1/2✓ Branch 3 → 4 taken 2188 times.
✗ Branch 3 → 5 not taken.
|
2188x | delete native_contexts_; | |
| 125 | 2188x | native_contexts_ = next; | ||
| 126 | } | |||
| 127 | 2220x | } | ||
| 128 | }; | |||
| 129 | ||||
| 130 | } // namespace detail | |||
| 131 | ||||
| 132 | /** Implementation of tls_context. | |||
| 133 | ||||
| 134 | Contains all portable TLS configuration data plus | |||
| 135 | cached native SSL contexts as an intrusive list. | |||
| 136 | */ | |||
| 137 | struct tls_context::impl : detail::tls_context_data | |||
| 138 | {}; | |||
| 139 | ||||
| 140 | namespace detail { | |||
| 141 | ||||
| 142 | /** Return the TLS context data. | |||
| 143 | ||||
| 144 | Provides read-only access to the portable configuration | |||
| 145 | stored in the context. | |||
| 146 | ||||
| 147 | @param ctx The TLS context. | |||
| 148 | ||||
| 149 | @return Reference to the context implementation. | |||
| 150 | */ | |||
| 151 | inline tls_context_data const& | |||
| 152 | 3768x | get_tls_context_data(tls_context const& ctx) noexcept | ||
| 153 | { | |||
| 154 | 3768x | return *ctx.impl_; | ||
| 155 | } | |||
| 156 | ||||
| 157 | } // namespace detail | |||
| 158 | ||||
| 159 | } // namespace boost::corosio | |||
| 160 | ||||
| 161 | #endif | |||
| 162 |