summaryrefslogtreecommitdiffstats
path: root/chromeos/cryptohome/system_salt_getter.cc
blob: f299f0f4744f37de6342ce0fbcc179c4c2e00ef5 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2012 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 "chromeos/cryptohome/system_salt_getter.h"

#include "base/bind.h"
#include "base/location.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"

namespace chromeos {
namespace {

SystemSaltGetter* g_system_salt_getter = NULL;

}  // namespace

SystemSaltGetter::SystemSaltGetter() : weak_ptr_factory_(this) {
}

SystemSaltGetter::~SystemSaltGetter() {
}

void SystemSaltGetter::GetSystemSalt(
    const GetSystemSaltCallback& callback) {
  DBusThreadManager::Get()->GetCryptohomeClient()->WaitForServiceToBeAvailable(
      base::Bind(&SystemSaltGetter::GetSystemSaltInternal,
                 weak_ptr_factory_.GetWeakPtr(),
                 callback));
}

std::string SystemSaltGetter::GetSystemSaltSync() {
  LoadSystemSalt();  // no-op if it's already loaded.
  return system_salt_;
}

std::string SystemSaltGetter::GetCachedSystemSalt() {
  return system_salt_;
}

void SystemSaltGetter::GetSystemSaltInternal(
    const GetSystemSaltCallback& callback,
    bool service_is_available) {
  LOG_IF(ERROR, !service_is_available) << "WaitForServiceToBeAvailable failed.";
  // TODO(hashimoto): Stop using GetSystemSaltSync(). crbug.com/141009
  callback.Run(GetSystemSaltSync());
}

void SystemSaltGetter::LoadSystemSalt() {
  if (!system_salt_.empty())
    return;
  std::vector<uint8> salt;
  DBusThreadManager::Get()->GetCryptohomeClient()->GetSystemSalt(&salt);
  if (salt.empty() || salt.size() % 2 != 0U) {
    LOG(WARNING) << "System salt not available";
    return;
  }
  system_salt_ = ConvertRawSaltToHexString(salt);
}

// static
void SystemSaltGetter::Initialize() {
  CHECK(!g_system_salt_getter);
  g_system_salt_getter = new SystemSaltGetter();
}

// static
bool SystemSaltGetter::IsInitialized() {
  return g_system_salt_getter;
}

// static
void SystemSaltGetter::Shutdown() {
  CHECK(g_system_salt_getter);
  delete g_system_salt_getter;
  g_system_salt_getter = NULL;
}

// static
SystemSaltGetter* SystemSaltGetter::Get() {
  CHECK(g_system_salt_getter)
      << "SystemSaltGetter::Get() called before Initialize()";
  return g_system_salt_getter;
}

// static
std::string SystemSaltGetter::ConvertRawSaltToHexString(
    const std::vector<uint8>& salt) {
  return StringToLowerASCII(base::HexEncode(
      reinterpret_cast<const void*>(salt.data()), salt.size()));
}

}  // namespace chromeos