blob: 039fa53e9b3ab33bb6df447489d5ac497920af4b [file] [log] [blame]
[email protected]f83bd432014-05-10 20:44:401// Copyright 2014 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
5#include "extensions/browser/content_hash_reader.h"
6
[email protected]abd4cb22014-05-16 05:22:567#include "base/base64.h"
thestig94712702014-09-10 07:46:598#include "base/files/file_util.h"
[email protected]abd4cb22014-05-16 05:22:569#include "base/json/json_reader.h"
asvitkinef5d4ee562016-11-07 18:57:0810#include "base/metrics/histogram_macros.h"
[email protected]abd4cb22014-05-16 05:22:5611#include "base/strings/string_util.h"
[email protected]bc9b5d92014-06-23 20:28:2712#include "base/timer/elapsed_timer.h"
[email protected]abd4cb22014-05-16 05:22:5613#include "base/values.h"
14#include "crypto/sha2.h"
15#include "extensions/browser/computed_hashes.h"
16#include "extensions/browser/content_hash_tree.h"
17#include "extensions/browser/verified_contents.h"
18#include "extensions/common/extension.h"
19#include "extensions/common/file_util.h"
20
21using base::DictionaryValue;
22using base::ListValue;
23using base::Value;
24
[email protected]f83bd432014-05-10 20:44:4025namespace extensions {
26
27ContentHashReader::ContentHashReader(const std::string& extension_id,
28 const base::Version& extension_version,
29 const base::FilePath& extension_root,
30 const base::FilePath& relative_path,
31 const ContentVerifierKey& key)
32 : extension_id_(extension_id),
33 extension_version_(extension_version.GetString()),
34 extension_root_(extension_root),
35 relative_path_(relative_path),
[email protected]abd4cb22014-05-16 05:22:5636 key_(key),
37 status_(NOT_INITIALIZED),
[email protected]b6641db2014-06-18 15:04:5438 have_verified_contents_(false),
39 have_computed_hashes_(false),
lazyboyd6dbb262017-03-30 00:57:3040 file_missing_from_verified_contents_(false),
41 block_size_(0) {}
[email protected]f83bd432014-05-10 20:44:4042
43ContentHashReader::~ContentHashReader() {
44}
45
46bool ContentHashReader::Init() {
[email protected]bc9b5d92014-06-23 20:28:2747 base::ElapsedTimer timer;
[email protected]abd4cb22014-05-16 05:22:5648 DCHECK_EQ(status_, NOT_INITIALIZED);
49 status_ = FAILURE;
50 base::FilePath verified_contents_path =
51 file_util::GetVerifiedContentsPath(extension_root_);
[email protected]abd4cb22014-05-16 05:22:5652 if (!base::PathExists(verified_contents_path))
53 return false;
54
lazyboy7e635ce2017-03-20 20:16:5755 VerifiedContents verified_contents(key_.data, key_.size);
56 if (!verified_contents.InitFrom(verified_contents_path) ||
57 !verified_contents.valid_signature() ||
58 verified_contents.version() != extension_version_ ||
59 verified_contents.extension_id() != extension_id_) {
[email protected]abd4cb22014-05-16 05:22:5660 return false;
lazyboy7e635ce2017-03-20 20:16:5761 }
[email protected]b6641db2014-06-18 15:04:5462
63 have_verified_contents_ = true;
[email protected]abd4cb22014-05-16 05:22:5664
65 base::FilePath computed_hashes_path =
66 file_util::GetComputedHashesPath(extension_root_);
67 if (!base::PathExists(computed_hashes_path))
68 return false;
69
70 ComputedHashes::Reader reader;
[email protected]b6641db2014-06-18 15:04:5471 if (!reader.InitFromFile(computed_hashes_path))
[email protected]abd4cb22014-05-16 05:22:5672 return false;
[email protected]b6641db2014-06-18 15:04:5473
74 have_computed_hashes_ = true;
75
lazyboyd6dbb262017-03-30 00:57:3076 if (!verified_contents.HasTreeHashRoot(relative_path_)) {
77 // Extension is requesting a non-existent resource that does not have an
78 // entry in verified_contents.json. This can happen when an extension sends
79 // XHR to its non-existent resource. This should not result in content
80 // verification failure.
81 file_missing_from_verified_contents_ = true;
82 return false;
83 }
84
[email protected]b6641db2014-06-18 15:04:5485 if (!reader.GetHashes(relative_path_, &block_size_, &hashes_) ||
86 block_size_ % crypto::kSHA256Length != 0)
87 return false;
88
[email protected]abd4cb22014-05-16 05:22:5689 std::string root =
90 ComputeTreeHashRoot(hashes_, block_size_ / crypto::kSHA256Length);
lazyboy7e635ce2017-03-20 20:16:5791 if (!verified_contents.TreeHashRootEquals(relative_path_, root))
[email protected]abd4cb22014-05-16 05:22:5692 return false;
[email protected]abd4cb22014-05-16 05:22:5693
94 status_ = SUCCESS;
[email protected]bc9b5d92014-06-23 20:28:2795 UMA_HISTOGRAM_TIMES("ExtensionContentHashReader.InitLatency",
96 timer.Elapsed());
[email protected]f83bd432014-05-10 20:44:4097 return true;
98}
99
100int ContentHashReader::block_count() const {
[email protected]abd4cb22014-05-16 05:22:56101 DCHECK(status_ != NOT_INITIALIZED);
102 return hashes_.size();
[email protected]f83bd432014-05-10 20:44:40103}
104
105int ContentHashReader::block_size() const {
[email protected]abd4cb22014-05-16 05:22:56106 DCHECK(status_ != NOT_INITIALIZED);
107 return block_size_;
[email protected]f83bd432014-05-10 20:44:40108}
109
110bool ContentHashReader::GetHashForBlock(int block_index,
111 const std::string** result) const {
[email protected]abd4cb22014-05-16 05:22:56112 if (status_ != SUCCESS)
113 return false;
114 DCHECK(block_index >= 0);
115
116 if (static_cast<unsigned>(block_index) >= hashes_.size())
117 return false;
118 *result = &hashes_[block_index];
119
120 return true;
[email protected]f83bd432014-05-10 20:44:40121}
122
123} // namespace extensions