| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/ownership/owner_settings_service.h" | 5 #include "chrome/browser/chromeos/ownership/owner_settings_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/path_service.h" | |
| 13 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/chromeos/ownership/owner_settings_service_factory.h" | |
| 16 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 14 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 17 #include "chrome/browser/chromeos/settings/cros_settings.h" | 15 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 18 #include "chrome/browser/chromeos/settings/session_manager_operation.h" | 16 #include "chrome/browser/chromeos/settings/session_manager_operation.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chromeos/chromeos_paths.h" | |
| 21 #include "chromeos/dbus/dbus_thread_manager.h" | 18 #include "chromeos/dbus/dbus_thread_manager.h" |
| 22 #include "components/ownership/owner_key_util_impl.h" | |
| 23 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 19 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
| 24 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
| 26 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 28 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| 29 #include "crypto/nss_util.h" | 25 #include "crypto/nss_util.h" |
| 30 #include "crypto/nss_util_internal.h" | 26 #include "crypto/nss_util_internal.h" |
| 31 #include "crypto/rsa_private_key.h" | 27 #include "crypto/rsa_private_key.h" |
| 32 #include "crypto/scoped_nss_types.h" | 28 #include "crypto/scoped_nss_types.h" |
| 33 #include "crypto/signature_creator.h" | 29 #include "crypto/signature_creator.h" |
| 34 | 30 |
| 35 namespace em = enterprise_management; | 31 namespace em = enterprise_management; |
| 36 | 32 |
| 37 using content::BrowserThread; | 33 using content::BrowserThread; |
| 38 using ownership::OwnerKeyUtil; | 34 using ownership::OwnerKeyUtil; |
| 39 using ownership::PrivateKey; | 35 using ownership::PrivateKey; |
| 40 using ownership::PublicKey; | 36 using ownership::PublicKey; |
| 41 | 37 |
| 42 namespace chromeos { | 38 namespace chromeos { |
| 43 | 39 |
| 44 namespace { | 40 namespace { |
| 45 | 41 |
| 46 scoped_refptr<OwnerKeyUtil>* g_owner_key_util_for_testing = NULL; | |
| 47 DeviceSettingsService* g_device_settings_service_for_testing = NULL; | 42 DeviceSettingsService* g_device_settings_service_for_testing = NULL; |
| 48 | 43 |
| 49 bool IsOwnerInTests(const std::string& user_id) { | 44 bool IsOwnerInTests(const std::string& user_id) { |
| 50 if (user_id.empty() || | 45 if (user_id.empty() || |
| 51 !CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType) || | 46 !CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType) || |
| 52 !CrosSettings::IsInitialized()) { | 47 !CrosSettings::IsInitialized()) { |
| 53 return false; | 48 return false; |
| 54 } | 49 } |
| 55 const base::Value* value = CrosSettings::Get()->GetPref(kDeviceOwner); | 50 const base::Value* value = CrosSettings::Get()->GetPref(kDeviceOwner); |
| 56 if (!value || value->GetType() != base::Value::TYPE_STRING) | 51 if (!value || value->GetType() != base::Value::TYPE_STRING) |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 return false; | 166 return false; |
| 172 scoped_ptr<crypto::RSAPrivateKey> key( | 167 scoped_ptr<crypto::RSAPrivateKey> key( |
| 173 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key)); | 168 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key)); |
| 174 bool is_owner = key.get() != NULL; | 169 bool is_owner = key.get() != NULL; |
| 175 return is_owner; | 170 return is_owner; |
| 176 } | 171 } |
| 177 | 172 |
| 178 // Checks whether NSS slots with private key are mounted or | 173 // Checks whether NSS slots with private key are mounted or |
| 179 // not. Responds via |callback|. | 174 // not. Responds via |callback|. |
| 180 void DoesPrivateKeyExistAsync( | 175 void DoesPrivateKeyExistAsync( |
| 176 const scoped_refptr<OwnerKeyUtil>& owner_key_util, |
| 181 const OwnerSettingsService::IsOwnerCallback& callback) { | 177 const OwnerSettingsService::IsOwnerCallback& callback) { |
| 182 scoped_refptr<OwnerKeyUtil> owner_key_util; | |
| 183 if (g_owner_key_util_for_testing) | |
| 184 owner_key_util = *g_owner_key_util_for_testing; | |
| 185 else | |
| 186 owner_key_util = OwnerSettingsService::MakeOwnerKeyUtil(); | |
| 187 if (!owner_key_util) { | 178 if (!owner_key_util) { |
| 188 callback.Run(false); | 179 callback.Run(false); |
| 189 return; | 180 return; |
| 190 } | 181 } |
| 191 scoped_refptr<base::TaskRunner> task_runner = | 182 scoped_refptr<base::TaskRunner> task_runner = |
| 192 content::BrowserThread::GetBlockingPool() | 183 content::BrowserThread::GetBlockingPool() |
| 193 ->GetTaskRunnerWithShutdownBehavior( | 184 ->GetTaskRunnerWithShutdownBehavior( |
| 194 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 185 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 195 base::PostTaskAndReplyWithResult( | 186 base::PostTaskAndReplyWithResult( |
| 196 task_runner.get(), | 187 task_runner.get(), |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // For consumer management unenrollment. | 225 // For consumer management unenrollment. |
| 235 return new_mode == em::PolicyData::NOT_MANAGED; | 226 return new_mode == em::PolicyData::NOT_MANAGED; |
| 236 } | 227 } |
| 237 | 228 |
| 238 NOTREACHED(); | 229 NOTREACHED(); |
| 239 return false; | 230 return false; |
| 240 } | 231 } |
| 241 | 232 |
| 242 } // namespace | 233 } // namespace |
| 243 | 234 |
| 244 OwnerSettingsService::OwnerSettingsService(Profile* profile) | 235 OwnerSettingsService::OwnerSettingsService( |
| 236 Profile* profile, |
| 237 const scoped_refptr<OwnerKeyUtil>& owner_key_util) |
| 245 : profile_(profile), | 238 : profile_(profile), |
| 246 owner_key_util_(MakeOwnerKeyUtil()), | 239 owner_key_util_(owner_key_util), |
| 247 waiting_for_profile_creation_(true), | 240 waiting_for_profile_creation_(true), |
| 248 waiting_for_tpm_token_(true), | 241 waiting_for_tpm_token_(true), |
| 249 weak_factory_(this) { | 242 weak_factory_(this) { |
| 250 if (TPMTokenLoader::IsInitialized()) { | 243 if (TPMTokenLoader::IsInitialized()) { |
| 251 waiting_for_tpm_token_ = !TPMTokenLoader::Get()->IsTPMTokenReady(); | 244 waiting_for_tpm_token_ = !TPMTokenLoader::Get()->IsTPMTokenReady(); |
| 252 TPMTokenLoader::Get()->AddObserver(this); | 245 TPMTokenLoader::Get()->AddObserver(this); |
| 253 } | 246 } |
| 254 | 247 |
| 255 if (DBusThreadManager::IsInitialized() && | 248 if (DBusThreadManager::IsInitialized() && |
| 256 DBusThreadManager::Get()->GetSessionManagerClient()) { | 249 DBusThreadManager::Get()->GetSessionManagerClient()) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 void OwnerSettingsService::OwnerKeySet(bool success) { | 371 void OwnerSettingsService::OwnerKeySet(bool success) { |
| 379 DCHECK(thread_checker_.CalledOnValidThread()); | 372 DCHECK(thread_checker_.CalledOnValidThread()); |
| 380 if (success) | 373 if (success) |
| 381 ReloadPrivateKey(); | 374 ReloadPrivateKey(); |
| 382 } | 375 } |
| 383 | 376 |
| 384 // static | 377 // static |
| 385 void OwnerSettingsService::IsOwnerForSafeModeAsync( | 378 void OwnerSettingsService::IsOwnerForSafeModeAsync( |
| 386 const std::string& user_id, | 379 const std::string& user_id, |
| 387 const std::string& user_hash, | 380 const std::string& user_hash, |
| 381 const scoped_refptr<OwnerKeyUtil>& owner_key_util, |
| 388 const IsOwnerCallback& callback) { | 382 const IsOwnerCallback& callback) { |
| 389 CHECK(chromeos::LoginState::Get()->IsInSafeMode()); | 383 CHECK(chromeos::LoginState::Get()->IsInSafeMode()); |
| 390 | 384 |
| 391 // Make sure NSS is initialized and NSS DB is loaded for the user before | 385 // Make sure NSS is initialized and NSS DB is loaded for the user before |
| 392 // searching for the owner key. | 386 // searching for the owner key. |
| 393 BrowserThread::PostTaskAndReply( | 387 BrowserThread::PostTaskAndReply( |
| 394 BrowserThread::IO, | 388 BrowserThread::IO, |
| 395 FROM_HERE, | 389 FROM_HERE, |
| 396 base::Bind(base::IgnoreResult(&crypto::InitializeNSSForChromeOSUser), | 390 base::Bind(base::IgnoreResult(&crypto::InitializeNSSForChromeOSUser), |
| 397 user_id, | 391 user_id, |
| 398 user_hash, | 392 user_hash, |
| 399 ProfileHelper::GetProfilePathByUserIdHash(user_hash)), | 393 ProfileHelper::GetProfilePathByUserIdHash(user_hash)), |
| 400 base::Bind(&DoesPrivateKeyExistAsync, callback)); | 394 base::Bind(&DoesPrivateKeyExistAsync, owner_key_util, callback)); |
| 401 } | 395 } |
| 402 | 396 |
| 403 // static | 397 // static |
| 404 scoped_refptr<ownership::OwnerKeyUtil> | |
| 405 OwnerSettingsService::MakeOwnerKeyUtil() { | |
| 406 base::FilePath public_key_path; | |
| 407 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &public_key_path)) | |
| 408 return NULL; | |
| 409 return new ownership::OwnerKeyUtilImpl(public_key_path); | |
| 410 } | |
| 411 | |
| 412 // static | |
| 413 void OwnerSettingsService::SetOwnerKeyUtilForTesting( | |
| 414 const scoped_refptr<OwnerKeyUtil>& owner_key_util) { | |
| 415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 416 if (g_owner_key_util_for_testing) { | |
| 417 delete g_owner_key_util_for_testing; | |
| 418 g_owner_key_util_for_testing = NULL; | |
| 419 } | |
| 420 if (owner_key_util.get()) { | |
| 421 g_owner_key_util_for_testing = new scoped_refptr<OwnerKeyUtil>(); | |
| 422 *g_owner_key_util_for_testing = owner_key_util; | |
| 423 } | |
| 424 } | |
| 425 | |
| 426 // static | |
| 427 void OwnerSettingsService::SetDeviceSettingsServiceForTesting( | 398 void OwnerSettingsService::SetDeviceSettingsServiceForTesting( |
| 428 DeviceSettingsService* device_settings_service) { | 399 DeviceSettingsService* device_settings_service) { |
| 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 400 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 430 g_device_settings_service_for_testing = device_settings_service; | 401 g_device_settings_service_for_testing = device_settings_service; |
| 431 } | 402 } |
| 432 | 403 |
| 433 void OwnerSettingsService::ReloadPrivateKey() { | 404 void OwnerSettingsService::ReloadPrivateKey() { |
| 434 DCHECK(thread_checker_.CalledOnValidThread()); | 405 DCHECK(thread_checker_.CalledOnValidThread()); |
| 435 if (waiting_for_profile_creation_ || waiting_for_tpm_token_) | 406 if (waiting_for_profile_creation_ || waiting_for_tpm_token_) |
| 436 return; | 407 return; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 void OwnerSettingsService::HandleError(DeviceSettingsService::Status status, | 496 void OwnerSettingsService::HandleError(DeviceSettingsService::Status status, |
| 526 const base::Closure& callback) { | 497 const base::Closure& callback) { |
| 527 LOG(ERROR) << "Session manager operation failed: " << status; | 498 LOG(ERROR) << "Session manager operation failed: " << status; |
| 528 GetDeviceSettingsService()->OnSignAndStoreOperationCompleted(status); | 499 GetDeviceSettingsService()->OnSignAndStoreOperationCompleted(status); |
| 529 if (!callback.is_null()) | 500 if (!callback.is_null()) |
| 530 callback.Run(); | 501 callback.Run(); |
| 531 } | 502 } |
| 532 | 503 |
| 533 scoped_refptr<OwnerKeyUtil> OwnerSettingsService::GetOwnerKeyUtil() { | 504 scoped_refptr<OwnerKeyUtil> OwnerSettingsService::GetOwnerKeyUtil() { |
| 534 DCHECK(thread_checker_.CalledOnValidThread()); | 505 DCHECK(thread_checker_.CalledOnValidThread()); |
| 535 if (g_owner_key_util_for_testing) | |
| 536 return *g_owner_key_util_for_testing; | |
| 537 return owner_key_util_; | 506 return owner_key_util_; |
| 538 } | 507 } |
| 539 | 508 |
| 540 DeviceSettingsService* OwnerSettingsService::GetDeviceSettingsService() { | 509 DeviceSettingsService* OwnerSettingsService::GetDeviceSettingsService() { |
| 541 DCHECK(thread_checker_.CalledOnValidThread()); | 510 DCHECK(thread_checker_.CalledOnValidThread()); |
| 542 if (g_device_settings_service_for_testing) | 511 if (g_device_settings_service_for_testing) |
| 543 return g_device_settings_service_for_testing; | 512 return g_device_settings_service_for_testing; |
| 544 if (DeviceSettingsService::IsInitialized()) | 513 if (DeviceSettingsService::IsInitialized()) |
| 545 return DeviceSettingsService::Get(); | 514 return DeviceSettingsService::Get(); |
| 546 return NULL; | 515 return NULL; |
| 547 } | 516 } |
| 548 | 517 |
| 549 } // namespace chromeos | 518 } // namespace chromeos |
| OLD | NEW |