Make it possible to configure an empty trust anchor ID list

The trust anchors extension can be empty, to indicate support for the extension (there's a retry flow we can trigger) without actually advertising a list of trust anchors. It's to hint that the server make a more optimistic pick of certificate.

PiperOrigin-RevId: 792704403
diff --git a/quiche/quic/core/quic_types.h b/quiche/quic/core/quic_types.h
index dbe7464..78bf922 100644
--- a/quiche/quic/core/quic_types.h
+++ b/quiche/quic/core/quic_types.h
@@ -876,12 +876,13 @@
   // As a client, whether ECH GREASE is enabled. If `ech_config_list` is
   // not empty, this value does nothing.
   bool ech_grease_enabled = false;
-  // If non-empty, the TLS Trust Anchor IDs to send in the TLS handshake. (See
+  // If not nullopt, the TLS Trust Anchor IDs to send in the TLS handshake. (See
   // https://tlswg.org/tls-trust-anchor-ids/draft-ietf-tls-trust-anchor-ids.html.)
   // The value should be a series of Trust Anchor IDs in wire format (a series
-  // of non-empty, 8-bit length-prefixed strings). If empty, the Trust Anchor
-  // IDs extension will not be sent.
-  std::string trust_anchor_ids;
+  // of non-empty, 8-bit length-prefixed strings). The list may be empty to
+  // signal support for the extension without advertising particular trust
+  // anchors. If nullopt, the Trust Anchor IDs extension will not be sent.
+  std::optional<std::string> trust_anchor_ids;
 };
 
 QUICHE_EXPORT bool operator==(const QuicSSLConfig& lhs,
diff --git a/quiche/quic/core/tls_client_handshaker.cc b/quiche/quic/core/tls_client_handshaker.cc
index 6dc4ef9..00f56ed 100644
--- a/quiche/quic/core/tls_client_handshaker.cc
+++ b/quiche/quic/core/tls_client_handshaker.cc
@@ -157,12 +157,12 @@
   if (GetQuicReloadableFlag(enable_tls_trust_anchor_ids)) {
     QUIC_RELOADABLE_FLAG_COUNT_N(enable_tls_trust_anchor_ids, 2, 2);
 #if defined(BORINGSSL_API_VERSION) && BORINGSSL_API_VERSION >= 36
-    if (!tls_connection_.ssl_config().trust_anchor_ids.empty()) {
+    if (tls_connection_.ssl_config().trust_anchor_ids.has_value()) {
       if (!SSL_set1_requested_trust_anchors(
               ssl(),
               reinterpret_cast<const uint8_t*>(
-                  tls_connection_.ssl_config().trust_anchor_ids.data()),
-              tls_connection_.ssl_config().trust_anchor_ids.size())) {
+                  tls_connection_.ssl_config().trust_anchor_ids->data()),
+              tls_connection_.ssl_config().trust_anchor_ids->size())) {
         CloseConnection(QUIC_HANDSHAKE_FAILED,
                         "Client failed to set TLS Trust Anchor IDs");
         return false;
diff --git a/quiche/quic/core/tls_client_handshaker_test.cc b/quiche/quic/core/tls_client_handshaker_test.cc
index 675b994..58d26a0 100644
--- a/quiche/quic/core/tls_client_handshaker_test.cc
+++ b/quiche/quic/core/tls_client_handshaker_test.cc
@@ -398,6 +398,43 @@
   ASSERT_TRUE(stream()->encryption_established());
   EXPECT_TRUE(stream()->MatchedTrustAnchorIdForTesting());
 }
+
+// Tests that the client can complete a handshake in which it sends no Trust
+// Anchor IDs.
+TEST_P(TlsClientHandshakerTest, HandshakeWithEmptyTrustAnchorIdList) {
+  SetQuicReloadableFlag(enable_tls_trust_anchor_ids, true);
+  InitializeFakeServer("");
+  ssl_config_.emplace();
+  ssl_config_->trust_anchor_ids.emplace();
+  CreateConnection();
+
+  // Add a DoS callback on the server, to test that the client sent an empty
+  // extension. This is a bit of a hack. TlsServerHandshaker already configures
+  // the certificate selection callback, but does not usefully expose any way
+  // for tests to inspect the ClientHello. So, instead, we register a different
+  // callback that also gets the ClientHello.
+  static bool callback_ran;
+  callback_ran = false;
+  SSL_CTX_set_dos_protection_cb(
+      server_crypto_config_->ssl_ctx(),
+      [](const SSL_CLIENT_HELLO* client_hello) -> int {
+        const uint8_t* data;
+        size_t len;
+        EXPECT_TRUE(SSL_early_callback_ctx_extension_get(
+            client_hello, TLSEXT_TYPE_trust_anchors, &data, &len));
+        // The extension should contain an empty list, i.e. a two-byte encoding
+        // of a zero length.
+        EXPECT_EQ(len, 2u);
+        EXPECT_EQ(data[0], 0x00);
+        EXPECT_EQ(data[1], 0x00);
+        callback_ran = true;
+        return 1;
+      });
+
+  CompleteCryptoHandshake();
+  ASSERT_TRUE(stream()->encryption_established());
+  EXPECT_TRUE(callback_ran);
+}
 #endif
 
 TEST_P(TlsClientHandshakerTest, Resumption) {