Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "chrome/common/component_flash_hint_file.h" | |
| 6 | |
| 7 #include "base/base64.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/files/important_file_writer.h" | |
| 11 #include "base/files/memory_mapped_file.h" | |
| 12 #include "base/json/json_string_value_serializer.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/stl_util.h" | |
| 15 #include "base/values.h" | |
| 16 #include "chrome/common/chrome_paths.h" | |
| 17 #include "crypto/sha2.h" | |
| 18 | |
| 19 namespace chrome { | |
| 20 | |
| 21 // The current version of the hints file. | |
| 22 static const int kCurrentHintFileVersion = 0x10; | |
| 23 // The earliest version of the hints file. | |
| 24 static const int kEarliestHintFileVersion = 0x10; | |
| 25 // The Version field in the JSON encoded file. | |
| 26 static const char kVersionField[] = "Version"; | |
| 27 // The HashAlgorithm field in the JSON encoded file. | |
| 28 static const char kHashAlgoField[] = "HashAlgorithm"; | |
| 29 // The Hash field in the JSON encoded file. | |
| 30 static const char kHashField[] = "Hash"; | |
| 31 // The PluginPath field in the JSON encoded file. | |
| 32 static const char kPluginPath[] = "PluginPath"; | |
| 33 // The PluginVersion field in the JSON encoded file. | |
| 34 static const char kPluginVersion[] = "PluginVersion"; | |
| 35 | |
| 36 ComponentFlashHintFile::ComponentFlashHintFile() | |
| 37 : was_previous_hint_file_(false), previous_hint_file_() {} | |
| 38 | |
| 39 bool ComponentFlashHintFile::RecordFlashUpdate(base::FilePath& unpacked_plugin, | |
| 40 base::FilePath& moved_plugin, | |
| 41 const std::string& version) { | |
| 42 base::MemoryMappedFile mapped_file; | |
| 43 if (!mapped_file.Initialize(unpacked_plugin)) | |
| 44 return false; | |
| 45 | |
| 46 std::string hash(crypto::kSHA256Length, 0); | |
| 47 crypto::SHA256HashBytes(mapped_file.data(), mapped_file.length(), | |
| 48 string_as_array(&hash), hash.size()); | |
| 49 | |
| 50 return WriteToDisk(kCurrentHintFileVersion, | |
| 51 crypto::SecureHash::Algorithm::SHA256, hash, moved_plugin, | |
| 52 version); | |
| 53 } | |
| 54 | |
| 55 // static | |
| 56 bool ComponentFlashHintFile::DoesHintFileExist() { | |
| 57 base::FilePath hint_file_path; | |
| 58 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | |
| 59 return false; | |
| 60 return base::PathExists(hint_file_path); | |
| 61 } | |
| 62 | |
| 63 bool ComponentFlashHintFile::Revert() { | |
| 64 if (!was_previous_hint_file_) | |
| 65 return false; | |
| 66 base::FilePath hint_file_path; | |
| 67 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | |
| 68 return false; | |
| 69 return base::ImportantFileWriter::WriteFileAtomically(hint_file_path, | |
| 70 previous_hint_file_); | |
| 71 } | |
| 72 | |
| 73 bool ComponentFlashHintFile::WriteToDisk( | |
| 74 int version, | |
|
rickyz (no longer on Chrome)
2015/07/31 00:13:56
Don't think this param is used - kCurrentHintFileV
Greg K
2015/08/04 00:21:17
That is correct, although I corrected it to use th
| |
| 75 crypto::SecureHash::Algorithm algorithm, | |
| 76 const std::string& hash, | |
| 77 base::FilePath& plugin_path, | |
| 78 const std::string& flash_version) { | |
| 79 base::FilePath hint_file_path; | |
| 80 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | |
| 81 return false; | |
| 82 | |
| 83 // Check for and try to back up the current hints file in case it needs to be | |
| 84 // reverted. | |
| 85 was_previous_hint_file_ = base::PathExists(hint_file_path); | |
| 86 if (was_previous_hint_file_) { | |
| 87 if (!ReadFileToString(hint_file_path, &previous_hint_file_)) | |
|
rickyz (no longer on Chrome)
2015/07/31 00:13:56
Might it be easier to back it up to a file instead
Greg K
2015/08/04 00:21:17
We don't know to revert at all anymore since the f
| |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 std::string encoded_hash; | |
| 92 base::Base64Encode(hash, &encoded_hash); | |
| 93 | |
| 94 // Now construct a Value object to convert to JSON. | |
| 95 base::DictionaryValue dict; | |
| 96 dict.SetInteger(kVersionField, kCurrentHintFileVersion); | |
| 97 dict.SetInteger(kHashAlgoField, crypto::SecureHash::SHA256); | |
| 98 dict.SetString(kHashField, encoded_hash); | |
| 99 dict.SetString(kPluginPath, plugin_path.value()); | |
| 100 dict.SetString(kPluginVersion, flash_version); | |
| 101 // Do the serialization of the DictionaryValue to JSON. | |
| 102 std::string json_string; | |
| 103 JSONStringValueSerializer serializer(&json_string); | |
| 104 if (!serializer.Serialize(dict)) | |
| 105 return false; | |
| 106 | |
| 107 return base::ImportantFileWriter::WriteFileAtomically(hint_file_path, | |
| 108 json_string); | |
| 109 } | |
| 110 | |
| 111 // static | |
| 112 bool ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
| 113 base::FilePath* path, | |
| 114 std::string* flash_version) { | |
| 115 base::FilePath hint_file_path; | |
| 116 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | |
| 117 return false; | |
| 118 | |
| 119 std::string json_string; | |
| 120 if (!base::ReadFileToString(hint_file_path, &json_string)) | |
| 121 return false; | |
| 122 | |
| 123 int error_code; | |
| 124 std::string error_message; | |
| 125 JSONStringValueDeserializer deserializer(json_string); | |
| 126 scoped_ptr<base::Value> value( | |
| 127 deserializer.Deserialize(&error_code, &error_message)); | |
| 128 | |
| 129 if (value == nullptr) { | |
| 130 LOG(ERROR) | |
| 131 << "Could not deserialize the component updated flash hint file. Error " | |
| 132 << error_code << ": " << error_message; | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 base::DictionaryValue* dict = nullptr; | |
| 137 if (!value->GetAsDictionary(&dict)) | |
| 138 return false; | |
| 139 | |
| 140 int version; | |
| 141 if (!dict->GetInteger(kVersionField, &version)) | |
| 142 return false; | |
| 143 if (version < kEarliestHintFileVersion || version > kCurrentHintFileVersion) | |
| 144 return false; | |
| 145 | |
| 146 int hash_algorithm; | |
| 147 if (!dict->GetInteger(kHashAlgoField, &hash_algorithm)) | |
| 148 return false; | |
| 149 if (hash_algorithm != crypto::SecureHash::SHA256) | |
| 150 return false; | |
| 151 | |
| 152 std::string hash; | |
| 153 if (!dict->GetString(kHashField, &hash)) | |
| 154 return false; | |
| 155 | |
| 156 std::string plugin_path_str; | |
| 157 if (!dict->GetString(kPluginPath, &plugin_path_str)) | |
| 158 return false; | |
| 159 | |
| 160 std::string plugin_version_str; | |
| 161 if (!dict->GetString(kPluginVersion, &plugin_version_str)) | |
| 162 return false; | |
| 163 | |
| 164 std::string decoded_hash; | |
| 165 if (!base::Base64Decode(hash, &decoded_hash)) | |
| 166 return false; | |
| 167 | |
| 168 base::FilePath plugin_path(plugin_path_str); | |
| 169 base::MemoryMappedFile plugin_file; | |
| 170 if (!plugin_file.Initialize(plugin_path)) | |
| 171 return false; | |
| 172 | |
| 173 std::vector<uint8_t> file_hash(crypto::kSHA256Length, 0); | |
| 174 crypto::SHA256HashBytes(plugin_file.data(), plugin_file.length(), | |
| 175 &file_hash[0], file_hash.size()); | |
| 176 bool hashes_same = memcmp(&file_hash[0], string_as_array(&decoded_hash), | |
| 177 crypto::kSHA256Length) == 0; | |
| 178 DCHECK(hashes_same); | |
|
rickyz (no longer on Chrome)
2015/07/31 00:13:56
I'd probably replace this with a LOG(WARNING) inst
Greg K
2015/08/04 00:21:17
I went with LOG(ERROR) on the grounds that this co
| |
| 179 if (!hashes_same) | |
| 180 return false; | |
| 181 | |
| 182 *path = plugin_path; | |
| 183 flash_version->assign(plugin_version_str); | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 } // namespace chrome | |
| OLD | NEW |