OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "chrome/browser/sync/util/cryptographer.h" | 8 #include "chrome/browser/sync/util/cryptographer.h" |
9 #include "chrome/browser/password_manager/encryptor.h" | 9 #include "chrome/browser/password_manager/encryptor.h" |
10 | 10 |
11 namespace browser_sync { | 11 namespace browser_sync { |
12 | 12 |
13 const char kNigoriTag[] = "google_chrome_nigori"; | 13 const char kNigoriTag[] = "google_chrome_nigori"; |
14 | 14 |
15 // We name a particular Nigori instance (ie. a triplet consisting of a hostname, | 15 // We name a particular Nigori instance (ie. a triplet consisting of a hostname, |
16 // a username, and a password) by calling Permute on this string. Since the | 16 // a username, and a password) by calling Permute on this string. Since the |
17 // output of Permute is always the same for a given triplet, clients will always | 17 // output of Permute is always the same for a given triplet, clients will always |
18 // assign the same name to a particular triplet. | 18 // assign the same name to a particular triplet. |
19 const char kNigoriKeyName[] = "nigori-key"; | 19 const char kNigoriKeyName[] = "nigori-key"; |
20 | 20 |
21 Cryptographer::Observer::~Observer() {} | 21 Cryptographer::Observer::~Observer() {} |
22 | 22 |
23 Cryptographer::Cryptographer() | 23 Cryptographer::Cryptographer() |
24 : default_nigori_(NULL), | 24 : default_nigori_(NULL), |
25 encrypt_everything_(false) { | 25 encrypted_types_(SensitiveTypes()), |
26 syncable::ModelTypeSet sensitive_types = SensitiveTypes(); | 26 encrypt_everything_(false) {} |
27 encrypted_types_.insert(sensitive_types.begin(), sensitive_types.end()); | |
28 } | |
29 | 27 |
30 Cryptographer::~Cryptographer() {} | 28 Cryptographer::~Cryptographer() {} |
31 | 29 |
32 void Cryptographer::AddObserver(Observer* observer) { | 30 void Cryptographer::AddObserver(Observer* observer) { |
33 observers_.AddObserver(observer); | 31 observers_.AddObserver(observer); |
34 } | 32 } |
35 | 33 |
36 void Cryptographer::RemoveObserver(Observer* observer) { | 34 void Cryptographer::RemoveObserver(Observer* observer) { |
37 observers_.RemoveObserver(observer); | 35 observers_.RemoveObserver(observer); |
38 } | 36 } |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 return Cryptographer::SUCCESS; | 266 return Cryptographer::SUCCESS; |
269 } else { | 267 } else { |
270 SetPendingKeys(nigori.encrypted()); | 268 SetPendingKeys(nigori.encrypted()); |
271 return Cryptographer::NEEDS_PASSPHRASE; | 269 return Cryptographer::NEEDS_PASSPHRASE; |
272 } | 270 } |
273 } | 271 } |
274 return Cryptographer::SUCCESS; | 272 return Cryptographer::SUCCESS; |
275 } | 273 } |
276 | 274 |
277 // Static | 275 // Static |
278 syncable::ModelTypeSet Cryptographer::SensitiveTypes() { | 276 syncable::ModelEnumSet Cryptographer::SensitiveTypes() { |
279 syncable::ModelTypeSet types; | |
280 // Both of these have their own encryption schemes, but we include them | 277 // Both of these have their own encryption schemes, but we include them |
281 // anyways. | 278 // anyways. |
282 types.insert(syncable::PASSWORDS); | 279 syncable::ModelEnumSet types; |
283 types.insert(syncable::NIGORI); | 280 types.Put(syncable::PASSWORDS); |
| 281 types.Put(syncable::NIGORI); |
284 return types; | 282 return types; |
285 } | 283 } |
286 | 284 |
287 void Cryptographer::UpdateEncryptedTypesFromNigori( | 285 void Cryptographer::UpdateEncryptedTypesFromNigori( |
288 const sync_pb::NigoriSpecifics& nigori) { | 286 const sync_pb::NigoriSpecifics& nigori) { |
289 if (nigori.encrypt_everything()) { | 287 if (nigori.encrypt_everything()) { |
290 set_encrypt_everything(); | 288 set_encrypt_everything(); |
291 return; | 289 return; |
292 } | 290 } |
293 | 291 |
294 syncable::ModelTypeSet encrypted_types(SensitiveTypes()); | 292 syncable::ModelEnumSet encrypted_types(SensitiveTypes()); |
295 if (nigori.encrypt_bookmarks()) | 293 if (nigori.encrypt_bookmarks()) |
296 encrypted_types.insert(syncable::BOOKMARKS); | 294 encrypted_types.Put(syncable::BOOKMARKS); |
297 if (nigori.encrypt_preferences()) | 295 if (nigori.encrypt_preferences()) |
298 encrypted_types.insert(syncable::PREFERENCES); | 296 encrypted_types.Put(syncable::PREFERENCES); |
299 if (nigori.encrypt_autofill_profile()) | 297 if (nigori.encrypt_autofill_profile()) |
300 encrypted_types.insert(syncable::AUTOFILL_PROFILE); | 298 encrypted_types.Put(syncable::AUTOFILL_PROFILE); |
301 if (nigori.encrypt_autofill()) | 299 if (nigori.encrypt_autofill()) |
302 encrypted_types.insert(syncable::AUTOFILL); | 300 encrypted_types.Put(syncable::AUTOFILL); |
303 if (nigori.encrypt_themes()) | 301 if (nigori.encrypt_themes()) |
304 encrypted_types.insert(syncable::THEMES); | 302 encrypted_types.Put(syncable::THEMES); |
305 if (nigori.encrypt_typed_urls()) | 303 if (nigori.encrypt_typed_urls()) |
306 encrypted_types.insert(syncable::TYPED_URLS); | 304 encrypted_types.Put(syncable::TYPED_URLS); |
307 if (nigori.encrypt_extension_settings()) | 305 if (nigori.encrypt_extension_settings()) |
308 encrypted_types.insert(syncable::EXTENSION_SETTINGS); | 306 encrypted_types.Put(syncable::EXTENSION_SETTINGS); |
309 if (nigori.encrypt_extensions()) | 307 if (nigori.encrypt_extensions()) |
310 encrypted_types.insert(syncable::EXTENSIONS); | 308 encrypted_types.Put(syncable::EXTENSIONS); |
311 if (nigori.encrypt_search_engines()) | 309 if (nigori.encrypt_search_engines()) |
312 encrypted_types.insert(syncable::SEARCH_ENGINES); | 310 encrypted_types.Put(syncable::SEARCH_ENGINES); |
313 if (nigori.encrypt_sessions()) | 311 if (nigori.encrypt_sessions()) |
314 encrypted_types.insert(syncable::SESSIONS); | 312 encrypted_types.Put(syncable::SESSIONS); |
315 if (nigori.encrypt_app_settings()) | 313 if (nigori.encrypt_app_settings()) |
316 encrypted_types.insert(syncable::APP_SETTINGS); | 314 encrypted_types.Put(syncable::APP_SETTINGS); |
317 if (nigori.encrypt_apps()) | 315 if (nigori.encrypt_apps()) |
318 encrypted_types.insert(syncable::APPS); | 316 encrypted_types.Put(syncable::APPS); |
319 if (nigori.encrypt_app_notifications()) | 317 if (nigori.encrypt_app_notifications()) |
320 encrypted_types.insert(syncable::APP_NOTIFICATIONS); | 318 encrypted_types.Put(syncable::APP_NOTIFICATIONS); |
321 | 319 |
322 // Note: the initial version with encryption did not support the | 320 // Note: the initial version with encryption did not support the |
323 // encrypt_everything field. If anything more than the sensitive types were | 321 // encrypt_everything field. If anything more than the sensitive types were |
324 // encrypted, it meant we were encrypting everything. | 322 // encrypted, it meant we were encrypting everything. |
325 if (!nigori.has_encrypt_everything() && | 323 if (!nigori.has_encrypt_everything() && |
326 encrypted_types.size() > SensitiveTypes().size()) { | 324 !Difference(encrypted_types, SensitiveTypes()).Empty()) { |
327 set_encrypt_everything(); | 325 set_encrypt_everything(); |
328 return; | 326 return; |
329 } | 327 } |
330 | 328 |
331 MergeEncryptedTypes(encrypted_types); | 329 MergeEncryptedTypes(encrypted_types); |
332 } | 330 } |
333 | 331 |
334 void Cryptographer::UpdateNigoriFromEncryptedTypes( | 332 void Cryptographer::UpdateNigoriFromEncryptedTypes( |
335 sync_pb::NigoriSpecifics* nigori) const { | 333 sync_pb::NigoriSpecifics* nigori) const { |
336 nigori->set_encrypt_everything(encrypt_everything_); | 334 nigori->set_encrypt_everything(encrypt_everything_); |
337 nigori->set_encrypt_bookmarks( | 335 nigori->set_encrypt_bookmarks( |
338 encrypted_types_.count(syncable::BOOKMARKS) > 0); | 336 encrypted_types_.Has(syncable::BOOKMARKS)); |
339 nigori->set_encrypt_preferences( | 337 nigori->set_encrypt_preferences( |
340 encrypted_types_.count(syncable::PREFERENCES) > 0); | 338 encrypted_types_.Has(syncable::PREFERENCES)); |
341 nigori->set_encrypt_autofill_profile( | 339 nigori->set_encrypt_autofill_profile( |
342 encrypted_types_.count(syncable::AUTOFILL_PROFILE) > 0); | 340 encrypted_types_.Has(syncable::AUTOFILL_PROFILE)); |
343 nigori->set_encrypt_autofill(encrypted_types_.count(syncable::AUTOFILL) > 0); | 341 nigori->set_encrypt_autofill(encrypted_types_.Has(syncable::AUTOFILL)); |
344 nigori->set_encrypt_themes(encrypted_types_.count(syncable::THEMES) > 0); | 342 nigori->set_encrypt_themes(encrypted_types_.Has(syncable::THEMES)); |
345 nigori->set_encrypt_typed_urls( | 343 nigori->set_encrypt_typed_urls( |
346 encrypted_types_.count(syncable::TYPED_URLS) > 0); | 344 encrypted_types_.Has(syncable::TYPED_URLS)); |
347 nigori->set_encrypt_extension_settings( | 345 nigori->set_encrypt_extension_settings( |
348 encrypted_types_.count(syncable::EXTENSION_SETTINGS) > 0); | 346 encrypted_types_.Has(syncable::EXTENSION_SETTINGS)); |
349 nigori->set_encrypt_extensions( | 347 nigori->set_encrypt_extensions( |
350 encrypted_types_.count(syncable::EXTENSIONS) > 0); | 348 encrypted_types_.Has(syncable::EXTENSIONS)); |
351 nigori->set_encrypt_search_engines( | 349 nigori->set_encrypt_search_engines( |
352 encrypted_types_.count(syncable::SEARCH_ENGINES) > 0); | 350 encrypted_types_.Has(syncable::SEARCH_ENGINES)); |
353 nigori->set_encrypt_sessions(encrypted_types_.count(syncable::SESSIONS) > 0); | 351 nigori->set_encrypt_sessions(encrypted_types_.Has(syncable::SESSIONS)); |
354 nigori->set_encrypt_app_settings( | 352 nigori->set_encrypt_app_settings( |
355 encrypted_types_.count(syncable::APP_SETTINGS) > 0); | 353 encrypted_types_.Has(syncable::APP_SETTINGS)); |
356 nigori->set_encrypt_apps(encrypted_types_.count(syncable::APPS) > 0); | 354 nigori->set_encrypt_apps(encrypted_types_.Has(syncable::APPS)); |
357 nigori->set_encrypt_app_notifications( | 355 nigori->set_encrypt_app_notifications( |
358 encrypted_types_.count(syncable::APP_NOTIFICATIONS) > 0); | 356 encrypted_types_.Has(syncable::APP_NOTIFICATIONS)); |
359 } | 357 } |
360 | 358 |
361 void Cryptographer::set_encrypt_everything() { | 359 void Cryptographer::set_encrypt_everything() { |
362 if (encrypt_everything_) { | 360 if (encrypt_everything_) { |
363 DCHECK(encrypted_types_ == syncable::GetAllRealModelTypes()); | 361 DCHECK(encrypted_types_.Equals(syncable::ModelEnumSet::All())); |
364 return; | 362 return; |
365 } | 363 } |
366 encrypt_everything_ = true; | 364 encrypt_everything_ = true; |
367 // Change |encrypted_types_| directly to avoid sending more than one | 365 // Change |encrypted_types_| directly to avoid sending more than one |
368 // notification. | 366 // notification. |
369 encrypted_types_ = syncable::GetAllRealModelTypes(); | 367 encrypted_types_ = syncable::ModelEnumSet::All(); |
370 EmitEncryptedTypesChangedNotification(); | 368 EmitEncryptedTypesChangedNotification(); |
371 } | 369 } |
372 | 370 |
373 bool Cryptographer::encrypt_everything() const { | 371 bool Cryptographer::encrypt_everything() const { |
374 return encrypt_everything_; | 372 return encrypt_everything_; |
375 } | 373 } |
376 | 374 |
377 syncable::ModelTypeSet Cryptographer::GetEncryptedTypes() const { | 375 syncable::ModelEnumSet Cryptographer::GetEncryptedTypes() const { |
378 return encrypted_types_; | 376 return encrypted_types_; |
379 } | 377 } |
380 | 378 |
381 void Cryptographer::MergeEncryptedTypesForTest( | 379 void Cryptographer::MergeEncryptedTypesForTest( |
382 const syncable::ModelTypeSet& encrypted_types) { | 380 syncable::ModelEnumSet encrypted_types) { |
383 MergeEncryptedTypes(encrypted_types); | 381 MergeEncryptedTypes(encrypted_types); |
384 } | 382 } |
385 | 383 |
386 void Cryptographer::MergeEncryptedTypes( | 384 void Cryptographer::MergeEncryptedTypes( |
387 const syncable::ModelTypeSet& encrypted_types) { | 385 syncable::ModelEnumSet encrypted_types) { |
388 if (std::includes(encrypted_types_.begin(), encrypted_types_.end(), | 386 if (encrypted_types_.HasAll(encrypted_types)) { |
389 encrypted_types.begin(), encrypted_types.end())) { | |
390 return; | 387 return; |
391 } | 388 } |
392 encrypted_types_ = encrypted_types; | 389 encrypted_types_ = encrypted_types; |
393 EmitEncryptedTypesChangedNotification(); | 390 EmitEncryptedTypesChangedNotification(); |
394 } | 391 } |
395 | 392 |
396 void Cryptographer::EmitEncryptedTypesChangedNotification() { | 393 void Cryptographer::EmitEncryptedTypesChangedNotification() { |
397 FOR_EACH_OBSERVER( | 394 FOR_EACH_OBSERVER( |
398 Observer, observers_, | 395 Observer, observers_, |
399 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_)); | 396 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_)); |
(...skipping 14 matching lines...) Expand all Loading... |
414 continue; | 411 continue; |
415 } | 412 } |
416 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); | 413 nigoris_[key.name()] = make_linked_ptr(new_nigori.release()); |
417 } | 414 } |
418 } | 415 } |
419 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); | 416 DCHECK(nigoris_.end() != nigoris_.find(default_key_name)); |
420 default_nigori_ = &*nigoris_.find(default_key_name); | 417 default_nigori_ = &*nigoris_.find(default_key_name); |
421 } | 418 } |
422 | 419 |
423 } // namespace browser_sync | 420 } // namespace browser_sync |
OLD | NEW |