| 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/dbus/cryptohome_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/chromeos/chromeos_version.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/synchronization/waitable_event.h" | |
| 11 #include "dbus/bus.h" | |
| 12 #include "dbus/message.h" | |
| 13 #include "dbus/object_path.h" | |
| 14 #include "dbus/object_proxy.h" | |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // A convenient macro to initialize a dbus::MethodCall while checking the dbus | |
| 22 // method name matches to the C++ method name. | |
| 23 #define INITIALIZE_METHOD_CALL(method_call_name, method_name) \ | |
| 24 DCHECK_EQ(std::string(method_name), __FUNCTION__); \ | |
| 25 dbus::MethodCall method_call_name(cryptohome::kCryptohomeInterface, \ | |
| 26 method_name); | |
| 27 | |
| 28 // Calls PopBool with |reader|. This method is used to create callbacks. | |
| 29 bool PopBool(bool* output, dbus::MessageReader* reader) { | |
| 30 return reader->PopBool(output); | |
| 31 } | |
| 32 | |
| 33 // Calls PopString with |reader|. This method is used to create callbacks. | |
| 34 bool PopString(std::string* output, dbus::MessageReader* reader) { | |
| 35 return reader->PopString(output); | |
| 36 } | |
| 37 | |
| 38 // Get array of bytes as vector. | |
| 39 // This method is used to create callbacks. | |
| 40 bool PopArrayOfBytesAsVector(std::vector<uint8>* output, | |
| 41 dbus::MessageReader* reader) { | |
| 42 uint8* bytes = NULL; | |
| 43 size_t length = 0; | |
| 44 if (!reader->PopArrayOfBytes(&bytes, &length)) | |
| 45 return false; | |
| 46 output->assign(bytes, bytes + length); | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 // Does nothing. | |
| 51 // This method is used to create callbacks to call functions without result. | |
| 52 bool PopNothing(dbus::MessageReader*) { | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 // Runs |popper1| and |popper2|. This method is used to create callbacks to | |
| 57 // call functions with multiple results. | |
| 58 bool PopTwoValues(base::Callback<bool(dbus::MessageReader* reader)> popper1, | |
| 59 base::Callback<bool(dbus::MessageReader* reader)> popper2, | |
| 60 dbus::MessageReader* reader) { | |
| 61 return popper1.Run(reader) && popper2.Run(reader); | |
| 62 } | |
| 63 | |
| 64 // The CryptohomeClient implementation. | |
| 65 class CryptohomeClientImpl : public CryptohomeClient { | |
| 66 public: | |
| 67 explicit CryptohomeClientImpl(dbus::Bus* bus) | |
| 68 : bus_(bus), | |
| 69 proxy_(bus->GetObjectProxy( | |
| 70 cryptohome::kCryptohomeServiceName, | |
| 71 dbus::ObjectPath(cryptohome::kCryptohomeServicePath))), | |
| 72 weak_ptr_factory_(this), | |
| 73 on_blocking_method_call_(false /* manual_reset */, | |
| 74 false /* initially_signaled */) { | |
| 75 proxy_->ConnectToSignal( | |
| 76 cryptohome::kCryptohomeInterface, | |
| 77 cryptohome::kSignalAsyncCallStatus, | |
| 78 base::Bind(&CryptohomeClientImpl::OnAsyncCallStatus, | |
| 79 weak_ptr_factory_.GetWeakPtr()), | |
| 80 base::Bind(&CryptohomeClientImpl::OnSignalConnected, | |
| 81 weak_ptr_factory_.GetWeakPtr())); | |
| 82 } | |
| 83 | |
| 84 // CryptohomeClient override. | |
| 85 virtual void SetAsyncCallStatusHandler(AsyncCallStatusHandler handler) | |
| 86 OVERRIDE { | |
| 87 async_call_status_handler_ = handler; | |
| 88 } | |
| 89 | |
| 90 // CryptohomeClient override. | |
| 91 virtual void ResetAsyncCallStatusHandler() OVERRIDE { | |
| 92 async_call_status_handler_.Reset(); | |
| 93 } | |
| 94 | |
| 95 // CryptohomeClient override. | |
| 96 virtual bool IsMounted(bool* is_mounted) OVERRIDE { | |
| 97 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeIsMounted); | |
| 98 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, is_mounted)); | |
| 99 } | |
| 100 | |
| 101 // CryptohomeClient override. | |
| 102 virtual void AsyncCheckKey(const std::string& username, | |
| 103 const std::string& key, | |
| 104 AsyncMethodCallback callback) OVERRIDE { | |
| 105 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncCheckKey); | |
| 106 dbus::MessageWriter writer(&method_call); | |
| 107 writer.AppendString(username); | |
| 108 writer.AppendString(key); | |
| 109 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 110 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
| 111 weak_ptr_factory_.GetWeakPtr(), | |
| 112 callback)); | |
| 113 } | |
| 114 | |
| 115 // CryptohomeClient override. | |
| 116 virtual void AsyncMigrateKey(const std::string& username, | |
| 117 const std::string& from_key, | |
| 118 const std::string& to_key, | |
| 119 AsyncMethodCallback callback) OVERRIDE { | |
| 120 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncMigrateKey); | |
| 121 dbus::MessageWriter writer(&method_call); | |
| 122 writer.AppendString(username); | |
| 123 writer.AppendString(from_key); | |
| 124 writer.AppendString(to_key); | |
| 125 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 126 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
| 127 weak_ptr_factory_.GetWeakPtr(), | |
| 128 callback)); | |
| 129 } | |
| 130 | |
| 131 // CryptohomeClient override. | |
| 132 virtual void AsyncRemove(const std::string& username, | |
| 133 AsyncMethodCallback callback) OVERRIDE { | |
| 134 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncRemove); | |
| 135 dbus::MessageWriter writer(&method_call); | |
| 136 writer.AppendString(username); | |
| 137 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 138 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
| 139 weak_ptr_factory_.GetWeakPtr(), | |
| 140 callback)); | |
| 141 } | |
| 142 | |
| 143 // CryptohomeClient override. | |
| 144 virtual bool GetSystemSalt(std::vector<uint8>* salt) OVERRIDE { | |
| 145 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeGetSystemSalt); | |
| 146 return CallMethodAndBlock(&method_call, | |
| 147 base::Bind(&PopArrayOfBytesAsVector, salt)); | |
| 148 } | |
| 149 | |
| 150 // CryptohomeClient override. | |
| 151 virtual void AsyncMount(const std::string& username, | |
| 152 const std::string& key, | |
| 153 const bool create_if_missing, | |
| 154 AsyncMethodCallback callback) OVERRIDE { | |
| 155 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncMount); | |
| 156 dbus::MessageWriter writer(&method_call); | |
| 157 writer.AppendString(username); | |
| 158 writer.AppendString(key); | |
| 159 writer.AppendBool(create_if_missing); | |
| 160 writer.AppendBool(false); // deprecated_replace_tracked_subdirectories | |
| 161 // deprecated_tracked_subdirectories | |
| 162 writer.AppendArrayOfStrings(std::vector<std::string>()); | |
| 163 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 164 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
| 165 weak_ptr_factory_.GetWeakPtr(), | |
| 166 callback)); | |
| 167 } | |
| 168 | |
| 169 // CryptohomeClient override. | |
| 170 virtual void AsyncMountGuest(AsyncMethodCallback callback) OVERRIDE { | |
| 171 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncMountGuest); | |
| 172 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 173 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
| 174 weak_ptr_factory_.GetWeakPtr(), | |
| 175 callback)); | |
| 176 } | |
| 177 | |
| 178 // CryptohomeClient override. | |
| 179 virtual bool TpmIsReady(bool* ready) OVERRIDE { | |
| 180 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsReady); | |
| 181 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, ready)); | |
| 182 } | |
| 183 | |
| 184 // CryptohomeClient override. | |
| 185 virtual bool TpmIsEnabled(bool* enabled) OVERRIDE { | |
| 186 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsEnabled); | |
| 187 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, enabled)); | |
| 188 } | |
| 189 | |
| 190 // CryptohomeClient override. | |
| 191 virtual bool TpmGetPassword(std::string* password) OVERRIDE { | |
| 192 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmGetPassword); | |
| 193 return CallMethodAndBlock(&method_call, base::Bind(&PopString, password)); | |
| 194 } | |
| 195 | |
| 196 // CryptohomeClient override. | |
| 197 virtual bool TpmIsOwned(bool* owned) OVERRIDE { | |
| 198 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsOwned); | |
| 199 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, owned)); | |
| 200 } | |
| 201 | |
| 202 // CryptohomeClient override. | |
| 203 virtual bool TpmIsBeingOwned(bool* owning) OVERRIDE { | |
| 204 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsBeingOwned); | |
| 205 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, owning)); | |
| 206 } | |
| 207 | |
| 208 // CryptohomeClient override. | |
| 209 virtual bool TpmCanAttemptOwnership() OVERRIDE { | |
| 210 INITIALIZE_METHOD_CALL(method_call, | |
| 211 cryptohome::kCryptohomeTpmCanAttemptOwnership); | |
| 212 return CallMethodAndBlock(&method_call, base::Bind(&PopNothing)); | |
| 213 } | |
| 214 | |
| 215 // CryptohomeClient override. | |
| 216 virtual bool TpmClearStoredPassword() OVERRIDE { | |
| 217 INITIALIZE_METHOD_CALL(method_call, | |
| 218 cryptohome::kCryptohomeTpmClearStoredPassword); | |
| 219 return CallMethodAndBlock(&method_call, base::Bind(&PopNothing)); | |
| 220 } | |
| 221 | |
| 222 // CryptohomeClient override. | |
| 223 virtual void Pkcs11IsTpmTokenReady(Pkcs11IsTpmTokenReadyCallback callback) | |
| 224 OVERRIDE { | |
| 225 INITIALIZE_METHOD_CALL(method_call, | |
| 226 cryptohome::kCryptohomePkcs11IsTpmTokenReady); | |
| 227 proxy_->CallMethod( | |
| 228 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 229 base::Bind( | |
| 230 &CryptohomeClientImpl::OnPkcs11IsTpmTokenReady, | |
| 231 weak_ptr_factory_.GetWeakPtr(), | |
| 232 callback)); | |
| 233 } | |
| 234 | |
| 235 // CryptohomeClient override. | |
| 236 virtual void Pkcs11GetTpmTokenInfo(Pkcs11GetTpmTokenInfoCallback callback) | |
| 237 OVERRIDE { | |
| 238 INITIALIZE_METHOD_CALL(method_call, | |
| 239 cryptohome::kCryptohomePkcs11GetTpmTokenInfo); | |
| 240 proxy_->CallMethod( | |
| 241 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 242 base::Bind( | |
| 243 &CryptohomeClientImpl::OnPkcs11GetTpmTokenInfo, | |
| 244 weak_ptr_factory_.GetWeakPtr(), | |
| 245 callback)); | |
| 246 } | |
| 247 | |
| 248 // CryptohomeClient override. | |
| 249 virtual bool InstallAttributesGet(const std::string& name, | |
| 250 std::vector<uint8>* value, | |
| 251 bool* successful) OVERRIDE { | |
| 252 INITIALIZE_METHOD_CALL(method_call, | |
| 253 cryptohome::kCryptohomeInstallAttributesGet); | |
| 254 dbus::MessageWriter writer(&method_call); | |
| 255 writer.AppendString(name); | |
| 256 if (!CallMethodAndBlock( | |
| 257 &method_call, | |
| 258 base::Bind(&PopTwoValues, | |
| 259 base::Bind(&PopArrayOfBytesAsVector, value), | |
| 260 base::Bind(&PopBool, successful)))) { | |
| 261 value->clear(); | |
| 262 *successful = false; | |
| 263 return false; | |
| 264 } | |
| 265 return true; | |
| 266 } | |
| 267 | |
| 268 // CryptohomeClient override. | |
| 269 virtual bool InstallAttributesSet(const std::string& name, | |
| 270 const std::vector<uint8>& value, | |
| 271 bool* successful) OVERRIDE { | |
| 272 INITIALIZE_METHOD_CALL(method_call, | |
| 273 cryptohome::kCryptohomeInstallAttributesSet); | |
| 274 dbus::MessageWriter writer(&method_call); | |
| 275 writer.AppendString(name); | |
| 276 writer.AppendArrayOfBytes(value.data(), value.size()); | |
| 277 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, successful)); | |
| 278 } | |
| 279 | |
| 280 // CryptohomeClient override. | |
| 281 virtual bool InstallAttributesFinalize(bool* successful) OVERRIDE { | |
| 282 INITIALIZE_METHOD_CALL(method_call, | |
| 283 cryptohome::kCryptohomeInstallAttributesFinalize); | |
| 284 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, successful)); | |
| 285 } | |
| 286 | |
| 287 // CryptohomeClient override. | |
| 288 virtual bool InstallAttributesIsReady(bool* is_ready) OVERRIDE { | |
| 289 INITIALIZE_METHOD_CALL(method_call, | |
| 290 cryptohome::kCryptohomeInstallAttributesIsReady); | |
| 291 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, is_ready)); | |
| 292 } | |
| 293 | |
| 294 // CryptohomeClient override. | |
| 295 virtual bool InstallAttributesIsInvalid(bool* is_invalid) OVERRIDE { | |
| 296 INITIALIZE_METHOD_CALL(method_call, | |
| 297 cryptohome::kCryptohomeInstallAttributesIsInvalid); | |
| 298 return CallMethodAndBlock(&method_call, base::Bind(&PopBool, is_invalid)); | |
| 299 } | |
| 300 | |
| 301 // CryptohomeClient override. | |
| 302 virtual bool InstallAttributesIsFirstInstall(bool* is_first_install) OVERRIDE | |
| 303 { | |
| 304 INITIALIZE_METHOD_CALL( | |
| 305 method_call, cryptohome::kCryptohomeInstallAttributesIsFirstInstall); | |
| 306 return CallMethodAndBlock(&method_call, | |
| 307 base::Bind(&PopBool, is_first_install)); | |
| 308 } | |
| 309 | |
| 310 private: | |
| 311 // A utility class to ensure the WaitableEvent is signaled. | |
| 312 class WaitableEventSignaler { | |
| 313 public: | |
| 314 explicit WaitableEventSignaler(base::WaitableEvent* event) : event_(event) { | |
| 315 } | |
| 316 | |
| 317 ~WaitableEventSignaler() { | |
| 318 event_->Signal(); | |
| 319 } | |
| 320 | |
| 321 private: | |
| 322 base::WaitableEvent* event_; | |
| 323 }; | |
| 324 | |
| 325 // Handles the result of AsyncXXX methods. | |
| 326 void OnAsyncMethodCall(AsyncMethodCallback callback, | |
| 327 dbus::Response* response) { | |
| 328 if (!response) | |
| 329 return; | |
| 330 dbus::MessageReader reader(response); | |
| 331 int async_id = 0; | |
| 332 if (!reader.PopInt32(&async_id)) { | |
| 333 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
| 334 return; | |
| 335 } | |
| 336 callback.Run(async_id); | |
| 337 } | |
| 338 | |
| 339 // Calls the method and block until it returns. | |
| 340 // |callback| is called with the result on the DBus thread. | |
| 341 bool CallMethodAndBlock(dbus::MethodCall* method_call, | |
| 342 base::Callback<bool(dbus::MessageReader*)> callback) { | |
| 343 WaitableEventSignaler* signaler = | |
| 344 new WaitableEventSignaler(&on_blocking_method_call_); | |
| 345 bool success = false; | |
| 346 bus_->PostTaskToDBusThread( | |
| 347 FROM_HERE, | |
| 348 base::Bind(&CryptohomeClientImpl::CallMethodAndBlockInternal, | |
| 349 base::Unretained(this), | |
| 350 base::Owned(signaler), | |
| 351 &success, | |
| 352 method_call, | |
| 353 callback)); | |
| 354 on_blocking_method_call_.Wait(); | |
| 355 return success; | |
| 356 } | |
| 357 | |
| 358 // This method is a part of CallMethodAndBlock implementation. | |
| 359 void CallMethodAndBlockInternal( | |
| 360 WaitableEventSignaler* signaler, | |
| 361 bool* success, | |
| 362 dbus::MethodCall* method_call, | |
| 363 base::Callback<bool(dbus::MessageReader*)> callback) { | |
| 364 dbus::Response* response = proxy_->CallMethodAndBlock( | |
| 365 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); | |
| 366 if (response) { | |
| 367 dbus::MessageReader reader(response); | |
| 368 *success = callback.Run(&reader); | |
| 369 } | |
| 370 } | |
| 371 | |
| 372 // Handles responses for Pkcs11IsTpmTokenReady. | |
| 373 void OnPkcs11IsTpmTokenReady(Pkcs11IsTpmTokenReadyCallback callback, | |
| 374 dbus::Response* response) { | |
| 375 if (!response) { | |
| 376 callback.Run(FAILURE, false); | |
| 377 return; | |
| 378 } | |
| 379 dbus::MessageReader reader(response); | |
| 380 bool ready = false; | |
| 381 if (!reader.PopBool(&ready)) { | |
| 382 callback.Run(FAILURE, false); | |
| 383 return; | |
| 384 } | |
| 385 callback.Run(SUCCESS, ready); | |
| 386 } | |
| 387 | |
| 388 // Handles responses for Pkcs11GetTpmtTokenInfo. | |
| 389 void OnPkcs11GetTpmTokenInfo(Pkcs11GetTpmTokenInfoCallback callback, | |
| 390 dbus::Response* response) { | |
| 391 if (!response) { | |
| 392 callback.Run(FAILURE, std::string(), std::string()); | |
| 393 return; | |
| 394 } | |
| 395 dbus::MessageReader reader(response); | |
| 396 std::string label; | |
| 397 std::string user_pin; | |
| 398 if (!reader.PopString(&label) || !reader.PopString(&user_pin)) { | |
| 399 callback.Run(FAILURE, std::string(), std::string()); | |
| 400 return; | |
| 401 } | |
| 402 callback.Run(SUCCESS, label, user_pin); | |
| 403 } | |
| 404 | |
| 405 // Handles AsyncCallStatus signal. | |
| 406 void OnAsyncCallStatus(dbus::Signal* signal) { | |
| 407 dbus::MessageReader reader(signal); | |
| 408 int async_id = 0; | |
| 409 bool return_status = false; | |
| 410 int return_code = 0; | |
| 411 if (!reader.PopInt32(&async_id) || | |
| 412 !reader.PopBool(&return_status) || | |
| 413 !reader.PopInt32(&return_code)) { | |
| 414 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 415 return; | |
| 416 } | |
| 417 if (!async_call_status_handler_.is_null()) | |
| 418 async_call_status_handler_.Run(async_id, return_status, return_code); | |
| 419 } | |
| 420 | |
| 421 // Handles the result of signal connection setup. | |
| 422 void OnSignalConnected(const std::string& interface, | |
| 423 const std::string& signal, | |
| 424 bool successed) { | |
| 425 LOG_IF(ERROR, !successed) << "Connect to " << interface << " " << | |
| 426 signal << " failed."; | |
| 427 } | |
| 428 | |
| 429 dbus::Bus* bus_; | |
| 430 dbus::ObjectProxy* proxy_; | |
| 431 base::WeakPtrFactory<CryptohomeClientImpl> weak_ptr_factory_; | |
| 432 base::WaitableEvent on_blocking_method_call_; | |
| 433 AsyncCallStatusHandler async_call_status_handler_; | |
| 434 | |
| 435 DISALLOW_COPY_AND_ASSIGN(CryptohomeClientImpl); | |
| 436 }; | |
| 437 | |
| 438 // A stub implementaion of CryptohomeClient. | |
| 439 class CryptohomeClientStubImpl : public CryptohomeClient { | |
| 440 public: | |
| 441 CryptohomeClientStubImpl() | |
| 442 : weak_ptr_factory_(this), | |
| 443 async_call_id_(1), | |
| 444 tpm_is_ready_counter_(0), | |
| 445 locked_(false) { | |
| 446 } | |
| 447 | |
| 448 virtual ~CryptohomeClientStubImpl() {} | |
| 449 | |
| 450 // CryptohomeClient override. | |
| 451 virtual void SetAsyncCallStatusHandler(AsyncCallStatusHandler handler) | |
| 452 OVERRIDE { | |
| 453 async_call_status_handler_ = handler; | |
| 454 } | |
| 455 | |
| 456 // CryptohomeClient override. | |
| 457 virtual void ResetAsyncCallStatusHandler() OVERRIDE { | |
| 458 async_call_status_handler_.Reset(); | |
| 459 } | |
| 460 | |
| 461 // CryptohomeClient override. | |
| 462 virtual bool IsMounted(bool* is_mounted) OVERRIDE { | |
| 463 *is_mounted = true; | |
| 464 return true; | |
| 465 } | |
| 466 | |
| 467 // CryptohomeClient override. | |
| 468 virtual void AsyncCheckKey(const std::string& username, | |
| 469 const std::string& key, | |
| 470 AsyncMethodCallback callback) OVERRIDE { | |
| 471 ReturnAsyncMethodResult(callback); | |
| 472 } | |
| 473 | |
| 474 // CryptohomeClient override. | |
| 475 virtual void AsyncMigrateKey(const std::string& username, | |
| 476 const std::string& from_key, | |
| 477 const std::string& to_key, | |
| 478 AsyncMethodCallback callback) OVERRIDE { | |
| 479 ReturnAsyncMethodResult(callback); | |
| 480 } | |
| 481 | |
| 482 // CryptohomeClient override. | |
| 483 virtual void AsyncRemove(const std::string& username, | |
| 484 AsyncMethodCallback callback) OVERRIDE { | |
| 485 ReturnAsyncMethodResult(callback); | |
| 486 } | |
| 487 | |
| 488 // CryptohomeClient override. | |
| 489 virtual bool GetSystemSalt(std::vector<uint8>* salt) OVERRIDE { | |
| 490 const char kStubSystemSalt[] = "stub_system_salt"; | |
| 491 salt->assign(kStubSystemSalt, | |
| 492 kStubSystemSalt + arraysize(kStubSystemSalt)); | |
| 493 return true; | |
| 494 } | |
| 495 | |
| 496 // CryptohomeClient override. | |
| 497 virtual void AsyncMount(const std::string& username, | |
| 498 const std::string& key, | |
| 499 const bool create_if_missing, | |
| 500 AsyncMethodCallback callback) OVERRIDE { | |
| 501 ReturnAsyncMethodResult(callback); | |
| 502 } | |
| 503 | |
| 504 // CryptohomeClient override. | |
| 505 virtual void AsyncMountGuest(AsyncMethodCallback callback) OVERRIDE { | |
| 506 ReturnAsyncMethodResult(callback); | |
| 507 } | |
| 508 | |
| 509 // CryptohomeClient override. | |
| 510 virtual bool TpmIsReady(bool* ready) OVERRIDE { | |
| 511 *ready = (tpm_is_ready_counter_++ > 20); | |
| 512 return true; | |
| 513 } | |
| 514 | |
| 515 // CryptohomeClient override. | |
| 516 virtual bool TpmIsEnabled(bool* enabled) OVERRIDE { | |
| 517 *enabled = true; | |
| 518 return true; | |
| 519 } | |
| 520 | |
| 521 // CryptohomeClient override. | |
| 522 virtual bool TpmGetPassword(std::string* password) OVERRIDE { | |
| 523 const char kStubTpmPassword[] = "Stub-TPM-password"; | |
| 524 *password = kStubTpmPassword; | |
| 525 return true; | |
| 526 } | |
| 527 | |
| 528 // CryptohomeClient override. | |
| 529 virtual bool TpmIsOwned(bool* owned) OVERRIDE { | |
| 530 *owned = true; | |
| 531 return true; | |
| 532 } | |
| 533 | |
| 534 // CryptohomeClient override. | |
| 535 virtual bool TpmIsBeingOwned(bool* owning) OVERRIDE { | |
| 536 *owning = true; | |
| 537 return true; | |
| 538 } | |
| 539 | |
| 540 // CryptohomeClient override. | |
| 541 virtual bool TpmCanAttemptOwnership() OVERRIDE { return true; } | |
| 542 | |
| 543 // CryptohomeClient override. | |
| 544 virtual bool TpmClearStoredPassword() OVERRIDE { return true; } | |
| 545 | |
| 546 // CryptohomeClient override. | |
| 547 virtual void Pkcs11IsTpmTokenReady(base::Callback<void(CallStatus call_status, | |
| 548 bool ready)> callback) | |
| 549 OVERRIDE { | |
| 550 MessageLoop::current()->PostTask(FROM_HERE, | |
| 551 base::Bind(callback, SUCCESS, true)); | |
| 552 } | |
| 553 | |
| 554 // CryptohomeClient override. | |
| 555 virtual void Pkcs11GetTpmTokenInfo( | |
| 556 base::Callback<void(CallStatus call_status, | |
| 557 const std::string& label, | |
| 558 const std::string& user_pin)> callback) OVERRIDE { | |
| 559 const char kStubLabel[] = "Stub TPM Token"; | |
| 560 const char kStubUserPin[] = "012345"; | |
| 561 MessageLoop::current()->PostTask( | |
| 562 FROM_HERE, base::Bind(callback, SUCCESS, kStubLabel, kStubUserPin)); | |
| 563 } | |
| 564 | |
| 565 // CryptohomeClient override. | |
| 566 virtual bool InstallAttributesGet(const std::string& name, | |
| 567 std::vector<uint8>* value, | |
| 568 bool* successful) OVERRIDE { | |
| 569 if (install_attrs_.find(name) != install_attrs_.end()) { | |
| 570 *value = install_attrs_[name]; | |
| 571 *successful = true; | |
| 572 } else { | |
| 573 value->clear(); | |
| 574 *successful = false; | |
| 575 } | |
| 576 return true; | |
| 577 } | |
| 578 | |
| 579 // CryptohomeClient override. | |
| 580 virtual bool InstallAttributesSet(const std::string& name, | |
| 581 const std::vector<uint8>& value, | |
| 582 bool* successful) OVERRIDE { | |
| 583 install_attrs_[name] = value; | |
| 584 *successful = true; | |
| 585 return true; | |
| 586 } | |
| 587 | |
| 588 // CryptohomeClient override. | |
| 589 virtual bool InstallAttributesFinalize(bool* successful) OVERRIDE { | |
| 590 locked_ = true; | |
| 591 *successful = true; | |
| 592 return true; | |
| 593 } | |
| 594 | |
| 595 // CryptohomeClient override. | |
| 596 virtual bool InstallAttributesIsReady(bool* is_ready) OVERRIDE { | |
| 597 *is_ready = true; | |
| 598 return true; | |
| 599 } | |
| 600 | |
| 601 // CryptohomeClient override. | |
| 602 virtual bool InstallAttributesIsInvalid(bool* is_invalid) OVERRIDE { | |
| 603 *is_invalid = false; | |
| 604 return true; | |
| 605 } | |
| 606 | |
| 607 // CryptohomeClient override. | |
| 608 virtual bool InstallAttributesIsFirstInstall(bool* is_first_install) OVERRIDE | |
| 609 { | |
| 610 *is_first_install = !locked_; | |
| 611 return true; | |
| 612 } | |
| 613 | |
| 614 private: | |
| 615 // Posts tasks which return fake results to the UI thread. | |
| 616 void ReturnAsyncMethodResult(AsyncMethodCallback callback) { | |
| 617 MessageLoop::current()->PostTask( | |
| 618 FROM_HERE, | |
| 619 base::Bind(&CryptohomeClientStubImpl::ReturnAsyncMethodResultInternal, | |
| 620 weak_ptr_factory_.GetWeakPtr(), | |
| 621 callback)); | |
| 622 } | |
| 623 | |
| 624 // This method is used to implement ReturnAsyncMethodResult. | |
| 625 void ReturnAsyncMethodResultInternal(AsyncMethodCallback callback) { | |
| 626 callback.Run(async_call_id_); | |
| 627 if (!async_call_status_handler_.is_null()) { | |
| 628 MessageLoop::current()->PostTask( | |
| 629 FROM_HERE, | |
| 630 base::Bind(async_call_status_handler_, | |
| 631 async_call_id_, | |
| 632 true, | |
| 633 cryptohome::MOUNT_ERROR_NONE)); | |
| 634 } | |
| 635 ++async_call_id_; | |
| 636 } | |
| 637 | |
| 638 base::WeakPtrFactory<CryptohomeClientStubImpl> weak_ptr_factory_; | |
| 639 int async_call_id_; | |
| 640 AsyncCallStatusHandler async_call_status_handler_; | |
| 641 int tpm_is_ready_counter_; | |
| 642 std::map<std::string, std::vector<uint8> > install_attrs_; | |
| 643 bool locked_; | |
| 644 | |
| 645 DISALLOW_COPY_AND_ASSIGN(CryptohomeClientStubImpl); | |
| 646 }; | |
| 647 | |
| 648 } // namespace | |
| 649 | |
| 650 //////////////////////////////////////////////////////////////////////////////// | |
| 651 // CryptohomeClient | |
| 652 | |
| 653 CryptohomeClient::CryptohomeClient() {} | |
| 654 | |
| 655 CryptohomeClient::~CryptohomeClient() {} | |
| 656 | |
| 657 // static | |
| 658 CryptohomeClient* CryptohomeClient::Create(dbus::Bus* bus) { | |
| 659 if (base::chromeos::IsRunningOnChromeOS()) | |
| 660 return new CryptohomeClientImpl(bus); | |
| 661 else | |
| 662 return new CryptohomeClientStubImpl(); | |
| 663 } | |
| 664 | |
| 665 } // namespace chromeos | |
| OLD | NEW |