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