Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/renderer_host/pepper/device_id_fetcher.h" | 5 #include "chrome/browser/renderer_host/pepper/device_id_fetcher.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 using content::RenderProcessHost; | 29 using content::RenderProcessHost; |
| 30 | 30 |
| 31 namespace chrome { | 31 namespace chrome { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 const char kDRMIdentifierFile[] = "Pepper DRM ID.0"; | 35 const char kDRMIdentifierFile[] = "Pepper DRM ID.0"; |
| 36 | 36 |
| 37 const uint32_t kSaltLength = 32; | 37 const uint32_t kSaltLength = 32; |
| 38 | 38 |
| 39 void GetMachineIDAsync(const DeviceIDFetcher::IDCallback& callback) { | |
| 40 std::string result; | |
| 41 #if defined(OS_WIN) && defined(ENABLE_RLZ) | |
| 42 rlz_lib::GetMachineId(&result); | |
| 43 #elif defined(OS_CHROMEOS) | |
| 44 result = chromeos::CryptohomeLibrary::Get()->GetSystemSalt(); | |
| 45 if (result.empty()) { | |
|
zel
2013/09/20 16:28:05
this logic should move to async version of Cryptoh
stevenjb
2013/09/20 16:42:23
Agreed. This will require some significant changes
| |
| 46 // cryptohome must not be running; re-request after a delay. | |
| 47 const int64 kRequestSystemSaltDelayMs = 500; | |
| 48 base::MessageLoop::current()->PostDelayedTask( | |
| 49 FROM_HERE, | |
| 50 base::Bind(&GetMachineIDAsync, callback), | |
| 51 base::TimeDelta::FromMilliseconds(kRequestSystemSaltDelayMs)); | |
| 52 return; | |
| 53 } | |
| 54 #else | |
| 55 // Not implemented for other platforms. | |
| 56 NOTREACHED(); | |
| 57 #endif | |
| 58 callback.Run(result); | |
| 59 } | |
| 60 | |
| 39 } // namespace | 61 } // namespace |
| 40 | 62 |
| 41 DeviceIDFetcher::DeviceIDFetcher(int render_process_id) | 63 DeviceIDFetcher::DeviceIDFetcher(int render_process_id) |
| 42 : in_progress_(false), | 64 : in_progress_(false), |
| 43 render_process_id_(render_process_id) { | 65 render_process_id_(render_process_id) { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 45 } | 67 } |
| 46 | 68 |
| 47 DeviceIDFetcher::~DeviceIDFetcher() { | 69 DeviceIDFetcher::~DeviceIDFetcher() { |
| 48 } | 70 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 73 "", | 95 "", |
| 74 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 96 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 75 } | 97 } |
| 76 | 98 |
| 77 // static | 99 // static |
| 78 base::FilePath DeviceIDFetcher::GetLegacyDeviceIDPath( | 100 base::FilePath DeviceIDFetcher::GetLegacyDeviceIDPath( |
| 79 const base::FilePath& profile_path) { | 101 const base::FilePath& profile_path) { |
| 80 return profile_path.AppendASCII(kDRMIdentifierFile); | 102 return profile_path.AppendASCII(kDRMIdentifierFile); |
| 81 } | 103 } |
| 82 | 104 |
| 83 // TODO(raymes): Change this to just return the device id salt and call it with | |
| 84 // PostTaskAndReply once the legacy ChromeOS codepath is removed. | |
| 85 void DeviceIDFetcher::CheckPrefsOnUIThread() { | 105 void DeviceIDFetcher::CheckPrefsOnUIThread() { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 87 | 107 |
| 88 Profile* profile = NULL; | 108 Profile* profile = NULL; |
| 89 RenderProcessHost* render_process_host = | 109 RenderProcessHost* render_process_host = |
| 90 RenderProcessHost::FromID(render_process_id_); | 110 RenderProcessHost::FromID(render_process_id_); |
| 91 if (render_process_host && render_process_host->GetBrowserContext()) { | 111 if (render_process_host && render_process_host->GetBrowserContext()) { |
| 92 profile = Profile::FromBrowserContext( | 112 profile = Profile::FromBrowserContext( |
| 93 render_process_host->GetBrowserContext()); | 113 render_process_host->GetBrowserContext()); |
| 94 } | 114 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 106 uint8_t salt_bytes[kSaltLength]; | 126 uint8_t salt_bytes[kSaltLength]; |
| 107 crypto::RandBytes(salt_bytes, arraysize(salt_bytes)); | 127 crypto::RandBytes(salt_bytes, arraysize(salt_bytes)); |
| 108 // Since it will be stored in a string pref, convert it to hex. | 128 // Since it will be stored in a string pref, convert it to hex. |
| 109 salt = base::HexEncode(salt_bytes, arraysize(salt_bytes)); | 129 salt = base::HexEncode(salt_bytes, arraysize(salt_bytes)); |
| 110 profile->GetPrefs()->SetString(prefs::kDRMSalt, salt); | 130 profile->GetPrefs()->SetString(prefs::kDRMSalt, salt); |
| 111 } | 131 } |
| 112 | 132 |
| 113 #if defined(OS_CHROMEOS) | 133 #if defined(OS_CHROMEOS) |
| 114 // Try the legacy path first for ChromeOS. We pass the new salt in as well | 134 // Try the legacy path first for ChromeOS. We pass the new salt in as well |
| 115 // in case the legacy id doesn't exist. | 135 // in case the legacy id doesn't exist. |
| 116 BrowserThread::PostBlockingPoolTask(FROM_HERE, | 136 BrowserThread::PostBlockingPoolTask( |
| 117 base::Bind(&DeviceIDFetcher::ComputeOnBlockingPool, this, | 137 FROM_HERE, |
| 138 base::Bind(&DeviceIDFetcher::LegacyComputeOnBlockingPool, | |
| 139 this, | |
| 118 profile->GetPath(), salt)); | 140 profile->GetPath(), salt)); |
| 119 #else | 141 #else |
| 120 BrowserThread::PostTask( | 142 // Get the machine ID and call ComputeOnUIThread with salt + machine_id. |
| 121 BrowserThread::IO, FROM_HERE, | 143 GetMachineIDAsync(base::Bind(&DeviceIDFetcher::ComputeOnUIThread, |
| 122 base::Bind(&DeviceIDFetcher::ComputeOnIOThread, this, salt)); | 144 this, salt)); |
| 123 #endif | 145 #endif |
| 124 } | 146 } |
| 125 | 147 |
| 148 void DeviceIDFetcher::ComputeOnUIThread(const std::string& salt, | |
| 149 const std::string& machine_id) { | |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 126 | 151 |
| 127 void DeviceIDFetcher::ComputeOnIOThread(const std::string& salt) { | 152 if (machine_id.empty()) { |
| 153 LOG(ERROR) << "Empty machine id"; | |
| 154 RunCallbackOnIOThread(std::string()); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 // Build the identifier as follows: | |
| 159 // SHA256(machine-id||service||SHA256(machine-id||service||salt)) | |
| 128 std::vector<uint8> salt_bytes; | 160 std::vector<uint8> salt_bytes; |
| 129 if (!base::HexStringToBytes(salt, &salt_bytes)) | 161 if (!base::HexStringToBytes(salt, &salt_bytes)) |
| 130 salt_bytes.clear(); | 162 salt_bytes.clear(); |
| 131 | 163 if (salt_bytes.size() != kSaltLength) { |
| 132 // Build the identifier as follows: | 164 LOG(ERROR) << "Unexpected salt bytes length: " << salt_bytes.size(); |
| 133 // SHA256(machine-id||service||SHA256(machine-id||service||salt)) | |
| 134 std::string machine_id = GetMachineID(); | |
| 135 if (machine_id.empty() || salt_bytes.size() != kSaltLength) { | |
| 136 NOTREACHED(); | |
| 137 RunCallbackOnIOThread(std::string()); | 165 RunCallbackOnIOThread(std::string()); |
| 138 return; | 166 return; |
| 139 } | 167 } |
| 140 | 168 |
| 141 char id_buf[256 / 8]; // 256-bits for SHA256 | 169 char id_buf[256 / 8]; // 256-bits for SHA256 |
| 142 std::string input = machine_id; | 170 std::string input = machine_id; |
| 143 input.append(kDRMIdentifierFile); | 171 input.append(kDRMIdentifierFile); |
| 144 input.append(salt_bytes.begin(), salt_bytes.end()); | 172 input.append(salt_bytes.begin(), salt_bytes.end()); |
| 145 crypto::SHA256HashString(input, &id_buf, sizeof(id_buf)); | 173 crypto::SHA256HashString(input, &id_buf, sizeof(id_buf)); |
| 146 std::string id = StringToLowerASCII( | 174 std::string id = StringToLowerASCII( |
| 147 base::HexEncode(reinterpret_cast<const void*>(id_buf), sizeof(id_buf))); | 175 base::HexEncode(reinterpret_cast<const void*>(id_buf), sizeof(id_buf))); |
| 148 input = machine_id; | 176 input = machine_id; |
| 149 input.append(kDRMIdentifierFile); | 177 input.append(kDRMIdentifierFile); |
| 150 input.append(id); | 178 input.append(id); |
| 151 crypto::SHA256HashString(input, &id_buf, sizeof(id_buf)); | 179 crypto::SHA256HashString(input, &id_buf, sizeof(id_buf)); |
| 152 id = StringToLowerASCII(base::HexEncode( | 180 id = StringToLowerASCII(base::HexEncode( |
| 153 reinterpret_cast<const void*>(id_buf), | 181 reinterpret_cast<const void*>(id_buf), |
| 154 sizeof(id_buf))); | 182 sizeof(id_buf))); |
| 155 | 183 |
| 156 RunCallbackOnIOThread(id); | 184 RunCallbackOnIOThread(id); |
| 157 } | 185 } |
| 158 | 186 |
| 159 // TODO(raymes): This is temporary code to migrate ChromeOS devices to the new | 187 // TODO(raymes): This is temporary code to migrate ChromeOS devices to the new |
| 160 // scheme for generating device IDs. Delete this once we are sure most ChromeOS | 188 // scheme for generating device IDs. Delete this once we are sure most ChromeOS |
| 161 // devices have been migrated. | 189 // devices have been migrated. |
| 162 void DeviceIDFetcher::ComputeOnBlockingPool(const base::FilePath& profile_path, | 190 void DeviceIDFetcher::LegacyComputeOnBlockingPool( |
| 163 const std::string& salt) { | 191 const base::FilePath& profile_path, |
| 192 const std::string& salt) { | |
| 164 std::string id; | 193 std::string id; |
| 165 // First check if the legacy device ID file exists on ChromeOS. If it does, we | 194 // First check if the legacy device ID file exists on ChromeOS. If it does, we |
| 166 // should just return that. | 195 // should just return that. |
| 167 base::FilePath id_path = GetLegacyDeviceIDPath(profile_path); | 196 base::FilePath id_path = GetLegacyDeviceIDPath(profile_path); |
| 168 if (base::PathExists(id_path)) { | 197 if (base::PathExists(id_path)) { |
| 169 if (base::ReadFileToString(id_path, &id) && !id.empty()) { | 198 if (base::ReadFileToString(id_path, &id) && !id.empty()) { |
| 170 RunCallbackOnIOThread(id); | 199 RunCallbackOnIOThread(id); |
| 171 return; | 200 return; |
| 172 } | 201 } |
| 173 } | 202 } |
| 174 // If we didn't find an ID, go back to the new code path to generate an ID. | 203 // If we didn't find an ID, get the machine ID and call the new code path to |
| 204 // generate an ID. | |
| 175 BrowserThread::PostTask( | 205 BrowserThread::PostTask( |
| 176 BrowserThread::IO, FROM_HERE, | 206 BrowserThread::UI, FROM_HERE, |
| 177 base::Bind(&DeviceIDFetcher::ComputeOnIOThread, this, salt)); | 207 base::Bind(&GetMachineIDAsync, |
| 208 base::Bind(&DeviceIDFetcher::ComputeOnUIThread, | |
| 209 this, salt))); | |
| 178 } | 210 } |
| 179 | 211 |
| 180 | |
| 181 void DeviceIDFetcher::RunCallbackOnIOThread(const std::string& id) { | 212 void DeviceIDFetcher::RunCallbackOnIOThread(const std::string& id) { |
| 182 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 213 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 183 BrowserThread::PostTask( | 214 BrowserThread::PostTask( |
| 184 BrowserThread::IO, FROM_HERE, | 215 BrowserThread::IO, FROM_HERE, |
| 185 base::Bind(&DeviceIDFetcher::RunCallbackOnIOThread, this, id)); | 216 base::Bind(&DeviceIDFetcher::RunCallbackOnIOThread, this, id)); |
| 186 return; | 217 return; |
| 187 } | 218 } |
| 188 in_progress_ = false; | 219 in_progress_ = false; |
| 189 callback_.Run(id); | 220 callback_.Run(id); |
| 190 } | 221 } |
| 191 | 222 |
| 192 std::string DeviceIDFetcher::GetMachineID() { | |
| 193 #if defined(OS_WIN) && defined(ENABLE_RLZ) | |
| 194 std::string result; | |
| 195 rlz_lib::GetMachineId(&result); | |
| 196 return result; | |
| 197 #elif defined(OS_CHROMEOS) | |
| 198 chromeos::CryptohomeLibrary* c_home = chromeos::CryptohomeLibrary::Get(); | |
| 199 return c_home->GetSystemSalt(); | |
| 200 #else | |
| 201 // Not implemented for other platforms. | |
| 202 NOTREACHED(); | |
| 203 return ""; | |
| 204 #endif | |
| 205 } | |
| 206 | |
| 207 } // namespace chrome | 223 } // namespace chrome |
| OLD | NEW |