| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/chromeos/cros/cryptohome_library.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "base/string_util.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "chromeos/dbus/cryptohome_client.h" | |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 15 | |
| 16 namespace chromeos { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kStubSystemSalt[] = "stub_system_salt"; | |
| 21 | |
| 22 // Does nothing. Used as a Cryptohome::VoidMethodCallback. | |
| 23 void DoNothing(DBusMethodCallStatus call_status) {} | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 // This class handles the interaction with the ChromeOS cryptohome library APIs. | |
| 28 class CryptohomeLibraryImpl : public CryptohomeLibrary { | |
| 29 public: | |
| 30 CryptohomeLibraryImpl() : weak_ptr_factory_(this) { | |
| 31 } | |
| 32 | |
| 33 virtual ~CryptohomeLibraryImpl() { | |
| 34 } | |
| 35 | |
| 36 virtual bool TpmIsEnabled() OVERRIDE { | |
| 37 bool result = false; | |
| 38 DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsEnabledAndBlock( | |
| 39 &result); | |
| 40 return result; | |
| 41 } | |
| 42 | |
| 43 virtual bool TpmIsOwned() OVERRIDE { | |
| 44 bool result = false; | |
| 45 DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsOwnedAndBlock( | |
| 46 &result); | |
| 47 return result; | |
| 48 } | |
| 49 | |
| 50 virtual bool TpmIsBeingOwned() OVERRIDE { | |
| 51 bool result = false; | |
| 52 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 53 CallTpmIsBeingOwnedAndBlock(&result); | |
| 54 return result; | |
| 55 } | |
| 56 | |
| 57 virtual void TpmCanAttemptOwnership() OVERRIDE { | |
| 58 DBusThreadManager::Get()->GetCryptohomeClient()->TpmCanAttemptOwnership( | |
| 59 base::Bind(&DoNothing)); | |
| 60 } | |
| 61 | |
| 62 virtual void TpmClearStoredPassword() OVERRIDE { | |
| 63 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 64 CallTpmClearStoredPasswordAndBlock(); | |
| 65 } | |
| 66 | |
| 67 virtual bool InstallAttributesGet( | |
| 68 const std::string& name, std::string* value) OVERRIDE { | |
| 69 std::vector<uint8> buf; | |
| 70 bool success = false; | |
| 71 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 72 InstallAttributesGet(name, &buf, &success); | |
| 73 if (success) { | |
| 74 // Cryptohome returns 'buf' with a terminating '\0' character. | |
| 75 DCHECK(!buf.empty()); | |
| 76 DCHECK_EQ(buf.back(), 0); | |
| 77 value->assign(reinterpret_cast<char*>(buf.data()), buf.size() - 1); | |
| 78 } | |
| 79 return success; | |
| 80 } | |
| 81 | |
| 82 virtual bool InstallAttributesSet( | |
| 83 const std::string& name, const std::string& value) OVERRIDE { | |
| 84 std::vector<uint8> buf(value.c_str(), value.c_str() + value.size() + 1); | |
| 85 bool success = false; | |
| 86 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 87 InstallAttributesSet(name, buf, &success); | |
| 88 return success; | |
| 89 } | |
| 90 | |
| 91 virtual bool InstallAttributesFinalize() OVERRIDE { | |
| 92 bool success = false; | |
| 93 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 94 InstallAttributesFinalize(&success); | |
| 95 return success; | |
| 96 } | |
| 97 | |
| 98 virtual bool InstallAttributesIsInvalid() OVERRIDE { | |
| 99 bool result = false; | |
| 100 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 101 InstallAttributesIsInvalid(&result); | |
| 102 return result; | |
| 103 } | |
| 104 | |
| 105 virtual bool InstallAttributesIsFirstInstall() OVERRIDE { | |
| 106 bool result = false; | |
| 107 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 108 InstallAttributesIsFirstInstall(&result); | |
| 109 return result; | |
| 110 } | |
| 111 | |
| 112 virtual std::string GetSystemSalt() OVERRIDE { | |
| 113 LoadSystemSalt(); // no-op if it's already loaded. | |
| 114 return StringToLowerASCII(base::HexEncode( | |
| 115 reinterpret_cast<const void*>(system_salt_.data()), | |
| 116 system_salt_.size())); | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 void LoadSystemSalt() { | |
| 121 if (!system_salt_.empty()) | |
| 122 return; | |
| 123 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
| 124 GetSystemSalt(&system_salt_); | |
| 125 CHECK(!system_salt_.empty()); | |
| 126 CHECK_EQ(system_salt_.size() % 2, 0U); | |
| 127 } | |
| 128 | |
| 129 base::WeakPtrFactory<CryptohomeLibraryImpl> weak_ptr_factory_; | |
| 130 std::vector<uint8> system_salt_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(CryptohomeLibraryImpl); | |
| 133 }; | |
| 134 | |
| 135 class CryptohomeLibraryStubImpl : public CryptohomeLibrary { | |
| 136 public: | |
| 137 CryptohomeLibraryStubImpl() | |
| 138 : locked_(false) {} | |
| 139 virtual ~CryptohomeLibraryStubImpl() {} | |
| 140 | |
| 141 virtual bool TpmIsEnabled() OVERRIDE { | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 virtual bool TpmIsOwned() OVERRIDE { | |
| 146 return true; | |
| 147 } | |
| 148 | |
| 149 virtual bool TpmIsBeingOwned() OVERRIDE { | |
| 150 return true; | |
| 151 } | |
| 152 | |
| 153 virtual void TpmCanAttemptOwnership() OVERRIDE {} | |
| 154 | |
| 155 virtual void TpmClearStoredPassword() OVERRIDE {} | |
| 156 | |
| 157 virtual bool InstallAttributesGet( | |
| 158 const std::string& name, std::string* value) OVERRIDE { | |
| 159 if (install_attrs_.find(name) != install_attrs_.end()) { | |
| 160 *value = install_attrs_[name]; | |
| 161 return true; | |
| 162 } | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 virtual bool InstallAttributesSet( | |
| 167 const std::string& name, const std::string& value) OVERRIDE { | |
| 168 install_attrs_[name] = value; | |
| 169 return true; | |
| 170 } | |
| 171 | |
| 172 virtual bool InstallAttributesFinalize() OVERRIDE { | |
| 173 locked_ = true; | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 virtual bool InstallAttributesIsInvalid() OVERRIDE { | |
| 178 return false; | |
| 179 } | |
| 180 | |
| 181 virtual bool InstallAttributesIsFirstInstall() OVERRIDE { | |
| 182 return !locked_; | |
| 183 } | |
| 184 | |
| 185 virtual std::string GetSystemSalt() OVERRIDE { | |
| 186 return kStubSystemSalt; | |
| 187 } | |
| 188 | |
| 189 private: | |
| 190 std::map<std::string, std::string> install_attrs_; | |
| 191 bool locked_; | |
| 192 DISALLOW_COPY_AND_ASSIGN(CryptohomeLibraryStubImpl); | |
| 193 }; | |
| 194 | |
| 195 CryptohomeLibrary::CryptohomeLibrary() {} | |
| 196 CryptohomeLibrary::~CryptohomeLibrary() {} | |
| 197 | |
| 198 // static | |
| 199 CryptohomeLibrary* CryptohomeLibrary::GetImpl(bool stub) { | |
| 200 CryptohomeLibrary* impl; | |
| 201 if (stub) | |
| 202 impl = new CryptohomeLibraryStubImpl(); | |
| 203 else | |
| 204 impl = new CryptohomeLibraryImpl(); | |
| 205 return impl; | |
| 206 } | |
| 207 | |
| 208 } // namespace chromeos | |
| OLD | NEW |