Skip to content

Commit

Permalink
transportsocket: add interface to improve reusable tls session
Browse files Browse the repository at this point in the history
Signed-off-by: Yuchen Dai <[email protected]>
  • Loading branch information
lambdai committed Apr 13, 2024
1 parent bdff856 commit 4314fc5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
6 changes: 6 additions & 0 deletions envoy/network/transport_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ class TransportSocketOptions {
* that are marked as shared with the upstream connection.
*/
virtual const StreamInfo::FilterState::Objects& downstreamSharedFilterStateObjects() const PURE;

/**
* @return the tag as the hint to select SSL session. This value does not participate the conn
* pool key.
*/
virtual absl::optional<uint64_t> sslSessionTag() const PURE;
};

using TransportSocketOptionsConstSharedPtr = std::shared_ptr<const TransportSocketOptions>;
Expand Down
12 changes: 9 additions & 3 deletions source/common/network/transport_socket_options_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class AlpnDecoratingTransportSocketOptions : public TransportSocketOptions {
const StreamInfo::FilterState::Objects& downstreamSharedFilterStateObjects() const override {
return inner_options_->downstreamSharedFilterStateObjects();
}
absl::optional<uint64_t> sslSessionTag() const override {
return inner_options_->sslSessionTag();
}

private:
const std::vector<std::string> alpn_fallback_;
Expand All @@ -51,15 +54,16 @@ class TransportSocketOptionsImpl : public TransportSocketOptions {
absl::optional<Network::ProxyProtocolData> proxy_proto_options = absl::nullopt,
StreamInfo::FilterState::ObjectsPtr filter_state_objects =
std::make_unique<StreamInfo::FilterState::Objects>(),
std::unique_ptr<const Http11ProxyInfo>&& proxy_info = nullptr)
std::unique_ptr<const Http11ProxyInfo>&& proxy_info = nullptr,
absl::optional<uint64_t> ssl_session_tag = absl::nullopt)
: override_server_name_(override_server_name.empty()
? absl::nullopt
: absl::optional<std::string>(override_server_name)),
override_verify_san_list_{std::move(override_verify_san_list)},
override_alpn_list_{std::move(override_alpn)}, alpn_fallback_{std::move(fallback_alpn)},
proxy_protocol_options_(proxy_proto_options),
filter_state_objects_(std::move(filter_state_objects)), proxy_info_(std::move(proxy_info)) {
}
filter_state_objects_(std::move(filter_state_objects)), proxy_info_(std::move(proxy_info)),
ssl_session_tag_(ssl_session_tag) {}

// Network::TransportSocketOptions
const absl::optional<std::string>& serverNameOverride() const override {
Expand All @@ -86,6 +90,7 @@ class TransportSocketOptionsImpl : public TransportSocketOptions {
const StreamInfo::FilterState::Objects& downstreamSharedFilterStateObjects() const override {
return *filter_state_objects_;
}
absl::optional<uint64_t> sslSessionTag() const override { return ssl_session_tag_; }

private:
const absl::optional<std::string> override_server_name_;
Expand All @@ -96,6 +101,7 @@ class TransportSocketOptionsImpl : public TransportSocketOptions {
const StreamInfo::FilterState::ObjectsPtr filter_state_objects_;
const StreamInfo::FilterStateSharedPtr filter_state_;
std::unique_ptr<const Http11ProxyInfo> proxy_info_;
const absl::optional<uint64_t> ssl_session_tag_;
};

class TransportSocketOptionsUtility {
Expand Down
16 changes: 14 additions & 2 deletions source/common/tls/context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,16 @@ ClientContextImpl::ClientContextImpl(Stats::Scope& scope,
static_cast<ContextImpl*>(SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl)));
ClientContextImpl* client_context_impl = dynamic_cast<ClientContextImpl*>(context_impl);
RELEASE_ASSERT(client_context_impl != nullptr, ""); // for Coverity
return client_context_impl->newSessionKey(session);

const auto* transport_socket_options_shared_ptr_ptr =
static_cast<const Network::TransportSocketOptionsConstSharedPtr*>(
SSL_get_app_data(ssl));
if (transport_socket_options_shared_ptr_ptr == nullptr) {
// 0 indicates that the session is not taken ownership of.
return 0;
}
return client_context_impl->newSessionKey(
(*transport_socket_options_shared_ptr_ptr)->sslSessionTag(), session);
});
}
}
Expand Down Expand Up @@ -771,7 +780,10 @@ ClientContextImpl::newSsl(const Network::TransportSocketOptionsConstSharedPtr& o
return ssl_con;
}

int ClientContextImpl::newSessionKey(SSL_SESSION* session) {
int ClientContextImpl::newSessionKey([[maybe_unused]] absl::optional<uint64_t> session_tag,
SSL_SESSION* session) {
// TODO(lambdai): save the session according to ``session_tag``.

// In case we ever store single-use session key (TLS 1.3),
// we need to switch to using write/write locks.
if (SSL_SESSION_should_be_single_use(session)) {
Expand Down
5 changes: 4 additions & 1 deletion source/common/tls/context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ class ClientContextImpl : public ContextImpl, public Envoy::Ssl::ClientContext {
newSsl(const Network::TransportSocketOptionsConstSharedPtr& options) override;

private:
int newSessionKey(SSL_SESSION* session);
/**
* @return 1 ClientContextImpl retains the reference, 0 otherwise.
*/
int newSessionKey(absl::optional<uint64_t> session_tag, SSL_SESSION* session);

const std::string server_name_indication_;
const bool allow_renegotiation_;
Expand Down

0 comments on commit 4314fc5

Please sign in to comment.