2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "doq-common.hh"
24 #include "dnsdist-random.hh"
27 #ifdef HAVE_DNS_OVER_QUIC
30 #define DEBUGLOG_ENABLED
31 #define DEBUGLOG(x) std::cerr << x << std::endl;
36 namespace dnsdist::doq
39 static const std::string s_quicRetryTokenKey
= dnsdist::crypto::authenticated::newKey(false);
41 PacketBuffer
mintToken(const PacketBuffer
& dcid
, const ComboAddress
& peer
)
44 dnsdist::crypto::authenticated::Nonce nonce
;
47 const auto addrBytes
= peer
.toByteString();
48 // this token will be valid for 60s
49 const uint64_t ttd
= time(nullptr) + 60U;
50 PacketBuffer plainTextToken
;
51 plainTextToken
.reserve(sizeof(ttd
) + addrBytes
.size() + dcid
.size());
52 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic)
53 plainTextToken
.insert(plainTextToken
.end(), reinterpret_cast<const uint8_t*>(&ttd
), reinterpret_cast<const uint8_t*>(&ttd
) + sizeof(ttd
));
54 plainTextToken
.insert(plainTextToken
.end(), addrBytes
.begin(), addrBytes
.end());
55 plainTextToken
.insert(plainTextToken
.end(), dcid
.begin(), dcid
.end());
56 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
57 const auto encryptedToken
= dnsdist::crypto::authenticated::encryptSym(std::string_view(reinterpret_cast<const char*>(plainTextToken
.data()), plainTextToken
.size()), s_quicRetryTokenKey
, nonce
, false);
58 // a bit sad, let's see if we can do better later
59 PacketBuffer encryptedTokenPacket
;
60 encryptedTokenPacket
.reserve(encryptedToken
.size() + nonce
.value
.size());
61 encryptedTokenPacket
.insert(encryptedTokenPacket
.begin(), encryptedToken
.begin(), encryptedToken
.end());
62 encryptedTokenPacket
.insert(encryptedTokenPacket
.begin(), nonce
.value
.begin(), nonce
.value
.end());
63 return encryptedTokenPacket
;
65 catch (const std::exception
& exp
) {
66 vinfolog("Error while minting DoH3 token: %s", exp
.what());
71 void fillRandom(PacketBuffer
& buffer
, size_t size
)
75 buffer
.insert(buffer
.end(), dnsdist::getRandomValue(std::numeric_limits
<uint8_t>::max()));
80 std::optional
<PacketBuffer
> getCID()
84 fillRandom(buffer
, LOCAL_CONN_ID_LEN
);
89 // returns the original destination ID if the token is valid, nothing otherwise
90 std::optional
<PacketBuffer
> validateToken(const PacketBuffer
& token
, const ComboAddress
& peer
)
93 dnsdist::crypto::authenticated::Nonce nonce
;
94 auto addrBytes
= peer
.toByteString();
95 const uint64_t now
= time(nullptr);
96 const auto minimumSize
= nonce
.value
.size() + sizeof(now
) + addrBytes
.size();
97 if (token
.size() <= minimumSize
) {
101 memcpy(nonce
.value
.data(), token
.data(), nonce
.value
.size());
103 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
104 auto cipher
= std::string_view(reinterpret_cast<const char*>(&token
.at(nonce
.value
.size())), token
.size() - nonce
.value
.size());
105 auto plainText
= dnsdist::crypto::authenticated::decryptSym(cipher
, s_quicRetryTokenKey
, nonce
, false);
107 if (plainText
.size() <= sizeof(now
) + addrBytes
.size()) {
112 memcpy(&ttd
, plainText
.data(), sizeof(ttd
));
117 if (std::memcmp(&plainText
.at(sizeof(ttd
)), &*addrBytes
.begin(), addrBytes
.size()) != 0) {
120 // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
121 return PacketBuffer(plainText
.begin() + (sizeof(ttd
) + addrBytes
.size()), plainText
.end());
123 catch (const std::exception
& exp
) {
124 vinfolog("Error while validating DoH3 token: %s", exp
.what());
129 void handleStatelessRetry(Socket
& sock
, const PacketBuffer
& clientConnID
, const PacketBuffer
& serverConnID
, const ComboAddress
& peer
, uint32_t version
, PacketBuffer
& buffer
)
131 auto newServerConnID
= getCID();
132 if (!newServerConnID
) {
136 auto token
= mintToken(serverConnID
, peer
);
138 buffer
.resize(MAX_DATAGRAM_SIZE
);
139 auto written
= quiche_retry(clientConnID
.data(), clientConnID
.size(),
140 serverConnID
.data(), serverConnID
.size(),
141 newServerConnID
->data(), newServerConnID
->size(),
142 token
.data(), token
.size(),
144 buffer
.data(), buffer
.size());
147 DEBUGLOG("failed to create retry packet " << written
);
151 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
152 sock
.sendTo(reinterpret_cast<const char*>(buffer
.data()), static_cast<size_t>(written
), peer
);
155 void handleVersionNegociation(Socket
& sock
, const PacketBuffer
& clientConnID
, const PacketBuffer
& serverConnID
, const ComboAddress
& peer
, PacketBuffer
& buffer
)
157 buffer
.resize(MAX_DATAGRAM_SIZE
);
159 auto written
= quiche_negotiate_version(clientConnID
.data(), clientConnID
.size(),
160 serverConnID
.data(), serverConnID
.size(),
161 buffer
.data(), buffer
.size());
164 DEBUGLOG("failed to create vneg packet " << written
);
167 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
168 sock
.sendTo(reinterpret_cast<const char*>(buffer
.data()), static_cast<size_t>(written
), peer
);
171 void flushEgress(Socket
& sock
, QuicheConnection
& conn
, const ComboAddress
& peer
, PacketBuffer
& buffer
)
173 buffer
.resize(MAX_DATAGRAM_SIZE
);
174 quiche_send_info send_info
;
177 auto written
= quiche_conn_send(conn
.get(), buffer
.data(), buffer
.size(), &send_info
);
178 if (written
== QUICHE_ERR_DONE
) {
185 // FIXME pacing (as send_info.at should tell us when to send the packet) ?
186 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
187 sock
.sendTo(reinterpret_cast<const char*>(buffer
.data()), static_cast<size_t>(written
), peer
);
191 void configureQuiche(QuicheConfig
& config
, const QuicheParams
& params
, bool isHTTP
)
193 for (const auto& pair
: params
.d_tlsConfig
.d_certKeyPairs
) {
194 auto res
= quiche_config_load_cert_chain_from_pem_file(config
.get(), pair
.d_cert
.c_str());
196 throw std::runtime_error("Error loading the server certificate: " + std::to_string(res
));
199 res
= quiche_config_load_priv_key_from_pem_file(config
.get(), pair
.d_key
->c_str());
201 throw std::runtime_error("Error loading the server key: " + std::to_string(res
));
207 auto res
= quiche_config_set_application_protos(config
.get(),
208 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
209 reinterpret_cast<const uint8_t*>(params
.d_alpn
.data()),
210 params
.d_alpn
.size());
212 throw std::runtime_error("Error setting ALPN: " + std::to_string(res
));
216 quiche_config_set_max_idle_timeout(config
.get(), params
.d_idleTimeout
* 1000);
217 /* maximum size of an outgoing packet, which means the buffer we pass to quiche_conn_send() should be at least that big */
218 quiche_config_set_max_send_udp_payload_size(config
.get(), MAX_DATAGRAM_SIZE
);
219 quiche_config_set_max_recv_udp_payload_size(config
.get(), MAX_DATAGRAM_SIZE
);
221 // The number of concurrent remotely-initiated bidirectional streams to be open at any given time
222 // https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_streams_bidi
223 // 0 means none will get accepted, that's why we have a default value of 65535
224 quiche_config_set_initial_max_streams_bidi(config
.get(), params
.d_maxInFlight
);
226 // The number of bytes of incoming stream data to be buffered for each localy or remotely-initiated bidirectional stream
227 quiche_config_set_initial_max_stream_data_bidi_local(config
.get(), 8192);
228 quiche_config_set_initial_max_stream_data_bidi_remote(config
.get(), 8192);
231 /* see rfc9114 section 6.2. Unidirectional Streams:
232 Each endpoint needs to create at least one unidirectional stream for the HTTP control stream.
233 QPACK requires two additional unidirectional streams, and other extensions might require further streams.
234 Therefore, the transport parameters sent by both clients and servers MUST allow the peer to create at least three
235 unidirectional streams.
236 These transport parameters SHOULD also provide at least 1,024 bytes of flow-control credit to each unidirectional stream.
238 quiche_config_set_initial_max_streams_uni(config
.get(), 3U);
239 quiche_config_set_initial_max_stream_data_uni(config
.get(), 1024U);
242 // The number of total bytes of incoming stream data to be buffered for the whole connection
243 // https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_data
244 quiche_config_set_initial_max_data(config
.get(), 8192 * params
.d_maxInFlight
);
245 if (!params
.d_keyLogFile
.empty()) {
246 quiche_config_log_keys(config
.get());
249 auto algo
= dnsdist::doq::s_available_cc_algorithms
.find(params
.d_ccAlgo
);
250 if (algo
!= dnsdist::doq::s_available_cc_algorithms
.end()) {
251 quiche_config_set_cc_algorithm(config
.get(), static_cast<enum quiche_cc_algorithm
>(algo
->second
));
255 PacketBuffer resetToken
;
256 fillRandom(resetToken
, 16);
257 quiche_config_set_stateless_reset_token(config
.get(), resetToken
.data());