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