OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/extensions/api/music_manager_private/device_id.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" |
| 11 #if defined(OS_CHROMEOS) |
| 12 #include "chromeos/cryptohome/cryptohome_library.h" |
| 13 #endif |
| 14 #include "crypto/hmac.h" |
| 15 #if defined(ENABLE_RLZ) |
| 16 #include "rlz/lib/machine_id.h" |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Return a unique identifier for the machine/device. Currently, this is only |
| 22 // supported on Windows (with RLZ enabled) and ChromeOS. |
| 23 std::string GetMachineID() { |
| 24 #if defined(OS_WIN) && defined(ENABLE_RLZ) |
| 25 std::string result; |
| 26 rlz_lib::GetMachineId(&result); |
| 27 return result; |
| 28 #elif defined(OS_CHROMEOS) |
| 29 chromeos::CryptohomeLibrary* c_home = chromeos::CryptohomeLibrary::Get(); |
| 30 return c_home->GetSystemSalt(); |
| 31 #else |
| 32 // Not implemented for other platforms. |
| 33 return ""; |
| 34 #endif |
| 35 } |
| 36 |
| 37 // Compute HMAC-SHA256(|key|, |text|) as a string. |
| 38 bool ComputeHmacSha256(const std::string& key, |
| 39 const std::string& text, |
| 40 std::string* signature_return) { |
| 41 crypto::HMAC hmac(crypto::HMAC::SHA256); |
| 42 const size_t digest_length = hmac.DigestLength(); |
| 43 std::vector<uint8> digest(digest_length); |
| 44 bool result = hmac.Init(key) && |
| 45 hmac.Sign(text, &digest[0], digest.size()) && |
| 46 base::Base64Encode(std::string(reinterpret_cast<char*>(&digest[0]), |
| 47 digest.size()), |
| 48 signature_return); |
| 49 return result; |
| 50 } |
| 51 |
| 52 } |
| 53 |
| 54 namespace device_id { |
| 55 |
| 56 std::string GetDeviceID(const std::string& salt) { |
| 57 CHECK(!salt.empty()); |
| 58 std::string machine_id = GetMachineID(); |
| 59 if (machine_id.empty()) { |
| 60 DLOG(WARNING) << "API is not supported on current platform."; |
| 61 return ""; |
| 62 } |
| 63 |
| 64 std::string device_id; |
| 65 if (!ComputeHmacSha256(machine_id, salt, &device_id)) { |
| 66 DLOG(ERROR) << "Error while computing HMAC-SHA256 of device id."; |
| 67 return ""; |
| 68 } |
| 69 return device_id; |
| 70 } |
| 71 |
| 72 } // namespace device_id |
OLD | NEW |