No public description

PiperOrigin-RevId: 1024897573
diff --git a/quiche/quic/core/crypto/tls_server_connection.cc b/quiche/quic/core/crypto/tls_server_connection.cc
index 10b37d6..a1cc1dd 100644
--- a/quiche/quic/core/crypto/tls_server_connection.cc
+++ b/quiche/quic/core/crypto/tls_server_connection.cc
@@ -115,6 +115,14 @@
   UpdateCertVerifyCallback();
 }
 
+void TlsServerConnection::DisableEarlyData() {
+  if (!ssl_config().early_data_enabled.value_or(true)) {
+    return;
+  }
+  mutable_ssl_config().early_data_enabled = false;
+  SSL_set_early_data_enabled(ssl(), 0);
+}
+
 void TlsServerConnection::UpdateCertVerifyCallback() {
   const ClientCertMode client_cert_mode = ssl_config().client_cert_mode;
   if (client_cert_mode == ClientCertMode::kNone) {
diff --git a/quiche/quic/core/crypto/tls_server_connection.h b/quiche/quic/core/crypto/tls_server_connection.h
index ea4ace9..76de575 100644
--- a/quiche/quic/core/crypto/tls_server_connection.h
+++ b/quiche/quic/core/crypto/tls_server_connection.h
@@ -147,6 +147,10 @@
   // to has an effect.
   void SetClientCertMode(ClientCertMode client_cert_mode);
 
+  // Disables TLS 1.3 0-RTT early data on this connection. Must be called before
+  // cert selection / session selection completes.
+  void DisableEarlyData();
+
  private:
   // Specialization of TlsConnection::ConnectionFromSsl.
   static TlsServerConnection* ConnectionFromSsl(SSL* ssl);
diff --git a/quiche/quic/core/quic_types.cc b/quiche/quic/core/quic_types.cc
index f26b88c..e3dcda2 100644
--- a/quiche/quic/core/quic_types.cc
+++ b/quiche/quic/core/quic_types.cc
@@ -502,7 +502,9 @@
          lhs.client_cert_mode == rhs.client_cert_mode &&
          lhs.ech_config_list == rhs.ech_config_list &&
          lhs.ech_grease_enabled == rhs.ech_grease_enabled &&
-         lhs.reject_unusable_ech_config == rhs.reject_unusable_ech_config;
+         lhs.reject_unusable_ech_config == rhs.reject_unusable_ech_config &&
+         lhs.require_retry_token_for_zero_rtt ==
+             rhs.require_retry_token_for_zero_rtt;
 }
 
 #undef RETURN_STRING_LITERAL  // undef for jumbo builds
diff --git a/quiche/quic/core/quic_types.h b/quiche/quic/core/quic_types.h
index 287940b..7dd3214d 100644
--- a/quiche/quic/core/quic_types.h
+++ b/quiche/quic/core/quic_types.h
@@ -927,6 +927,9 @@
   // server_padding_to_request.
   // This is experimental, and will be removed once the experiment is complete.
   bool server_padding_enabled = false;
+  // Only used by the server. If true, require a validated retry token in the
+  // client Initial packet to accept 0-RTT in IETF QUIC.
+  bool require_retry_token_for_zero_rtt = false;
 };
 
 QUICHE_EXPORT bool operator==(const QuicSSLConfig& lhs,
diff --git a/quiche/quic/core/quic_types_test.cc b/quiche/quic/core/quic_types_test.cc
index 0a9f635..40ad772 100644
--- a/quiche/quic/core/quic_types_test.cc
+++ b/quiche/quic/core/quic_types_test.cc
@@ -40,6 +40,11 @@
   EXPECT_NE(config1, config2);
   config2.reject_unusable_ech_config = true;
   EXPECT_EQ(config1, config2);
+
+  config1.require_retry_token_for_zero_rtt = true;
+  EXPECT_NE(config1, config2);
+  config2.require_retry_token_for_zero_rtt = true;
+  EXPECT_EQ(config1, config2);
 }
 
 }  // namespace
diff --git a/quiche/quic/core/tls_server_handshaker.cc b/quiche/quic/core/tls_server_handshaker.cc
index 759a398..0a52ef4 100644
--- a/quiche/quic/core/tls_server_handshaker.cc
+++ b/quiche/quic/core/tls_server_handshaker.cc
@@ -1061,6 +1061,18 @@
     return ssl_select_cert_error;
   }
 
+  if (early_data_attempted_ &&
+      tls_connection_.ssl_config().early_data_enabled.value_or(true)) {
+    if (session()->connection()->GetStats().address_validated_via_token) {
+      QUIC_CODE_COUNT(quic_address_validated_for_zero_rtt);
+    } else {
+      QUIC_CODE_COUNT(quic_address_not_validated_for_zero_rtt);
+      if (tls_connection_.ssl_config().require_retry_token_for_zero_rtt) {
+        tls_connection_.DisableEarlyData();
+      }
+    }
+  }
+
   can_disable_resumption_ = false;
   const QuicAsyncStatus status = proof_source_handle_->SelectCertificate(
       session()->connection()->self_address().Normalized(),
diff --git a/quiche/quic/core/tls_server_handshaker_test.cc b/quiche/quic/core/tls_server_handshaker_test.cc
index 0985465..dcb870c 100644
--- a/quiche/quic/core/tls_server_handshaker_test.cc
+++ b/quiche/quic/core/tls_server_handshaker_test.cc
@@ -321,6 +321,8 @@
             server_connection_, DefaultQuicConfig(), supported_versions_,
             server_crypto_config_.get(), &server_compressed_certs_cache_);
     server_session->set_client_cert_mode(initial_client_cert_mode_);
+    server_session->set_require_retry_token_for_zero_rtt(
+        initial_require_retry_token_for_zero_rtt_);
     server_session->Initialize();
 
     // We advance the clock initially because the default time is zero and the
@@ -351,6 +353,16 @@
         server_crypto_config_.get());
   }
 
+  void RunSyncHandshakeWithFakeProofSourceHandle() {
+    InitializeServerWithFakeProofSourceHandle();
+    server_handshaker_->SetupProofSourceHandle(
+        /*select_cert_action=*/FakeProofSourceHandle::Action::DELEGATE_SYNC,
+        /*compute_signature_action=*/
+        FakeProofSourceHandle::Action::DELEGATE_SYNC);
+    InitializeFakeClient();
+    CompleteCryptoHandshake();
+  }
+
   // Initializes the crypto server stream state for testing.  May be
   // called multiple times.
   void InitializeServer() {
@@ -430,6 +442,11 @@
     EXPECT_CALL(*server_connection_, OnCanWrite()).Times(testing::AnyNumber());
     // Call CryptoConnect if we haven't moved any client messages yet.
     if (moved_messages_counts_.first == 0) {
+      EXPECT_CALL(*server_connection_, SendControlFrame(_))
+          .Times(testing::AnyNumber())
+          .WillRepeatedly(
+              testing::Invoke(server_connection_,
+                              &PacketSavingConnection::ReallySendControlFrame));
       client_stream()->CryptoConnect();
     }
     moved_messages_counts_ = crypto_test_utils::AdvanceHandshake(
@@ -536,6 +553,7 @@
   QuicCompressedCertsCache server_compressed_certs_cache_;
   QuicServerId server_id_;
   ClientCertMode initial_client_cert_mode_ = ClientCertMode::kNone;
+  std::optional<bool> initial_require_retry_token_for_zero_rtt_;
 
   // Client state.
   PacketSavingConnection* client_connection_;
@@ -1949,6 +1967,100 @@
   EXPECT_EQ(TlsServerHandshakerPeer::GetServerParams(handshaker), nullptr);
 }
 
+TEST_P(TlsServerHandshakerTest, ZeroRttRejectedWithoutRetryToken) {
+  if (GetParam().disable_resumption) {
+    return;
+  }
+  initial_require_retry_token_for_zero_rtt_ = true;
+  SetQuicFlag(quic_reject_retry_token_in_initial_packet, false);
+
+  // Initial 1-RTT connection has no retry token and does not attempt early
+  // data, so early data remains enabled in SelectCertArgs and a 0-RTT-capable
+  // ticket plus NEW_TOKEN are issued.
+  RunSyncHandshakeWithFakeProofSourceHandle();
+  ExpectHandshakeSuccessful();
+  EXPECT_FALSE(server_connection_->GetStats().address_validated_via_token);
+  EXPECT_FALSE(server_stream()->IsResumption());
+  EXPECT_FALSE(server_stream()->IsZeroRtt());
+  EXPECT_TRUE(
+      last_select_cert_args().ssl_config.early_data_enabled.value_or(true));
+
+  // Resumed connection where retry token validation fails (simulating no
+  // valid retry token on 0-RTT) rejects 0-RTT when the option is enabled and
+  // sets early_data_enabled=false before SelectCertificate.
+  SetQuicFlag(quic_reject_retry_token_in_initial_packet, true);
+  RunSyncHandshakeWithFakeProofSourceHandle();
+  ExpectHandshakeSuccessful();
+  EXPECT_FALSE(server_connection_->GetStats().address_validated_via_token);
+  EXPECT_TRUE(client_stream()->IsResumption());
+  EXPECT_TRUE(server_stream()->IsResumption());
+  EXPECT_EQ(last_select_cert_args().ssl_config.early_data_enabled,
+            std::optional<bool>(false));
+  EXPECT_FALSE(server_stream()->IsZeroRtt());
+  EXPECT_FALSE(client_stream()->EarlyDataAccepted());
+}
+
+TEST_P(TlsServerHandshakerTest,
+       ZeroRttAllowedWithoutRetryTokenWhenNotRequired) {
+  if (GetParam().disable_resumption) {
+    return;
+  }
+  initial_require_retry_token_for_zero_rtt_ = false;
+  SetQuicFlag(quic_reject_retry_token_in_initial_packet, false);
+
+  // Initial 1-RTT connection issues a 0-RTT-capable ticket.
+  RunSyncHandshakeWithFakeProofSourceHandle();
+  ExpectHandshakeSuccessful();
+  EXPECT_FALSE(server_connection_->GetStats().address_validated_via_token);
+  EXPECT_FALSE(server_stream()->IsResumption());
+  EXPECT_FALSE(server_stream()->IsZeroRtt());
+  EXPECT_TRUE(
+      last_select_cert_args().ssl_config.early_data_enabled.value_or(true));
+
+  // Resumed connection without a validated retry token still accepts 0-RTT
+  // when require_retry_token_for_zero_rtt is false.
+  SetQuicFlag(quic_reject_retry_token_in_initial_packet, true);
+  RunSyncHandshakeWithFakeProofSourceHandle();
+  ExpectHandshakeSuccessful();
+  EXPECT_FALSE(server_connection_->GetStats().address_validated_via_token);
+  EXPECT_TRUE(client_stream()->IsResumption());
+  EXPECT_TRUE(server_stream()->IsResumption());
+  EXPECT_TRUE(
+      last_select_cert_args().ssl_config.early_data_enabled.value_or(true));
+  EXPECT_TRUE(server_stream()->IsZeroRtt());
+  EXPECT_TRUE(client_stream()->EarlyDataAccepted());
+}
+
+TEST_P(TlsServerHandshakerTest,
+       ZeroRttAcceptedWithRetryTokenAfterInitialConnectionWithoutToken) {
+  if (GetParam().disable_resumption) {
+    return;
+  }
+  initial_require_retry_token_for_zero_rtt_ = true;
+  SetQuicFlag(quic_reject_retry_token_in_initial_packet, false);
+
+  // Connection 1: initial 1-RTT handshake without a retry token still issues a
+  // 0-RTT-capable session ticket and NEW_TOKEN.
+  RunSyncHandshakeWithFakeProofSourceHandle();
+  ExpectHandshakeSuccessful();
+  EXPECT_FALSE(server_connection_->GetStats().address_validated_via_token);
+  EXPECT_FALSE(server_stream()->IsZeroRtt());
+  EXPECT_TRUE(
+      last_select_cert_args().ssl_config.early_data_enabled.value_or(true));
+
+  // Connection 2: resumed handshake carrying the validated retry token accepts
+  // 0-RTT when require_retry_token_for_zero_rtt is enabled.
+  RunSyncHandshakeWithFakeProofSourceHandle();
+  ExpectHandshakeSuccessful();
+  EXPECT_TRUE(server_connection_->GetStats().address_validated_via_token);
+  EXPECT_TRUE(client_stream()->IsResumption());
+  EXPECT_TRUE(server_stream()->IsResumption());
+  EXPECT_TRUE(
+      last_select_cert_args().ssl_config.early_data_enabled.value_or(true));
+  EXPECT_TRUE(server_stream()->IsZeroRtt());
+  EXPECT_TRUE(client_stream()->EarlyDataAccepted());
+}
+
 }  // namespace
 }  // namespace test
 }  // namespace quic
diff --git a/quiche/quic/test_tools/crypto_test_utils.cc b/quiche/quic/test_tools/crypto_test_utils.cc
index abf3419..0f8824a 100644
--- a/quiche/quic/test_tools/crypto_test_utils.cc
+++ b/quiche/quic/test_tools/crypto_test_utils.cc
@@ -175,9 +175,23 @@
         }
       }
     }
+    if (dest_conn.version().IsIetfQuic() &&
+        dest_conn.perspective() == Perspective::IS_SERVER &&
+        !framer.header().retry_token.empty() &&
+        QuicConnectionPeer::GetVisitor(&dest_conn) != nullptr &&
+        QuicConnectionPeer::GetVisitor(&dest_conn)
+            ->ValidateToken(framer.header().retry_token)) {
+      QuicConnectionPeer::SetAddressValidatedViaToken(&dest_conn, true);
+    }
     for (const auto& crypto_frame : framer.crypto_frames()) {
       dest_stream.OnCryptoFrame(*crypto_frame);
     }
+    if (dest_conn.version().IsIetfQuic() &&
+        dest_conn.perspective() == Perspective::IS_CLIENT) {
+      for (const auto& new_token_frame : framer.new_token_frames()) {
+        dest_conn.OnNewTokenFrame(new_token_frame);
+      }
+    }
     if (!framer.connection_close_frames().empty() && dest_conn.connected()) {
       dest_conn.OnConnectionCloseFrame(framer.connection_close_frames()[0]);
     }
@@ -350,6 +364,10 @@
                                    testing::_, testing::_))
       .Times(testing::AnyNumber());
   EXPECT_CALL(*server_conn, OnCanWrite()).Times(testing::AnyNumber());
+  EXPECT_CALL(*server_conn, SendControlFrame(_))
+      .Times(testing::AnyNumber())
+      .WillRepeatedly(testing::Invoke(
+          server_conn, &PacketSavingConnection::ReallySendControlFrame));
   EXPECT_CALL(*client_conn, OnCanWrite()).Times(testing::AnyNumber());
   EXPECT_CALL(*server_conn, SendCryptoData(_, _, _))
       .Times(testing::AnyNumber());
diff --git a/quiche/quic/test_tools/quic_connection_peer.cc b/quiche/quic/test_tools/quic_connection_peer.cc
index b458cd0..c147d7e 100644
--- a/quiche/quic/test_tools/quic_connection_peer.cc
+++ b/quiche/quic/test_tools/quic_connection_peer.cc
@@ -680,5 +680,17 @@
   return connection->last_received_packet_info_;
 }
 
+// static
+QuicConnectionVisitorInterface* QuicConnectionPeer::GetVisitor(
+    QuicConnection* connection) {
+  return connection->visitor_;
+}
+
+// static
+void QuicConnectionPeer::SetAddressValidatedViaToken(QuicConnection* connection,
+                                                     bool validated) {
+  connection->stats_.address_validated_via_token = validated;
+}
+
 }  // namespace test
 }  // namespace quic
diff --git a/quiche/quic/test_tools/quic_connection_peer.h b/quiche/quic/test_tools/quic_connection_peer.h
index 7aa14ef..539c284 100644
--- a/quiche/quic/test_tools/quic_connection_peer.h
+++ b/quiche/quic/test_tools/quic_connection_peer.h
@@ -279,6 +279,11 @@
 
   static QuicConnection::ReceivedPacketInfo& GetLastReceivedPacketInfo(
       QuicConnection* connection);
+
+  static QuicConnectionVisitorInterface* GetVisitor(QuicConnection* connection);
+
+  static void SetAddressValidatedViaToken(QuicConnection* connection,
+                                          bool validated);
 };
 
 }  // namespace test
diff --git a/quiche/quic/test_tools/quic_test_utils.h b/quiche/quic/test_tools/quic_test_utils.h
index ff96e6e..9ff2b1e 100644
--- a/quiche/quic/test_tools/quic_test_utils.h
+++ b/quiche/quic/test_tools/quic_test_utils.h
@@ -1107,6 +1107,10 @@
     if (server_padding_enabled_.has_value()) {
       ssl_config.server_padding_enabled = *server_padding_enabled_;
     }
+    if (require_retry_token_for_zero_rtt_.has_value()) {
+      ssl_config.require_retry_token_for_zero_rtt =
+          *require_retry_token_for_zero_rtt_;
+    }
 
     return ssl_config;
   }
@@ -1118,6 +1122,9 @@
   void set_server_padding_enabled(std::optional<bool> enabled) {
     server_padding_enabled_ = enabled;
   }
+  void set_require_retry_token_for_zero_rtt(std::optional<bool> required) {
+    require_retry_token_for_zero_rtt_ = required;
+  }
 
  private:
   MockQuicSessionVisitor visitor_;
@@ -1131,6 +1138,9 @@
   // If not nullopt, override the server_padding_enabled value from base class'
   // ssl_config.
   std::optional<bool> server_padding_enabled_;
+  // If not nullopt, override the require_retry_token_for_zero_rtt value from
+  // base class' ssl_config.
+  std::optional<bool> require_retry_token_for_zero_rtt_;
 };
 
 class TestQuicSpdyClientSession : public QuicSpdyClientSessionBase {
diff --git a/quiche/quic/test_tools/simple_quic_framer.cc b/quiche/quic/test_tools/simple_quic_framer.cc
index d18df38..8f22171 100644
--- a/quiche/quic/test_tools/simple_quic_framer.cc
+++ b/quiche/quic/test_tools/simple_quic_framer.cc
@@ -285,6 +285,9 @@
   const std::vector<QuicGoAwayFrame>& goaway_frames() const {
     return goaway_frames_;
   }
+  const std::vector<QuicNewTokenFrame>& new_token_frames() const {
+    return new_token_frames_;
+  }
   const std::vector<QuicMaxStreamsFrame>& max_streams_frames() const {
     return max_streams_frames_;
   }
@@ -477,6 +480,11 @@
   return visitor_->goaway_frames();
 }
 
+const std::vector<QuicNewTokenFrame>& SimpleQuicFramer::new_token_frames()
+    const {
+  return visitor_->new_token_frames();
+}
+
 const std::vector<QuicConnectionCloseFrame>&
 SimpleQuicFramer::connection_close_frames() const {
   return visitor_->connection_close_frames();
diff --git a/quiche/quic/test_tools/simple_quic_framer.h b/quiche/quic/test_tools/simple_quic_framer.h
index 49a3a92..13e99f6 100644
--- a/quiche/quic/test_tools/simple_quic_framer.h
+++ b/quiche/quic/test_tools/simple_quic_framer.h
@@ -46,6 +46,7 @@
   const std::vector<QuicWindowUpdateFrame>& window_update_frames() const;
   const std::vector<QuicGoAwayFrame>& goaway_frames() const;
   const std::vector<QuicRstStreamFrame>& rst_stream_frames() const;
+  const std::vector<QuicNewTokenFrame>& new_token_frames() const;
   const std::vector<std::unique_ptr<QuicStreamFrame>>& stream_frames() const;
   const std::vector<std::unique_ptr<QuicCryptoFrame>>& crypto_frames() const;
   const std::vector<QuicPaddingFrame>& padding_frames() const;