Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: chrome/browser/policy/user_cloud_policy_store_chromeos.cc

Issue 12183017: Verify the signature on user cloud policy downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/policy/user_cloud_policy_store_chromeos.h" 5 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h"
6 6
7 #include <string>
8
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
11 #include "base/callback.h" 9 #include "base/callback.h"
12 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/logging.h"
13 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/stl_util.h"
14 #include "base/stringprintf.h"
14 #include "chrome/browser/policy/proto/cloud_policy.pb.h" 15 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
15 #include "chrome/browser/policy/proto/device_management_local.pb.h" 16 #include "chrome/browser/policy/proto/device_management_local.pb.h"
16 #include "chrome/browser/policy/user_policy_disk_cache.h" 17 #include "chrome/browser/policy/user_policy_disk_cache.h"
17 #include "chrome/browser/policy/user_policy_token_loader.h" 18 #include "chrome/browser/policy/user_policy_token_loader.h"
19 #include "chromeos/dbus/cryptohome_client.h"
18 #include "chromeos/dbus/session_manager_client.h" 20 #include "chromeos/dbus/session_manager_client.h"
19 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
20 #include "google_apis/gaia/gaia_auth_util.h" 22 #include "google_apis/gaia/gaia_auth_util.h"
21 23
22 namespace em = enterprise_management; 24 namespace em = enterprise_management;
23 25
24 namespace policy { 26 namespace policy {
25 27
26 namespace { 28 namespace {
27 // Subdirectory in the user's profile for storing user policies. 29
28 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management"); 30 // Path within |user_policy_key_dir_| that contains the policy key.
29 // File in the above directory for stroing user policy dmtokens. 31 // "%s" must be substituted with the sanitized username.
30 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token"); 32 const FilePath::CharType kPolicyKeyFile[] = FILE_PATH_LITERAL("%s/policy.pub");
31 // File in the above directory for storing user policy data. 33
32 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy"); 34 // Maximum key size that will be loaded, in bytes.
35 const int kKeySizeLimit = 16 * 1024;
36
33 } // namespace 37 } // namespace
34 38
35 39
36 // Helper class for loading legacy policy caches. 40 // Helper class for loading legacy policy caches.
37 class LegacyPolicyCacheLoader : public UserPolicyTokenLoader::Delegate, 41 class LegacyPolicyCacheLoader : public UserPolicyTokenLoader::Delegate,
38 public UserPolicyDiskCache::Delegate { 42 public UserPolicyDiskCache::Delegate {
39 public: 43 public:
40 typedef base::Callback<void(const std::string&, 44 typedef base::Callback<void(const std::string&,
41 const std::string&, 45 const std::string&,
42 CloudPolicyStore::Status, 46 CloudPolicyStore::Status,
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 return CloudPolicyStore::STATUS_OK; 143 return CloudPolicyStore::STATUS_OK;
140 case UserPolicyDiskCache::LOAD_RESULT_PARSE_ERROR: 144 case UserPolicyDiskCache::LOAD_RESULT_PARSE_ERROR:
141 case UserPolicyDiskCache::LOAD_RESULT_READ_ERROR: 145 case UserPolicyDiskCache::LOAD_RESULT_READ_ERROR:
142 return CloudPolicyStore::STATUS_LOAD_ERROR; 146 return CloudPolicyStore::STATUS_LOAD_ERROR;
143 } 147 }
144 NOTREACHED(); 148 NOTREACHED();
145 return CloudPolicyStore::STATUS_OK; 149 return CloudPolicyStore::STATUS_OK;
146 } 150 }
147 151
148 UserCloudPolicyStoreChromeOS::UserCloudPolicyStoreChromeOS( 152 UserCloudPolicyStoreChromeOS::UserCloudPolicyStoreChromeOS(
153 chromeos::CryptohomeClient* cryptohome_client,
149 chromeos::SessionManagerClient* session_manager_client, 154 chromeos::SessionManagerClient* session_manager_client,
150 const std::string& username, 155 const std::string& username,
156 const FilePath& user_policy_key_dir,
151 const FilePath& legacy_token_cache_file, 157 const FilePath& legacy_token_cache_file,
152 const FilePath& legacy_policy_cache_file) 158 const FilePath& legacy_policy_cache_file)
153 : session_manager_client_(session_manager_client), 159 : cryptohome_client_(cryptohome_client),
160 session_manager_client_(session_manager_client),
154 username_(username), 161 username_(username),
162 user_policy_key_dir_(user_policy_key_dir),
155 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 163 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
156 legacy_cache_dir_(legacy_token_cache_file.DirName()), 164 legacy_cache_dir_(legacy_token_cache_file.DirName()),
157 legacy_loader_(new LegacyPolicyCacheLoader(legacy_token_cache_file, 165 legacy_loader_(new LegacyPolicyCacheLoader(legacy_token_cache_file,
158 legacy_policy_cache_file)), 166 legacy_policy_cache_file)),
159 legacy_caches_loaded_(false) {} 167 legacy_caches_loaded_(false),
168 policy_key_loaded_(false) {}
160 169
161 UserCloudPolicyStoreChromeOS::~UserCloudPolicyStoreChromeOS() {} 170 UserCloudPolicyStoreChromeOS::~UserCloudPolicyStoreChromeOS() {}
162 171
163 void UserCloudPolicyStoreChromeOS::Store( 172 void UserCloudPolicyStoreChromeOS::Store(
164 const em::PolicyFetchResponse& policy) { 173 const em::PolicyFetchResponse& policy) {
165 // Cancel all pending requests. 174 // Cancel all pending requests.
166 weak_factory_.InvalidateWeakPtrs(); 175 weak_factory_.InvalidateWeakPtrs();
167 Validate( 176 scoped_ptr<em::PolicyFetchResponse> response(
168 scoped_ptr<em::PolicyFetchResponse>(new em::PolicyFetchResponse(policy)), 177 new em::PolicyFetchResponse(policy));
169 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyToStoreValidated, 178 EnsurePolicyKeyLoaded(
170 weak_factory_.GetWeakPtr())); 179 base::Bind(&UserCloudPolicyStoreChromeOS::ValidatePolicyForStore,
180 weak_factory_.GetWeakPtr(),
181 base::Passed(&response)));
171 } 182 }
172 183
173 void UserCloudPolicyStoreChromeOS::Load() { 184 void UserCloudPolicyStoreChromeOS::Load() {
174 // Cancel all pending requests. 185 // Cancel all pending requests.
175 weak_factory_.InvalidateWeakPtrs(); 186 weak_factory_.InvalidateWeakPtrs();
176 session_manager_client_->RetrieveUserPolicy( 187 session_manager_client_->RetrieveUserPolicy(
177 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyRetrieved, 188 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyRetrieved,
178 weak_factory_.GetWeakPtr())); 189 weak_factory_.GetWeakPtr()));
179 } 190 }
180 191
192 void UserCloudPolicyStoreChromeOS::ValidatePolicyForStore(
193 scoped_ptr<em::PolicyFetchResponse> policy) {
194 // Create and configure a validator.
195 scoped_ptr<UserCloudPolicyValidator> validator =
196 CreateValidator(policy.Pass());
197 validator->ValidateUsername(username_);
198 if (policy_key_.empty()) {
199 validator->ValidateInitialKey();
200 } else {
201 const bool allow_rotation = true;
202 validator->ValidateSignature(policy_key_, allow_rotation);
203 }
204
205 // Start validation. The Validator will delete itself once validation is
206 // complete.
207 validator.release()->StartValidation(
208 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyToStoreValidated,
209 weak_factory_.GetWeakPtr()));
210 }
211
212 void UserCloudPolicyStoreChromeOS::OnPolicyToStoreValidated(
213 UserCloudPolicyValidator* validator) {
214 validation_status_ = validator->status();
215 if (!validator->success()) {
216 status_ = STATUS_VALIDATION_ERROR;
217 NotifyStoreError();
218 return;
219 }
220
221 std::string policy_blob;
222 if (!validator->policy()->SerializeToString(&policy_blob)) {
223 status_ = STATUS_SERIALIZE_ERROR;
224 NotifyStoreError();
225 return;
226 }
227
228 session_manager_client_->StoreUserPolicy(
229 policy_blob,
230 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyStored,
231 weak_factory_.GetWeakPtr()));
232 }
233
234 void UserCloudPolicyStoreChromeOS::OnPolicyStored(bool success) {
235 if (!success) {
236 status_ = STATUS_STORE_ERROR;
237 NotifyStoreError();
238 } else {
239 // Load the policy right after storing it, to make sure it was accepted by
240 // the session manager. An additional validation is performed after the
241 // load; reload the key for that validation too, in case it was rotated.
242 ReloadPolicyKey(base::Bind(&UserCloudPolicyStoreChromeOS::Load,
243 weak_factory_.GetWeakPtr()));
244 }
245 }
246
181 void UserCloudPolicyStoreChromeOS::OnPolicyRetrieved( 247 void UserCloudPolicyStoreChromeOS::OnPolicyRetrieved(
182 const std::string& policy_blob) { 248 const std::string& policy_blob) {
183 if (policy_blob.empty()) { 249 if (policy_blob.empty()) {
184 // Policy fetch failed. Try legacy caches if we haven't done that already. 250 // Policy fetch failed. Try legacy caches if we haven't done that already.
185 if (!legacy_caches_loaded_ && legacy_loader_.get()) { 251 if (!legacy_caches_loaded_ && legacy_loader_.get()) {
186 legacy_caches_loaded_ = true; 252 legacy_caches_loaded_ = true;
187 legacy_loader_->Load( 253 legacy_loader_->Load(
188 base::Bind(&UserCloudPolicyStoreChromeOS::OnLegacyLoadFinished, 254 base::Bind(&UserCloudPolicyStoreChromeOS::OnLegacyLoadFinished,
189 weak_factory_.GetWeakPtr())); 255 weak_factory_.GetWeakPtr()));
190 } else { 256 } else {
191 // session_manager doesn't have policy. Adjust internal state and notify 257 // session_manager doesn't have policy. Adjust internal state and notify
192 // the world about the policy update. 258 // the world about the policy update.
193 policy_.reset(); 259 policy_.reset();
194 NotifyStoreLoaded(); 260 NotifyStoreLoaded();
195 } 261 }
196 return; 262 return;
197 } 263 }
198 264
199 // Policy is supplied by session_manager. Disregard legacy data from now on. 265 // Policy is supplied by session_manager. Disregard legacy data from now on.
200 legacy_loader_.reset(); 266 legacy_loader_.reset();
201 267
202 scoped_ptr<em::PolicyFetchResponse> policy(new em::PolicyFetchResponse()); 268 scoped_ptr<em::PolicyFetchResponse> policy(new em::PolicyFetchResponse());
203 if (!policy->ParseFromString(policy_blob)) { 269 if (!policy->ParseFromString(policy_blob)) {
204 status_ = STATUS_PARSE_ERROR; 270 status_ = STATUS_PARSE_ERROR;
205 NotifyStoreError(); 271 NotifyStoreError();
206 return; 272 return;
207 } 273 }
208 274
209 Validate(policy.Pass(), 275 // Load |policy_key_| to verify the loaded policy.
210 base::Bind(&UserCloudPolicyStoreChromeOS::OnRetrievedPolicyValidated, 276 EnsurePolicyKeyLoaded(
211 weak_factory_.GetWeakPtr())); 277 base::Bind(&UserCloudPolicyStoreChromeOS::ValidateRetrievedPolicy,
278 weak_factory_.GetWeakPtr(),
279 base::Passed(&policy)));
280 }
281
282 void UserCloudPolicyStoreChromeOS::ValidateRetrievedPolicy(
283 scoped_ptr<em::PolicyFetchResponse> policy) {
284 // Create and configure a validator for the loaded policy.
285 scoped_ptr<UserCloudPolicyValidator> validator =
286 CreateValidator(policy.Pass());
287 validator->ValidateUsername(username_);
288 const bool allow_rotation = false;
289 validator->ValidateSignature(policy_key_, allow_rotation);
290 // Start validation. The Validator will delete itself once validation is
291 // complete.
292 validator.release()->StartValidation(
293 base::Bind(&UserCloudPolicyStoreChromeOS::OnRetrievedPolicyValidated,
294 weak_factory_.GetWeakPtr()));
212 } 295 }
213 296
214 void UserCloudPolicyStoreChromeOS::OnRetrievedPolicyValidated( 297 void UserCloudPolicyStoreChromeOS::OnRetrievedPolicyValidated(
215 UserCloudPolicyValidator* validator) { 298 UserCloudPolicyValidator* validator) {
216 validation_status_ = validator->status(); 299 validation_status_ = validator->status();
217 if (!validator->success()) { 300 if (!validator->success()) {
218 status_ = STATUS_VALIDATION_ERROR; 301 status_ = STATUS_VALIDATION_ERROR;
219 NotifyStoreError(); 302 NotifyStoreError();
220 return; 303 return;
221 } 304 }
222 305
223 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); 306 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass());
224 status_ = STATUS_OK; 307 status_ = STATUS_OK;
225 308
226 // Policy has been loaded successfully. This indicates that new-style policy 309 // Policy has been loaded successfully. This indicates that new-style policy
227 // is working, so the legacy cache directory can be removed. 310 // is working, so the legacy cache directory can be removed.
228 if (!legacy_cache_dir_.empty()) { 311 if (!legacy_cache_dir_.empty()) {
229 content::BrowserThread::PostBlockingPoolTask( 312 content::BrowserThread::PostBlockingPoolTask(
230 FROM_HERE, 313 FROM_HERE,
231 base::Bind(&UserCloudPolicyStoreChromeOS::RemoveLegacyCacheDir, 314 base::Bind(&UserCloudPolicyStoreChromeOS::RemoveLegacyCacheDir,
232 legacy_cache_dir_)); 315 legacy_cache_dir_));
233 legacy_cache_dir_.clear(); 316 legacy_cache_dir_.clear();
234 } 317 }
235 NotifyStoreLoaded(); 318 NotifyStoreLoaded();
236 } 319 }
237 320
238 void UserCloudPolicyStoreChromeOS::OnPolicyToStoreValidated(
239 UserCloudPolicyValidator* validator) {
240 validation_status_ = validator->status();
241 if (!validator->success()) {
242 status_ = STATUS_VALIDATION_ERROR;
243 NotifyStoreError();
244 return;
245 }
246
247 std::string policy_blob;
248 if (!validator->policy()->SerializeToString(&policy_blob)) {
249 status_ = STATUS_SERIALIZE_ERROR;
250 NotifyStoreError();
251 return;
252 }
253
254 session_manager_client_->StoreUserPolicy(
255 policy_blob,
256 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyStored,
257 weak_factory_.GetWeakPtr()));
258 }
259
260 void UserCloudPolicyStoreChromeOS::OnPolicyStored(bool success) {
261 if (!success) {
262 status_ = STATUS_STORE_ERROR;
263 NotifyStoreError();
264 } else {
265 // TODO(mnissler): Once we do signature verifications, we'll have to reload
266 // the key at this point to account for key rotations.
267 Load();
268 }
269 }
270
271 void UserCloudPolicyStoreChromeOS::Validate(
272 scoped_ptr<em::PolicyFetchResponse> policy,
273 const UserCloudPolicyValidator::CompletionCallback& callback) {
274 // Configure the validator.
275 scoped_ptr<UserCloudPolicyValidator> validator =
276 CreateValidator(policy.Pass());
277 validator->ValidateUsername(username_);
278
279 // TODO(mnissler): Do a signature check here as well. The key is stored by
280 // session_manager in the root-owned cryptohome area, which is currently
281 // inaccessible to Chrome though.
282
283 // Start validation. The Validator will free itself once validation is
284 // complete.
285 validator.release()->StartValidation(callback);
286 }
287
288 void UserCloudPolicyStoreChromeOS::OnLegacyLoadFinished( 321 void UserCloudPolicyStoreChromeOS::OnLegacyLoadFinished(
289 const std::string& dm_token, 322 const std::string& dm_token,
290 const std::string& device_id, 323 const std::string& device_id,
291 Status status, 324 Status status,
292 scoped_ptr<em::PolicyFetchResponse> policy) { 325 scoped_ptr<em::PolicyFetchResponse> policy) {
293 status_ = status; 326 status_ = status;
294 if (policy.get()) { 327 if (policy.get()) {
295 Validate(policy.Pass(), 328 // Create and configure a validator for the loaded legacy policy. Note that
296 base::Bind(&UserCloudPolicyStoreChromeOS::OnLegacyPolicyValidated, 329 // the signature on this policy is not verified.
297 weak_factory_.GetWeakPtr(), 330 scoped_ptr<UserCloudPolicyValidator> validator =
298 dm_token, device_id)); 331 CreateValidator(policy.Pass());
332 validator->ValidateUsername(username_);
333 validator.release()->StartValidation(
334 base::Bind(&UserCloudPolicyStoreChromeOS::OnLegacyPolicyValidated,
335 weak_factory_.GetWeakPtr(),
336 dm_token,
337 device_id));
299 } else { 338 } else {
300 InstallLegacyTokens(dm_token, device_id); 339 InstallLegacyTokens(dm_token, device_id);
301 } 340 }
302 } 341 }
303 342
304 void UserCloudPolicyStoreChromeOS::OnLegacyPolicyValidated( 343 void UserCloudPolicyStoreChromeOS::OnLegacyPolicyValidated(
305 const std::string& dm_token, 344 const std::string& dm_token,
306 const std::string& device_id, 345 const std::string& device_id,
307 UserCloudPolicyValidator* validator) { 346 UserCloudPolicyValidator* validator) {
308 validation_status_ = validator->status(); 347 validation_status_ = validator->status();
309 if (validator->success()) { 348 if (validator->success()) {
310 status_ = STATUS_OK; 349 status_ = STATUS_OK;
311 InstallPolicy(validator->policy_data().Pass(), 350 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass());
312 validator->payload().Pass());
313 351
314 // Clear the public key version. The public key version field would 352 // Clear the public key version. The public key version field would
315 // otherwise indicate that we have key installed in the store when in fact 353 // otherwise indicate that we have key installed in the store when in fact
316 // we haven't. This may result in policy updates failing signature 354 // we haven't. This may result in policy updates failing signature
317 // verification. 355 // verification.
318 policy_->clear_public_key_version(); 356 policy_->clear_public_key_version();
319 } else { 357 } else {
320 status_ = STATUS_VALIDATION_ERROR; 358 status_ = STATUS_VALIDATION_ERROR;
321 } 359 }
322 360
(...skipping 16 matching lines...) Expand all
339 // Tell the rest of the world that the policy load completed. 377 // Tell the rest of the world that the policy load completed.
340 NotifyStoreLoaded(); 378 NotifyStoreLoaded();
341 } 379 }
342 380
343 // static 381 // static
344 void UserCloudPolicyStoreChromeOS::RemoveLegacyCacheDir(const FilePath& dir) { 382 void UserCloudPolicyStoreChromeOS::RemoveLegacyCacheDir(const FilePath& dir) {
345 if (file_util::PathExists(dir) && !file_util::Delete(dir, true)) 383 if (file_util::PathExists(dir) && !file_util::Delete(dir, true))
346 LOG(ERROR) << "Failed to remove cache dir " << dir.value(); 384 LOG(ERROR) << "Failed to remove cache dir " << dir.value();
347 } 385 }
348 386
387 void UserCloudPolicyStoreChromeOS::ReloadPolicyKey(
388 const base::Closure& callback) {
389 std::vector<uint8>* key = new std::vector<uint8>();
390 content::BrowserThread::PostBlockingPoolTaskAndReply(
391 FROM_HERE,
392 base::Bind(&UserCloudPolicyStoreChromeOS::LoadPolicyKey,
393 policy_key_path_,
394 key),
395 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyKeyReloaded,
396 weak_factory_.GetWeakPtr(),
397 base::Owned(key),
398 callback));
399 }
400
401 // static
402 void UserCloudPolicyStoreChromeOS::LoadPolicyKey(const FilePath& path,
403 std::vector<uint8>* key) {
404 if (!file_util::PathExists(path)) {
405 VLOG(1) << "No key at " << path.value();
406 return;
407 }
408
409 int64 size;
410 if (!file_util::GetFileSize(path, &size)) {
411 LOG(ERROR) << "Could not get size of " << path.value();
412 } else if (size == 0 || size > kKeySizeLimit) {
413 LOG(ERROR) << "Key at " << path.value() << " has bad size " << size;
414 } else {
415 key->resize(size);
416 int read_size = file_util::ReadFile(
417 path, reinterpret_cast<char*>(vector_as_array(key)), size);
418 if (read_size != size) {
419 LOG(ERROR) << "Failed to read key at " << path.value();
420 key->clear();
421 }
422 }
423 }
424
425 void UserCloudPolicyStoreChromeOS::OnPolicyKeyReloaded(
426 std::vector<uint8>* key,
427 const base::Closure& callback) {
428 policy_key_.swap(*key);
429 policy_key_loaded_ = true;
430 callback.Run();
431 }
432
433 void UserCloudPolicyStoreChromeOS::EnsurePolicyKeyLoaded(
434 const base::Closure& callback) {
435 if (policy_key_loaded_) {
436 callback.Run();
437 } else {
438 // Get the hashed username that's part of the key's path, to determine
439 // |policy_key_path_|.
440 cryptohome_client_->GetSanitizedUsername(username_,
441 base::Bind(&UserCloudPolicyStoreChromeOS::OnGetSanitizedUsername,
442 weak_factory_.GetWeakPtr(),
443 callback));
444 }
445 }
446
447 void UserCloudPolicyStoreChromeOS::OnGetSanitizedUsername(
448 const base::Closure& callback,
449 chromeos::DBusMethodCallStatus call_status,
450 const std::string& sanitized_username) {
451 // The default empty path will always yield an empty key.
452 if (call_status == chromeos::DBUS_METHOD_CALL_SUCCESS &&
453 !sanitized_username.empty()) {
454 policy_key_path_ = user_policy_key_dir_.Append(
455 base::StringPrintf(kPolicyKeyFile, sanitized_username.c_str()));
456 }
457 ReloadPolicyKey(callback);
458 }
459
349 } // namespace policy 460 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698