integrity_block_parser.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_WEB_PACKAGE_SIGNED_WEB_BUNDLES_INTEGRITY_BLOCK_PARSER_H_
  5. #define COMPONENTS_WEB_PACKAGE_SIGNED_WEB_BUNDLES_INTEGRITY_BLOCK_PARSER_H_
  6. #include "components/web_package/web_bundle_parser.h"
  7. #include "third_party/abseil-cpp/absl/types/optional.h"
  8. namespace web_package {
  9. // A parser for a signed bundle's metadata. This class owns itself and will self
  10. // destruct after calling the ParseIntergrityBlockCallback.
  11. class IntegrityBlockParser : WebBundleParser::SharedBundleDataSource::Observer {
  12. public:
  13. IntegrityBlockParser(
  14. scoped_refptr<WebBundleParser::SharedBundleDataSource> data_source,
  15. WebBundleParser::ParseIntegrityBlockCallback callback);
  16. IntegrityBlockParser(const IntegrityBlockParser&) = delete;
  17. IntegrityBlockParser& operator=(const IntegrityBlockParser&) = delete;
  18. ~IntegrityBlockParser() override;
  19. void Start();
  20. private:
  21. void ParseMagicBytesAndVersion(
  22. const absl::optional<std::vector<uint8_t>>& data);
  23. void ParseSignatureStack(uint64_t offset_in_stream,
  24. const absl::optional<std::vector<uint8_t>>& data);
  25. void ReadSignatureStackEntry(const uint64_t offset_in_stream,
  26. const uint64_t signature_stack_entries_left);
  27. void ParseSignatureStackEntry(
  28. uint64_t offset_in_stream,
  29. const uint64_t signature_stack_entries_left,
  30. const absl::optional<std::vector<uint8_t>>& data);
  31. void ParseSignatureStackEntryAttributesHeader(
  32. uint64_t offset_in_stream,
  33. const uint64_t signature_stack_entries_left,
  34. mojom::BundleIntegrityBlockSignatureStackEntryPtr signature_stack_entry,
  35. const absl::optional<std::vector<uint8_t>>& data);
  36. void ParseSignatureStackEntryAttributesPublicKeyKey(
  37. uint64_t offset_in_stream,
  38. const uint64_t signature_stack_entries_left,
  39. mojom::BundleIntegrityBlockSignatureStackEntryPtr signature_stack_entry,
  40. const absl::optional<std::vector<uint8_t>>& data);
  41. void ReadSignatureStackEntryAttributesPublicKeyValue(
  42. uint64_t offset_in_stream,
  43. const uint64_t signature_stack_entries_left,
  44. mojom::BundleIntegrityBlockSignatureStackEntryPtr signature_stack_entry,
  45. const absl::optional<std::vector<uint8_t>>& public_key);
  46. void ParseSignatureStackEntrySignatureHeader(
  47. uint64_t offset_in_stream,
  48. const uint64_t signature_stack_entries_left,
  49. mojom::BundleIntegrityBlockSignatureStackEntryPtr signature_stack_entry,
  50. const absl::optional<std::vector<uint8_t>>& data);
  51. void ParseSignatureStackEntrySignature(
  52. uint64_t offset_in_stream,
  53. uint64_t signature_stack_entries_left,
  54. mojom::BundleIntegrityBlockSignatureStackEntryPtr signature_stack_entry,
  55. const absl::optional<std::vector<uint8_t>>& signature);
  56. void RunSuccessCallbackAndDestroy(const uint64_t offset_in_stream);
  57. void RunErrorCallbackAndDestroy(
  58. const std::string& message,
  59. mojom::BundleParseErrorType error_type =
  60. mojom::BundleParseErrorType::kFormatError);
  61. // Implements SharedBundleDataSource::Observer.
  62. void OnDisconnect() override;
  63. scoped_refptr<WebBundleParser::SharedBundleDataSource> data_source_;
  64. WebBundleParser::ParseIntegrityBlockCallback callback_;
  65. std::vector<mojom::BundleIntegrityBlockSignatureStackEntryPtr>
  66. signature_stack_;
  67. base::WeakPtrFactory<IntegrityBlockParser> weak_factory_{this};
  68. };
  69. } // namespace web_package
  70. #endif // COMPONENTS_WEB_PACKAGE_SIGNED_WEB_BUNDLES_INTEGRITY_BLOCK_PARSER_H_