xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/oblivious_http/oblivious_http_gateway.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 #include "quiche/oblivious_http/oblivious_http_gateway.h"
2 
3 #include <stdint.h>
4 
5 #include <memory>
6 #include <string>
7 #include <utility>
8 
9 #include "absl/memory/memory.h"
10 #include "absl/status/status.h"
11 #include "absl/status/statusor.h"
12 #include "absl/strings/string_view.h"
13 #include "quiche/common/quiche_crypto_logging.h"
14 #include "quiche/common/quiche_random.h"
15 
16 namespace quiche {
17 
18 // Constructor.
ObliviousHttpGateway(bssl::UniquePtr<EVP_HPKE_KEY> recipient_key,const ObliviousHttpHeaderKeyConfig & ohttp_key_config,QuicheRandom * quiche_random)19 ObliviousHttpGateway::ObliviousHttpGateway(
20     bssl::UniquePtr<EVP_HPKE_KEY> recipient_key,
21     const ObliviousHttpHeaderKeyConfig& ohttp_key_config,
22     QuicheRandom* quiche_random)
23     : server_hpke_key_(std::move(recipient_key)),
24       ohttp_key_config_(ohttp_key_config),
25       quiche_random_(quiche_random) {}
26 
27 // Initialize ObliviousHttpGateway(Recipient/Server) context.
Create(absl::string_view hpke_private_key,const ObliviousHttpHeaderKeyConfig & ohttp_key_config,QuicheRandom * quiche_random)28 absl::StatusOr<ObliviousHttpGateway> ObliviousHttpGateway::Create(
29     absl::string_view hpke_private_key,
30     const ObliviousHttpHeaderKeyConfig& ohttp_key_config,
31     QuicheRandom* quiche_random) {
32   if (hpke_private_key.empty()) {
33     return absl::InvalidArgumentError("Invalid/Empty HPKE private key.");
34   }
35   // Initialize HPKE key and context.
36   bssl::UniquePtr<EVP_HPKE_KEY> recipient_key(EVP_HPKE_KEY_new());
37   if (recipient_key == nullptr) {
38     return SslErrorAsStatus(
39         "Failed to initialize ObliviousHttpGateway/Server's Key.");
40   }
41   if (!EVP_HPKE_KEY_init(
42           recipient_key.get(), ohttp_key_config.GetHpkeKem(),
43           reinterpret_cast<const uint8_t*>(hpke_private_key.data()),
44           hpke_private_key.size())) {
45     return SslErrorAsStatus("Failed to import HPKE private key.");
46   }
47   if (quiche_random == nullptr) quiche_random = QuicheRandom::GetInstance();
48   return ObliviousHttpGateway(std::move(recipient_key), ohttp_key_config,
49                               quiche_random);
50 }
51 
52 absl::StatusOr<ObliviousHttpRequest>
DecryptObliviousHttpRequest(absl::string_view encrypted_data,absl::string_view request_label) const53 ObliviousHttpGateway::DecryptObliviousHttpRequest(
54     absl::string_view encrypted_data, absl::string_view request_label) const {
55   return ObliviousHttpRequest::CreateServerObliviousRequest(
56       encrypted_data, *(server_hpke_key_), ohttp_key_config_, request_label);
57 }
58 
59 absl::StatusOr<ObliviousHttpResponse>
CreateObliviousHttpResponse(std::string plaintext_data,ObliviousHttpRequest::Context & oblivious_http_request_context,absl::string_view response_label) const60 ObliviousHttpGateway::CreateObliviousHttpResponse(
61     std::string plaintext_data,
62     ObliviousHttpRequest::Context& oblivious_http_request_context,
63     absl::string_view response_label) const {
64   return ObliviousHttpResponse::CreateServerObliviousResponse(
65       std::move(plaintext_data), oblivious_http_request_context, response_label,
66       quiche_random_);
67 }
68 
69 }  // namespace quiche
70