Unconditionally call CryptoUtils::CreateInitialObfuscators

gfe-relnote: refactor how CryptoUtils::CreateInitialObfuscators gets called, no behavior change
PiperOrigin-RevId: 268972396
Change-Id: Ie3dfa509138d0b756dbd239dadc6cad68545ed6f
diff --git a/quic/core/chlo_extractor.cc b/quic/core/chlo_extractor.cc
index 1cf6e14..0430f8d 100644
--- a/quic/core/chlo_extractor.cc
+++ b/quic/core/chlo_extractor.cc
@@ -120,22 +120,10 @@
     const QuicPacketHeader& header) {
   connection_id_ = header.destination_connection_id;
   // QuicFramer creates a NullEncrypter and NullDecrypter at level
-  // ENCRYPTION_INITIAL, which are the correct ones to use with the QUIC Crypto
-  // handshake. When the TLS handshake is used, the IETF-style initial crypters
-  // are used instead, so those need to be created and installed.
-  if (header.version.handshake_protocol == PROTOCOL_TLS1_3) {
-    CrypterPair crypters;
-    CryptoUtils::CreateInitialObfuscators(
-        Perspective::IS_SERVER, header.version,
-        header.destination_connection_id, &crypters);
-    framer_->SetEncrypter(ENCRYPTION_INITIAL, std::move(crypters.encrypter));
-    if (framer_->version().KnowsWhichDecrypterToUse()) {
-      framer_->InstallDecrypter(ENCRYPTION_INITIAL,
-                                std::move(crypters.decrypter));
-    } else {
-      framer_->SetDecrypter(ENCRYPTION_INITIAL, std::move(crypters.decrypter));
-    }
-  }
+  // ENCRYPTION_INITIAL. While those are the correct ones to use with some
+  // versions of QUIC, others use the IETF-style initial crypters, so those need
+  // to be created and installed.
+  framer_->SetInitialObfuscators(header.destination_connection_id);
   return true;
 }
 bool ChloFramerVisitor::OnUnauthenticatedHeader(
diff --git a/quic/core/chlo_extractor_test.cc b/quic/core/chlo_extractor_test.cc
index 806f6dc..4da7810 100644
--- a/quic/core/chlo_extractor_test.cc
+++ b/quic/core/chlo_extractor_test.cc
@@ -70,14 +70,7 @@
     }
     QuicFramer framer(SupportedVersions(header_.version), QuicTime::Zero(),
                       Perspective::IS_CLIENT, kQuicDefaultConnectionIdLength);
-    if (version.handshake_protocol == PROTOCOL_TLS1_3) {
-      CrypterPair crypters;
-      CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT, version,
-                                            TestConnectionId(), &crypters);
-      framer.SetEncrypter(ENCRYPTION_INITIAL, std::move(crypters.encrypter));
-      framer.InstallDecrypter(ENCRYPTION_INITIAL,
-                              std::move(crypters.decrypter));
-    }
+    framer.SetInitialObfuscators(TestConnectionId());
     if (!QuicVersionUsesCryptoFrames(version.transport_version) ||
         munge_stream_id) {
       QuicStreamId stream_id =
diff --git a/quic/core/crypto/crypto_handshake.h b/quic/core/crypto/crypto_handshake.h
index 424727f..ecf81d8 100644
--- a/quic/core/crypto/crypto_handshake.h
+++ b/quic/core/crypto/crypto_handshake.h
@@ -90,7 +90,9 @@
 // A CrypterPair contains the encrypter and decrypter for an encryption level.
 struct QUIC_EXPORT_PRIVATE CrypterPair {
   CrypterPair();
+  CrypterPair(CrypterPair&&) = default;
   ~CrypterPair();
+
   std::unique_ptr<QuicEncrypter> encrypter;
   std::unique_ptr<QuicDecrypter> decrypter;
 };
diff --git a/quic/core/quic_framer.cc b/quic/core/quic_framer.cc
index 727917b..db6b867 100644
--- a/quic/core/quic_framer.cc
+++ b/quic/core/quic_framer.cc
@@ -4166,6 +4166,14 @@
   encrypter_[level] = std::move(encrypter);
 }
 
+void QuicFramer::SetInitialObfuscators(QuicConnectionId connection_id) {
+  CrypterPair crypters;
+  CryptoUtils::CreateInitialObfuscators(perspective_, version_, connection_id,
+                                        &crypters);
+  encrypter_[ENCRYPTION_INITIAL] = std::move(crypters.encrypter);
+  decrypter_[ENCRYPTION_INITIAL] = std::move(crypters.decrypter);
+}
+
 size_t QuicFramer::EncryptInPlace(EncryptionLevel level,
                                   QuicPacketNumber packet_number,
                                   size_t ad_len,
diff --git a/quic/core/quic_framer.h b/quic/core/quic_framer.h
index f429a83..de026a7 100644
--- a/quic/core/quic_framer.h
+++ b/quic/core/quic_framer.h
@@ -549,6 +549,9 @@
   void SetEncrypter(EncryptionLevel level,
                     std::unique_ptr<QuicEncrypter> encrypter);
 
+  // Sets the encrypter and decrypter for the ENCRYPTION_INITIAL level.
+  void SetInitialObfuscators(QuicConnectionId connection_id);
+
   // Encrypts a payload in |buffer|.  |ad_len| is the length of the associated
   // data. |total_len| is the length of the associated data plus plaintext.
   // |buffer_len| is the full length of the allocated buffer.
diff --git a/quic/test_tools/quic_test_utils.cc b/quic/test_tools/quic_test_utils.cc
index 594f9af..3b71894 100644
--- a/quic/test_tools/quic_test_utils.cc
+++ b/quic/test_tools/quic_test_utils.cc
@@ -899,18 +899,8 @@
   ParsedQuicVersion version = (*versions)[0];
   EncryptionLevel level =
       header.version_flag ? ENCRYPTION_INITIAL : ENCRYPTION_FORWARD_SECURE;
-  if (version.handshake_protocol == PROTOCOL_TLS1_3 &&
-      level == ENCRYPTION_INITIAL) {
-    CrypterPair crypters;
-    CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT, version,
-                                          destination_connection_id, &crypters);
-    framer.SetEncrypter(ENCRYPTION_INITIAL, std::move(crypters.encrypter));
-    if (version.KnowsWhichDecrypterToUse()) {
-      framer.InstallDecrypter(ENCRYPTION_INITIAL,
-                              std::move(crypters.decrypter));
-    } else {
-      framer.SetDecrypter(ENCRYPTION_INITIAL, std::move(crypters.decrypter));
-    }
+  if (level == ENCRYPTION_INITIAL) {
+    framer.SetInitialObfuscators(destination_connection_id);
   }
   if (!QuicVersionUsesCryptoFrames(version.transport_version)) {
     QuicFrame frame(
@@ -987,12 +977,8 @@
   QuicFramer framer(versions != nullptr ? *versions : AllSupportedVersions(),
                     QuicTime::Zero(), perspective,
                     kQuicDefaultConnectionIdLength);
-  if (version.handshake_protocol == PROTOCOL_TLS1_3 && version_flag) {
-    CrypterPair crypters;
-    CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT, version,
-                                          destination_connection_id, &crypters);
-    framer.SetEncrypter(ENCRYPTION_INITIAL, std::move(crypters.encrypter));
-    framer.InstallDecrypter(ENCRYPTION_INITIAL, std::move(crypters.decrypter));
+  if (version_flag) {
+    framer.SetInitialObfuscators(destination_connection_id);
   }
   // We need a minimum of 7 bytes of encrypted payload. This will guarantee that
   // we have at least that much. (It ignores the overhead of the stream/crypto