MySQL 8.4.0
Source Code Documentation
tls_base.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_OPENSSL_INCLUDE_TLS_DETAILS_TLS_BASE_H_
27#define ROUTER_SRC_OPENSSL_INCLUDE_TLS_DETAILS_TLS_BASE_H_
28
29#include <memory>
30#include <utility>
31
35
37
38namespace net {
39namespace tls {
40
41template <typename LowerLayer>
42class TlsBase {
43 private:
44 template <typename Resource, typename Result,
45 Result (*free_resource)(Resource *)>
46 class Free {
47 public:
48 void operator()(Resource *res) { free_resource(res); }
49 };
50
51 using SslPtr = std::unique_ptr<SSL, Free<SSL, void, SSL_free>>;
52 using BioPtr = std::unique_ptr<BIO, Free<BIO, int, BIO_free>>;
54 using protocol_type = typename LowerLayer::protocol_type;
55
56 TlsBase(LowerLayer &&layer, TlsContext *tls_context)
57 : lower_layer_(std::forward<LowerLayer>(layer)),
58 tls_context_{tls_context} {
59 ssl_.reset(SSL_new(tls_context_->get()));
60 BIO *internal_bio;
61 BIO *external_bio;
62 BIO_new_bio_pair(&internal_bio, 0, &external_bio, 0);
63 SSL_set_bio(ssl_.get(), internal_bio, internal_bio);
64 network_bio_.reset(external_bio);
65 }
66
67 public:
68 using LowerLayerType = LowerLayer;
69
70 template <typename... Args>
71 TlsBase(TlsServerContext *tls_context, Args &&... args)
72 : TlsBase(LowerLayer{std::forward<Args>(args)...}, tls_context) {
73 SSL_set_accept_state(ssl_.get());
74 }
75
76 template <typename... Args>
77 TlsBase(TlsClientContext *tls_context, Args &&... args)
78 : TlsBase(LowerLayer{std::forward<Args>(args)...}, tls_context) {
79 SSL_set_connect_state(ssl_.get());
80 }
81
83 : lower_layer_{std::move(other.lower_layer_)},
85 ssl_{std::move(other.ssl_)},
86 network_bio_{std::move(other.network_bio_)} {}
87
88 protected:
89 template <typename SslIO, typename BufferSequence, typename Token,
90 typename TlsLayer, typename Action>
92
93 constexpr static uint32_t k_tls_buffer_size = 32000;
94
95 LowerLayer lower_layer_;
103};
104
105} // namespace tls
106} // namespace net
107
108#endif // ROUTER_SRC_OPENSSL_INCLUDE_TLS_DETAILS_TLS_BASE_H_
Client TLS Context.
Definition: tls_client_context.h:43
wraps SSL_CTX.
Definition: tls_context.h:85
SSL_CTX * get() const
get non-owning pointer to SSL_CTX.
Definition: tls_context.h:159
TLS Context for the server side.
Definition: tls_server_context.h:51
Definition: flexible_buffer.h:105
Definition: flexible_buffer.h:91
Definition: ssl_io_completion.h:129
Definition: tls_base.h:46
void operator()(Resource *res)
Definition: tls_base.h:48
Definition: tls_base.h:42
uint8_t input_buffer_[k_tls_buffer_size]
Definition: tls_base.h:100
typename LowerLayer::native_handle_type socket_type
Definition: tls_base.h:53
TlsBase(TlsServerContext *tls_context, Args &&... args)
Definition: tls_base.h:71
LowerLayer lower_layer_
Definition: tls_base.h:95
TlsBase(TlsBase &&other)
Definition: tls_base.h:82
SslPtr ssl_
Definition: tls_base.h:97
LowerLayer LowerLayerType
Definition: tls_base.h:68
constexpr static uint32_t k_tls_buffer_size
Definition: tls_base.h:93
TlsBase(TlsClientContext *tls_context, Args &&... args)
Definition: tls_base.h:77
FlexibleOutputBuffer output_
Definition: tls_base.h:101
TlsContext * tls_context_
Definition: tls_base.h:96
typename LowerLayer::protocol_type protocol_type
Definition: tls_base.h:54
FlexibleInputBuffer input_
Definition: tls_base.h:102
std::unique_ptr< SSL, Free< SSL, void, SSL_free > > SslPtr
Definition: tls_base.h:51
TlsBase(LowerLayer &&layer, TlsContext *tls_context)
Definition: tls_base.h:56
uint8_t output_buffer_[k_tls_buffer_size]
Definition: tls_base.h:99
BioPtr network_bio_
Definition: tls_base.h:98
std::unique_ptr< BIO, Free< BIO, int, BIO_free > > BioPtr
Definition: tls_base.h:52
int native_handle_type
Definition: socket_constants.h:51
Definition: buffer.h:45
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
Definition: gcs_xcom_synode.h:64
Result
Definition: result.h:34
Definition: tls_keylog_dumper.h:32
message Action
Definition: replication_group_member_actions.proto:30