File: integrity_block_parser.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (252 lines) | stat: -rw-r--r-- 8,603 bytes parent folder | download | duplicates (7)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/web_package/signed_web_bundles/integrity_block_parser.h"

#include "base/containers/map_util.h"
#include "base/functional/bind.h"
#include "base/strings/to_string.h"
#include "base/types/expected.h"
#include "base/types/expected_macros.h"
#include "components/web_package/input_reader.h"
#include "components/web_package/mojom/web_bundle_parser.mojom.h"
#include "components/web_package/signed_web_bundles/attribute_map_parser.h"
#include "components/web_package/signed_web_bundles/constants.h"
#include "components/web_package/signed_web_bundles/integrity_block_attributes.h"
#include "components/web_package/signed_web_bundles/signature_entry_parser.h"
#include "components/web_package/signed_web_bundles/types.h"

namespace web_package {

namespace {

std::optional<base::span<const uint8_t>> ReadByteStringWithHeader(
    InputReader& input) {
  if (std::optional<uint64_t> size =
          input.ReadCBORHeader(CBORType::kByteString)) {
    return input.ReadBytes(*size);
  }
  return std::nullopt;
}

}  // namespace

IntegrityBlockParser::IntegrityBlockParser(
    mojom::BundleDataSource& data_source,
    WebBundleParser::ParseIntegrityBlockCallback callback)
    : data_source_(data_source), result_callback_(std::move(callback)) {}

IntegrityBlockParser::~IntegrityBlockParser() {
  if (!complete_callback_.is_null()) {
    RunErrorCallback("Data source disconnected.",
                     mojom::BundleParseErrorType::kParserInternalError);
  }
}

void IntegrityBlockParser::StartParsing(
    WebBundleParser::WebBundleSectionParser::ParsingCompleteCallback callback) {
  complete_callback_ = std::move(callback);
  // First, we will parse the `magic` and `version` bytes.
  constexpr static uint64_t kMagicBytesAndVersionHeaderLength =
      /*array_header=*/kMaxCBORItemHeaderSize +
      /*magic_bytes_header=*/kMaxCBORItemHeaderSize +
      /*magic_bytes=*/kIntegrityBlockMagicBytes.size() +
      /*version_bytes_header=*/kMaxCBORItemHeaderSize +
      /*version_bytes=*/kIntegrityBlockV2VersionBytes.size();
  data_source_->Read(
      0, kMagicBytesAndVersionHeaderLength,
      base::BindOnce(&IntegrityBlockParser::ParseMagicBytesAndVersion,
                     weak_factory_.GetWeakPtr()));
}

void IntegrityBlockParser::ParseMagicBytesAndVersion(
    const std::optional<BinaryData>& data) {
  if (!data) {
    RunErrorCallback("Error reading the integrity block array structure.");
    return;
  }

  InputReader input(*data);
  std::optional<uint64_t> array_length = input.ReadCBORHeader(CBORType::kArray);
  if (!array_length) {
    RunErrorCallback("Error reading the integrity block array structure.");
    return;
  }

  if (*array_length < 2) {
    RunErrorCallback(
        "Integrity block array size is too short -- expected at least 2 "
        "elements, got " +
        base::ToString(*array_length) + ".");
    return;
  }

  if (ReadByteStringWithHeader(input) != kIntegrityBlockMagicBytes) {
    RunErrorCallback("Unexpected magic bytes.");
    return;
  }

  std::optional<base::span<const uint8_t>> version_bytes =
      ReadByteStringWithHeader(input);
  if (version_bytes == kIntegrityBlockV1VersionBytes) {
    RunErrorCallback(
        "Integrity Block V1 has been deprecated since M129. Please re-sign "
        "your bundle.");
    return;
  }
  if (version_bytes != kIntegrityBlockV2VersionBytes) {
    RunErrorCallback("Unexpected version bytes: expected `2b\\0\\0` (for v2).",
                     mojom::BundleParseErrorType::kVersionError);
    return;
  }
  if (*array_length != kIntegrityBlockV2TopLevelArrayLength) {
    RunErrorCallback("Integrity block array of length " +
                     base::ToString(*array_length) + " - should be " +
                     base::ToString(kIntegrityBlockV2TopLevelArrayLength) +
                     ".");
    return;
  }
  offset_in_stream_ = input.CurrentOffset();
  ReadAttributes();
}

void IntegrityBlockParser::ReadAttributes() {
  attributes_parser_ = std::make_unique<AttributeMapParser>(
      *data_source_, base::BindOnce(&IntegrityBlockParser::ParseAttributes,
                                    weak_factory_.GetWeakPtr()));
  attributes_parser_->Parse(offset_in_stream_);
}

void IntegrityBlockParser::ParseAttributes(
    AttributeMapParser::ParsingResult result) {
  ASSIGN_OR_RETURN(
      (auto [attributes_map, offset_to_end_of_map]), std::move(result),
      [&](std::string error) { RunErrorCallback(std::move(error)); });

  const cbor::Value* web_bundle_id =
      base::FindOrNull(attributes_map, kWebBundleIdAttributeName);
  if (!web_bundle_id || !web_bundle_id->is_string() ||
      web_bundle_id->GetString().empty()) {
    RunErrorCallback(
        "`webBundleId` field in integrity block attributes is missing or "
        "malformed.");
    return;
  }

  uint64_t attribute_map_size = offset_to_end_of_map - offset_in_stream_;
  data_source_->Read(
      offset_in_stream_, attribute_map_size,
      base::BindOnce(&IntegrityBlockParser::ReadAttributesBytes,
                     weak_factory_.GetWeakPtr(), web_bundle_id->GetString()));
}

void IntegrityBlockParser::ReadAttributesBytes(
    std::string web_bundle_id,
    const std::optional<BinaryData>& data) {
  if (!data) {
    RunErrorCallback("Error reading integrity block attributes.");
    return;
  }

  attributes_ = IntegrityBlockAttributes(std::move(web_bundle_id), *data);
  offset_in_stream_ += data->size();

  ReadSignatureStack();
}

void IntegrityBlockParser::ReadSignatureStack() {
  data_source_->Read(offset_in_stream_, kMaxCBORItemHeaderSize,
                     base::BindOnce(&IntegrityBlockParser::ParseSignatureStack,
                                    weak_factory_.GetWeakPtr()));
}

void IntegrityBlockParser::ParseSignatureStack(
    const std::optional<BinaryData>& data) {
  if (!data) {
    RunErrorCallback("Error reading signature stack.");
    return;
  }

  InputReader input(*data);

  const auto signature_stack_size = input.ReadCBORHeader(CBORType::kArray);
  if (!signature_stack_size) {
    RunErrorCallback("Cannot parse the size of the signature stack.");
    return;
  }

  if (*signature_stack_size == 0) {
    RunErrorCallback(
        "The signature stack must contain at least one signature.");
    return;
  }

  offset_in_stream_ += input.CurrentOffset();
  signature_stack_entries_left_ = *signature_stack_size;
  ReadSignatureStackEntry();
}

void IntegrityBlockParser::ReadSignatureStackEntry() {
  current_signature_stack_entry_parser_ =
      std::make_unique<SignatureStackEntryParser>(
          *data_source_,
          base::BindOnce(&IntegrityBlockParser::NextSignatureStackEntry,
                         weak_factory_.GetWeakPtr()));

  current_signature_stack_entry_parser_->Parse(offset_in_stream_);
}

void IntegrityBlockParser::NextSignatureStackEntry(
    base::expected<
        std::pair<mojom::BundleIntegrityBlockSignatureStackEntryPtr, uint64_t>,
        std::string> result) {
  CHECK_GT(signature_stack_entries_left_, 0u);

  if (!result.has_value()) {
    RunErrorCallback(std::move(result.error()),
                     mojom::BundleParseErrorType::kFormatError);
    return;
  }

  auto [signature_entry, offset] = std::move(result.value());

  if (signature_entry->signature_info->is_unknown() &&
      signature_stack_.size() == 0) {
    RunErrorCallback("Unknown cipher type of the first signature.");
    return;
  }

  offset_in_stream_ = offset;
  signature_stack_.emplace_back(std::move(signature_entry));

  --signature_stack_entries_left_;
  if (signature_stack_entries_left_ > 0) {
    ReadSignatureStackEntry();
  } else {
    RunSuccessCallback();
  }
}

void IntegrityBlockParser::RunSuccessCallback() {
  mojom::BundleIntegrityBlockPtr integrity_block =
      mojom::BundleIntegrityBlock::New();
  integrity_block->size = offset_in_stream_;
  integrity_block->signature_stack = std::move(signature_stack_);
  integrity_block->attributes = std::move(*attributes_);

  std::move(complete_callback_)
      .Run(base::BindOnce(std::move(result_callback_),
                          std::move(integrity_block), nullptr));
}

void IntegrityBlockParser::RunErrorCallback(
    const std::string& message,
    mojom::BundleParseErrorType error_type) {
  std::move(complete_callback_)
      .Run(base::BindOnce(
          std::move(result_callback_), nullptr,
          mojom::BundleIntegrityBlockParseError::New(error_type, message)));
}

}  // namespace web_package