From: Remi Gacogne Date: Mon, 31 Jul 2023 09:56:30 +0000 (+0200) Subject: dnsdist: Make TLSFrontend and TCPIOHandler suitable for DoH as well X-Git-Tag: rec-5.0.0-alpha1~19^2~36 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5024b5e4340325a2fc057fecf21415f742d9f6c7;p=thirdparty%2Fpdns.git dnsdist: Make TLSFrontend and TCPIOHandler suitable for DoH as well --- diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 34a6aaf51b..3224b8763f 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -2579,7 +2579,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } setLuaSideEffect(); - shared_ptr frontend = std::make_shared(); + shared_ptr frontend = std::make_shared(TLSFrontend::ALPN::DoT); if (!loadTLSCertificateAndKeys("addTLSLocal", frontend->d_tlsConfig.d_certKeyPairs, certFiles, keyFiles)) { return; } diff --git a/pdns/tcpiohandler.cc b/pdns/tcpiohandler.cc index d085125553..1b7018c028 100644 --- a/pdns/tcpiohandler.cc +++ b/pdns/tcpiohandler.cc @@ -11,7 +11,7 @@ const bool TCPIOHandler::s_disableConnectForUnitTests = false; #include #endif /* HAVE_LIBSODIUM */ -#ifdef HAVE_DNS_OVER_TLS +#if defined(HAVE_DNS_OVER_TLS) || defined(HAVE_DNS_OVER_HTTPS) #ifdef HAVE_LIBSSL #include @@ -1811,7 +1811,7 @@ private: #endif /* HAVE_GNUTLS */ -#endif /* HAVE_DNS_OVER_TLS */ +#endif /* HAVE_DNS_OVER_TLS || HAVE_DNS_OVER_HTTPS */ bool setupDoTProtocolNegotiation(std::shared_ptr& ctx) { @@ -1824,46 +1824,57 @@ bool setupDoTProtocolNegotiation(std::shared_ptr& ctx) return true; } +bool setupDoHProtocolNegotiation(std::shared_ptr& ctx) +{ + if (ctx == nullptr) { + return false; + } + /* we want to set the ALPN to doh */ + const std::vector> dohAlpns = {{'h', '2'}}; + ctx->setALPNProtos(dohAlpns); + return true; +} + bool TLSFrontend::setupTLS() { -#ifdef HAVE_DNS_OVER_TLS +#if defined(HAVE_DNS_OVER_TLS) || defined(HAVE_DNS_OVER_HTTPS) std::shared_ptr newCtx{nullptr}; /* get the "best" available provider */ - if (!d_provider.empty()) { #ifdef HAVE_GNUTLS - if (d_provider == "gnutls") { - newCtx = std::make_shared(*this); - setupDoTProtocolNegotiation(newCtx); - std::atomic_store_explicit(&d_ctx, newCtx, std::memory_order_release); - return true; - } + if (d_provider == "gnutls") { + newCtx = std::make_shared(*this); + } #endif /* HAVE_GNUTLS */ #ifdef HAVE_LIBSSL - if (d_provider == "openssl") { - newCtx = std::make_shared(*this); - setupDoTProtocolNegotiation(newCtx); - std::atomic_store_explicit(&d_ctx, newCtx, std::memory_order_release); - return true; - } -#endif /* HAVE_LIBSSL */ + if (d_provider == "openssl") { + newCtx = std::make_shared(*this); } +#endif /* HAVE_LIBSSL */ + if (!newCtx) { #ifdef HAVE_LIBSSL - newCtx = std::make_shared(*this); + newCtx = std::make_shared(*this); #else /* HAVE_LIBSSL */ #ifdef HAVE_GNUTLS - newCtx = std::make_shared(*this); + newCtx = std::make_shared(*this); #endif /* HAVE_GNUTLS */ #endif /* HAVE_LIBSSL */ + } + + if (d_alpn == ALPN::DoT) { + setupDoTProtocolNegotiation(newCtx); + } + else if (d_alpn == ALPN::DoH) { + setupDoHProtocolNegotiation(newCtx); + } - setupDoTProtocolNegotiation(newCtx); std::atomic_store_explicit(&d_ctx, newCtx, std::memory_order_release); -#endif /* HAVE_DNS_OVER_TLS */ +#endif /* HAVE_DNS_OVER_TLS || HAVE_DNS_OVER_HTTPS */ return true; } std::shared_ptr getTLSContext([[maybe_unused]] const TLSContextParameters& params) { -#ifdef HAVE_DNS_OVER_TLS +#if defined(HAVE_DNS_OVER_TLS) || defined(HAVE_DNS_OVER_HTTPS) /* get the "best" available provider */ if (!params.d_provider.empty()) { #ifdef HAVE_GNUTLS @@ -1886,6 +1897,6 @@ std::shared_ptr getTLSContext([[maybe_unused]] const TLSContextParameter #endif /* HAVE_GNUTLS */ #endif /* HAVE_LIBSSL */ -#endif /* HAVE_DNS_OVER_TLS */ +#endif /* HAVE_DNS_OVER_TLS || HAVE_DNS_OVER_HTTPS */ return nullptr; } diff --git a/pdns/tcpiohandler.hh b/pdns/tcpiohandler.hh index 88f0dc724b..29b59a01f9 100644 --- a/pdns/tcpiohandler.hh +++ b/pdns/tcpiohandler.hh @@ -136,7 +136,9 @@ protected: class TLSFrontend { public: - TLSFrontend() + enum class ALPN : uint8_t { Unset, DoT, DoH }; + + TLSFrontend(ALPN alpn) : d_alpn(alpn) { } @@ -223,7 +225,7 @@ public: TLSErrorCounters d_tlsCounters; ComboAddress d_addr; std::string d_provider; - + ALPN d_alpn{ALPN::Unset}; protected: std::shared_ptr d_ctx{nullptr}; }; @@ -582,3 +584,4 @@ struct TLSContextParameters std::shared_ptr getTLSContext(const TLSContextParameters& params); bool setupDoTProtocolNegotiation(std::shared_ptr& ctx); +bool setupDoHProtocolNegotiation(std::shared_ptr& ctx);