Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnsdist: Always store the OpenSSLTLSIOCtx in the connection #14671

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions pdns/dnsdistdist/dnsdist-backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,19 @@ DownstreamState::DownstreamState(DownstreamState::Config&& config, std::shared_p

setName(d_config.name);

if (d_tlsCtx) {
if (!d_config.d_dohPath.empty()) {
if (d_tlsCtx && !d_config.d_dohPath.empty()) {
#ifdef HAVE_NGHTTP2
setupDoHClientProtocolNegotiation(d_tlsCtx);

auto outgoingDoHWorkerThreads = dnsdist::configuration::getImmutableConfiguration().d_outgoingDoHWorkers;
if (dnsdist::configuration::isImmutableConfigurationDone() && outgoingDoHWorkerThreads && *outgoingDoHWorkerThreads == 0) {
throw std::runtime_error("Error: setOutgoingDoHWorkerThreads() is set to 0 so no outgoing DoH worker thread is available to serve queries");
}

if (!dnsdist::configuration::isImmutableConfigurationDone() && (!outgoingDoHWorkerThreads || *outgoingDoHWorkerThreads == 0)) {
dnsdist::configuration::updateImmutableConfiguration([](dnsdist::configuration::ImmutableConfiguration& immutableConfig) {
immutableConfig.d_outgoingDoHWorkers = 1;
});
}
#endif /* HAVE_NGHTTP2 */
auto outgoingDoHWorkerThreads = dnsdist::configuration::getImmutableConfiguration().d_outgoingDoHWorkers;
if (dnsdist::configuration::isImmutableConfigurationDone() && outgoingDoHWorkerThreads && *outgoingDoHWorkerThreads == 0) {
throw std::runtime_error("Error: setOutgoingDoHWorkerThreads() is set to 0 so no outgoing DoH worker thread is available to serve queries");
}
else {
setupDoTProtocolNegotiation(d_tlsCtx);

if (!dnsdist::configuration::isImmutableConfigurationDone() && (!outgoingDoHWorkerThreads || *outgoingDoHWorkerThreads == 0)) {
dnsdist::configuration::updateImmutableConfiguration([](dnsdist::configuration::ImmutableConfiguration& immutableConfig) {
immutableConfig.d_outgoingDoHWorkers = 1;
});
}
#endif /* HAVE_NGHTTP2 */
}

if (connect && !isTCPOnly()) {
Expand Down
7 changes: 6 additions & 1 deletion pdns/dnsdistdist/dnsdist-lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
if (getOptionalValue<std::string>(vars, "tls", valueStr) > 0) {
serverPort = 853;
config.d_tlsParams.d_provider = valueStr;
tlsCtx = getTLSContext(config.d_tlsParams);

if (getOptionalValue<std::string>(vars, "dohPath", valueStr) > 0) {
#if !defined(HAVE_DNS_OVER_HTTPS) || !defined(HAVE_NGHTTP2)
Expand All @@ -585,9 +584,15 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)

serverPort = 443;
config.d_dohPath = valueStr;
config.d_tlsParams.d_alpn = TLSFrontend::ALPN::DoH;

getOptionalValue<bool>(vars, "addXForwardedHeaders", config.d_addXForwardedHeaders);
}
else {
config.d_tlsParams.d_alpn = TLSFrontend::ALPN::DoT;
}

tlsCtx = getTLSContext(config.d_tlsParams);
}

try {
Expand Down
26 changes: 0 additions & 26 deletions pdns/dnsdistdist/dnsdist-nghttp2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,16 +922,6 @@ static void dohClientThread(pdns::channel::Receiver<CrossProtocolQuery>&& receiv
errlog("Fatal error in outgoing DoH thread: %s", e.what());
}
}

static bool select_next_proto_callback(unsigned char** out, unsigned char* outlen, const unsigned char* in, unsigned int inlen)
{
if (nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) {
vinfolog("The remote DoH backend did not advertise " NGHTTP2_PROTO_VERSION_ID);
return false;
}
return true;
}

#endif /* HAVE_DNS_OVER_HTTPS && HAVE_NGHTTP2 */

struct DoHClientCollection::DoHWorkerThread
Expand Down Expand Up @@ -1042,22 +1032,6 @@ bool initDoHWorkers()
#endif /* HAVE_DNS_OVER_HTTPS && HAVE_NGHTTP2 */
}

bool setupDoHClientProtocolNegotiation(std::shared_ptr<TLSCtx>& ctx)
{
if (ctx == nullptr) {
return false;
}
#if defined(HAVE_DNS_OVER_HTTPS) && defined(HAVE_NGHTTP2)
/* we want to set the ALPN to h2, if only to mitigate the ALPACA attack */
const std::vector<std::vector<uint8_t>> h2Alpns = {{'h', '2'}};
ctx->setALPNProtos(h2Alpns);
ctx->setNextProtocolSelectCallback(select_next_proto_callback);
return true;
#else /* HAVE_DNS_OVER_HTTPS && HAVE_NGHTTP2 */
return false;
#endif /* HAVE_DNS_OVER_HTTPS && HAVE_NGHTTP2 */
}

bool sendH2Query(const std::shared_ptr<DownstreamState>& ds, std::unique_ptr<FDMultiplexer>& mplexer, std::shared_ptr<TCPQuerySender>& sender, InternalQuery&& query, bool healthCheck)
{
#if defined(HAVE_DNS_OVER_HTTPS) && defined(HAVE_NGHTTP2)
Expand Down
1 change: 0 additions & 1 deletion pdns/dnsdistdist/dnsdist-nghttp2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ extern std::atomic<uint64_t> g_dohStatesDumpRequested;
class TLSCtx;

bool initDoHWorkers();
bool setupDoHClientProtocolNegotiation(std::shared_ptr<TLSCtx>& ctx);

/* opens a new HTTP/2 connection to the supplied backend (attached to the supplied multiplexer), sends the query,
waits for the response to come back or an error to occur then notifies the sender, closing the connection. */
Expand Down
11 changes: 1 addition & 10 deletions pdns/libssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1091,16 +1091,7 @@ pdns::UniqueFilePtr libssl_set_key_log_file(std::unique_ptr<SSL_CTX, decltype(&S
#endif /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */
}

/* called in a client context, if the client advertised more than one ALPN values and the server returned more than one as well, to select the one to use. */
#ifndef DISABLE_NPN
void libssl_set_npn_select_callback(SSL_CTX* ctx, int (*cb)(SSL* s, unsigned char** out, unsigned char* outlen, const unsigned char* in, unsigned int inlen, void* arg), void* arg)
{
#ifdef HAVE_SSL_CTX_SET_NEXT_PROTO_SELECT_CB
SSL_CTX_set_next_proto_select_cb(ctx, cb, arg);
#endif
}
#endif /* DISABLE_NPN */

/* called in a client context, if the client advertised more than one ALPN value and the server returned more than one as well, to select the one to use. */
void libssl_set_alpn_select_callback(SSL_CTX* ctx, int (*cb)(SSL* s, const unsigned char** out, unsigned char* outlen, const unsigned char* in, unsigned int inlen, void* arg), void* arg)
{
#ifdef HAVE_SSL_CTX_SET_ALPN_SELECT_CB
Expand Down
5 changes: 0 additions & 5 deletions pdns/libssl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,6 @@ std::pair<std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>, std::vector<std::st

pdns::UniqueFilePtr libssl_set_key_log_file(std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>& ctx, const std::string& logFile);

/* called in a client context, if the client advertised more than one ALPN values and the server returned more than one as well, to select the one to use. */
#ifndef DISABLE_NPN
void libssl_set_npn_select_callback(SSL_CTX* ctx, int (*cb)(SSL* s, unsigned char** out, unsigned char* outlen, const unsigned char* in, unsigned int inlen, void* arg), void* arg);
#endif /* DISABLE_NPN */

/* called in a server context, to select an ALPN value advertised by the client if any */
void libssl_set_alpn_select_callback(SSL_CTX* ctx, int (*cb)(SSL* s, const unsigned char** out, unsigned char* outlen, const unsigned char* in, unsigned int inlen, void* arg), void* arg);
/* set the supported ALPN protos in client context */
Expand Down
Loading
Loading