| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| // |
| |
| #ifndef THIRD_PARTY_CREDENTIO_BINDINGS_MERKLE_VALIDATOR_H_ |
| #define THIRD_PARTY_CREDENTIO_BINDINGS_MERKLE_VALIDATOR_H_ |
| |
| #include <memory> |
| #include <utility> |
| #include <vector> |
| |
| #include "absl/base/nullability.h" |
| #include "absl/status/status.h" |
| #include "absl/status/status_macros.h" |
| #include "absl/status/statusor.h" |
| #include "absl/strings/string_view.h" |
| #include "crypto/algorithms.h" |
| #include "crypto/default/hasher.h" |
| #include "crypto/hash.h" |
| #include "formats/bmff/box_header.h" |
| #include "proto/bmff_based_hash_assertion.pb.h" |
| #include "riegeli/bytes/reader.h" |
| #include "utils/status_tracker.h" |
| |
| namespace credentio { |
| |
| class HasherFactoryProvider { |
| public: |
| virtual ~HasherFactoryProvider() = default; |
| virtual absl::StatusOr<std::unique_ptr<HasherFactory> absl_nonnull> Create( |
| absl::string_view algo) const = 0; |
| }; |
| |
| class DefaultHasherFactoryProvider : public HasherFactoryProvider { |
| public: |
| absl::StatusOr<std::unique_ptr<HasherFactory> absl_nonnull> Create( |
| absl::string_view algo) const override { |
| ABSL_ASSIGN_OR_RETURN(auto algorithm, ParseHashAlgorithm(algo)); |
| switch (algorithm) { |
| case HashAlgorithm::kSha256: |
| return std::make_unique<Sha256HasherFactory>(); |
| case HashAlgorithm::kSha384: |
| return std::make_unique<Sha384HasherFactory>(); |
| case HashAlgorithm::kSha512: |
| return std::make_unique<Sha512HasherFactory>(); |
| } |
| return nullptr; |
| } |
| }; |
| |
| class MerkleValidator { |
| public: |
| // Creates a MerkleValidator that will validate the merkle-maps contained |
| // within the BmffBasedHashAssertion. The validator will use the provided |
| // factory to create hashers for each hash algorithm it encounters. |
| MerkleValidator( |
| riegeli::Reader& contents, const BmffBasedHashAssertion& assertion, |
| absl::string_view assertion_uri, |
| std::unique_ptr<HasherFactoryProvider> absl_nonnull factory_provider = |
| std::make_unique<DefaultHasherFactoryProvider>()) |
| : contents_(contents), |
| factory_provider_(std::move(factory_provider)), |
| assertion_(assertion), |
| assertion_uri_(assertion_uri) {} |
| |
| ~MerkleValidator() = default; |
| |
| // Validates all the merkle-maps contained within the BmffBasedHashAssertion, |
| // recording any failures in the StatusTracker. This method will not add any |
| // success codes to the tracker, as that is handled by the main |
| // BmffHashHardBindingValidator. |
| absl::Status Validate(StatusTracker& tracker) const; |
| |
| private: |
| riegeli::Reader& contents_; |
| std::unique_ptr<HasherFactoryProvider> factory_provider_; |
| const BmffBasedHashAssertion& assertion_; |
| absl::string_view assertion_uri_; |
| |
| // Validates a single merkle map, recording any failures in the |
| // StatusTracker. |
| absl::Status ValidateMerkleMap( |
| const BmffMerkle& merkle, const BmffBoxHeader& mdat_atom, |
| std::vector<BmffMerkleMap> auxiliary_merkle_maps, |
| absl::string_view fallback_algo, StatusTracker& tracker) const; |
| }; |
| |
| } // namespace credentio |
| |
| #endif // THIRD_PARTY_CREDENTIO_BINDINGS_MERKLE_VALIDATOR_H_ |