[email protected] | f83bd43 | 2014-05-10 20:44:40 | [diff] [blame] | 1 | // 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] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 7 | #include "base/base64.h" |
thestig | 9471270 | 2014-09-10 07:46:59 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 9 | #include "base/json/json_reader.h" |
asvitkine | f5d4ee56 | 2016-11-07 18:57:08 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | bc9b5d9 | 2014-06-23 20:28:27 | [diff] [blame] | 12 | #include "base/timer/elapsed_timer.h" |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 13 | #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 | |
| 21 | using base::DictionaryValue; |
| 22 | using base::ListValue; |
| 23 | using base::Value; |
| 24 | |
[email protected] | f83bd43 | 2014-05-10 20:44:40 | [diff] [blame] | 25 | namespace extensions { |
| 26 | |
| 27 | ContentHashReader::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] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 36 | key_(key), |
| 37 | status_(NOT_INITIALIZED), |
[email protected] | b6641db | 2014-06-18 15:04:54 | [diff] [blame] | 38 | have_verified_contents_(false), |
| 39 | have_computed_hashes_(false), |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 40 | file_missing_from_verified_contents_(false), |
| 41 | block_size_(0) {} |
[email protected] | f83bd43 | 2014-05-10 20:44:40 | [diff] [blame] | 42 | |
| 43 | ContentHashReader::~ContentHashReader() { |
| 44 | } |
| 45 | |
| 46 | bool ContentHashReader::Init() { |
[email protected] | bc9b5d9 | 2014-06-23 20:28:27 | [diff] [blame] | 47 | base::ElapsedTimer timer; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 48 | DCHECK_EQ(status_, NOT_INITIALIZED); |
| 49 | status_ = FAILURE; |
| 50 | base::FilePath verified_contents_path = |
| 51 | file_util::GetVerifiedContentsPath(extension_root_); |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 52 | if (!base::PathExists(verified_contents_path)) |
| 53 | return false; |
| 54 | |
lazyboy | 7e635ce | 2017-03-20 20:16:57 | [diff] [blame] | 55 | 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] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 60 | return false; |
lazyboy | 7e635ce | 2017-03-20 20:16:57 | [diff] [blame] | 61 | } |
[email protected] | b6641db | 2014-06-18 15:04:54 | [diff] [blame] | 62 | |
| 63 | have_verified_contents_ = true; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 64 | |
| 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] | b6641db | 2014-06-18 15:04:54 | [diff] [blame] | 71 | if (!reader.InitFromFile(computed_hashes_path)) |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 72 | return false; |
[email protected] | b6641db | 2014-06-18 15:04:54 | [diff] [blame] | 73 | |
| 74 | have_computed_hashes_ = true; |
| 75 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 76 | 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] | b6641db | 2014-06-18 15:04:54 | [diff] [blame] | 85 | if (!reader.GetHashes(relative_path_, &block_size_, &hashes_) || |
| 86 | block_size_ % crypto::kSHA256Length != 0) |
| 87 | return false; |
| 88 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 89 | std::string root = |
| 90 | ComputeTreeHashRoot(hashes_, block_size_ / crypto::kSHA256Length); |
lazyboy | 7e635ce | 2017-03-20 20:16:57 | [diff] [blame] | 91 | if (!verified_contents.TreeHashRootEquals(relative_path_, root)) |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 92 | return false; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 93 | |
| 94 | status_ = SUCCESS; |
[email protected] | bc9b5d9 | 2014-06-23 20:28:27 | [diff] [blame] | 95 | UMA_HISTOGRAM_TIMES("ExtensionContentHashReader.InitLatency", |
| 96 | timer.Elapsed()); |
[email protected] | f83bd43 | 2014-05-10 20:44:40 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
| 100 | int ContentHashReader::block_count() const { |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 101 | DCHECK(status_ != NOT_INITIALIZED); |
| 102 | return hashes_.size(); |
[email protected] | f83bd43 | 2014-05-10 20:44:40 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int ContentHashReader::block_size() const { |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 106 | DCHECK(status_ != NOT_INITIALIZED); |
| 107 | return block_size_; |
[email protected] | f83bd43 | 2014-05-10 20:44:40 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool ContentHashReader::GetHashForBlock(int block_index, |
| 111 | const std::string** result) const { |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 112 | 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] | f83bd43 | 2014-05-10 20:44:40 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace extensions |