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> | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 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" | |
| 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 bool STHSetComponentInstallerTraits::OnCustomInstall( | |
| 57 const base::DictionaryValue& manifest, | |
| 58 const base::FilePath& install_dir) { | |
| 59 return true; // Nothing custom here. | |
| 60 } | |
| 61 | |
| 62 base::FilePath STHSetComponentInstallerTraits::GetInstalledPath( | |
| 63 const base::FilePath& base) { | |
| 64 return base.Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 65 .Append(FILE_PATH_LITERAL("all")) | |
| 66 .Append(kSTHsDirName); | |
| 67 } | |
| 68 | |
| 69 void STHSetComponentInstallerTraits::ComponentReady( | |
| 70 const base::Version& version, | |
| 71 const base::FilePath& install_dir, | |
| 72 scoped_ptr<base::DictionaryValue> manifest) { | |
| 73 VLOG(1) << "Component ready, version " << version.GetString() << " in " | |
|
Sorin Jianu
2016/04/04 22:06:13
At like to request a small favor. Could you please
Eran Messeri
2016/04/05 15:34:06
Done.
| |
| 74 << install_dir.value(); | |
| 75 | |
| 76 if (!content::BrowserThread::PostBlockingPoolTask( | |
| 77 FROM_HERE, | |
| 78 base::Bind(&STHSetComponentInstallerTraits::LoadSTHsFromDisk, | |
| 79 base::Unretained(this), GetInstalledPath(install_dir), | |
| 80 version))) { | |
| 81 NOTREACHED(); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 // Called during startup and installation before ComponentReady(). | |
| 86 bool STHSetComponentInstallerTraits::VerifyInstallation( | |
| 87 const base::DictionaryValue& manifest, | |
| 88 const base::FilePath& install_dir) const { | |
| 89 return base::PathExists(GetInstalledPath(install_dir)); | |
| 90 } | |
| 91 | |
| 92 base::FilePath STHSetComponentInstallerTraits::GetBaseDirectory() const { | |
| 93 base::FilePath result; | |
| 94 PathService::Get(DIR_CERT_TRANS_TREE_STATES, &result); | |
| 95 return result; | |
| 96 } | |
| 97 | |
| 98 void STHSetComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { | |
| 99 hash->assign(kPublicKeySHA256, | |
|
Sorin Jianu
2016/04/04 22:06:13
we could now use std::begin and std::end, would th
Eran Messeri
2016/04/05 15:34:06
Done.
| |
| 100 kPublicKeySHA256 + arraysize(kPublicKeySHA256)); | |
| 101 } | |
| 102 | |
| 103 std::string STHSetComponentInstallerTraits::GetName() const { | |
| 104 return kSTHSetFetcherManifestName; | |
| 105 } | |
| 106 | |
| 107 void STHSetComponentInstallerTraits::LoadSTHsFromDisk( | |
| 108 const base::FilePath& sths_path, | |
| 109 const base::Version& version) { | |
| 110 if (sths_path.empty()) | |
| 111 return; | |
| 112 | |
| 113 base::FileEnumerator sth_file_enumerator(sths_path, false, | |
| 114 base::FileEnumerator::FILES, | |
| 115 FILE_PATH_LITERAL("*.sth")); | |
| 116 base::FilePath sth_file_path; | |
| 117 | |
| 118 while (!(sth_file_path = sth_file_enumerator.Next()).empty()) { | |
| 119 VLOG(1) << "Reading STH from file: " << sth_file_path.value(); | |
| 120 | |
| 121 std::string log_id_hex = | |
|
Sorin Jianu
2016/04/04 22:06:13
can this be const?
Eran Messeri
2016/04/05 15:34:06
Done.
| |
| 122 sth_file_path.BaseName().RemoveExtension().MaybeAsASCII(); | |
| 123 if (log_id_hex.empty()) { | |
| 124 DVLOG(1) << "Error extracting log_id from: " | |
| 125 << sth_file_path.BaseName().LossyDisplayName(); | |
| 126 continue; | |
| 127 } | |
| 128 | |
| 129 std::vector<uint8_t> decoding_output; | |
| 130 if (!base::HexStringToBytes(log_id_hex, &decoding_output)) { | |
| 131 DVLOG(1) << "Failed to decode Log ID: " << log_id_hex; | |
| 132 continue; | |
| 133 } | |
| 134 | |
| 135 std::string log_id; | |
|
Sorin Jianu
2016/04/04 22:06:13
I think this can be initialized at construction ti
Eran Messeri
2016/04/05 15:34:06
Done.
| |
| 136 log_id.assign(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 |