OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/content_hash_reader.h" | 5 #include "extensions/browser/content_hash_reader.h" |
6 | 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/json/json_reader.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "base/values.h" |
| 12 #include "crypto/sha2.h" |
| 13 #include "extensions/browser/computed_hashes.h" |
| 14 #include "extensions/browser/content_hash_tree.h" |
| 15 #include "extensions/browser/verified_contents.h" |
| 16 #include "extensions/common/extension.h" |
| 17 #include "extensions/common/file_util.h" |
| 18 |
| 19 using base::DictionaryValue; |
| 20 using base::ListValue; |
| 21 using base::Value; |
| 22 |
7 namespace extensions { | 23 namespace extensions { |
8 | 24 |
9 ContentHashReader::ContentHashReader(const std::string& extension_id, | 25 ContentHashReader::ContentHashReader(const std::string& extension_id, |
10 const base::Version& extension_version, | 26 const base::Version& extension_version, |
11 const base::FilePath& extension_root, | 27 const base::FilePath& extension_root, |
12 const base::FilePath& relative_path, | 28 const base::FilePath& relative_path, |
13 const ContentVerifierKey& key) | 29 const ContentVerifierKey& key) |
14 : extension_id_(extension_id), | 30 : extension_id_(extension_id), |
15 extension_version_(extension_version.GetString()), | 31 extension_version_(extension_version.GetString()), |
16 extension_root_(extension_root), | 32 extension_root_(extension_root), |
17 relative_path_(relative_path), | 33 relative_path_(relative_path), |
18 key_(key) { | 34 key_(key), |
| 35 status_(NOT_INITIALIZED), |
| 36 block_size_(0) { |
19 } | 37 } |
20 | 38 |
21 ContentHashReader::~ContentHashReader() { | 39 ContentHashReader::~ContentHashReader() { |
22 } | 40 } |
23 | 41 |
24 bool ContentHashReader::Init() { | 42 bool ContentHashReader::Init() { |
| 43 DCHECK_EQ(status_, NOT_INITIALIZED); |
| 44 status_ = FAILURE; |
| 45 base::FilePath verified_contents_path = |
| 46 file_util::GetVerifiedContentsPath(extension_root_); |
| 47 |
| 48 if (!base::PathExists(verified_contents_path)) |
| 49 return false; |
| 50 |
| 51 verified_contents_.reset(new VerifiedContents(key_.data, key_.size)); |
| 52 if (!verified_contents_->InitFrom(verified_contents_path, false) || |
| 53 !verified_contents_->valid_signature() || |
| 54 !verified_contents_->version().Equals(extension_version_) || |
| 55 verified_contents_->extension_id() != extension_id_) { |
| 56 base::DeleteFile(verified_contents_path, false /* recursive */); |
| 57 return false; |
| 58 } |
| 59 |
| 60 base::FilePath computed_hashes_path = |
| 61 file_util::GetComputedHashesPath(extension_root_); |
| 62 if (!base::PathExists(computed_hashes_path)) |
| 63 return false; |
| 64 |
| 65 ComputedHashes::Reader reader; |
| 66 if (!reader.InitFromFile(computed_hashes_path) || |
| 67 !reader.GetHashes(relative_path_, &block_size_, &hashes_) || |
| 68 block_size_ % crypto::kSHA256Length != 0) { |
| 69 base::DeleteFile(computed_hashes_path, false /* recursive */); |
| 70 return false; |
| 71 } |
| 72 |
| 73 std::string root = |
| 74 ComputeTreeHashRoot(hashes_, block_size_ / crypto::kSHA256Length); |
| 75 const std::string* expected_root = NULL; |
| 76 expected_root = verified_contents_->GetTreeHashRoot(relative_path_); |
| 77 if (expected_root && *expected_root != root) { |
| 78 base::DeleteFile(computed_hashes_path, false /* recursive */); |
| 79 return false; |
| 80 } |
| 81 |
| 82 status_ = SUCCESS; |
25 return true; | 83 return true; |
26 } | 84 } |
27 | 85 |
28 int ContentHashReader::block_count() const { | 86 int ContentHashReader::block_count() const { |
29 return 0; | 87 DCHECK(status_ != NOT_INITIALIZED); |
| 88 return hashes_.size(); |
30 } | 89 } |
31 | 90 |
32 int ContentHashReader::block_size() const { | 91 int ContentHashReader::block_size() const { |
33 return 0; | 92 DCHECK(status_ != NOT_INITIALIZED); |
| 93 return block_size_; |
34 } | 94 } |
35 | 95 |
36 bool ContentHashReader::GetHashForBlock(int block_index, | 96 bool ContentHashReader::GetHashForBlock(int block_index, |
37 const std::string** result) const { | 97 const std::string** result) const { |
38 return false; | 98 if (status_ != SUCCESS) |
| 99 return false; |
| 100 DCHECK(block_index >= 0); |
| 101 |
| 102 if (static_cast<unsigned>(block_index) >= hashes_.size()) |
| 103 return false; |
| 104 *result = &hashes_[block_index]; |
| 105 |
| 106 return true; |
39 } | 107 } |
40 | 108 |
41 } // namespace extensions | 109 } // namespace extensions |
OLD | NEW |