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_chromeos.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/callback.h" | |
12 #include "base/command_line.h" | 11 #include "base/command_line.h" |
13 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
14 #include "base/threading/thread_checker.h" | |
15 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.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/dbus/dbus_thread_manager.h" | 18 #include "chromeos/dbus/dbus_thread_manager.h" |
21 #include "chromeos/tpm_token_loader.h" | 19 #include "chromeos/tpm_token_loader.h" |
22 #include "components/ownership/owner_key_util.h" | |
23 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 20 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
24 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
26 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
27 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
28 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
29 #include "crypto/nss_util.h" | 26 #include "crypto/nss_util.h" |
30 #include "crypto/nss_util_internal.h" | 27 #include "crypto/nss_util_internal.h" |
31 #include "crypto/rsa_private_key.h" | 28 #include "crypto/rsa_private_key.h" |
32 #include "crypto/scoped_nss_types.h" | 29 #include "crypto/scoped_nss_types.h" |
(...skipping 17 matching lines...) Expand all Loading... |
50 !CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType) || | 47 !CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType) || |
51 !CrosSettings::IsInitialized()) { | 48 !CrosSettings::IsInitialized()) { |
52 return false; | 49 return false; |
53 } | 50 } |
54 const base::Value* value = CrosSettings::Get()->GetPref(kDeviceOwner); | 51 const base::Value* value = CrosSettings::Get()->GetPref(kDeviceOwner); |
55 if (!value || value->GetType() != base::Value::TYPE_STRING) | 52 if (!value || value->GetType() != base::Value::TYPE_STRING) |
56 return false; | 53 return false; |
57 return static_cast<const base::StringValue*>(value)->GetString() == user_id; | 54 return static_cast<const base::StringValue*>(value)->GetString() == user_id; |
58 } | 55 } |
59 | 56 |
| 57 // Assembles PolicyData based on |settings|, |policy_data| and |
| 58 // |user_id|. |
| 59 scoped_ptr<em::PolicyData> AssemblePolicy( |
| 60 const std::string& user_id, |
| 61 const em::PolicyData* policy_data, |
| 62 const em::ChromeDeviceSettingsProto* settings) { |
| 63 scoped_ptr<em::PolicyData> policy(new em::PolicyData()); |
| 64 if (policy_data) { |
| 65 // Preserve management settings. |
| 66 if (policy_data->has_management_mode()) |
| 67 policy->set_management_mode(policy_data->management_mode()); |
| 68 if (policy_data->has_request_token()) |
| 69 policy->set_request_token(policy_data->request_token()); |
| 70 if (policy_data->has_device_id()) |
| 71 policy->set_device_id(policy_data->device_id()); |
| 72 } else { |
| 73 // If there's no previous policy data, this is the first time the device |
| 74 // setting is set. We set the management mode to NOT_MANAGED initially. |
| 75 policy->set_management_mode(em::PolicyData::NOT_MANAGED); |
| 76 } |
| 77 policy->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType); |
| 78 policy->set_timestamp( |
| 79 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()); |
| 80 policy->set_username(user_id); |
| 81 if (!settings->SerializeToString(policy->mutable_policy_value())) |
| 82 return scoped_ptr<em::PolicyData>(); |
| 83 |
| 84 return policy.Pass(); |
| 85 } |
| 86 |
| 87 std::string AssembleAndSignPolicy(scoped_ptr<em::PolicyData> policy, |
| 88 crypto::RSAPrivateKey* private_key) { |
| 89 // Assemble the policy. |
| 90 em::PolicyFetchResponse policy_response; |
| 91 if (!policy->SerializeToString(policy_response.mutable_policy_data())) { |
| 92 LOG(ERROR) << "Failed to encode policy payload."; |
| 93 return std::string(); |
| 94 } |
| 95 |
| 96 // Generate the signature. |
| 97 scoped_ptr<crypto::SignatureCreator> signature_creator( |
| 98 crypto::SignatureCreator::Create(private_key)); |
| 99 signature_creator->Update( |
| 100 reinterpret_cast<const uint8*>(policy_response.policy_data().c_str()), |
| 101 policy_response.policy_data().size()); |
| 102 std::vector<uint8> signature_bytes; |
| 103 std::string policy_blob; |
| 104 if (!signature_creator->Final(&signature_bytes)) { |
| 105 LOG(ERROR) << "Failed to create policy signature."; |
| 106 return std::string(); |
| 107 } |
| 108 |
| 109 policy_response.mutable_policy_data_signature()->assign( |
| 110 reinterpret_cast<const char*>(vector_as_array(&signature_bytes)), |
| 111 signature_bytes.size()); |
| 112 return policy_response.SerializeAsString(); |
| 113 } |
| 114 |
60 void LoadPrivateKeyByPublicKey( | 115 void LoadPrivateKeyByPublicKey( |
61 const scoped_refptr<OwnerKeyUtil>& owner_key_util, | 116 const scoped_refptr<OwnerKeyUtil>& owner_key_util, |
62 scoped_refptr<PublicKey> public_key, | 117 scoped_refptr<PublicKey> public_key, |
63 const std::string& username_hash, | 118 const std::string& username_hash, |
64 const base::Callback<void(const scoped_refptr<PublicKey>& public_key, | 119 const base::Callback<void(scoped_refptr<PublicKey> public_key, |
65 const scoped_refptr<PrivateKey>& private_key)>& | 120 scoped_refptr<PrivateKey> private_key)>& |
66 callback) { | 121 callback) { |
67 crypto::EnsureNSSInit(); | 122 crypto::EnsureNSSInit(); |
68 crypto::ScopedPK11Slot slot = | 123 crypto::ScopedPK11Slot slot = |
69 crypto::GetPublicSlotForChromeOSUser(username_hash); | 124 crypto::GetPublicSlotForChromeOSUser(username_hash); |
70 scoped_refptr<PrivateKey> private_key(new PrivateKey( | 125 scoped_refptr<PrivateKey> private_key(new PrivateKey( |
71 owner_key_util->FindPrivateKeyInSlot(public_key->data(), slot.get()))); | 126 owner_key_util->FindPrivateKeyInSlot(public_key->data(), slot.get()))); |
72 BrowserThread::PostTask(BrowserThread::UI, | 127 BrowserThread::PostTask(BrowserThread::UI, |
73 FROM_HERE, | 128 FROM_HERE, |
74 base::Bind(callback, public_key, private_key)); | 129 base::Bind(callback, public_key, private_key)); |
75 } | 130 } |
76 | 131 |
77 void LoadPrivateKey( | 132 void LoadPrivateKey(const scoped_refptr<OwnerKeyUtil>& owner_key_util, |
78 const scoped_refptr<OwnerKeyUtil>& owner_key_util, | 133 const std::string username_hash, |
79 const std::string username_hash, | 134 const base::Callback<void( |
80 const base::Callback<void(const scoped_refptr<PublicKey>& public_key, | 135 scoped_refptr<PublicKey> public_key, |
81 const scoped_refptr<PrivateKey>& private_key)>& | 136 scoped_refptr<PrivateKey> private_key)>& callback) { |
82 callback) { | |
83 std::vector<uint8> public_key_data; | 137 std::vector<uint8> public_key_data; |
84 scoped_refptr<PublicKey> public_key; | 138 scoped_refptr<PublicKey> public_key; |
85 if (!owner_key_util->ImportPublicKey(&public_key_data)) { | 139 if (!owner_key_util->ImportPublicKey(&public_key_data)) { |
86 scoped_refptr<PrivateKey> private_key; | 140 scoped_refptr<PrivateKey> private_key; |
87 BrowserThread::PostTask(BrowserThread::UI, | 141 BrowserThread::PostTask(BrowserThread::UI, |
88 FROM_HERE, | 142 FROM_HERE, |
89 base::Bind(callback, public_key, private_key)); | 143 base::Bind(callback, public_key, private_key)); |
90 return; | 144 return; |
91 } | 145 } |
92 public_key = new PublicKey(); | 146 public_key = new PublicKey(); |
(...skipping 21 matching lines...) Expand all Loading... |
114 scoped_ptr<crypto::RSAPrivateKey> key( | 168 scoped_ptr<crypto::RSAPrivateKey> key( |
115 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key)); | 169 crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key)); |
116 bool is_owner = key.get() != NULL; | 170 bool is_owner = key.get() != NULL; |
117 return is_owner; | 171 return is_owner; |
118 } | 172 } |
119 | 173 |
120 // Checks whether NSS slots with private key are mounted or | 174 // Checks whether NSS slots with private key are mounted or |
121 // not. Responds via |callback|. | 175 // not. Responds via |callback|. |
122 void DoesPrivateKeyExistAsync( | 176 void DoesPrivateKeyExistAsync( |
123 const scoped_refptr<OwnerKeyUtil>& owner_key_util, | 177 const scoped_refptr<OwnerKeyUtil>& owner_key_util, |
124 const OwnerSettingsServiceChromeOS::IsOwnerCallback& callback) { | 178 const OwnerSettingsService::IsOwnerCallback& callback) { |
125 if (!owner_key_util) { | 179 if (!owner_key_util) { |
126 callback.Run(false); | 180 callback.Run(false); |
127 return; | 181 return; |
128 } | 182 } |
129 scoped_refptr<base::TaskRunner> task_runner = | 183 scoped_refptr<base::TaskRunner> task_runner = |
130 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 184 content::BrowserThread::GetBlockingPool() |
131 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 185 ->GetTaskRunnerWithShutdownBehavior( |
| 186 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
132 base::PostTaskAndReplyWithResult( | 187 base::PostTaskAndReplyWithResult( |
133 task_runner.get(), | 188 task_runner.get(), |
134 FROM_HERE, | 189 FROM_HERE, |
135 base::Bind(&DoesPrivateKeyExistAsyncHelper, owner_key_util), | 190 base::Bind(&DoesPrivateKeyExistAsyncHelper, owner_key_util), |
136 callback); | 191 callback); |
137 } | 192 } |
138 | 193 |
139 DeviceSettingsService* GetDeviceSettingsService() { | 194 // Returns the current management mode. |
140 if (g_device_settings_service_for_testing) | 195 em::PolicyData::ManagementMode GetManagementMode( |
141 return g_device_settings_service_for_testing; | 196 DeviceSettingsService* service) { |
142 return DeviceSettingsService::IsInitialized() ? DeviceSettingsService::Get() | 197 if (!service) { |
143 : NULL; | 198 LOG(ERROR) << "DeviceSettingsService is not initialized"; |
| 199 return em::PolicyData::NOT_MANAGED; |
| 200 } |
| 201 |
| 202 const em::PolicyData* policy_data = service->policy_data(); |
| 203 if (policy_data && policy_data->has_management_mode()) |
| 204 return policy_data->management_mode(); |
| 205 return em::PolicyData::NOT_MANAGED; |
| 206 } |
| 207 |
| 208 // Returns true if it is okay to transfer from the current mode to the new |
| 209 // mode. This function should be called in SetManagementMode(). |
| 210 bool CheckManagementModeTransition(em::PolicyData::ManagementMode current_mode, |
| 211 em::PolicyData::ManagementMode new_mode) { |
| 212 // Mode is not changed. |
| 213 if (current_mode == new_mode) |
| 214 return true; |
| 215 |
| 216 switch (current_mode) { |
| 217 case em::PolicyData::NOT_MANAGED: |
| 218 // For consumer management enrollment. |
| 219 return new_mode == em::PolicyData::CONSUMER_MANAGED; |
| 220 |
| 221 case em::PolicyData::ENTERPRISE_MANAGED: |
| 222 // Management mode cannot be set when it is currently ENTERPRISE_MANAGED. |
| 223 return false; |
| 224 |
| 225 case em::PolicyData::CONSUMER_MANAGED: |
| 226 // For consumer management unenrollment. |
| 227 return new_mode == em::PolicyData::NOT_MANAGED; |
| 228 } |
| 229 |
| 230 NOTREACHED(); |
| 231 return false; |
144 } | 232 } |
145 | 233 |
146 } // namespace | 234 } // namespace |
147 | 235 |
148 OwnerSettingsServiceChromeOS::OwnerSettingsServiceChromeOS( | 236 OwnerSettingsService::OwnerSettingsService( |
149 Profile* profile, | 237 Profile* profile, |
150 const scoped_refptr<OwnerKeyUtil>& owner_key_util) | 238 const scoped_refptr<OwnerKeyUtil>& owner_key_util) |
151 : ownership::OwnerSettingsService(owner_key_util), | 239 : profile_(profile), |
152 profile_(profile), | 240 owner_key_util_(owner_key_util), |
153 waiting_for_profile_creation_(true), | 241 waiting_for_profile_creation_(true), |
154 waiting_for_tpm_token_(true), | 242 waiting_for_tpm_token_(true), |
155 weak_factory_(this) { | 243 weak_factory_(this) { |
156 if (TPMTokenLoader::IsInitialized()) { | 244 if (TPMTokenLoader::IsInitialized()) { |
157 TPMTokenLoader::TPMTokenStatus tpm_token_status = | 245 TPMTokenLoader::TPMTokenStatus tpm_token_status = |
158 TPMTokenLoader::Get()->IsTPMTokenEnabled( | 246 TPMTokenLoader::Get()->IsTPMTokenEnabled( |
159 base::Bind(&OwnerSettingsServiceChromeOS::OnTPMTokenReady, | 247 base::Bind(&OwnerSettingsService::OnTPMTokenReady, as_weak_ptr())); |
160 weak_factory_.GetWeakPtr())); | |
161 waiting_for_tpm_token_ = | 248 waiting_for_tpm_token_ = |
162 tpm_token_status == TPMTokenLoader::TPM_TOKEN_STATUS_UNDETERMINED; | 249 tpm_token_status == TPMTokenLoader::TPM_TOKEN_STATUS_UNDETERMINED; |
163 } | 250 } |
164 | 251 |
165 if (DBusThreadManager::IsInitialized() && | 252 if (DBusThreadManager::IsInitialized() && |
166 DBusThreadManager::Get()->GetSessionManagerClient()) { | 253 DBusThreadManager::Get()->GetSessionManagerClient()) { |
167 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); | 254 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); |
168 } | 255 } |
169 | 256 |
170 registrar_.Add(this, | 257 registrar_.Add(this, |
171 chrome::NOTIFICATION_PROFILE_CREATED, | 258 chrome::NOTIFICATION_PROFILE_CREATED, |
172 content::Source<Profile>(profile_)); | 259 content::Source<Profile>(profile_)); |
173 } | 260 } |
174 | 261 |
175 OwnerSettingsServiceChromeOS::~OwnerSettingsServiceChromeOS() { | 262 OwnerSettingsService::~OwnerSettingsService() { |
176 DCHECK(thread_checker_.CalledOnValidThread()); | 263 DCHECK(thread_checker_.CalledOnValidThread()); |
177 if (DBusThreadManager::IsInitialized() && | 264 if (DBusThreadManager::IsInitialized() && |
178 DBusThreadManager::Get()->GetSessionManagerClient()) { | 265 DBusThreadManager::Get()->GetSessionManagerClient()) { |
179 DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this); | 266 DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this); |
180 } | 267 } |
181 } | 268 } |
182 | 269 |
183 void OwnerSettingsServiceChromeOS::OnTPMTokenReady( | 270 bool OwnerSettingsService::IsOwner() { |
184 bool /* tpm_token_enabled */) { | |
185 DCHECK(thread_checker_.CalledOnValidThread()); | 271 DCHECK(thread_checker_.CalledOnValidThread()); |
186 waiting_for_tpm_token_ = false; | 272 return private_key_ && private_key_->key(); |
187 | |
188 // TPMTokenLoader initializes the TPM and NSS database which is necessary to | |
189 // determine ownership. Force a reload once we know these are initialized. | |
190 ReloadKeypair(); | |
191 } | 273 } |
192 | 274 |
193 void OwnerSettingsServiceChromeOS::SignAndStorePolicyAsync( | 275 void OwnerSettingsService::IsOwnerAsync(const IsOwnerCallback& callback) { |
| 276 DCHECK(thread_checker_.CalledOnValidThread()); |
| 277 if (private_key_) { |
| 278 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 279 base::Bind(callback, IsOwner())); |
| 280 } else { |
| 281 pending_is_owner_callbacks_.push_back(callback); |
| 282 } |
| 283 } |
| 284 |
| 285 bool OwnerSettingsService::AssembleAndSignPolicyAsync( |
194 scoped_ptr<em::PolicyData> policy, | 286 scoped_ptr<em::PolicyData> policy, |
| 287 const AssembleAndSignPolicyCallback& callback) { |
| 288 DCHECK(thread_checker_.CalledOnValidThread()); |
| 289 if (!IsOwner()) |
| 290 return false; |
| 291 base::PostTaskAndReplyWithResult( |
| 292 BrowserThread::GetBlockingPool(), |
| 293 FROM_HERE, |
| 294 base::Bind( |
| 295 &AssembleAndSignPolicy, base::Passed(&policy), private_key_->key()), |
| 296 callback); |
| 297 return true; |
| 298 } |
| 299 |
| 300 void OwnerSettingsService::SignAndStoreAsync( |
| 301 scoped_ptr<em::ChromeDeviceSettingsProto> settings, |
195 const base::Closure& callback) { | 302 const base::Closure& callback) { |
196 DCHECK(thread_checker_.CalledOnValidThread()); | 303 DCHECK(thread_checker_.CalledOnValidThread()); |
197 SignAndStoreSettingsOperation* operation = new SignAndStoreSettingsOperation( | 304 scoped_ptr<em::PolicyData> policy = AssemblePolicy( |
198 base::Bind(&OwnerSettingsServiceChromeOS::HandleCompletedOperation, | 305 user_id_, GetDeviceSettingsService()->policy_data(), settings.get()); |
199 weak_factory_.GetWeakPtr(), | 306 if (!policy) { |
200 callback), | 307 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); |
201 policy.Pass()); | 308 return; |
202 operation->set_owner_settings_service(weak_factory_.GetWeakPtr()); | 309 } |
203 pending_operations_.push_back(operation); | 310 |
204 if (pending_operations_.front() == operation) | 311 EnqueueSignAndStore(policy.Pass(), callback); |
205 StartNextOperation(); | |
206 } | 312 } |
207 | 313 |
208 void OwnerSettingsServiceChromeOS::Observe( | 314 void OwnerSettingsService::SetManagementSettingsAsync( |
| 315 em::PolicyData::ManagementMode management_mode, |
| 316 const std::string& request_token, |
| 317 const std::string& device_id, |
| 318 const base::Closure& callback) { |
| 319 em::PolicyData::ManagementMode current_mode = |
| 320 GetManagementMode(GetDeviceSettingsService()); |
| 321 if (!CheckManagementModeTransition(current_mode, management_mode)) { |
| 322 LOG(ERROR) << "Invalid management mode transition: current mode = " |
| 323 << current_mode << ", new mode = " << management_mode; |
| 324 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); |
| 325 return; |
| 326 } |
| 327 |
| 328 DeviceSettingsService* service = GetDeviceSettingsService(); |
| 329 scoped_ptr<em::PolicyData> policy = AssemblePolicy( |
| 330 user_id_, service->policy_data(), service->device_settings()); |
| 331 if (!policy) { |
| 332 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); |
| 333 return; |
| 334 } |
| 335 |
| 336 policy->set_management_mode(management_mode); |
| 337 policy->set_request_token(request_token); |
| 338 policy->set_device_id(device_id); |
| 339 |
| 340 EnqueueSignAndStore(policy.Pass(), callback); |
| 341 } |
| 342 |
| 343 void OwnerSettingsService::Observe( |
209 int type, | 344 int type, |
210 const content::NotificationSource& source, | 345 const content::NotificationSource& source, |
211 const content::NotificationDetails& details) { | 346 const content::NotificationDetails& details) { |
212 DCHECK(thread_checker_.CalledOnValidThread()); | 347 DCHECK(thread_checker_.CalledOnValidThread()); |
213 if (type != chrome::NOTIFICATION_PROFILE_CREATED) { | 348 if (type != chrome::NOTIFICATION_PROFILE_CREATED) { |
214 NOTREACHED(); | 349 NOTREACHED(); |
215 return; | 350 return; |
216 } | 351 } |
217 | 352 |
218 Profile* profile = content::Source<Profile>(source).ptr(); | 353 Profile* profile = content::Source<Profile>(source).ptr(); |
219 if (profile != profile_) { | 354 if (profile != profile_) { |
220 NOTREACHED(); | 355 NOTREACHED(); |
221 return; | 356 return; |
222 } | 357 } |
223 | 358 |
224 waiting_for_profile_creation_ = false; | 359 waiting_for_profile_creation_ = false; |
225 ReloadKeypair(); | 360 ReloadPrivateKey(); |
226 } | 361 } |
227 | 362 |
228 void OwnerSettingsServiceChromeOS::OwnerKeySet(bool success) { | 363 void OwnerSettingsService::OnTPMTokenReady(bool /* unused token_enabled */) { |
| 364 DCHECK(thread_checker_.CalledOnValidThread()); |
| 365 waiting_for_tpm_token_ = false; |
| 366 |
| 367 // TPMTokenLoader initializes the TPM and NSS database which is necessary to |
| 368 // determine ownership. Force a reload once we know these are initialized. |
| 369 ReloadPrivateKey(); |
| 370 } |
| 371 |
| 372 void OwnerSettingsService::OwnerKeySet(bool success) { |
229 DCHECK(thread_checker_.CalledOnValidThread()); | 373 DCHECK(thread_checker_.CalledOnValidThread()); |
230 if (success) | 374 if (success) |
231 ReloadKeypair(); | 375 ReloadPrivateKey(); |
232 } | 376 } |
233 | 377 |
234 // static | 378 // static |
235 void OwnerSettingsServiceChromeOS::IsOwnerForSafeModeAsync( | 379 void OwnerSettingsService::IsOwnerForSafeModeAsync( |
236 const std::string& user_hash, | 380 const std::string& user_hash, |
237 const scoped_refptr<OwnerKeyUtil>& owner_key_util, | 381 const scoped_refptr<OwnerKeyUtil>& owner_key_util, |
238 const IsOwnerCallback& callback) { | 382 const IsOwnerCallback& callback) { |
239 CHECK(chromeos::LoginState::Get()->IsInSafeMode()); | 383 CHECK(chromeos::LoginState::Get()->IsInSafeMode()); |
240 | 384 |
241 // 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 |
242 // searching for the owner key. | 386 // searching for the owner key. |
243 BrowserThread::PostTaskAndReply( | 387 BrowserThread::PostTaskAndReply( |
244 BrowserThread::IO, | 388 BrowserThread::IO, |
245 FROM_HERE, | 389 FROM_HERE, |
246 base::Bind(base::IgnoreResult(&crypto::InitializeNSSForChromeOSUser), | 390 base::Bind(base::IgnoreResult(&crypto::InitializeNSSForChromeOSUser), |
247 user_hash, | 391 user_hash, |
248 ProfileHelper::GetProfilePathByUserIdHash(user_hash)), | 392 ProfileHelper::GetProfilePathByUserIdHash(user_hash)), |
249 base::Bind(&DoesPrivateKeyExistAsync, owner_key_util, callback)); | 393 base::Bind(&DoesPrivateKeyExistAsync, owner_key_util, callback)); |
250 } | 394 } |
251 | 395 |
252 // static | 396 // static |
253 void OwnerSettingsServiceChromeOS::SetDeviceSettingsServiceForTesting( | 397 void OwnerSettingsService::SetDeviceSettingsServiceForTesting( |
254 DeviceSettingsService* device_settings_service) { | 398 DeviceSettingsService* device_settings_service) { |
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
256 g_device_settings_service_for_testing = device_settings_service; | 400 g_device_settings_service_for_testing = device_settings_service; |
257 } | 401 } |
258 | 402 |
259 void OwnerSettingsServiceChromeOS::OnPostKeypairLoadedActions() { | 403 void OwnerSettingsService::ReloadPrivateKey() { |
260 DCHECK(thread_checker_.CalledOnValidThread()); | 404 DCHECK(thread_checker_.CalledOnValidThread()); |
| 405 if (waiting_for_profile_creation_ || waiting_for_tpm_token_) |
| 406 return; |
| 407 scoped_refptr<base::TaskRunner> task_runner = |
| 408 content::BrowserThread::GetBlockingPool() |
| 409 ->GetTaskRunnerWithShutdownBehavior( |
| 410 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 411 task_runner->PostTask( |
| 412 FROM_HERE, |
| 413 base::Bind(&LoadPrivateKey, |
| 414 GetOwnerKeyUtil(), |
| 415 ProfileHelper::GetUserIdHashFromProfile(profile_), |
| 416 base::Bind(&OwnerSettingsService::OnPrivateKeyLoaded, |
| 417 weak_factory_.GetWeakPtr()))); |
| 418 } |
| 419 |
| 420 void OwnerSettingsService::OnPrivateKeyLoaded( |
| 421 scoped_refptr<PublicKey> public_key, |
| 422 scoped_refptr<PrivateKey> private_key) { |
| 423 DCHECK(thread_checker_.CalledOnValidThread()); |
| 424 public_key_ = public_key; |
| 425 private_key_ = private_key; |
261 | 426 |
262 user_id_ = profile_->GetProfileName(); | 427 user_id_ = profile_->GetProfileName(); |
263 const bool is_owner = IsOwner() || IsOwnerInTests(user_id_); | 428 const bool is_owner = IsOwner() || IsOwnerInTests(user_id_); |
264 if (is_owner && GetDeviceSettingsService()) | 429 if (is_owner && GetDeviceSettingsService()) |
265 GetDeviceSettingsService()->InitOwner(user_id_, weak_factory_.GetWeakPtr()); | 430 GetDeviceSettingsService()->InitOwner(user_id_, weak_factory_.GetWeakPtr()); |
| 431 |
| 432 std::vector<IsOwnerCallback> is_owner_callbacks; |
| 433 is_owner_callbacks.swap(pending_is_owner_callbacks_); |
| 434 for (std::vector<IsOwnerCallback>::iterator it(is_owner_callbacks.begin()); |
| 435 it != is_owner_callbacks.end(); |
| 436 ++it) { |
| 437 it->Run(is_owner); |
| 438 } |
266 } | 439 } |
267 | 440 |
268 void OwnerSettingsServiceChromeOS::ReloadKeypairImpl(const base::Callback< | 441 void OwnerSettingsService::EnqueueSignAndStore( |
269 void(const scoped_refptr<PublicKey>& public_key, | 442 scoped_ptr<em::PolicyData> policy, |
270 const scoped_refptr<PrivateKey>& private_key)>& callback) { | 443 const base::Closure& callback) { |
271 DCHECK(thread_checker_.CalledOnValidThread()); | 444 SignAndStoreSettingsOperation* operation = new SignAndStoreSettingsOperation( |
272 | 445 base::Bind(&OwnerSettingsService::HandleCompletedOperation, |
273 if (waiting_for_profile_creation_ || waiting_for_tpm_token_) | 446 weak_factory_.GetWeakPtr(), |
274 return; | 447 callback), |
275 scoped_refptr<base::TaskRunner> task_runner = | 448 policy.Pass()); |
276 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 449 operation->set_delegate(weak_factory_.GetWeakPtr()); |
277 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 450 pending_operations_.push_back(operation); |
278 task_runner->PostTask( | 451 if (pending_operations_.front() == operation) |
279 FROM_HERE, | 452 StartNextOperation(); |
280 base::Bind(&LoadPrivateKey, | |
281 owner_key_util_, | |
282 ProfileHelper::GetUserIdHashFromProfile(profile_), | |
283 callback)); | |
284 } | 453 } |
285 | 454 |
286 void OwnerSettingsServiceChromeOS::StartNextOperation() { | 455 void OwnerSettingsService::StartNextOperation() { |
287 DeviceSettingsService* service = GetDeviceSettingsService(); | 456 DeviceSettingsService* service = GetDeviceSettingsService(); |
288 if (!pending_operations_.empty() && service && | 457 if (!pending_operations_.empty() && service && |
289 service->session_manager_client()) { | 458 service->session_manager_client()) { |
290 pending_operations_.front()->Start( | 459 pending_operations_.front()->Start( |
291 service->session_manager_client(), owner_key_util_, public_key_); | 460 service->session_manager_client(), GetOwnerKeyUtil(), public_key_); |
292 } | 461 } |
293 } | 462 } |
294 | 463 |
295 void OwnerSettingsServiceChromeOS::HandleCompletedOperation( | 464 void OwnerSettingsService::HandleCompletedOperation( |
296 const base::Closure& callback, | 465 const base::Closure& callback, |
297 SessionManagerOperation* operation, | 466 SessionManagerOperation* operation, |
298 DeviceSettingsService::Status status) { | 467 DeviceSettingsService::Status status) { |
299 DCHECK_EQ(operation, pending_operations_.front()); | 468 DCHECK_EQ(operation, pending_operations_.front()); |
300 | 469 |
301 DeviceSettingsService* service = GetDeviceSettingsService(); | 470 DeviceSettingsService* service = GetDeviceSettingsService(); |
302 if (status == DeviceSettingsService::STORE_SUCCESS) { | 471 if (status == DeviceSettingsService::STORE_SUCCESS) { |
303 service->set_policy_data(operation->policy_data().Pass()); | 472 service->set_policy_data(operation->policy_data().Pass()); |
304 service->set_device_settings(operation->device_settings().Pass()); | 473 service->set_device_settings(operation->device_settings().Pass()); |
305 } | 474 } |
306 | 475 |
307 if ((operation->public_key() && !public_key_) || | 476 if ((operation->public_key() && !public_key_) || |
308 (operation->public_key() && public_key_ && | 477 (operation->public_key() && public_key_ && |
309 operation->public_key()->data() != public_key_->data())) { | 478 operation->public_key()->data() != public_key_->data())) { |
310 // Public part changed so we need to reload private part too. | 479 // Public part changed so we need to reload private part too. |
311 ReloadKeypair(); | 480 ReloadPrivateKey(); |
312 content::NotificationService::current()->Notify( | 481 content::NotificationService::current()->Notify( |
313 chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, | 482 chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, |
314 content::Source<OwnerSettingsServiceChromeOS>(this), | 483 content::Source<OwnerSettingsService>(this), |
315 content::NotificationService::NoDetails()); | 484 content::NotificationService::NoDetails()); |
316 } | 485 } |
317 service->OnSignAndStoreOperationCompleted(status); | 486 service->OnSignAndStoreOperationCompleted(status); |
318 if (!callback.is_null()) | 487 if (!callback.is_null()) |
319 callback.Run(); | 488 callback.Run(); |
320 | 489 |
321 pending_operations_.pop_front(); | 490 pending_operations_.pop_front(); |
322 delete operation; | 491 delete operation; |
323 StartNextOperation(); | 492 StartNextOperation(); |
324 } | 493 } |
325 | 494 |
| 495 void OwnerSettingsService::HandleError(DeviceSettingsService::Status status, |
| 496 const base::Closure& callback) { |
| 497 LOG(ERROR) << "Session manager operation failed: " << status; |
| 498 GetDeviceSettingsService()->OnSignAndStoreOperationCompleted(status); |
| 499 if (!callback.is_null()) |
| 500 callback.Run(); |
| 501 } |
| 502 |
| 503 scoped_refptr<OwnerKeyUtil> OwnerSettingsService::GetOwnerKeyUtil() { |
| 504 DCHECK(thread_checker_.CalledOnValidThread()); |
| 505 return owner_key_util_; |
| 506 } |
| 507 |
| 508 DeviceSettingsService* OwnerSettingsService::GetDeviceSettingsService() { |
| 509 DCHECK(thread_checker_.CalledOnValidThread()); |
| 510 if (g_device_settings_service_for_testing) |
| 511 return g_device_settings_service_for_testing; |
| 512 if (DeviceSettingsService::IsInitialized()) |
| 513 return DeviceSettingsService::Get(); |
| 514 return NULL; |
| 515 } |
| 516 |
326 } // namespace chromeos | 517 } // namespace chromeos |
OLD | NEW |