87.50% Lines (14/16)
88.89% Functions (8/9)
| 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 BOOST_COROSIO_TLS_CONTEXT_HPP | 11 | #ifndef BOOST_COROSIO_TLS_CONTEXT_HPP | |||||
| 12 | #define BOOST_COROSIO_TLS_CONTEXT_HPP | 12 | #define BOOST_COROSIO_TLS_CONTEXT_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/corosio/detail/config.hpp> | 14 | #include <boost/corosio/detail/config.hpp> | |||||
| 15 | 15 | |||||||
| 16 | #include <cstddef> | 16 | #include <cstddef> | |||||
| 17 | #include <functional> | 17 | #include <functional> | |||||
| 18 | #include <span> | 18 | #include <span> | |||||
| 19 | #include <system_error> | 19 | #include <system_error> | |||||
| 20 | #include <memory> | 20 | #include <memory> | |||||
| 21 | #include <string_view> | 21 | #include <string_view> | |||||
| 22 | 22 | |||||||
| 23 | namespace boost::corosio { | 23 | namespace boost::corosio { | |||||
| 24 | 24 | |||||||
| 25 | // | 25 | // | |||||
| 26 | // Enumerations | 26 | // Enumerations | |||||
| 27 | // | 27 | // | |||||
| 28 | 28 | |||||||
| 29 | /** TLS protocol version. | 29 | /** TLS protocol version. | |||||
| 30 | 30 | |||||||
| 31 | Specifies the minimum or maximum TLS protocol version to use | 31 | Specifies the minimum or maximum TLS protocol version to use | |||||
| 32 | for connections. Only modern, secure versions are supported. | 32 | for connections. Only modern, secure versions are supported. | |||||
| 33 | 33 | |||||||
| 34 | @see tls_context::set_min_protocol_version | 34 | @see tls_context::set_min_protocol_version | |||||
| 35 | @see tls_context::set_max_protocol_version | 35 | @see tls_context::set_max_protocol_version | |||||
| 36 | */ | 36 | */ | |||||
| 37 | enum class tls_version | 37 | enum class tls_version | |||||
| 38 | { | 38 | { | |||||
| 39 | /// TLS 1.2 (RFC 5246). | 39 | /// TLS 1.2 (RFC 5246). | |||||
| 40 | tls_1_2, | 40 | tls_1_2, | |||||
| 41 | 41 | |||||||
| 42 | /// TLS 1.3 (RFC 8446). | 42 | /// TLS 1.3 (RFC 8446). | |||||
| 43 | tls_1_3 | 43 | tls_1_3 | |||||
| 44 | }; | 44 | }; | |||||
| 45 | 45 | |||||||
| 46 | /** Certificate and key file format. | 46 | /** Certificate and key file format. | |||||
| 47 | 47 | |||||||
| 48 | Specifies the encoding format for certificate and key data. | 48 | Specifies the encoding format for certificate and key data. | |||||
| 49 | 49 | |||||||
| 50 | @see tls_context::use_certificate | 50 | @see tls_context::use_certificate | |||||
| 51 | @see tls_context::use_private_key | 51 | @see tls_context::use_private_key | |||||
| 52 | */ | 52 | */ | |||||
| 53 | enum class tls_file_format | 53 | enum class tls_file_format | |||||
| 54 | { | 54 | { | |||||
| 55 | /// PEM format (Base64-encoded with header/footer lines). | 55 | /// PEM format (Base64-encoded with header/footer lines). | |||||
| 56 | pem, | 56 | pem, | |||||
| 57 | 57 | |||||||
| 58 | /// DER format (raw ASN.1 binary encoding). | 58 | /// DER format (raw ASN.1 binary encoding). | |||||
| 59 | der | 59 | der | |||||
| 60 | }; | 60 | }; | |||||
| 61 | 61 | |||||||
| 62 | /** Peer certificate verification mode. | 62 | /** Peer certificate verification mode. | |||||
| 63 | 63 | |||||||
| 64 | Controls how the TLS implementation verifies the peer's | 64 | Controls how the TLS implementation verifies the peer's | |||||
| 65 | certificate during the handshake. | 65 | certificate during the handshake. | |||||
| 66 | 66 | |||||||
| 67 | @see tls_context::set_verify_mode | 67 | @see tls_context::set_verify_mode | |||||
| 68 | */ | 68 | */ | |||||
| 69 | enum class tls_verify_mode | 69 | enum class tls_verify_mode | |||||
| 70 | { | 70 | { | |||||
| 71 | /// Do not request or verify the peer certificate. | 71 | /// Do not request or verify the peer certificate. | |||||
| 72 | none, | 72 | none, | |||||
| 73 | 73 | |||||||
| 74 | /// Request and verify the peer certificate if presented. | 74 | /// Request and verify the peer certificate if presented. | |||||
| 75 | peer, | 75 | peer, | |||||
| 76 | 76 | |||||||
| 77 | /// Require and verify the peer certificate (fail if not presented). | 77 | /// Require and verify the peer certificate (fail if not presented). | |||||
| 78 | require_peer | 78 | require_peer | |||||
| 79 | }; | 79 | }; | |||||
| 80 | 80 | |||||||
| 81 | /** Certificate revocation checking policy. | 81 | /** Certificate revocation checking policy. | |||||
| 82 | 82 | |||||||
| 83 | Controls how certificate revocation status is checked during | 83 | Controls how certificate revocation status is checked during | |||||
| 84 | verification. | 84 | verification. | |||||
| 85 | 85 | |||||||
| 86 | @see tls_context::set_revocation_policy | 86 | @see tls_context::set_revocation_policy | |||||
| 87 | */ | 87 | */ | |||||
| 88 | enum class tls_revocation_policy | 88 | enum class tls_revocation_policy | |||||
| 89 | { | 89 | { | |||||
| 90 | /// Do not check revocation status. | 90 | /// Do not check revocation status. | |||||
| 91 | disabled, | 91 | disabled, | |||||
| 92 | 92 | |||||||
| 93 | /// Check revocation but allow connection if status is unknown. | 93 | /// Check revocation but allow connection if status is unknown. | |||||
| 94 | soft_fail, | 94 | soft_fail, | |||||
| 95 | 95 | |||||||
| 96 | /// Require successful revocation check (fail if status is unknown). | 96 | /// Require successful revocation check (fail if status is unknown). | |||||
| 97 | hard_fail | 97 | hard_fail | |||||
| 98 | }; | 98 | }; | |||||
| 99 | 99 | |||||||
| 100 | /** Purpose for password callback invocation. | 100 | /** Purpose for password callback invocation. | |||||
| 101 | 101 | |||||||
| 102 | Indicates whether the password is needed for reading (decrypting) | 102 | Indicates whether the password is needed for reading (decrypting) | |||||
| 103 | or writing (encrypting) key material. | 103 | or writing (encrypting) key material. | |||||
| 104 | 104 | |||||||
| 105 | @see tls_context::set_password_callback | 105 | @see tls_context::set_password_callback | |||||
| 106 | */ | 106 | */ | |||||
| 107 | enum class tls_password_purpose | 107 | enum class tls_password_purpose | |||||
| 108 | { | 108 | { | |||||
| 109 | /// Password needed to decrypt/read protected key material. | 109 | /// Password needed to decrypt/read protected key material. | |||||
| 110 | for_reading, | 110 | for_reading, | |||||
| 111 | 111 | |||||||
| 112 | /// Password needed to encrypt/write protected key material. | 112 | /// Password needed to encrypt/write protected key material. | |||||
| 113 | for_writing | 113 | for_writing | |||||
| 114 | }; | 114 | }; | |||||
| 115 | 115 | |||||||
| 116 | class tls_context; | 116 | class tls_context; | |||||
| 117 | 117 | |||||||
| 118 | /** A non-owning view of certificate verification state. | 118 | /** A non-owning view of certificate verification state. | |||||
| 119 | 119 | |||||||
| 120 | An instance is passed to the callback installed via | 120 | An instance is passed to the callback installed via | |||||
| 121 | tls_context::set_verify_callback during the TLS handshake. It | 121 | tls_context::set_verify_callback during the TLS handshake. It | |||||
| 122 | exposes the backend's native verification handle so the callback | 122 | exposes the backend's native verification handle so the callback | |||||
| 123 | can inspect the certificate and chain currently being verified. | 123 | can inspect the certificate and chain currently being verified. | |||||
| 124 | 124 | |||||||
| 125 | The value returned by native_handle() is, for the OpenSSL and | 125 | The value returned by native_handle() is, for the OpenSSL and | |||||
| 126 | WolfSSL backends, an `X509_STORE_CTX*`. For portable inspection that | 126 | WolfSSL backends, an `X509_STORE_CTX*`. For portable inspection that | |||||
| 127 | works across backends (for example certificate pinning), prefer | 127 | works across backends (for example certificate pinning), prefer | |||||
| 128 | certificate(), which returns the DER encoding of the certificate | 128 | certificate(), which returns the DER encoding of the certificate | |||||
| 129 | currently being verified. | 129 | currently being verified. | |||||
| 130 | 130 | |||||||
| 131 | @par Lifetime | 131 | @par Lifetime | |||||
| 132 | 132 | |||||||
| 133 | The wrapped handle and the certificate() bytes are owned by the TLS | 133 | The wrapped handle and the certificate() bytes are owned by the TLS | |||||
| 134 | backend and are valid only for the duration of a single callback | 134 | backend and are valid only for the duration of a single callback | |||||
| 135 | invocation. Do not retain them beyond the call. | 135 | invocation. Do not retain them beyond the call. | |||||
| 136 | 136 | |||||||
| 137 | @see tls_context::set_verify_callback | 137 | @see tls_context::set_verify_callback | |||||
| 138 | */ | 138 | */ | |||||
| 139 | class verify_context | 139 | class verify_context | |||||
| 140 | { | 140 | { | |||||
| 141 | void* handle_; | 141 | void* handle_; | |||||
| 142 | unsigned char const* der_; | 142 | unsigned char const* der_; | |||||
| 143 | std::size_t der_len_; | 143 | std::size_t der_len_; | |||||
| 144 | 144 | |||||||
| 145 | public: | 145 | public: | |||||
| 146 | /** Construct from a native handle and the current certificate. | 146 | /** Construct from a native handle and the current certificate. | |||||
| 147 | 147 | |||||||
| 148 | @param handle The backend verification handle (for OpenSSL and | 148 | @param handle The backend verification handle (for OpenSSL and | |||||
| 149 | WolfSSL, an `X509_STORE_CTX*`). | 149 | WolfSSL, an `X509_STORE_CTX*`). | |||||
| 150 | @param der Pointer to the DER encoding of the certificate under | 150 | @param der Pointer to the DER encoding of the certificate under | |||||
| 151 | verification, or `nullptr` if unavailable. | 151 | verification, or `nullptr` if unavailable. | |||||
| 152 | @param der_len Length of the DER encoding in bytes. | 152 | @param der_len Length of the DER encoding in bytes. | |||||
| 153 | */ | 153 | */ | |||||
| 154 | verify_context( | 154 | verify_context( | |||||
| 155 | void* handle, unsigned char const* der, std::size_t der_len) noexcept | 155 | void* handle, unsigned char const* der, std::size_t der_len) noexcept | |||||
| 156 | : handle_(handle), der_(der), der_len_(der_len) | 156 | : handle_(handle), der_(der), der_len_(der_len) | |||||
| 157 | { | 157 | { | |||||
| 158 | } | 158 | } | |||||
| 159 | 159 | |||||||
| 160 | /** Return the native verification handle. | 160 | /** Return the native verification handle. | |||||
| 161 | 161 | |||||||
| 162 | Cast the result to the backend's verification context type | 162 | Cast the result to the backend's verification context type | |||||
| 163 | (e.g. `X509_STORE_CTX*`) to inspect the certificate chain using | 163 | (e.g. `X509_STORE_CTX*`) to inspect the certificate chain using | |||||
| 164 | backend-specific APIs. | 164 | backend-specific APIs. | |||||
| 165 | 165 | |||||||
| 166 | @return The native handle, or `nullptr` if none is available. | 166 | @return The native handle, or `nullptr` if none is available. | |||||
| 167 | */ | 167 | */ | |||||
| 168 | void* native_handle() const noexcept { return handle_; } | 168 | void* native_handle() const noexcept { return handle_; } | |||||
| 169 | 169 | |||||||
| 170 | /** Return the DER encoding of the certificate being verified. | 170 | /** Return the DER encoding of the certificate being verified. | |||||
| 171 | 171 | |||||||
| 172 | This is the portable way to inspect the peer certificate from a | 172 | This is the portable way to inspect the peer certificate from a | |||||
| 173 | verification callback: it works identically on every backend, | 173 | verification callback: it works identically on every backend, | |||||
| 174 | without depending on backend-specific build options. A DER | 174 | without depending on backend-specific build options. A DER | |||||
| 175 | certificate is an ASN.1 `SEQUENCE`, so the first byte is `0x30`. | 175 | certificate is an ASN.1 `SEQUENCE`, so the first byte is `0x30`. | |||||
| 176 | 176 | |||||||
| 177 | @return A view of the certificate's DER bytes, valid only for the | 177 | @return A view of the certificate's DER bytes, valid only for the | |||||
| 178 | duration of the callback. Empty if the certificate is not | 178 | duration of the callback. Empty if the certificate is not | |||||
| 179 | available. | 179 | available. | |||||
| 180 | */ | 180 | */ | |||||
| MISUBC | 181 | ✗ | std::span<unsigned char const> certificate() const noexcept | 181 | ✗ | std::span<unsigned char const> certificate() const noexcept | ||
| 182 | { | 182 | { | |||||
| MISUBC | 183 | ✗ | return {der_, der_len_}; | 183 | ✗ | return {der_, der_len_}; | ||
| 184 | } | 184 | } | |||||
| 185 | }; | 185 | }; | |||||
| 186 | 186 | |||||||
| 187 | namespace detail { | 187 | namespace detail { | |||||
| 188 | struct tls_context_data; | 188 | struct tls_context_data; | |||||
| 189 | tls_context_data const& get_tls_context_data(tls_context const&) noexcept; | 189 | tls_context_data const& get_tls_context_data(tls_context const&) noexcept; | |||||
| 190 | } // namespace detail | 190 | } // namespace detail | |||||
| 191 | 191 | |||||||
| 192 | /** A portable TLS context for certificate and settings storage. | 192 | /** A portable TLS context for certificate and settings storage. | |||||
| 193 | 193 | |||||||
| 194 | The `tls_context` class provides a backend-agnostic interface for | 194 | The `tls_context` class provides a backend-agnostic interface for | |||||
| 195 | configuring TLS connections. It stores credentials (certificates and | 195 | configuring TLS connections. It stores credentials (certificates and | |||||
| 196 | private keys), trust anchors, protocol settings, and verification | 196 | private keys), trust anchors, protocol settings, and verification | |||||
| 197 | options that are used when establishing TLS connections. | 197 | options that are used when establishing TLS connections. | |||||
| 198 | 198 | |||||||
| 199 | This class is a shared handle to an opaque implementation. Copies | 199 | This class is a shared handle to an opaque implementation. Copies | |||||
| 200 | share the same underlying state. This allows contexts to be passed | 200 | share the same underlying state. This allows contexts to be passed | |||||
| 201 | by value and shared across multiple TLS streams. | 201 | by value and shared across multiple TLS streams. | |||||
| 202 | 202 | |||||||
| 203 | This class abstracts the configuration phase of TLS across multiple | 203 | This class abstracts the configuration phase of TLS across multiple | |||||
| 204 | backend implementations (OpenSSL, WolfSSL, mbedTLS, Schannel, etc.), | 204 | backend implementations (OpenSSL, WolfSSL, mbedTLS, Schannel, etc.), | |||||
| 205 | allowing portable code that works regardless of which TLS library | 205 | allowing portable code that works regardless of which TLS library | |||||
| 206 | is linked. | 206 | is linked. | |||||
| 207 | 207 | |||||||
| 208 | @par Modification After Stream Creation | 208 | @par Modification After Stream Creation | |||||
| 209 | 209 | |||||||
| 210 | Modifying a context after a TLS stream has been created from it | 210 | Modifying a context after a TLS stream has been created from it | |||||
| 211 | results in undefined behavior. The context's configuration is | 211 | results in undefined behavior. The context's configuration is | |||||
| 212 | captured when the first stream is constructed, and subsequent | 212 | captured when the first stream is constructed, and subsequent | |||||
| 213 | modifications are not reflected in existing or new streams | 213 | modifications are not reflected in existing or new streams | |||||
| 214 | sharing the context. | 214 | sharing the context. | |||||
| 215 | 215 | |||||||
| 216 | If different configurations are needed, create separate context | 216 | If different configurations are needed, create separate context | |||||
| 217 | objects. | 217 | objects. | |||||
| 218 | 218 | |||||||
| 219 | @par Thread Safety | 219 | @par Thread Safety | |||||
| 220 | 220 | |||||||
| 221 | Distinct objects: Safe. | 221 | Distinct objects: Safe. | |||||
| 222 | 222 | |||||||
| 223 | Shared objects: Unsafe. A context must not be modified while | 223 | Shared objects: Unsafe. A context must not be modified while | |||||
| 224 | any thread is creating streams from it. | 224 | any thread is creating streams from it. | |||||
| 225 | 225 | |||||||
| 226 | @par Example | 226 | @par Example | |||||
| 227 | @code | 227 | @code | |||||
| 228 | // Create a client context with system trust anchors | 228 | // Create a client context with system trust anchors | |||||
| 229 | corosio::tls_context ctx; | 229 | corosio::tls_context ctx; | |||||
| 230 | - | ctx.set_default_verify_paths(); | 230 | + | if (auto ec = ctx.set_default_verify_paths()) | |||
| 231 | - | ctx.set_verify_mode( corosio::tls_verify_mode::peer ); | 231 | + | co_return; | |||
| 232 | + | if (auto ec = ctx.set_verify_mode( corosio::tls_verify_mode::peer )) | ||||||
| 233 | + | co_return; | ||||||
| 232 | 234 | |||||||
| 233 | // Use with a TLS stream | 235 | // Use with a TLS stream | |||||
| 234 | corosio::openssl_stream secure( &sock, ctx ); | 236 | corosio::openssl_stream secure( &sock, ctx ); | |||||
| 235 | secure.set_hostname( "example.com" ); | 237 | secure.set_hostname( "example.com" ); | |||||
| 236 | - | co_await secure.handshake( corosio::tls_role::client ); | 238 | + | if (auto [ec] = co_await secure.handshake( corosio::tls_role::client ); ec) | |||
| 239 | + | co_return; | ||||||
| 237 | @endcode | 240 | @endcode | |||||
| 238 | 241 | |||||||
| 239 | @see tls_role | 242 | @see tls_role | |||||
| 240 | */ | 243 | */ | |||||
| 241 | #ifdef _MSC_VER | 244 | #ifdef _MSC_VER | |||||
| 242 | #pragma warning(push) | 245 | #pragma warning(push) | |||||
| 243 | #pragma warning(disable : 4251) // shared_ptr needs dll-interface | 246 | #pragma warning(disable : 4251) // shared_ptr needs dll-interface | |||||
| 244 | #endif | 247 | #endif | |||||
| 245 | class BOOST_COROSIO_DECL tls_context | 248 | class BOOST_COROSIO_DECL tls_context | |||||
| 246 | { | 249 | { | |||||
| 247 | struct implementation; | 250 | struct implementation; | |||||
| 248 | std::shared_ptr<implementation> impl_; | 251 | std::shared_ptr<implementation> impl_; | |||||
| 249 | 252 | |||||||
| 250 | friend detail::tls_context_data const& | 253 | friend detail::tls_context_data const& | |||||
| 251 | detail::get_tls_context_data(tls_context const&) noexcept; | 254 | detail::get_tls_context_data(tls_context const&) noexcept; | |||||
| 252 | 255 | |||||||
| 253 | public: | 256 | public: | |||||
| 254 | /** Construct a default TLS context. | 257 | /** Construct a default TLS context. | |||||
| 255 | 258 | |||||||
| 256 | Creates a context with default settings suitable for TLS 1.2 | 259 | Creates a context with default settings suitable for TLS 1.2 | |||||
| 257 | and TLS 1.3 connections. No certificates or trust anchors are | 260 | and TLS 1.3 connections. No certificates or trust anchors are | |||||
| 258 | loaded; call the appropriate methods to configure credentials | 261 | loaded; call the appropriate methods to configure credentials | |||||
| 259 | and verification. | 262 | and verification. | |||||
| 260 | 263 | |||||||
| 261 | @par Example | 264 | @par Example | |||||
| 262 | @code | 265 | @code | |||||
| 263 | corosio::tls_context ctx; | 266 | corosio::tls_context ctx; | |||||
| 264 | @endcode | 267 | @endcode | |||||
| 265 | */ | 268 | */ | |||||
| 266 | tls_context(); | 269 | tls_context(); | |||||
| 267 | 270 | |||||||
| 268 | /** Copy constructor. | 271 | /** Copy constructor. | |||||
| 269 | 272 | |||||||
| 270 | Creates a new handle that shares ownership of the underlying | 273 | Creates a new handle that shares ownership of the underlying | |||||
| 271 | TLS context state with `other`. | 274 | TLS context state with `other`. | |||||
| 272 | 275 | |||||||
| 273 | @param other The context to copy from. | 276 | @param other The context to copy from. | |||||
| 274 | */ | 277 | */ | |||||
| HITCBC | 275 | 2 | tls_context(tls_context const& other) = default; | 278 | 2 | tls_context(tls_context const& other) = default; | ||
| 276 | 279 | |||||||
| 277 | /** Copy assignment operator. | 280 | /** Copy assignment operator. | |||||
| 278 | 281 | |||||||
| 279 | Releases the current context's shared ownership and acquires | 282 | Releases the current context's shared ownership and acquires | |||||
| 280 | shared ownership of `other`'s underlying state. | 283 | shared ownership of `other`'s underlying state. | |||||
| 281 | 284 | |||||||
| 282 | @param other The context to copy from. | 285 | @param other The context to copy from. | |||||
| 283 | 286 | |||||||
| 284 | @return Reference to this context. | 287 | @return Reference to this context. | |||||
| 285 | */ | 288 | */ | |||||
| HITCBC | 286 | 1 | tls_context& operator=(tls_context const& other) = default; | 289 | 1 | tls_context& operator=(tls_context const& other) = default; | ||
| 287 | 290 | |||||||
| 288 | /** Move constructor. | 291 | /** Move constructor. | |||||
| 289 | 292 | |||||||
| 290 | Transfers ownership of the TLS context from another instance. | 293 | Transfers ownership of the TLS context from another instance. | |||||
| 291 | After the move, `other` is in a valid but empty state. | 294 | After the move, `other` is in a valid but empty state. | |||||
| 292 | 295 | |||||||
| 293 | @param other The context to move from. | 296 | @param other The context to move from. | |||||
| 294 | */ | 297 | */ | |||||
| HITCBC | 295 | 2 | tls_context(tls_context&& other) noexcept = default; | 298 | 2 | tls_context(tls_context&& other) noexcept = default; | ||
| 296 | 299 | |||||||
| 297 | /** Move assignment operator. | 300 | /** Move assignment operator. | |||||
| 298 | 301 | |||||||
| 299 | Releases the current context's shared ownership and transfers | 302 | Releases the current context's shared ownership and transfers | |||||
| 300 | ownership from another instance. After the move, `other` is | 303 | ownership from another instance. After the move, `other` is | |||||
| 301 | in a valid but empty state. | 304 | in a valid but empty state. | |||||
| 302 | 305 | |||||||
| 303 | @param other The context to move from. | 306 | @param other The context to move from. | |||||
| 304 | 307 | |||||||
| 305 | @return Reference to this context. | 308 | @return Reference to this context. | |||||
| 306 | */ | 309 | */ | |||||
| HITCBC | 307 | 1 | tls_context& operator=(tls_context&& other) noexcept = default; | 310 | 1 | tls_context& operator=(tls_context&& other) noexcept = default; | ||
| 308 | 311 | |||||||
| 309 | /** Destructor. | 312 | /** Destructor. | |||||
| 310 | 313 | |||||||
| 311 | Releases this handle's shared ownership of the underlying | 314 | Releases this handle's shared ownership of the underlying | |||||
| 312 | context. The context state is destroyed when the last handle | 315 | context. The context state is destroyed when the last handle | |||||
| 313 | is released. | 316 | is released. | |||||
| 314 | */ | 317 | */ | |||||
| HITCBC | 315 | 55 | ~tls_context() = default; | 318 | 55 | ~tls_context() = default; | ||
| 316 | 319 | |||||||
| 317 | // | 320 | // | |||||
| 318 | // Credential Loading | 321 | // Credential Loading | |||||
| 319 | // | 322 | // | |||||
| 320 | 323 | |||||||
| 321 | /** Load the entity certificate from a memory buffer. | 324 | /** Load the entity certificate from a memory buffer. | |||||
| 322 | 325 | |||||||
| 323 | Sets the certificate that identifies this endpoint to the peer. | 326 | Sets the certificate that identifies this endpoint to the peer. | |||||
| 324 | For servers, this is the server certificate. For clients using | 327 | For servers, this is the server certificate. For clients using | |||||
| 325 | mutual TLS, this is the client certificate. | 328 | mutual TLS, this is the client certificate. | |||||
| 326 | 329 | |||||||
| 327 | The certificate must match the private key loaded via | 330 | The certificate must match the private key loaded via | |||||
| 328 | `use_private_key()` or `use_private_key_file()`. | 331 | `use_private_key()` or `use_private_key_file()`. | |||||
| 329 | 332 | |||||||
| 330 | @param certificate The certificate data. | 333 | @param certificate The certificate data. | |||||
| 331 | 334 | |||||||
| 332 | @param format The encoding format of the certificate data. | 335 | @param format The encoding format of the certificate data. | |||||
| 333 | 336 | |||||||
| 334 | - | @return Success, or an error if the certificate could not be parsed | 337 | + | @return Success. The certificate is recorded and decoded when the | |||
| 335 | - | or is invalid. | 338 | + | native context is first built; a malformed certificate surfaces | |||
| 339 | + | as a handshake failure. | ||||||
| 336 | 340 | |||||||
| 337 | @see use_certificate_file | 341 | @see use_certificate_file | |||||
| 338 | @see use_private_key | 342 | @see use_private_key | |||||
| 339 | */ | 343 | */ | |||||
| 340 | - | std::error_code | 344 | + | [[nodiscard]] std::error_code | |||
| 341 | use_certificate(std::string_view certificate, tls_file_format format); | 345 | use_certificate(std::string_view certificate, tls_file_format format); | |||||
| 342 | 346 | |||||||
| 343 | /** Load the entity certificate from a file. | 347 | /** Load the entity certificate from a file. | |||||
| 344 | 348 | |||||||
| 345 | Sets the certificate that identifies this endpoint to the peer. | 349 | Sets the certificate that identifies this endpoint to the peer. | |||||
| 346 | For servers, this is the server certificate. For clients using | 350 | For servers, this is the server certificate. For clients using | |||||
| 347 | mutual TLS, this is the client certificate. | 351 | mutual TLS, this is the client certificate. | |||||
| 348 | 352 | |||||||
| 349 | @param filename Path to the certificate file. | 353 | @param filename Path to the certificate file. | |||||
| 350 | 354 | |||||||
| 351 | @param format The encoding format of the file. | 355 | @param format The encoding format of the file. | |||||
| 352 | 356 | |||||||
| 353 | - | @return Success, or an error if the file could not be read or the | 357 | + | @return Success, or an error if the file could not be read. The | |||
| 354 | - | certificate is invalid. | 358 | + | certificate is decoded when the native context is first built; | |||
| 359 | + | a malformed certificate surfaces as a handshake failure. | ||||||
| 355 | 360 | |||||||
| 356 | @par Example | 361 | @par Example | |||||
| 357 | @code | 362 | @code | |||||
| 358 | - | ctx.use_certificate_file( "server.crt", tls_file_format::pem ); | 363 | + | if (auto ec = ctx.use_certificate_file( | |||
| 364 | + | "server.crt", tls_file_format::pem )) | ||||||
| 365 | + | return; | ||||||
| 359 | @endcode | 366 | @endcode | |||||
| 360 | 367 | |||||||
| 361 | @see use_certificate | 368 | @see use_certificate | |||||
| 362 | @see use_private_key_file | 369 | @see use_private_key_file | |||||
| 363 | */ | 370 | */ | |||||
| 364 | - | std::error_code | 371 | + | [[nodiscard]] std::error_code | |||
| 365 | use_certificate_file(std::string_view filename, tls_file_format format); | 372 | use_certificate_file(std::string_view filename, tls_file_format format); | |||||
| 366 | 373 | |||||||
| 367 | /** Load a certificate chain from a memory buffer. | 374 | /** Load a certificate chain from a memory buffer. | |||||
| 368 | 375 | |||||||
| 369 | Loads the entity certificate followed by intermediate CA certificates. | 376 | Loads the entity certificate followed by intermediate CA certificates. | |||||
| 370 | The chain should be ordered from leaf to root (excluding the root). | 377 | The chain should be ordered from leaf to root (excluding the root). | |||||
| 371 | This is the typical format for PEM certificate bundles. | 378 | This is the typical format for PEM certificate bundles. | |||||
| 372 | 379 | |||||||
| 373 | @param chain The certificate chain data in PEM format (concatenated | 380 | @param chain The certificate chain data in PEM format (concatenated | |||||
| 374 | certificates). | 381 | certificates). | |||||
| 375 | 382 | |||||||
| 376 | - | @return Success, or an error if the chain could not be parsed. | 383 | + | @return Success. The chain is recorded and decoded when the native | |||
| 384 | + | context is first built; a malformed chain surfaces as a | ||||||
| 385 | + | handshake failure. | ||||||
| 377 | 386 | |||||||
| 378 | @see use_certificate_chain_file | 387 | @see use_certificate_chain_file | |||||
| 379 | */ | 388 | */ | |||||
| 380 | - | std::error_code use_certificate_chain(std::string_view chain); | 389 | + | [[nodiscard]] std::error_code use_certificate_chain(std::string_view chain); | |||
| 381 | 390 | |||||||
| 382 | /** Load a certificate chain from a file. | 391 | /** Load a certificate chain from a file. | |||||
| 383 | 392 | |||||||
| 384 | Loads the entity certificate followed by intermediate CA certificates | 393 | Loads the entity certificate followed by intermediate CA certificates | |||||
| 385 | from a PEM file. The file should contain concatenated PEM certificates | 394 | from a PEM file. The file should contain concatenated PEM certificates | |||||
| 386 | ordered from leaf to root (excluding the root). | 395 | ordered from leaf to root (excluding the root). | |||||
| 387 | 396 | |||||||
| 388 | @param filename Path to the certificate chain file. | 397 | @param filename Path to the certificate chain file. | |||||
| 389 | 398 | |||||||
| 390 | - | @return Success, or an error if the file could not be read or parsed. | 399 | + | @return Success, or an error if the file could not be read. The | |||
| 400 | + | chain is decoded when the native context is first built; a | ||||||
| 401 | + | malformed chain surfaces as a handshake failure. | ||||||
| 391 | 402 | |||||||
| 392 | @par Example | 403 | @par Example | |||||
| 393 | @code | 404 | @code | |||||
| 394 | - | // Load certificate chain (cert + intermediates) | 405 | + | if (auto ec = ctx.use_certificate_chain_file( "fullchain.pem" )) | |||
| 395 | - | ctx.use_certificate_chain_file( "fullchain.pem" ); | 406 | + | return; | |||
| 396 | @endcode | 407 | @endcode | |||||
| 397 | 408 | |||||||
| 398 | @see use_certificate_chain | 409 | @see use_certificate_chain | |||||
| 399 | */ | 410 | */ | |||||
| 400 | - | std::error_code use_certificate_chain_file(std::string_view filename); | 411 | + | [[nodiscard]] std::error_code use_certificate_chain_file(std::string_view filename); | |||
| 401 | 412 | |||||||
| 402 | /** Load the private key from a memory buffer. | 413 | /** Load the private key from a memory buffer. | |||||
| 403 | 414 | |||||||
| 404 | Sets the private key corresponding to the entity certificate. | 415 | Sets the private key corresponding to the entity certificate. | |||||
| 405 | The key must match the certificate loaded via `use_certificate()` | 416 | The key must match the certificate loaded via `use_certificate()` | |||||
| 406 | or `use_certificate_chain()`. | 417 | or `use_certificate_chain()`. | |||||
| 407 | 418 | |||||||
| 408 | If the key is encrypted, set a password callback via | 419 | If the key is encrypted, set a password callback via | |||||
| 409 | `set_password_callback()` before calling this function. | 420 | `set_password_callback()` before calling this function. | |||||
| 410 | 421 | |||||||
| 411 | @param private_key The private key data. | 422 | @param private_key The private key data. | |||||
| 412 | 423 | |||||||
| 413 | @param format The encoding format of the key data. | 424 | @param format The encoding format of the key data. | |||||
| 414 | 425 | |||||||
| 415 | - | @return Success, or an error if the key could not be parsed, | 426 | + | @return Success. The key is recorded and decoded when the native | |||
| 416 | - | is encrypted without a password callback, or doesn't match | 427 | + | context is first built; a malformed key, a missing password | |||
| 417 | - | the certificate. | 428 | + | callback for an encrypted key, or a certificate mismatch | |||
| 429 | + | surfaces as a handshake failure. | ||||||
| 418 | 430 | |||||||
| 419 | @see use_private_key_file | 431 | @see use_private_key_file | |||||
| 420 | @see set_password_callback | 432 | @see set_password_callback | |||||
| 421 | */ | 433 | */ | |||||
| 422 | - | std::error_code | 434 | + | [[nodiscard]] std::error_code | |||
| 423 | use_private_key(std::string_view private_key, tls_file_format format); | 435 | use_private_key(std::string_view private_key, tls_file_format format); | |||||
| 424 | 436 | |||||||
| 425 | /** Load the private key from a file. | 437 | /** Load the private key from a file. | |||||
| 426 | 438 | |||||||
| 427 | Sets the private key corresponding to the entity certificate. | 439 | Sets the private key corresponding to the entity certificate. | |||||
| 428 | The key must match the certificate loaded via `use_certificate_file()` | 440 | The key must match the certificate loaded via `use_certificate_file()` | |||||
| 429 | or `use_certificate_chain_file()`. | 441 | or `use_certificate_chain_file()`. | |||||
| 430 | 442 | |||||||
| 431 | If the key file is encrypted, set a password callback via | 443 | If the key file is encrypted, set a password callback via | |||||
| 432 | `set_password_callback()` before calling this function. | 444 | `set_password_callback()` before calling this function. | |||||
| 433 | 445 | |||||||
| 434 | @param filename Path to the private key file. | 446 | @param filename Path to the private key file. | |||||
| 435 | 447 | |||||||
| 436 | @param format The encoding format of the file. | 448 | @param format The encoding format of the file. | |||||
| 437 | 449 | |||||||
| 438 | - | @return Success, or an error if the file could not be read, | 450 | + | @return Success, or an error if the file could not be read. The | |||
| 439 | - | the key is invalid, or it doesn't match the certificate. | 451 | + | key is decoded when the native context is first built; a | |||
| 452 | + | malformed key or a certificate mismatch surfaces as a | ||||||
| 453 | + | handshake failure. | ||||||
| 440 | 454 | |||||||
| 441 | @par Example | 455 | @par Example | |||||
| 442 | @code | 456 | @code | |||||
| 443 | - | ctx.use_private_key_file( "server.key", tls_file_format::pem ); | 457 | + | if (auto ec = ctx.use_private_key_file( | |||
| 458 | + | "server.key", tls_file_format::pem )) | ||||||
| 459 | + | return; | ||||||
| 444 | @endcode | 460 | @endcode | |||||
| 445 | 461 | |||||||
| 446 | @see use_private_key | 462 | @see use_private_key | |||||
| 447 | @see set_password_callback | 463 | @see set_password_callback | |||||
| 448 | */ | 464 | */ | |||||
| 449 | - | std::error_code | 465 | + | [[nodiscard]] std::error_code | |||
| 450 | use_private_key_file(std::string_view filename, tls_file_format format); | 466 | use_private_key_file(std::string_view filename, tls_file_format format); | |||||
| 451 | 467 | |||||||
| 452 | /** Load credentials from a PKCS#12 bundle in memory. | 468 | /** Load credentials from a PKCS#12 bundle in memory. | |||||
| 453 | 469 | |||||||
| 454 | PKCS#12 (also known as PFX) is a binary format that bundles a | 470 | PKCS#12 (also known as PFX) is a binary format that bundles a | |||||
| 455 | certificate, private key, and optionally intermediate certificates | 471 | certificate, private key, and optionally intermediate certificates | |||||
| 456 | into a single password-protected file. | 472 | into a single password-protected file. | |||||
| 457 | 473 | |||||||
| 458 | @param data The PKCS#12 bundle data. | 474 | @param data The PKCS#12 bundle data. | |||||
| 459 | 475 | |||||||
| 460 | @param passphrase The password protecting the bundle. | 476 | @param passphrase The password protecting the bundle. | |||||
| 461 | 477 | |||||||
| 462 | @return Success. The bundle is recorded and decoded into the | 478 | @return Success. The bundle is recorded and decoded into the | |||||
| 463 | certificate, private key, and chain when the native context is | 479 | certificate, private key, and chain when the native context is | |||||
| 464 | first built; a malformed bundle or wrong passphrase surfaces as | 480 | first built; a malformed bundle or wrong passphrase surfaces as | |||||
| 465 | a handshake failure. | 481 | a handshake failure. | |||||
| 466 | 482 | |||||||
| 467 | @note Intermediate certificates inside the bundle are loaded and | 483 | @note Intermediate certificates inside the bundle are loaded and | |||||
| 468 | sent during the handshake on both backends. | 484 | sent during the handshake on both backends. | |||||
| 469 | 485 | |||||||
| 470 | @see use_pkcs12_file | 486 | @see use_pkcs12_file | |||||
| 471 | */ | 487 | */ | |||||
| 472 | - | std::error_code | 488 | + | [[nodiscard]] std::error_code | |||
| 473 | use_pkcs12(std::string_view data, std::string_view passphrase); | 489 | use_pkcs12(std::string_view data, std::string_view passphrase); | |||||
| 474 | 490 | |||||||
| 475 | /** Load credentials from a PKCS#12 file. | 491 | /** Load credentials from a PKCS#12 file. | |||||
| 476 | 492 | |||||||
| 477 | PKCS#12 (also known as PFX) is a binary format that bundles a | 493 | PKCS#12 (also known as PFX) is a binary format that bundles a | |||||
| 478 | certificate, private key, and optionally intermediate certificates | 494 | certificate, private key, and optionally intermediate certificates | |||||
| 479 | into a single password-protected file. This is common on Windows | 495 | into a single password-protected file. This is common on Windows | |||||
| 480 | and for certificates exported from browsers. | 496 | and for certificates exported from browsers. | |||||
| 481 | 497 | |||||||
| 482 | @param filename Path to the PKCS#12 file. | 498 | @param filename Path to the PKCS#12 file. | |||||
| 483 | 499 | |||||||
| 484 | @param passphrase The password protecting the file. | 500 | @param passphrase The password protecting the file. | |||||
| 485 | 501 | |||||||
| 486 | @return Success, or an error if the file could not be read. The | 502 | @return Success, or an error if the file could not be read. The | |||||
| 487 | bundle is decoded when the native context is first built; a | 503 | bundle is decoded when the native context is first built; a | |||||
| 488 | malformed bundle or wrong passphrase surfaces as a handshake | 504 | malformed bundle or wrong passphrase surfaces as a handshake | |||||
| 489 | failure. | 505 | failure. | |||||
| 490 | 506 | |||||||
| 491 | @note Intermediate certificates inside the bundle are loaded and | 507 | @note Intermediate certificates inside the bundle are loaded and | |||||
| 492 | sent during the handshake on both backends. | 508 | sent during the handshake on both backends. | |||||
| 493 | 509 | |||||||
| 494 | @par Example | 510 | @par Example | |||||
| 495 | @code | 511 | @code | |||||
| 496 | - | ctx.use_pkcs12_file( "credentials.pfx", "secret" ); | 512 | + | if (auto ec = ctx.use_pkcs12_file( "credentials.pfx", "secret" )) | |||
| 513 | + | return; | ||||||
| 497 | @endcode | 514 | @endcode | |||||
| 498 | 515 | |||||||
| 499 | @see use_pkcs12 | 516 | @see use_pkcs12 | |||||
| 500 | */ | 517 | */ | |||||
| 501 | - | std::error_code | 518 | + | [[nodiscard]] std::error_code | |||
| 502 | use_pkcs12_file(std::string_view filename, std::string_view passphrase); | 519 | use_pkcs12_file(std::string_view filename, std::string_view passphrase); | |||||
| 503 | 520 | |||||||
| 504 | // | 521 | // | |||||
| 505 | // Trust Anchors | 522 | // Trust Anchors | |||||
| 506 | // | 523 | // | |||||
| 507 | 524 | |||||||
| 508 | /** Add a certificate authority for peer verification. | 525 | /** Add a certificate authority for peer verification. | |||||
| 509 | 526 | |||||||
| 510 | Adds a single CA certificate to the trust store used for verifying | 527 | Adds a single CA certificate to the trust store used for verifying | |||||
| 511 | peer certificates. Call this multiple times to add multiple CAs, | 528 | peer certificates. Call this multiple times to add multiple CAs, | |||||
| 512 | or use `load_verify_file()` for a bundle. | 529 | or use `load_verify_file()` for a bundle. | |||||
| 513 | 530 | |||||||
| 514 | @param ca The CA certificate data in PEM format. | 531 | @param ca The CA certificate data in PEM format. | |||||
| 515 | 532 | |||||||
| 516 | - | @return Success, or an error if the certificate could not be parsed. | 533 | + | @return Success. The certificate is recorded and decoded when the | |||
| 534 | + | native context is first built; a malformed certificate | ||||||
| 535 | + | surfaces as a handshake failure. | ||||||
| 517 | 536 | |||||||
| 518 | @see load_verify_file | 537 | @see load_verify_file | |||||
| 519 | @see set_default_verify_paths | 538 | @see set_default_verify_paths | |||||
| 520 | */ | 539 | */ | |||||
| 521 | - | std::error_code add_certificate_authority(std::string_view ca); | 540 | + | [[nodiscard]] std::error_code add_certificate_authority(std::string_view ca); | |||
| 522 | 541 | |||||||
| 523 | /** Load CA certificates from a file. | 542 | /** Load CA certificates from a file. | |||||
| 524 | 543 | |||||||
| 525 | Loads one or more CA certificates from a PEM file. The file may | 544 | Loads one or more CA certificates from a PEM file. The file may | |||||
| 526 | contain multiple concatenated PEM certificates. | 545 | contain multiple concatenated PEM certificates. | |||||
| 527 | 546 | |||||||
| 528 | @param filename Path to a PEM file containing CA certificates. | 547 | @param filename Path to a PEM file containing CA certificates. | |||||
| 529 | 548 | |||||||
| 530 | - | @return Success, or an error if the file could not be read or parsed. | 549 | + | @return Success, or an error if the file could not be read. The | |||
| 550 | + | certificates are decoded when the native context is first | ||||||
| 551 | + | built; malformed certificates surface as a handshake failure. | ||||||
| 531 | 552 | |||||||
| 532 | @par Example | 553 | @par Example | |||||
| 533 | @code | 554 | @code | |||||
| 534 | - | // Load a custom CA bundle | 555 | + | if (auto ec = ctx.load_verify_file( | |||
| 535 | - | ctx.load_verify_file( "/etc/ssl/certs/ca-certificates.crt" ); | 556 | + | "/etc/ssl/certs/ca-certificates.crt" )) | |||
| 557 | + | return; | ||||||
| 536 | @endcode | 558 | @endcode | |||||
| 537 | 559 | |||||||
| 538 | @see add_certificate_authority | 560 | @see add_certificate_authority | |||||
| 539 | @see add_verify_path | 561 | @see add_verify_path | |||||
| 540 | */ | 562 | */ | |||||
| 541 | - | std::error_code load_verify_file(std::string_view filename); | 563 | + | [[nodiscard]] std::error_code load_verify_file(std::string_view filename); | |||
| 542 | 564 | |||||||
| 543 | /** Add a directory of CA certificates for verification. | 565 | /** Add a directory of CA certificates for verification. | |||||
| 544 | 566 | |||||||
| 545 | Adds a directory of CA certificates to the trust store. The | 567 | Adds a directory of CA certificates to the trust store. The | |||||
| 546 | directory is applied when the native context is first built from | 568 | directory is applied when the native context is first built from | |||||
| 547 | this context. | 569 | this context. | |||||
| 548 | 570 | |||||||
| 549 | The expected directory layout depends on the backend. OpenSSL | 571 | The expected directory layout depends on the backend. OpenSSL | |||||
| 550 | performs on-demand lookups and requires each certificate file to | 572 | performs on-demand lookups and requires each certificate file to | |||||
| 551 | be named by its subject-name hash (as generated by | 573 | be named by its subject-name hash (as generated by | |||||
| 552 | `openssl rehash` or `c_rehash`); WolfSSL loads every certificate | 574 | `openssl rehash` or `c_rehash`); WolfSSL loads every certificate | |||||
| 553 | file in the directory. | 575 | file in the directory. | |||||
| 554 | 576 | |||||||
| 555 | @param path Path to the directory of CA certificates. | 577 | @param path Path to the directory of CA certificates. | |||||
| 556 | 578 | |||||||
| 557 | @return Success. The path is recorded and applied when the native | 579 | @return Success. The path is recorded and applied when the native | |||||
| 558 | context is built; a directory that cannot be read at that time | 580 | context is built; a directory that cannot be read at that time | |||||
| 559 | is skipped rather than reported here. | 581 | is skipped rather than reported here. | |||||
| 560 | 582 | |||||||
| 561 | @par Example | 583 | @par Example | |||||
| 562 | @code | 584 | @code | |||||
| 563 | - | ctx.add_verify_path( "/etc/ssl/certs" ); | 585 | + | if (auto ec = ctx.add_verify_path( "/etc/ssl/certs" )) | |||
| 586 | + | return; | ||||||
| 564 | @endcode | 587 | @endcode | |||||
| 565 | 588 | |||||||
| 566 | @see load_verify_file | 589 | @see load_verify_file | |||||
| 567 | @see set_default_verify_paths | 590 | @see set_default_verify_paths | |||||
| 568 | */ | 591 | */ | |||||
| 569 | - | std::error_code add_verify_path(std::string_view path); | 592 | + | [[nodiscard]] std::error_code add_verify_path(std::string_view path); | |||
| 570 | 593 | |||||||
| 571 | /** Use the system default CA certificate store. | 594 | /** Use the system default CA certificate store. | |||||
| 572 | 595 | |||||||
| 573 | Configures the context to use the operating system's default | 596 | Configures the context to use the operating system's default | |||||
| 574 | trust store for peer certificate verification. This is the | 597 | trust store for peer certificate verification. This is the | |||||
| 575 | recommended approach for HTTPS clients connecting to public | 598 | recommended approach for HTTPS clients connecting to public | |||||
| 576 | servers. | 599 | servers. | |||||
| 577 | 600 | |||||||
| 578 | The system store is loaded when the native context is first built | 601 | The system store is loaded when the native context is first built | |||||
| 579 | from this context. For a verified-safe client, combine this with | 602 | from this context. For a verified-safe client, combine this with | |||||
| 580 | `set_verify_mode( tls_verify_mode::peer )` and, when connecting by | 603 | `set_verify_mode( tls_verify_mode::peer )` and, when connecting by | |||||
| 581 | name, `tls_stream::set_hostname()`. | 604 | name, `tls_stream::set_hostname()`. | |||||
| 582 | 605 | |||||||
| 583 | @return Success. The request is recorded and applied when the | 606 | @return Success. The request is recorded and applied when the | |||||
| 584 | native context is built; if the system store cannot be loaded | 607 | native context is built; if the system store cannot be loaded | |||||
| 585 | at that time it is skipped rather than reported here, so a | 608 | at that time it is skipped rather than reported here, so a | |||||
| 586 | context that must reject unverified peers should also use | 609 | context that must reject unverified peers should also use | |||||
| 587 | `set_verify_mode( tls_verify_mode::peer )`. | 610 | `set_verify_mode( tls_verify_mode::peer )`. | |||||
| 588 | 611 | |||||||
| 589 | @note The OpenSSL backend honors the `SSL_CERT_FILE` and | 612 | @note The OpenSSL backend honors the `SSL_CERT_FILE` and | |||||
| 590 | `SSL_CERT_DIR` environment variables. The WolfSSL backend | 613 | `SSL_CERT_DIR` environment variables. The WolfSSL backend | |||||
| 591 | requires a build with `WOLFSSL_SYS_CA_CERTS`; without it the | 614 | requires a build with `WOLFSSL_SYS_CA_CERTS`; without it the | |||||
| 592 | system store is unavailable and this call has no effect. | 615 | system store is unavailable and this call has no effect. | |||||
| 593 | 616 | |||||||
| 594 | @par Example | 617 | @par Example | |||||
| 595 | @code | 618 | @code | |||||
| 596 | // Trust the same CAs as the system | 619 | // Trust the same CAs as the system | |||||
| 597 | - | ctx.set_default_verify_paths(); | 620 | + | if (auto ec = ctx.set_default_verify_paths()) | |||
| 598 | - | ctx.set_verify_mode( tls_verify_mode::peer ); | 621 | + | return; | |||
| 622 | + | if (auto ec = ctx.set_verify_mode( tls_verify_mode::peer )) | ||||||
| 623 | + | return; | ||||||
| 599 | @endcode | 624 | @endcode | |||||
| 600 | 625 | |||||||
| 601 | @see load_verify_file | 626 | @see load_verify_file | |||||
| 602 | @see add_verify_path | 627 | @see add_verify_path | |||||
| 603 | @see set_verify_mode | 628 | @see set_verify_mode | |||||
| 604 | */ | 629 | */ | |||||
| 605 | - | std::error_code set_default_verify_paths(); | 630 | + | [[nodiscard]] std::error_code set_default_verify_paths(); | |||
| 606 | 631 | |||||||
| 607 | // | 632 | // | |||||
| 608 | // Protocol Configuration | 633 | // Protocol Configuration | |||||
| 609 | // | 634 | // | |||||
| 610 | 635 | |||||||
| 611 | /** Set the minimum TLS protocol version. | 636 | /** Set the minimum TLS protocol version. | |||||
| 612 | 637 | |||||||
| 613 | Connections will reject protocol versions older than this. | 638 | Connections will reject protocol versions older than this. | |||||
| 614 | The default allows TLS 1.2 and newer. | 639 | The default allows TLS 1.2 and newer. | |||||
| 615 | 640 | |||||||
| 616 | @param v The minimum protocol version to accept. | 641 | @param v The minimum protocol version to accept. | |||||
| 617 | 642 | |||||||
| 618 | - | @return Success, or an error if the version is not supported | 643 | + | @return Success. The version is recorded and applied when the | |||
| 619 | - | by the backend. | 644 | + | native context is first built. | |||
| 620 | 645 | |||||||
| 621 | @par Example | 646 | @par Example | |||||
| 622 | @code | 647 | @code | |||||
| 623 | // Require TLS 1.3 minimum | 648 | // Require TLS 1.3 minimum | |||||
| 624 | - | ctx.set_min_protocol_version( tls_version::tls_1_3 ); | 649 | + | if (auto ec = ctx.set_min_protocol_version( tls_version::tls_1_3 )) | |||
| 650 | + | return; | ||||||
| 625 | @endcode | 651 | @endcode | |||||
| 626 | 652 | |||||||
| 627 | @see set_max_protocol_version | 653 | @see set_max_protocol_version | |||||
| 628 | */ | 654 | */ | |||||
| 629 | - | std::error_code set_min_protocol_version(tls_version v); | 655 | + | [[nodiscard]] std::error_code set_min_protocol_version(tls_version v); | |||
| 630 | 656 | |||||||
| 631 | /** Set the maximum TLS protocol version. | 657 | /** Set the maximum TLS protocol version. | |||||
| 632 | 658 | |||||||
| 633 | Connections will not negotiate protocol versions newer than this. | 659 | Connections will not negotiate protocol versions newer than this. | |||||
| 634 | The default allows the newest supported version. | 660 | The default allows the newest supported version. | |||||
| 635 | 661 | |||||||
| 636 | @param v The maximum protocol version to accept. | 662 | @param v The maximum protocol version to accept. | |||||
| 637 | 663 | |||||||
| 638 | - | @return Success, or an error if the version is not supported | 664 | + | @return Success. The version is recorded and applied when the | |||
| 639 | - | by the backend. | 665 | + | native context is first built. | |||
| 640 | 666 | |||||||
| 641 | @note On WolfSSL the ceiling is applied by selecting a | 667 | @note On WolfSSL the ceiling is applied by selecting a | |||||
| 642 | version-specific method (no native set-max API exists); an | 668 | version-specific method (no native set-max API exists); an | |||||
| 643 | invalid window where the minimum exceeds the maximum yields a | 669 | invalid window where the minimum exceeds the maximum yields a | |||||
| 644 | context that fails the handshake. | 670 | context that fails the handshake. | |||||
| 645 | 671 | |||||||
| 646 | @see set_min_protocol_version | 672 | @see set_min_protocol_version | |||||
| 647 | */ | 673 | */ | |||||
| 648 | - | std::error_code set_max_protocol_version(tls_version v); | 674 | + | [[nodiscard]] std::error_code set_max_protocol_version(tls_version v); | |||
| 649 | 675 | |||||||
| 650 | /** Set the allowed cipher suites. | 676 | /** Set the allowed cipher suites. | |||||
| 651 | 677 | |||||||
| 652 | Configures which cipher suites may be used for connections. | 678 | Configures which cipher suites may be used for connections. | |||||
| 653 | The format is backend-specific but typically follows OpenSSL | 679 | The format is backend-specific but typically follows OpenSSL | |||||
| 654 | cipher list syntax. | 680 | cipher list syntax. | |||||
| 655 | 681 | |||||||
| 656 | @param ciphers The cipher suite specification string. | 682 | @param ciphers The cipher suite specification string. | |||||
| 657 | 683 | |||||||
| 658 | - | @return Success, or an error if the cipher string is invalid. | 684 | + | @return Success. The string is recorded and applied when the | |||
| 685 | + | native context is first built; an invalid cipher string | ||||||
| 686 | + | surfaces as a handshake failure. | ||||||
| 659 | 687 | |||||||
| 660 | @par Example | 688 | @par Example | |||||
| 661 | @code | 689 | @code | |||||
| 662 | // TLS 1.2 cipher suites (OpenSSL format) | 690 | // TLS 1.2 cipher suites (OpenSSL format) | |||||
| 663 | - | ctx.set_ciphersuites( "ECDHE+AESGCM:ECDHE+CHACHA20" ); | 691 | + | if (auto ec = ctx.set_ciphersuites( "ECDHE+AESGCM:ECDHE+CHACHA20" )) | |||
| 692 | + | return; | ||||||
| 664 | @endcode | 693 | @endcode | |||||
| 665 | 694 | |||||||
| 666 | @note This configures cipher suites for TLS 1.2 and below. For | 695 | @note This configures cipher suites for TLS 1.2 and below. For | |||||
| 667 | TLS 1.3, use @ref set_ciphersuites_tls13. | 696 | TLS 1.3, use @ref set_ciphersuites_tls13. | |||||
| 668 | */ | 697 | */ | |||||
| 669 | - | std::error_code set_ciphersuites(std::string_view ciphers); | 698 | + | [[nodiscard]] std::error_code set_ciphersuites(std::string_view ciphers); | |||
| 670 | 699 | |||||||
| 671 | /** Set the allowed TLS 1.3 cipher suites. | 700 | /** Set the allowed TLS 1.3 cipher suites. | |||||
| 672 | 701 | |||||||
| 673 | TLS 1.3 uses a distinct, fixed set of cipher suites configured | 702 | TLS 1.3 uses a distinct, fixed set of cipher suites configured | |||||
| 674 | separately from earlier versions. The format is a colon-separated | 703 | separately from earlier versions. The format is a colon-separated | |||||
| 675 | list of TLS 1.3 suite names. | 704 | list of TLS 1.3 suite names. | |||||
| 676 | 705 | |||||||
| 677 | @param ciphers The TLS 1.3 cipher suite list. | 706 | @param ciphers The TLS 1.3 cipher suite list. | |||||
| 678 | 707 | |||||||
| 679 | - | @return Success, or an error if the cipher string is invalid. | 708 | + | @return Success. The string is recorded and applied when the | |||
| 709 | + | native context is first built; an invalid cipher string | ||||||
| 710 | + | surfaces as a handshake failure. | ||||||
| 680 | 711 | |||||||
| 681 | @par Example | 712 | @par Example | |||||
| 682 | @code | 713 | @code | |||||
| 683 | - | ctx.set_ciphersuites_tls13( | 714 | + | if (auto ec = ctx.set_ciphersuites_tls13( | |||
| 684 | - | "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256" ); | 715 | + | "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256" )) | |||
| 716 | + | return; | ||||||
| 685 | @endcode | 717 | @endcode | |||||
| 686 | 718 | |||||||
| 687 | @note On the WolfSSL backend, TLS 1.2 and TLS 1.3 suites share a | 719 | @note On the WolfSSL backend, TLS 1.2 and TLS 1.3 suites share a | |||||
| 688 | single cipher list; this call and @ref set_ciphersuites are | 720 | single cipher list; this call and @ref set_ciphersuites are | |||||
| 689 | merged into one list. | 721 | merged into one list. | |||||
| 690 | 722 | |||||||
| 691 | @see set_ciphersuites | 723 | @see set_ciphersuites | |||||
| 692 | */ | 724 | */ | |||||
| 693 | - | std::error_code set_ciphersuites_tls13(std::string_view ciphers); | 725 | + | [[nodiscard]] std::error_code set_ciphersuites_tls13(std::string_view ciphers); | |||
| 694 | 726 | |||||||
| 695 | /** Set the ALPN protocol list. | 727 | /** Set the ALPN protocol list. | |||||
| 696 | 728 | |||||||
| 697 | Configures Application-Layer Protocol Negotiation (ALPN) for | 729 | Configures Application-Layer Protocol Negotiation (ALPN) for | |||||
| 698 | the connection. ALPN is used to negotiate which application | 730 | the connection. ALPN is used to negotiate which application | |||||
| 699 | protocol to use over the TLS connection (e.g., "h2" for HTTP/2, | 731 | protocol to use over the TLS connection (e.g., "h2" for HTTP/2, | |||||
| 700 | "http/1.1" for HTTP/1.1). | 732 | "http/1.1" for HTTP/1.1). | |||||
| 701 | 733 | |||||||
| 702 | The protocols are tried in preference order (first = highest). | 734 | The protocols are tried in preference order (first = highest). | |||||
| 703 | 735 | |||||||
| 704 | @param protocols Ordered list of protocol identifiers. | 736 | @param protocols Ordered list of protocol identifiers. | |||||
| 705 | 737 | |||||||
| 706 | @return Success, or an error if ALPN configuration fails. | 738 | @return Success, or an error if ALPN configuration fails. | |||||
| 707 | 739 | |||||||
| 708 | @note Read the negotiated protocol after the handshake via | 740 | @note Read the negotiated protocol after the handshake via | |||||
| 709 | @ref tls_stream::alpn_protocol. On WolfSSL, ALPN requires a | 741 | @ref tls_stream::alpn_protocol. On WolfSSL, ALPN requires a | |||||
| 710 | build with `HAVE_ALPN`; without it, offering protocols fails | 742 | build with `HAVE_ALPN`; without it, offering protocols fails | |||||
| 711 | the handshake with `std::errc::function_not_supported` rather | 743 | the handshake with `std::errc::function_not_supported` rather | |||||
| 712 | than negotiate nothing silently. | 744 | than negotiate nothing silently. | |||||
| 713 | 745 | |||||||
| 714 | @par Example | 746 | @par Example | |||||
| 715 | @code | 747 | @code | |||||
| 716 | // Prefer HTTP/2, fall back to HTTP/1.1 | 748 | // Prefer HTTP/2, fall back to HTTP/1.1 | |||||
| 717 | - | ctx.set_alpn( { "h2", "http/1.1" } ); | 749 | + | if (auto ec = ctx.set_alpn( { "h2", "http/1.1" } )) | |||
| 750 | + | return; | ||||||
| 718 | @endcode | 751 | @endcode | |||||
| 719 | */ | 752 | */ | |||||
| 720 | - | std::error_code set_alpn(std::initializer_list<std::string_view> protocols); | 753 | + | [[nodiscard]] std::error_code set_alpn(std::initializer_list<std::string_view> protocols); | |||
| 721 | 754 | |||||||
| 722 | // | 755 | // | |||||
| 723 | // Certificate Verification | 756 | // Certificate Verification | |||||
| 724 | // | 757 | // | |||||
| 725 | 758 | |||||||
| 726 | /** Set the peer certificate verification mode. | 759 | /** Set the peer certificate verification mode. | |||||
| 727 | 760 | |||||||
| 728 | Controls whether and how peer certificates are verified during | 761 | Controls whether and how peer certificates are verified during | |||||
| 729 | the TLS handshake. | 762 | the TLS handshake. | |||||
| 730 | 763 | |||||||
| 731 | @param mode The verification mode to use. | 764 | @param mode The verification mode to use. | |||||
| 732 | 765 | |||||||
| 733 | - | @return Success, or an error if the mode could not be set. | 766 | + | @return Success. The mode is recorded and applied when the native | |||
| 767 | + | context is first built. | ||||||
| 734 | 768 | |||||||
| 735 | @par Example | 769 | @par Example | |||||
| 736 | @code | 770 | @code | |||||
| 737 | - | // Verify peer certificate (typical for clients) | 771 | + | // Verify peer certificate (typical for clients; servers doing | |||
| 738 | - | ctx.set_verify_mode( tls_verify_mode::peer ); | 772 | + | // mTLS use tls_verify_mode::require_peer instead) | |||
| 739 | - | 773 | + | if (auto ec = ctx.set_verify_mode( tls_verify_mode::peer )) | ||||
| 740 | - | // Require client certificate (server-side mTLS) | 774 | + | return; | |||
| 741 | - | ctx.set_verify_mode( tls_verify_mode::require_peer ); | ||||||
| 742 | @endcode | 775 | @endcode | |||||
| 743 | 776 | |||||||
| 744 | @see tls_verify_mode | 777 | @see tls_verify_mode | |||||
| 745 | */ | 778 | */ | |||||
| 746 | - | std::error_code set_verify_mode(tls_verify_mode mode); | 779 | + | [[nodiscard]] std::error_code set_verify_mode(tls_verify_mode mode); | |||
| 747 | 780 | |||||||
| 748 | /** Set the maximum certificate chain verification depth. | 781 | /** Set the maximum certificate chain verification depth. | |||||
| 749 | 782 | |||||||
| 750 | Limits how many intermediate certificates can appear between | 783 | Limits how many intermediate certificates can appear between | |||||
| 751 | the peer certificate and a trusted root. The default is | 784 | the peer certificate and a trusted root. The default is | |||||
| 752 | typically 100, which is sufficient for most certificate chains. | 785 | typically 100, which is sufficient for most certificate chains. | |||||
| 753 | 786 | |||||||
| 754 | @param depth Maximum number of intermediate certificates allowed. | 787 | @param depth Maximum number of intermediate certificates allowed. | |||||
| 755 | 788 | |||||||
| 756 | - | @return Success, or an error if the depth is invalid. | 789 | + | @return Success. The depth is recorded and applied when the native | |||
| 790 | + | context is first built. | ||||||
| 757 | */ | 791 | */ | |||||
| 758 | - | std::error_code set_verify_depth(int depth); | 792 | + | [[nodiscard]] std::error_code set_verify_depth(int depth); | |||
| 759 | 793 | |||||||
| 760 | /** Set a custom certificate verification callback. | 794 | /** Set a custom certificate verification callback. | |||||
| 761 | 795 | |||||||
| 762 | Installs a callback that is invoked during certificate chain | 796 | Installs a callback that is invoked during certificate chain | |||||
| 763 | verification. The callback can perform additional validation | 797 | verification. The callback can perform additional validation | |||||
| 764 | beyond the standard checks and can override verification | 798 | beyond the standard checks and can override verification | |||||
| 765 | results. | 799 | results. | |||||
| 766 | 800 | |||||||
| 767 | The callback receives the built-in verification result so far and | 801 | The callback receives the built-in verification result so far and | |||||
| 768 | a verify_context describing the certificate being verified. Return | 802 | a verify_context describing the certificate being verified. Return | |||||
| 769 | `true` to accept the certificate, `false` to reject. Inspect the | 803 | `true` to accept the certificate, `false` to reject. Inspect the | |||||
| 770 | certificate portably via `verify_context::certificate()` (its DER | 804 | certificate portably via `verify_context::certificate()` (its DER | |||||
| 771 | encoding) — for example to pin a specific certificate. | 805 | encoding) — for example to pin a specific certificate. | |||||
| 772 | 806 | |||||||
| 773 | @par Backend Support | 807 | @par Backend Support | |||||
| 774 | 808 | |||||||
| 775 | The exact set of certificates the callback sees differs by backend: | 809 | The exact set of certificates the callback sees differs by backend: | |||||
| 776 | 810 | |||||||
| 777 | - OpenSSL: the callback runs once per certificate in the chain, | 811 | - OpenSSL: the callback runs once per certificate in the chain, | |||||
| 778 | including certificates that passed the built-in checks. It can | 812 | including certificates that passed the built-in checks. It can | |||||
| 779 | therefore both relax verification (return `true` for a | 813 | therefore both relax verification (return `true` for a | |||||
| 780 | certificate the library rejected) and tighten it (return `false` | 814 | certificate the library rejected) and tighten it (return `false` | |||||
| 781 | for a certificate the library accepted, e.g. pinning). | 815 | for a certificate the library accepted, e.g. pinning). | |||||
| 782 | - WolfSSL built with `WOLFSSL_ALWAYS_VERIFY_CB` (implied by | 816 | - WolfSSL built with `WOLFSSL_ALWAYS_VERIFY_CB` (implied by | |||||
| 783 | `--enable-opensslextra`): same as OpenSSL. | 817 | `--enable-opensslextra`): same as OpenSSL. | |||||
| 784 | - WolfSSL without that option: the library invokes the callback | 818 | - WolfSSL without that option: the library invokes the callback | |||||
| 785 | only on verification *failure*, so it cannot be honored on a | 819 | only on verification *failure*, so it cannot be honored on a | |||||
| 786 | successful handshake. To avoid silently ignoring a | 820 | successful handshake. To avoid silently ignoring a | |||||
| 787 | verification-tightening callback (which would fail open), a | 821 | verification-tightening callback (which would fail open), a | |||||
| 788 | context that carries a callback instead **fails the handshake** | 822 | context that carries a callback instead **fails the handshake** | |||||
| 789 | with `std::errc::function_not_supported` on such a build. Rebuild | 823 | with `std::errc::function_not_supported` on such a build. Rebuild | |||||
| 790 | WolfSSL with `WOLFSSL_ALWAYS_VERIFY_CB`, or omit the callback. | 824 | WolfSSL with `WOLFSSL_ALWAYS_VERIFY_CB`, or omit the callback. | |||||
| 791 | 825 | |||||||
| 792 | @tparam Callback A callable with signature | 826 | @tparam Callback A callable with signature | |||||
| 793 | `bool( bool preverified, verify_context& ctx )`. | 827 | `bool( bool preverified, verify_context& ctx )`. | |||||
| 794 | 828 | |||||||
| 795 | - | @param callback The verification callback. | 829 | + | @param callback The verification callback. Recorded here and | |||
| 796 | - | 830 | + | applied during the handshake; on a WolfSSL build that | ||||
| 797 | - | @return Success. The callback is recorded here and applied during the | 831 | + | cannot honor it, the handshake fails with | |||
| 798 | - | handshake. On a WolfSSL build that cannot honor it, the handshake | 832 | + | `std::errc::function_not_supported` (see Backend Support). | |||
| 799 | - | fails with `std::errc::function_not_supported` (see Backend | ||||||
| 800 | - | Support). | ||||||
| 801 | 833 | |||||||
| 802 | @par Example | 834 | @par Example | |||||
| 803 | @code | 835 | @code | |||||
| 804 | - | ctx.set_verify_mode( tls_verify_mode::peer ); | 836 | + | if (auto ec = ctx.set_verify_mode( tls_verify_mode::peer )) | |||
| 837 | + | return; | ||||||
| 805 | ctx.set_verify_callback( | 838 | ctx.set_verify_callback( | |||||
| 806 | []( bool preverified, verify_context& ctx ) -> bool | 839 | []( bool preverified, verify_context& ctx ) -> bool | |||||
| 807 | { | 840 | { | |||||
| 808 | if( ! preverified ) | 841 | if( ! preverified ) | |||||
| 809 | return false; | 842 | return false; | |||||
| 810 | // Pin: accept only a certificate whose DER matches. | 843 | // Pin: accept only a certificate whose DER matches. | |||||
| 811 | auto der = ctx.certificate(); | 844 | auto der = ctx.certificate(); | |||||
| 812 | return der.size() == expected_pin.size() && | 845 | return der.size() == expected_pin.size() && | |||||
| 813 | std::equal( der.begin(), der.end(), expected_pin.begin() ); | 846 | std::equal( der.begin(), der.end(), expected_pin.begin() ); | |||||
| 814 | }); | 847 | }); | |||||
| 815 | @endcode | 848 | @endcode | |||||
| 816 | 849 | |||||||
| 817 | @see verify_context | 850 | @see verify_context | |||||
| 818 | @see set_verify_mode | 851 | @see set_verify_mode | |||||
| 819 | */ | 852 | */ | |||||
| 820 | template<typename Callback> | 853 | template<typename Callback> | |||||
| 821 | - | std::error_code set_verify_callback(Callback callback); | 854 | + | void set_verify_callback(Callback callback); | |||
| 822 | 855 | |||||||
| 823 | /** Set a callback for Server Name Indication (SNI). | 856 | /** Set a callback for Server Name Indication (SNI). | |||||
| 824 | 857 | |||||||
| 825 | For server connections, this callback is invoked during the TLS | 858 | For server connections, this callback is invoked during the TLS | |||||
| 826 | handshake when a client sends an SNI extension. The callback | 859 | handshake when a client sends an SNI extension. The callback | |||||
| 827 | receives the requested hostname and can accept or reject the | 860 | receives the requested hostname and can accept or reject the | |||||
| 828 | connection. | 861 | connection. | |||||
| 829 | 862 | |||||||
| 830 | @tparam Callback A callable with signature | 863 | @tparam Callback A callable with signature | |||||
| 831 | `bool( std::string_view hostname )`. | 864 | `bool( std::string_view hostname )`. | |||||
| 832 | 865 | |||||||
| 833 | @param callback The SNI callback. Return `true` to accept the | 866 | @param callback The SNI callback. Return `true` to accept the | |||||
| 834 | connection or `false` to reject it with an alert. | 867 | connection or `false` to reject it with an alert. | |||||
| 835 | 868 | |||||||
| 836 | @par Example | 869 | @par Example | |||||
| 837 | @code | 870 | @code | |||||
| 838 | // Accept connections for specific domains only | 871 | // Accept connections for specific domains only | |||||
| 839 | ctx.set_servername_callback( | 872 | ctx.set_servername_callback( | |||||
| 840 | []( std::string_view hostname ) -> bool | 873 | []( std::string_view hostname ) -> bool | |||||
| 841 | { | 874 | { | |||||
| 842 | return hostname == "api.example.com" || | 875 | return hostname == "api.example.com" || | |||||
| 843 | hostname == "www.example.com"; | 876 | hostname == "www.example.com"; | |||||
| 844 | }); | 877 | }); | |||||
| 845 | @endcode | 878 | @endcode | |||||
| 846 | 879 | |||||||
| 847 | @note For virtual hosting with different certificates per hostname, | 880 | @note For virtual hosting with different certificates per hostname, | |||||
| 848 | create separate contexts and select the appropriate one before | 881 | create separate contexts and select the appropriate one before | |||||
| 849 | creating the TLS stream. | 882 | creating the TLS stream. | |||||
| 850 | 883 | |||||||
| 851 | @see tls_stream::set_hostname | 884 | @see tls_stream::set_hostname | |||||
| 852 | */ | 885 | */ | |||||
| 853 | template<typename Callback> | 886 | template<typename Callback> | |||||
| 854 | void set_servername_callback(Callback callback); | 887 | void set_servername_callback(Callback callback); | |||||
| 855 | 888 | |||||||
| 856 | private: | 889 | private: | |||||
| 857 | void set_servername_callback_impl( | 890 | void set_servername_callback_impl( | |||||
| 858 | std::function<bool(std::string_view)> callback); | 891 | std::function<bool(std::string_view)> callback); | |||||
| 859 | 892 | |||||||
| 860 | void set_password_callback_impl( | 893 | void set_password_callback_impl( | |||||
| 861 | std::function<std::string(std::size_t, tls_password_purpose)> callback); | 894 | std::function<std::string(std::size_t, tls_password_purpose)> callback); | |||||
| 862 | 895 | |||||||
| 863 | void set_verify_callback_impl( | 896 | void set_verify_callback_impl( | |||||
| 864 | std::function<bool(bool, verify_context&)> callback); | 897 | std::function<bool(bool, verify_context&)> callback); | |||||
| 865 | 898 | |||||||
| 866 | public: | 899 | public: | |||||
| 867 | // | 900 | // | |||||
| 868 | // Revocation Checking | 901 | // Revocation Checking | |||||
| 869 | // | 902 | // | |||||
| 870 | 903 | |||||||
| 871 | /** Add a Certificate Revocation List from memory. | 904 | /** Add a Certificate Revocation List from memory. | |||||
| 872 | 905 | |||||||
| 873 | Adds a CRL to the verification store for checking whether | 906 | Adds a CRL to the verification store for checking whether | |||||
| 874 | certificates have been revoked. CRLs are typically fetched | 907 | certificates have been revoked. CRLs are typically fetched | |||||
| 875 | from the URLs in a certificate's CRL Distribution Points | 908 | from the URLs in a certificate's CRL Distribution Points | |||||
| 876 | extension. | 909 | extension. | |||||
| 877 | 910 | |||||||
| 878 | @param crl The CRL data in DER or PEM format. | 911 | @param crl The CRL data in DER or PEM format. | |||||
| 879 | 912 | |||||||
| 880 | - | @return Success, or an error if the CRL could not be parsed. | 913 | + | @return Success. The CRL is recorded and decoded when the native | |||
| 914 | + | context is first built; a malformed CRL surfaces as a | ||||||
| 915 | + | handshake failure. | ||||||
| 881 | 916 | |||||||
| 882 | @note CRLs are consulted only when a revocation policy is set via | 917 | @note CRLs are consulted only when a revocation policy is set via | |||||
| 883 | @ref set_revocation_policy. On WolfSSL, CRL checking requires a | 918 | @ref set_revocation_policy. On WolfSSL, CRL checking requires a | |||||
| 884 | build with `HAVE_CRL`; without it, supplying a CRL or a | 919 | build with `HAVE_CRL`; without it, supplying a CRL or a | |||||
| 885 | revocation policy fails the handshake with | 920 | revocation policy fails the handshake with | |||||
| 886 | `std::errc::function_not_supported`. | 921 | `std::errc::function_not_supported`. | |||||
| 887 | 922 | |||||||
| 888 | @see add_crl_file | 923 | @see add_crl_file | |||||
| 889 | @see set_revocation_policy | 924 | @see set_revocation_policy | |||||
| 890 | */ | 925 | */ | |||||
| 891 | - | std::error_code add_crl(std::string_view crl); | 926 | + | [[nodiscard]] std::error_code add_crl(std::string_view crl); | |||
| 892 | 927 | |||||||
| 893 | /** Add a Certificate Revocation List from a file. | 928 | /** Add a Certificate Revocation List from a file. | |||||
| 894 | 929 | |||||||
| 895 | Adds a CRL to the verification store for checking whether | 930 | Adds a CRL to the verification store for checking whether | |||||
| 896 | certificates have been revoked. | 931 | certificates have been revoked. | |||||
| 897 | 932 | |||||||
| 898 | @param filename Path to a CRL file (DER or PEM format). | 933 | @param filename Path to a CRL file (DER or PEM format). | |||||
| 899 | 934 | |||||||
| 900 | - | @return Success, or an error if the file could not be read | 935 | + | @return Success, or an error if the file could not be read. The | |||
| 901 | - | or the CRL is invalid. | 936 | + | CRL is decoded when the native context is first built; a | |||
| 937 | + | malformed CRL surfaces as a handshake failure. | ||||||
| 902 | 938 | |||||||
| 903 | @note CRLs are consulted only when a revocation policy is set via | 939 | @note CRLs are consulted only when a revocation policy is set via | |||||
| 904 | @ref set_revocation_policy (WolfSSL requires a `HAVE_CRL` | 940 | @ref set_revocation_policy (WolfSSL requires a `HAVE_CRL` | |||||
| 905 | build). | 941 | build). | |||||
| 906 | 942 | |||||||
| 907 | @par Example | 943 | @par Example | |||||
| 908 | @code | 944 | @code | |||||
| 909 | - | ctx.add_crl_file( "issuer.crl" ); | 945 | + | if (auto ec = ctx.add_crl_file( "issuer.crl" )) | |||
| 946 | + | return; | ||||||
| 910 | @endcode | 947 | @endcode | |||||
| 911 | 948 | |||||||
| 912 | @see add_crl | 949 | @see add_crl | |||||
| 913 | @see set_revocation_policy | 950 | @see set_revocation_policy | |||||
| 914 | */ | 951 | */ | |||||
| 915 | - | std::error_code add_crl_file(std::string_view filename); | 952 | + | [[nodiscard]] std::error_code add_crl_file(std::string_view filename); | |||
| 916 | 953 | |||||||
| 917 | /** Set the certificate revocation checking policy. | 954 | /** Set the certificate revocation checking policy. | |||||
| 918 | 955 | |||||||
| 919 | Controls how certificate revocation status is checked during | 956 | Controls how certificate revocation status is checked during | |||||
| 920 | verification via CRLs. | 957 | verification via CRLs. | |||||
| 921 | 958 | |||||||
| 922 | @param policy The revocation checking policy. | 959 | @param policy The revocation checking policy. | |||||
| 923 | 960 | |||||||
| 924 | @par Example | 961 | @par Example | |||||
| 925 | @code | 962 | @code | |||||
| 926 | // Require successful revocation check | 963 | // Require successful revocation check | |||||
| 927 | ctx.set_revocation_policy( tls_revocation_policy::hard_fail ); | 964 | ctx.set_revocation_policy( tls_revocation_policy::hard_fail ); | |||||
| 928 | 965 | |||||||
| 929 | // Check but allow unknown status | 966 | // Check but allow unknown status | |||||
| 930 | ctx.set_revocation_policy( tls_revocation_policy::soft_fail ); | 967 | ctx.set_revocation_policy( tls_revocation_policy::soft_fail ); | |||||
| 931 | @endcode | 968 | @endcode | |||||
| 932 | 969 | |||||||
| 933 | @note Revocation is checked via CRLs supplied with @ref add_crl / | 970 | @note Revocation is checked via CRLs supplied with @ref add_crl / | |||||
| 934 | @ref add_crl_file. `soft_fail` accepts a certificate whose | 971 | @ref add_crl_file. `soft_fail` accepts a certificate whose | |||||
| 935 | status cannot be determined (missing/expired CRL) but rejects | 972 | status cannot be determined (missing/expired CRL) but rejects | |||||
| 936 | one that is actually revoked; `hard_fail` also rejects unknown | 973 | one that is actually revoked; `hard_fail` also rejects unknown | |||||
| 937 | status. OCSP-based revocation is not available (see the TLS | 974 | status. OCSP-based revocation is not available (see the TLS | |||||
| 938 | guide). On WolfSSL a non-disabled policy requires a `HAVE_CRL` | 975 | guide). On WolfSSL a non-disabled policy requires a `HAVE_CRL` | |||||
| 939 | build, else the handshake fails with | 976 | build, else the handshake fails with | |||||
| 940 | `std::errc::function_not_supported`. | 977 | `std::errc::function_not_supported`. | |||||
| 941 | 978 | |||||||
| 942 | @see tls_revocation_policy | 979 | @see tls_revocation_policy | |||||
| 943 | @see add_crl | 980 | @see add_crl | |||||
| 944 | */ | 981 | */ | |||||
| 945 | void set_revocation_policy(tls_revocation_policy policy); | 982 | void set_revocation_policy(tls_revocation_policy policy); | |||||
| 946 | 983 | |||||||
| 947 | // | 984 | // | |||||
| 948 | // Password Handling | 985 | // Password Handling | |||||
| 949 | // | 986 | // | |||||
| 950 | 987 | |||||||
| 951 | /** Set the password callback for encrypted keys. | 988 | /** Set the password callback for encrypted keys. | |||||
| 952 | 989 | |||||||
| 953 | Installs a callback that provides passwords for encrypted | 990 | Installs a callback that provides passwords for encrypted | |||||
| 954 | private keys and PKCS#12 files. The callback is invoked when | 991 | private keys and PKCS#12 files. The callback is invoked when | |||||
| 955 | loading encrypted key material. | 992 | loading encrypted key material. | |||||
| 956 | 993 | |||||||
| 957 | @tparam Callback A callable with signature | 994 | @tparam Callback A callable with signature | |||||
| 958 | `std::string( std::size_t max_length, password_purpose purpose )`. | 995 | `std::string( std::size_t max_length, password_purpose purpose )`. | |||||
| 959 | 996 | |||||||
| 960 | @param callback The password callback. It receives the maximum | 997 | @param callback The password callback. It receives the maximum | |||||
| 961 | password length and the purpose (reading or writing), and | 998 | password length and the purpose (reading or writing), and | |||||
| 962 | returns the password string. | 999 | returns the password string. | |||||
| 963 | 1000 | |||||||
| 964 | @par Example | 1001 | @par Example | |||||
| 965 | @code | 1002 | @code | |||||
| 966 | ctx.set_password_callback( | 1003 | ctx.set_password_callback( | |||||
| 967 | []( std::size_t max_len, tls_password_purpose purpose ) | 1004 | []( std::size_t max_len, tls_password_purpose purpose ) | |||||
| 968 | { | 1005 | { | |||||
| 969 | // In practice, prompt user or read from secure storage | 1006 | // In practice, prompt user or read from secure storage | |||||
| 970 | return std::string( "my-key-password" ); | 1007 | return std::string( "my-key-password" ); | |||||
| 971 | }); | 1008 | }); | |||||
| 972 | 1009 | |||||||
| 973 | // Now load encrypted key | 1010 | // Now load encrypted key | |||||
| 974 | - | ctx.use_private_key_file( "encrypted.key", tls_file_format::pem ); | 1011 | + | if (auto ec = ctx.use_private_key_file( | |||
| 1012 | + | "encrypted.key", tls_file_format::pem )) | ||||||
| 1013 | + | return; | ||||||
| 975 | @endcode | 1014 | @endcode | |||||
| 976 | 1015 | |||||||
| 977 | @see tls_password_purpose | 1016 | @see tls_password_purpose | |||||
| 978 | */ | 1017 | */ | |||||
| 979 | template<typename Callback> | 1018 | template<typename Callback> | |||||
| 980 | void set_password_callback(Callback callback); | 1019 | void set_password_callback(Callback callback); | |||||
| 981 | }; | 1020 | }; | |||||
| 982 | #ifdef _MSC_VER | 1021 | #ifdef _MSC_VER | |||||
| 983 | #pragma warning(pop) | 1022 | #pragma warning(pop) | |||||
| 984 | #endif | 1023 | #endif | |||||
| 985 | 1024 | |||||||
| 986 | template<typename Callback> | 1025 | template<typename Callback> | |||||
| 987 | void | 1026 | void | |||||
| HITCBC | 988 | 1 | tls_context::set_servername_callback(Callback callback) | 1027 | 1 | tls_context::set_servername_callback(Callback callback) | ||
| 989 | { | 1028 | { | |||||
| HITCBC | 990 | 1 | set_servername_callback_impl(std::move(callback)); | 1029 | 1 | set_servername_callback_impl(std::move(callback)); | ||
| HITCBC | 991 | 1 | } | 1030 | 1 | } | ||
| 992 | 1031 | |||||||
| 993 | template<typename Callback> | 1032 | template<typename Callback> | |||||
| 994 | void | 1033 | void | |||||
| HITCBC | 995 | 4 | tls_context::set_password_callback(Callback callback) | 1034 | 4 | tls_context::set_password_callback(Callback callback) | ||
| 996 | { | 1035 | { | |||||
| HITCBC | 997 | 4 | set_password_callback_impl(std::move(callback)); | 1036 | 4 | set_password_callback_impl(std::move(callback)); | ||
| HITCBC | 998 | 4 | } | 1037 | 4 | } | ||
| 999 | 1038 | |||||||
| 1000 | template<typename Callback> | 1039 | template<typename Callback> | |||||
| 1001 | - | std::error_code | 1040 | + | void | |||
| HITCBC | 1002 | 2 | tls_context::set_verify_callback(Callback callback) | 1041 | 2 | tls_context::set_verify_callback(Callback callback) | ||
| 1003 | { | 1042 | { | |||||
| DCB | 1004 | - | 2 | return {}; | ||||
| HITCBC | 1005 | 2 | set_verify_callback_impl(std::move(callback)); | 1043 | 2 | set_verify_callback_impl(std::move(callback)); | ||
| HITGIC | 1006 | } | 1044 | 2 | } | |||
| 1007 | 1045 | |||||||
| 1008 | } // namespace boost::corosio | 1046 | } // namespace boost::corosio | |||||
| 1009 | 1047 | |||||||
| 1010 | #endif | 1048 | #endif | |||||