electron/shell/browser/net/asar/asar_file_validator.h
trop[bot] 06ad763412
refactor: migrate hashing code to new upstream crypto::hash API (#46273)
* refactor: migrate AsarFileValidator to crypto::hash

This change migrates AsarFileValidator's uses of crypto::secure_hash
to the new crypto::hash API, which has more memory safety and less
heap allocations.

Xref: 6287609

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: migrate ValidateIntegrityOrDie to crypto::hash

This change migrates ValidateIntegrityOrDie's use of crypto::SHA256Hash
to the new crypto::hash API, which has more memory safety and less heap
allocations.

Xref: 6287609

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2025-03-25 19:41:49 -05:00

64 lines
2 KiB
C++

// Copyright (c) 2021 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_NET_ASAR_ASAR_FILE_VALIDATOR_H_
#define ELECTRON_SHELL_BROWSER_NET_ASAR_ASAR_FILE_VALIDATOR_H_
#include <optional>
#include "crypto/hash.h"
#include "mojo/public/cpp/system/file_data_source.h"
#include "mojo/public/cpp/system/filtered_data_source.h"
#include "shell/common/asar/archive.h"
namespace asar {
class AsarFileValidator : public mojo::FilteredDataSource::Filter {
public:
AsarFileValidator(IntegrityPayload integrity, base::File file);
~AsarFileValidator() override;
// disable copy
AsarFileValidator(const AsarFileValidator&) = delete;
AsarFileValidator& operator=(const AsarFileValidator&) = delete;
// mojo::FilteredDataSource::Filter
void OnRead(base::span<char> buffer,
mojo::FileDataSource::ReadResult* result) override;
void OnDone() override;
void SetRange(uint64_t read_start, uint64_t extra_read, uint64_t read_max);
void SetCurrentBlock(int current_block);
protected:
bool FinishBlock();
private:
void EnsureBlockHashExists();
base::File file_;
IntegrityPayload integrity_;
// The offset in the file_ that the underlying file reader is starting at
uint64_t read_start_ = 0;
// The number of bytes this DataSourceFilter will have seen that aren't used
// by the DataProducer. These extra bytes are exclusively for hash validation
// but we need to know how many we've used so we know when we're done.
uint64_t extra_read_ = 0;
// The maximum offset in the file_ that we should read to, used to determine
// which bytes we're missing or if we need to read up to a block boundary in
// OnDone
uint64_t read_max_ = 0;
bool done_reading_ = false;
int current_block_;
int max_block_;
uint64_t current_hash_byte_count_ = 0U;
uint64_t total_hash_byte_count_ = 0;
std::optional<crypto::hash::Hasher> current_hash_;
};
} // namespace asar
#endif // ELECTRON_SHELL_BROWSER_NET_ASAR_ASAR_FILE_VALIDATOR_H_