1 #ifndef QUICHE_OBLIVIOUS_HTTP_BUFFERS_OBLIVIOUS_HTTP_RESPONSE_H_ 2 #define QUICHE_OBLIVIOUS_HTTP_BUFFERS_OBLIVIOUS_HTTP_RESPONSE_H_ 3 4 #include <stddef.h> 5 6 #include <string> 7 8 #include "absl/status/statusor.h" 9 #include "absl/strings/string_view.h" 10 #include "quiche/common/quiche_random.h" 11 #include "quiche/oblivious_http/buffers/oblivious_http_request.h" 12 #include "quiche/oblivious_http/common/oblivious_http_header_key_config.h" 13 14 namespace quiche { 15 16 class QUICHE_EXPORT ObliviousHttpResponse { 17 public: 18 // Parse and decrypt the OHttp response using ObliviousHttpContext context obj 19 // that was returned from `CreateClientObliviousRequest` method. On success, 20 // returns obj that callers will use to `GetDecryptedMessage`. 21 // @params: Note that `oblivious_http_request_context` is required to stay 22 // alive only for the lifetime of this factory method call. 23 static absl::StatusOr<ObliviousHttpResponse> CreateClientObliviousResponse( 24 std::string encrypted_data, 25 ObliviousHttpRequest::Context& oblivious_http_request_context, 26 absl::string_view resp_label = 27 ObliviousHttpHeaderKeyConfig::kOhttpResponseLabel); 28 29 // Encrypt the input param `plaintext_payload` and create OHttp response using 30 // ObliviousHttpContext context obj that was returned from 31 // `CreateServerObliviousRequest` method. On success, returns obj that callers 32 // will use to `Serialize` OHttp response. Generic Usecase : server-side calls 33 // this method in the context of Response. 34 // @params: Note that `oblivious_http_request_context` is required to stay 35 // alive only for the lifetime of this factory method call. 36 // @params: If callers do not provide `quiche_random`, it will be initialized 37 // to default supplied `QuicheRandom::GetInstance()`. It's recommended that 38 // callers initialize `QuicheRandom* quiche_random` as a Singleton instance 39 // within their code and pass in the same, in order to have optimized random 40 // string generation. `quiche_random` is required to stay alive only for the 41 // lifetime of this factory method call. 42 static absl::StatusOr<ObliviousHttpResponse> CreateServerObliviousResponse( 43 std::string plaintext_payload, 44 ObliviousHttpRequest::Context& oblivious_http_request_context, 45 absl::string_view resp_label = 46 ObliviousHttpHeaderKeyConfig::kOhttpResponseLabel, 47 QuicheRandom* quiche_random = nullptr); 48 49 // Copyable. 50 ObliviousHttpResponse(const ObliviousHttpResponse& other) = default; 51 ObliviousHttpResponse& operator=(const ObliviousHttpResponse& other) = 52 default; 53 54 // Movable. 55 ObliviousHttpResponse(ObliviousHttpResponse&& other) = default; 56 ObliviousHttpResponse& operator=(ObliviousHttpResponse&& other) = default; 57 58 ~ObliviousHttpResponse() = default; 59 60 // Generic Usecase : server-side calls this method in the context of Response 61 // to serialize OHTTP response that will be returned to client-side. 62 // Returns serialized OHTTP response bytestring. 63 const std::string& EncapsulateAndSerialize() const; 64 65 const std::string& GetPlaintextData() const; ConsumePlaintextData()66 std::string ConsumePlaintextData() && { 67 return std::move(response_plaintext_); 68 } 69 70 private: 71 struct CommonAeadParamsResult { 72 const EVP_AEAD* evp_hpke_aead; 73 const size_t aead_key_len; 74 const size_t aead_nonce_len; 75 const size_t secret_len; 76 }; 77 78 struct CommonOperationsResult { 79 bssl::UniquePtr<EVP_AEAD_CTX> aead_ctx; 80 const std::string aead_nonce; 81 }; 82 83 explicit ObliviousHttpResponse(std::string encrypted_data, 84 std::string resp_plaintext); 85 86 // Determines AEAD key len(Nk), AEAD nonce len(Nn) based on HPKE context and 87 // further estimates secret_len = std::max(Nk, Nn) 88 static absl::StatusOr<CommonAeadParamsResult> GetCommonAeadParams( 89 ObliviousHttpRequest::Context& oblivious_http_request_context); 90 // Performs operations related to response handling that are common between 91 // client and server. 92 static absl::StatusOr<CommonOperationsResult> CommonOperationsToEncapDecap( 93 absl::string_view response_nonce, 94 ObliviousHttpRequest::Context& oblivious_http_request_context, 95 absl::string_view resp_label, const size_t aead_key_len, 96 const size_t aead_nonce_len, const size_t secret_len); 97 std::string encrypted_data_; 98 std::string response_plaintext_; 99 }; 100 101 } // namespace quiche 102 103 #endif // QUICHE_OBLIVIOUS_HTTP_BUFFERS_OBLIVIOUS_HTTP_RESPONSE_H_ 104