Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/browser/component_updater/sth_set_component_installer.h" | |
| 6 | |
| 7 #include <string> | |
|
Ryan Sleevi
2016/04/06 18:32:51
Is in header
Eran Messeri
2016/04/07 11:38:03
Done.
| |
| 8 #include <utility> | |
| 9 #include <vector> | |
|
Ryan Sleevi
2016/04/06 18:32:51
Is in header
Eran Messeri
2016/04/07 11:38:03
Done.
| |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/files/file_enumerator.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/files/file_util.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/macros.h" | |
|
Ryan Sleevi
2016/04/06 18:32:51
Is in header / unnecessary?
Eran Messeri
2016/04/07 11:38:03
Done.
| |
| 17 #include "base/path_service.h" | |
| 18 #include "base/strings/string_number_conversions.h" | |
| 19 #include "base/values.h" | |
| 20 #include "base/version.h" | |
| 21 #include "components/component_updater/component_updater_paths.h" | |
| 22 #include "components/safe_json/safe_json_parser.h" | |
| 23 #include "content/public/browser/browser_thread.h" | |
| 24 #include "crypto/sha2.h" | |
| 25 #include "net/cert/ct_known_logs_static.h" | |
| 26 #include "net/cert/ct_log_response_parser.h" | |
| 27 #include "net/cert/signed_tree_head.h" | |
| 28 | |
| 29 using component_updater::ComponentUpdateService; | |
| 30 | |
| 31 namespace { | |
| 32 const base::FilePath::CharType kSTHsDirName[] = FILE_PATH_LITERAL("sths"); | |
| 33 } // namespace | |
| 34 | |
| 35 namespace component_updater { | |
| 36 | |
| 37 // The SHA256 of the SubjectPublicKeyInfo used to sign the extension. | |
| 38 // The extension id is: ojjgnpkioondelmggbekfhllhdaimnho | |
| 39 const uint8_t kPublicKeySHA256[32] = { | |
| 40 0xe9, 0x96, 0xdf, 0xa8, 0xee, 0xd3, 0x4b, 0xc6, 0x61, 0x4a, 0x57, | |
| 41 0xbb, 0x73, 0x08, 0xcd, 0x7e, 0x51, 0x9b, 0xcc, 0x69, 0x08, 0x41, | |
| 42 0xe1, 0x96, 0x9f, 0x7c, 0xb1, 0x73, 0xef, 0x16, 0x80, 0x0a}; | |
| 43 | |
| 44 const char kSTHSetFetcherManifestName[] = "Signed Tree Heads"; | |
| 45 | |
| 46 STHSetComponentInstallerTraits::STHSetComponentInstallerTraits( | |
| 47 scoped_ptr<net::ct::STHObserver> sth_observer) | |
| 48 : sth_observer_(std::move(sth_observer)) {} | |
| 49 | |
| 50 STHSetComponentInstallerTraits::~STHSetComponentInstallerTraits() {} | |
| 51 | |
| 52 bool STHSetComponentInstallerTraits::CanAutoUpdate() const { | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 // Public data is delivered via this component, no need for encryption. | |
| 57 bool STHSetComponentInstallerTraits::RequiresNetworkEncryption() const { | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 bool STHSetComponentInstallerTraits::OnCustomInstall( | |
| 62 const base::DictionaryValue& manifest, | |
| 63 const base::FilePath& install_dir) { | |
| 64 return true; // Nothing custom here. | |
| 65 } | |
| 66 | |
| 67 base::FilePath STHSetComponentInstallerTraits::GetInstalledPath( | |
| 68 const base::FilePath& base) { | |
| 69 return base.Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 70 .Append(FILE_PATH_LITERAL("all")) | |
| 71 .Append(kSTHsDirName); | |
| 72 } | |
| 73 | |
| 74 void STHSetComponentInstallerTraits::ComponentReady( | |
| 75 const base::Version& version, | |
| 76 const base::FilePath& install_dir, | |
| 77 scoped_ptr<base::DictionaryValue> manifest) { | |
| 78 if (!content::BrowserThread::PostBlockingPoolTask( | |
| 79 FROM_HERE, | |
| 80 base::Bind(&STHSetComponentInstallerTraits::LoadSTHsFromDisk, | |
| 81 base::Unretained(this), GetInstalledPath(install_dir), | |
| 82 version))) { | |
| 83 NOTREACHED(); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 // Called during startup and installation before ComponentReady(). | |
| 88 bool STHSetComponentInstallerTraits::VerifyInstallation( | |
| 89 const base::DictionaryValue& manifest, | |
| 90 const base::FilePath& install_dir) const { | |
| 91 return base::PathExists(GetInstalledPath(install_dir)); | |
| 92 } | |
| 93 | |
| 94 base::FilePath STHSetComponentInstallerTraits::GetBaseDirectory() const { | |
| 95 base::FilePath result; | |
| 96 PathService::Get(DIR_CERT_TRANS_TREE_STATES, &result); | |
|
Ryan Sleevi
2016/04/06 18:32:50
Is it safe to ignore the return value here? Is tha
Eran Messeri
2016/04/07 11:38:03
I believe it's safe - it's done in all other class
| |
| 97 return result; | |
| 98 } | |
| 99 | |
| 100 void STHSetComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { | |
| 101 hash->assign(std::begin(kPublicKeySHA256), std::end(kPublicKeySHA256)); | |
| 102 } | |
| 103 | |
| 104 std::string STHSetComponentInstallerTraits::GetName() const { | |
| 105 return kSTHSetFetcherManifestName; | |
| 106 } | |
| 107 | |
| 108 void STHSetComponentInstallerTraits::LoadSTHsFromDisk( | |
| 109 const base::FilePath& sths_path, | |
| 110 const base::Version& version) { | |
| 111 if (sths_path.empty()) | |
| 112 return; | |
| 113 | |
| 114 base::FileEnumerator sth_file_enumerator(sths_path, false, | |
| 115 base::FileEnumerator::FILES, | |
| 116 FILE_PATH_LITERAL("*.sth")); | |
|
Ryan Sleevi
2016/04/06 18:32:50
DESIGN/Curious: Is there a reason to store these a
Eran Messeri
2016/04/07 11:38:03
This was done mainly for simplicity of the impleme
| |
| 117 base::FilePath sth_file_path; | |
| 118 | |
| 119 while (!(sth_file_path = sth_file_enumerator.Next()).empty()) { | |
| 120 VLOG(1) << "Reading STH from file: " << sth_file_path.value(); | |
|
Ryan Sleevi
2016/04/06 18:32:51
You asked me to review this as if it was done, but
Eran Messeri
2016/04/07 11:38:03
I've made a mental note to search for those before
| |
| 121 | |
| 122 const std::string log_id_hex = | |
| 123 sth_file_path.BaseName().RemoveExtension().MaybeAsASCII(); | |
| 124 if (log_id_hex.empty()) { | |
| 125 DVLOG(1) << "Error extracting log_id from: " | |
| 126 << sth_file_path.BaseName().LossyDisplayName(); | |
| 127 continue; | |
| 128 } | |
| 129 | |
| 130 std::vector<uint8_t> decoding_output; | |
| 131 if (!base::HexStringToBytes(log_id_hex, &decoding_output)) { | |
| 132 DVLOG(1) << "Failed to decode Log ID: " << log_id_hex; | |
| 133 continue; | |
| 134 } | |
| 135 | |
| 136 const std::string log_id(reinterpret_cast<const char*>(&decoding_output[0]), | |
| 137 decoding_output.size()); | |
| 138 | |
| 139 std::string json_sth; | |
| 140 if (!base::ReadFileToString(sth_file_path, &json_sth)) { | |
| 141 VLOG(1) << "Failed reading from " << sth_file_path.value(); | |
| 142 continue; | |
| 143 } | |
| 144 | |
| 145 VLOG(1) << "STH: Successfully read: " << json_sth; | |
| 146 safe_json::SafeJsonParser::Parse( | |
| 147 json_sth, | |
| 148 base::Bind(&STHSetComponentInstallerTraits::OnJsonParseSuccess, | |
| 149 base::Unretained(this), log_id), | |
| 150 base::Bind(&STHSetComponentInstallerTraits::OnJsonParseError, | |
| 151 base::Unretained(this), log_id)); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 void STHSetComponentInstallerTraits::OnJsonParseSuccess( | |
| 156 std::string log_id, | |
| 157 scoped_ptr<base::Value> parsed_json) { | |
| 158 net::ct::SignedTreeHead signed_tree_head; | |
| 159 VLOG(0) << "STH parsing success for log: " | |
| 160 << base::HexEncode(log_id.data(), log_id.length()); | |
| 161 if (!net::ct::FillSignedTreeHead(*(parsed_json.get()), &signed_tree_head)) { | |
| 162 LOG(WARNING) << "Failed to fill in signed tree head."; | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 // The log id is not a part of the response, fill in manually. | |
| 167 signed_tree_head.log_id = log_id; | |
| 168 content::BrowserThread::PostTask( | |
| 169 content::BrowserThread::IO, FROM_HERE, | |
| 170 base::Bind(&net::ct::STHObserver::NewSTHObserved, | |
| 171 base::Unretained(sth_observer_.get()), signed_tree_head)); | |
| 172 } | |
| 173 | |
| 174 void STHSetComponentInstallerTraits::OnJsonParseError( | |
| 175 std::string log_id, | |
| 176 const std::string& error) { | |
| 177 VLOG(0) << "STH loading failed: " << error | |
| 178 << " for log: " << base::HexEncode(log_id.data(), log_id.length()); | |
| 179 } | |
| 180 | |
| 181 void RegisterSTHSetComponent(ComponentUpdateService* cus, | |
| 182 const base::FilePath& user_data_dir, | |
| 183 scoped_ptr<net::ct::STHObserver> sth_observer) { | |
| 184 VLOG(1) << "Registering STH Set fetcher component."; | |
| 185 | |
| 186 scoped_ptr<ComponentInstallerTraits> traits( | |
| 187 new STHSetComponentInstallerTraits(std::move(sth_observer))); | |
| 188 // |cus| will take ownership of |installer| during installer->Register(cus). | |
| 189 DefaultComponentInstaller* installer = | |
| 190 new DefaultComponentInstaller(std::move(traits)); | |
| 191 installer->Register(cus, base::Closure()); | |
| 192 } | |
| 193 | |
| 194 } // namespace component_updater | |
| OLD | NEW |