summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/music_manager_private/device_id.cc
blob: e405030172dd8bfdec465bf8a9d725660dc62433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/music_manager_private/device_id.h"

#include "base/bind.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "content/public/browser/browser_thread.h"
#include "crypto/hmac.h"

namespace {

// Compute HMAC-SHA256(|key|, |text|) as a string.
bool ComputeHmacSha256(const std::string& key,
                       const std::string& text,
                       std::string* signature_return) {
  crypto::HMAC hmac(crypto::HMAC::SHA256);
  const size_t digest_length = hmac.DigestLength();
  std::vector<uint8> digest(digest_length);
  bool result = hmac.Init(key) &&
      hmac.Sign(text, &digest[0], digest.size());
  if (result) {
    *signature_return = StringToLowerASCII(base::HexEncode(digest.data(),
                                                           digest.size()));
  }
  return result;
}

void GetMachineIdCallback(const std::string& extension_id,
                          const extensions::api::DeviceId::IdCallback& callback,
                          const std::string& machine_id) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  if (machine_id.empty()) {
    callback.Run("");
  }

  std::string device_id;
  if (!ComputeHmacSha256(machine_id, extension_id, &device_id)) {
    DLOG(ERROR) << "Error while computing HMAC-SHA256 of device id.";
    callback.Run("");
  }
  callback.Run(device_id);
}

}

namespace extensions {
namespace api {

/* static */
void DeviceId::GetDeviceId(const std::string& extension_id,
                           const IdCallback& callback) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  CHECK(!extension_id.empty());

  // Forward call to platform specific implementation, then compute the HMAC
  // in the callback.
  GetMachineId(base::Bind(&GetMachineIdCallback, extension_id, callback));
}

}  // namespace api
}  // namespace extensions