File: interest_group_pmt_report_util.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (263 lines) | stat: -rw-r--r-- 10,039 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/interest_group/interest_group_pmt_report_util.h"

#include <optional>

#include "base/containers/span_writer.h"
#include "base/json/json_writer.h"
#include "base/strings/to_string.h"
#include "base/uuid.h"
#include "components/cbor/values.h"
#include "components/cbor/writer.h"
#include "content/browser/aggregation_service/public_key.h"
#include "content/services/auction_worklet/public/cpp/private_model_training_reporting.h"
namespace content {

namespace {
// Helper to handle padding the payload and then framing it with it's length.
std::optional<std::vector<uint8_t>> FrameAndSerializePayload(
    mojo_base::BigBuffer payload,
    uint32_t desired_size) {
  // Header will only contain 4 bytes to represent the length
  const size_t kFramingHeaderSize = 4;
  std::vector<uint8_t> framed_payload(desired_size + kFramingHeaderSize, 0);

  // When the desired_size is greater than the allowed kMaxPayloadLength or
  // the payload size is greater than the desired_size, we will pass an empty
  // vector with a length of 0.
  if (payload.size() > desired_size) {
    return framed_payload;
  }

  base::SpanWriter<uint8_t> writer(framed_payload);

  // Set CBOR Payload Size Bytes
  if (!writer.WriteU32BigEndian(base::checked_cast<uint32_t>(payload.size()))) {
    return std::nullopt;
  }

  // Copy payload data
  if (!writer.Write(base::as_byte_span(payload))) {
    return std::nullopt;
  }
  return framed_payload;
}

// Handle encrypting the serialized payload with HPKE.
// Returns empty vector on error.
std::optional<std::vector<uint8_t>> EncryptPayloadWithHpke(
    std::vector<uint8_t> unencrypted_payload,
    uint32_t desired_size,
    std::vector<uint8_t> encryption_shared_info,
    const BiddingAndAuctionServerKey& public_key) {
  bssl::ScopedEVP_HPKE_CTX sender_context;
  base::span<const uint8_t> public_key_bytes =
      base::as_byte_span(public_key.key);
  // Max overhead based on the current context.
  // Needed here so that we can allocate the payload properly and avoid making a
  // copy, if the result of `EVP_HPKE_CTX_max_overhead()` changes from this, it
  // would hit our DCHECK.
  const size_t max_overhead = 16;
  // This vector will hold the encapsulated shared secret "enc" followed by
  // the symmetrically encrypted ciphertext "ct".
  std::vector<uint8_t> payload(EVP_HPKE_MAX_ENC_LENGTH +
                               unencrypted_payload.size() + max_overhead);
  size_t encapsulated_shared_secret_len;

  if (!EVP_HPKE_CTX_setup_sender(
          /*ctx=*/sender_context.get(),
          /*out_enc=*/payload.data(),
          /*out_enc_len=*/&encapsulated_shared_secret_len,
          /*max_enc=*/payload.size(),
          /*kem=*/EVP_hpke_x25519_hkdf_sha256(), /*kdf=*/EVP_hpke_hkdf_sha256(),
          /*aead=*/EVP_hpke_chacha20_poly1305(),
          /*peer_public_key=*/public_key_bytes.data(),
          /*peer_public_key_len=*/public_key_bytes.size(),
          /*info=*/encryption_shared_info.data(),
          /*info_len=*/encryption_shared_info.size())) {
    return std::nullopt;
  }
  // This ensures the resize will not grow the buffer and not require a copy.
  DCHECK_GE(payload.size(),
            encapsulated_shared_secret_len + unencrypted_payload.size() +
                EVP_HPKE_CTX_max_overhead(sender_context.get()));

  payload.resize(encapsulated_shared_secret_len + unencrypted_payload.size() +
                 EVP_HPKE_CTX_max_overhead(sender_context.get()));

  base::span<uint8_t> ciphertext =
      base::span(payload).subspan(encapsulated_shared_secret_len);
  size_t ciphertext_len;

  if (!EVP_HPKE_CTX_seal(
          /*ctx=*/sender_context.get(), /*out=*/ciphertext.data(),
          /*out_len=*/&ciphertext_len,
          /*max_out_len=*/ciphertext.size(),
          /*in=*/unencrypted_payload.data(),
          /*in_len*/ unencrypted_payload.size(),
          /*ad=*/nullptr,
          /*ad_len=*/0)) {
    return std::nullopt;
  }
  payload.resize(encapsulated_shared_secret_len + ciphertext_len);
  return payload;
}

}  // namespace

// PrivateModelTrainingRequest
PrivateModelTrainingRequest::PrivateModelTrainingRequest(
    auction_worklet::mojom::PrivateModelTrainingRequestDataPtr pmt_request_data,
    url::Origin reporting_origin,
    BiddingAndAuctionServerKey public_key)
    : PrivateModelTrainingRequest(std::move(pmt_request_data),
                                  std::move(reporting_origin),
                                  std::move(public_key),
                                  base::Uuid::GenerateRandomV4(),
                                  base::Time::Now()) {}

PrivateModelTrainingRequest::PrivateModelTrainingRequest(
    auction_worklet::mojom::PrivateModelTrainingRequestDataPtr pmt_request_data,
    url::Origin reporting_origin,
    BiddingAndAuctionServerKey public_key,
    base::Uuid report_id,
    base::Time scheduled_report_time)
    : shared_info_(std::move(report_id),
                   std::move(reporting_origin),
                   scheduled_report_time),
      pmt_request_data_(std::move(pmt_request_data)),
      public_key_(std::move(public_key)) {}

PrivateModelTrainingRequest::PrivateModelTrainingRequest(
    PrivateModelTrainingRequest&& other) = default;

PrivateModelTrainingRequest& PrivateModelTrainingRequest::operator=(
    PrivateModelTrainingRequest&& other) = default;

PrivateModelTrainingRequest::~PrivateModelTrainingRequest() = default;

// static
PrivateModelTrainingRequest
PrivateModelTrainingRequest::CreateRequestForTesting(
    auction_worklet::mojom::PrivateModelTrainingRequestDataPtr pmt_request_data,
    url::Origin reporting_origin,
    BiddingAndAuctionServerKey public_key,
    base::Uuid report_id,
    base::Time scheduled_report_time) {
  return PrivateModelTrainingRequest(
      std::move(pmt_request_data), std::move(reporting_origin),
      std::move(public_key), std::move(report_id),
      std::move(scheduled_report_time));
}

std::optional<std::vector<uint8_t>>
PrivateModelTrainingRequest::SerializeAndEncryptRequest() {
  // Ensure payload has not been moved.
  if (!pmt_request_data_->payload.data()) {
    return std::nullopt;
  }
  // Serialize and encrypt the payload
  const std::optional<std::vector<uint8_t>> serialized_payload =
      FrameAndSerializePayload(std::move(pmt_request_data_->payload),
                               pmt_request_data_->payload_length);
  if (!serialized_payload.has_value()) {
    return {};
  }

  std::optional<std::vector<uint8_t>> shared_info =
      GetSharedInfoCborWithPrefix();
  if (!shared_info.has_value()) {
    return std::nullopt;
  }

  std::optional<std::vector<uint8_t>> encrypted_payload =
      EncryptPayloadWithHpke(std::move(serialized_payload.value()),
                             pmt_request_data_->payload_length,
                             std::move(shared_info.value()), public_key_);

  if (!encrypted_payload.has_value()) {
    return std::nullopt;
  }

  cbor::Value::MapValue aggregation_service_payload_cbor;

  aggregation_service_payload_cbor.emplace("payload",
                                           encrypted_payload.value());
  aggregation_service_payload_cbor.emplace("key_id", public_key_.id);

  cbor::Value::MapValue cbor_payload;
  cbor_payload.emplace("shared_info", GetSharedInfoCborMap());
  cbor_payload.emplace(
      "aggregation_coordinator_origin",
      pmt_request_data_->aggregation_coordinator_origin.spec());
  cbor_payload.emplace("aggregation_service_payload",
                       std::move(aggregation_service_payload_cbor));

  std::optional<std::vector<uint8_t>> final_cbor =
      cbor::Writer::Write(cbor::Value(std::move(cbor_payload)));

  return final_cbor;
}

std::optional<std::vector<uint8_t>>
PrivateModelTrainingRequest::GetSharedInfoCborWithPrefix() {
  cbor::Value::MapValue cbor_map = GetSharedInfoCborMap();

  std::optional<std::vector<uint8_t>> cbor_data =
      cbor::Writer::Write(cbor::Value(std::move(cbor_map)));
  if (!cbor_data.has_value()) {
    return std::nullopt;
  }
  std::vector<uint8_t> prefixed_cbor;
  prefixed_cbor.reserve(kDomainSeparationPrefix.size() +
                        cbor_data.value().size());

  prefixed_cbor.insert(prefixed_cbor.end(), kDomainSeparationPrefix.begin(),
                       kDomainSeparationPrefix.end());

  prefixed_cbor.insert(prefixed_cbor.end(), cbor_data.value().begin(),
                       cbor_data.value().end());
  return prefixed_cbor;
}

cbor::Value::MapValue PrivateModelTrainingRequest::GetSharedInfoCborMap() {
  cbor::Value::MapValue shared_info_cbor;
  shared_info_cbor.emplace("report_id",
                           shared_info_.report_id.AsLowercaseString());
  shared_info_cbor.emplace("reporting_origin",
                           shared_info_.reporting_origin.GetURL().spec());

  shared_info_cbor.emplace(
      "scheduled_report_time",
      shared_info_.scheduled_report_time.InMillisecondsSinceUnixEpoch());
  shared_info_cbor.emplace("version", shared_info_.version);
  shared_info_cbor.emplace("api", shared_info_.api);

  return shared_info_cbor;
}

// Shared Info
PrivateModelTrainingRequest::SharedInfo::SharedInfo(
    base::Uuid report_id,
    url::Origin reporting_origin,
    base::Time scheduled_report_time)
    : report_id(std::move(report_id)),
      reporting_origin(std::move(reporting_origin)),
      scheduled_report_time(scheduled_report_time) {}
PrivateModelTrainingRequest::SharedInfo::~SharedInfo() = default;
PrivateModelTrainingRequest::SharedInfo::SharedInfo(const SharedInfo&) =
    default;
PrivateModelTrainingRequest::SharedInfo::SharedInfo(SharedInfo&&) = default;

PrivateModelTrainingRequest::SharedInfo&
PrivateModelTrainingRequest::SharedInfo::operator=(const SharedInfo& other) =
    default;

PrivateModelTrainingRequest::SharedInfo&
PrivateModelTrainingRequest::SharedInfo::operator=(SharedInfo&& other) =
    default;

}  // namespace content