oblivious_http: Overloaded Parsing method to read payload using QuicheDataReader

PiperOrigin-RevId: 482235418
diff --git a/quiche/oblivious_http/buffers/oblivious_http_request.cc b/quiche/oblivious_http/buffers/oblivious_http_request.cc
index e7903b6..76029a3 100644
--- a/quiche/oblivious_http/buffers/oblivious_http_request.cc
+++ b/quiche/oblivious_http/buffers/oblivious_http_request.cc
@@ -49,23 +49,22 @@
   if (gateway_ctx == nullptr) {
     return SslErrorAsStatus("Failed to initialize Gateway/Server's Context.");
   }
-  // TODO(anov) Add ParseOhttpPayloadHeader(QuicheDataReader) to read fields out
-  // of payload, and eliminate sub-stringing.
-  auto is_hdr_ok = ohttp_key_config.ParseOhttpPayloadHeader(encrypted_data);
+
+  QuicheDataReader reader(encrypted_data);
+
+  auto is_hdr_ok = ohttp_key_config.ParseOhttpPayloadHeader(reader);
   if (!is_hdr_ok.ok()) {
     return is_hdr_ok;
   }
-  absl::string_view enc_plus_ciphertext =
-      encrypted_data.substr(ObliviousHttpHeaderKeyConfig::kHeaderLength);
 
   size_t enc_key_len = EVP_HPKE_KEM_enc_len(EVP_HPKE_KEY_kem(&gateway_key));
-  if (enc_plus_ciphertext.size() < enc_key_len) {
+
+  absl::string_view enc_key_received;
+  if (!reader.ReadStringPiece(&enc_key_received, enc_key_len)) {
     return absl::FailedPreconditionError(absl::StrCat(
         "Failed to extract encapsulation key of expected len=", enc_key_len,
         "from payload."));
   }
-  absl::string_view enc_key_received =
-      enc_plus_ciphertext.substr(0, enc_key_len);
   std::string info = ohttp_key_config.SerializeRecipientContextInfo();
   if (!EVP_HPKE_CTX_setup_recipient(
           gateway_ctx.get(), &gateway_key, ohttp_key_config.GetHpkeKdf(),
@@ -76,9 +75,7 @@
     return SslErrorAsStatus("Failed to setup recipient context");
   }
 
-  absl::string_view ciphertext_received =
-      enc_plus_ciphertext.substr(enc_key_len);
-
+  absl::string_view ciphertext_received = reader.ReadRemainingPayload();
   // Decrypt the message.
   std::string decrypted(ciphertext_received.size(), '\0');
   size_t decrypted_len;
diff --git a/quiche/oblivious_http/common/oblivious_http_header_key_config.cc b/quiche/oblivious_http/common/oblivious_http_header_key_config.cc
index d378b4c..2963ca2 100644
--- a/quiche/oblivious_http/common/oblivious_http_header_key_config.cc
+++ b/quiche/oblivious_http/common/oblivious_http_header_key_config.cc
@@ -125,6 +125,11 @@
     return absl::InvalidArgumentError("Empty request payload.");
   }
   QuicheDataReader reader(payload_bytes);
+  return ParseOhttpPayloadHeader(reader);
+}
+
+absl::Status ObliviousHttpHeaderKeyConfig::ParseOhttpPayloadHeader(
+    QuicheDataReader& reader) const {
   uint8_t key_id;
   if (!reader.ReadUInt8(&key_id)) {
     return absl::InvalidArgumentError("Failed to read key_id from header.");
diff --git a/quiche/oblivious_http/common/oblivious_http_header_key_config.h b/quiche/oblivious_http/common/oblivious_http_header_key_config.h
index 1c89bc3..6447bab 100644
--- a/quiche/oblivious_http/common/oblivious_http_header_key_config.h
+++ b/quiche/oblivious_http/common/oblivious_http_header_key_config.h
@@ -60,7 +60,6 @@
   // https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-03.html#section-4.1-10
   std::string SerializeRecipientContextInfo() const;
 
-  // TODO(anov): Replace with ObliviousRequest/Response abstraction
   // Parses the below Header
   // [keyID(1 byte), kemID(2 bytes), kdfID(2 bytes), aeadID(2 bytes)]
   // from the payload received in Ohttp Request, and verifies that these values
@@ -69,6 +68,14 @@
   // https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-03.html#section-4.1-7
   absl::Status ParseOhttpPayloadHeader(absl::string_view payload_bytes) const;
 
+  // Parses the Oblivious HTTP header [keyID(1 byte), kemID(2 bytes), kdfID(2
+  // bytes), aeadID(2 bytes)] from the buffer initialized within
+  // `QuicheDataReader`, and  verifies these values against instantiated class
+  // data namely [key_id_, kem_id_, kdf_id_, aead_id_] for a match. On
+  // success(i.e., if matched successfully), leaves `reader` pointing at the
+  // first byte after the header.
+  absl::Status ParseOhttpPayloadHeader(QuicheDataReader& reader) const;
+
   // Extracts Key ID from the OHTTP Request payload.
   static absl::StatusOr<uint8_t> ParseKeyIdFromObliviousHttpRequestPayload(
       absl::string_view payload_bytes);