| OLD | NEW |
| 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 "sync/syncable/nigori_util.h" | 5 #include "sync/syncable/nigori_util.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "sync/syncable/directory.h" | 12 #include "sync/syncable/directory.h" |
| 13 #include "sync/syncable/entry.h" | 13 #include "sync/syncable/entry.h" |
| 14 #include "sync/syncable/nigori_handler.h" |
| 14 #include "sync/syncable/mutable_entry.h" | 15 #include "sync/syncable/mutable_entry.h" |
| 15 #include "sync/syncable/syncable_util.h" | 16 #include "sync/syncable/syncable_util.h" |
| 16 #include "sync/syncable/write_transaction.h" | 17 #include "sync/syncable/write_transaction.h" |
| 17 #include "sync/util/cryptographer.h" | 18 #include "sync/util/cryptographer.h" |
| 18 | 19 |
| 19 namespace syncer { | 20 namespace syncer { |
| 20 namespace syncable { | 21 namespace syncable { |
| 21 | 22 |
| 22 bool ProcessUnsyncedChangesForEncryption( | 23 bool ProcessUnsyncedChangesForEncryption( |
| 23 WriteTransaction* const trans, | 24 WriteTransaction* const trans) { |
| 24 Cryptographer* cryptographer) { | 25 NigoriHandler* nigori_handler = trans->directory()->GetNigoriHandler(); |
| 26 ModelTypeSet encrypted_types = nigori_handler->GetEncryptedTypes(trans); |
| 27 Cryptographer* cryptographer = trans->directory()->GetCryptographer(trans); |
| 25 DCHECK(cryptographer->is_ready()); | 28 DCHECK(cryptographer->is_ready()); |
| 29 |
| 26 // Get list of all datatypes with unsynced changes. It's possible that our | 30 // Get list of all datatypes with unsynced changes. It's possible that our |
| 27 // local changes need to be encrypted if encryption for that datatype was | 31 // local changes need to be encrypted if encryption for that datatype was |
| 28 // just turned on (and vice versa). | 32 // just turned on (and vice versa). |
| 29 // Note: we do not attempt to re-encrypt data with a new key here as key | 33 // Note: we do not attempt to re-encrypt data with a new key here as key |
| 30 // changes in this code path are likely due to consistency issues (we have | 34 // changes in this code path are likely due to consistency issues (we have |
| 31 // to be updated to a key we already have, e.g. an old key). | 35 // to be updated to a key we already have, e.g. an old key). |
| 32 std::vector<int64> handles; | 36 std::vector<int64> handles; |
| 33 GetUnsyncedEntries(trans, &handles); | 37 GetUnsyncedEntries(trans, &handles); |
| 34 for (size_t i = 0; i < handles.size(); ++i) { | 38 for (size_t i = 0; i < handles.size(); ++i) { |
| 35 MutableEntry entry(trans, GET_BY_HANDLE, handles[i]); | 39 MutableEntry entry(trans, GET_BY_HANDLE, handles[i]); |
| 36 const sync_pb::EntitySpecifics& specifics = entry.Get(SPECIFICS); | 40 const sync_pb::EntitySpecifics& specifics = entry.Get(SPECIFICS); |
| 37 // Ignore types that don't need encryption or entries that are already | 41 // Ignore types that don't need encryption or entries that are already |
| 38 // encrypted. | 42 // encrypted. |
| 39 if (!SpecificsNeedsEncryption(cryptographer->GetEncryptedTypes(), | 43 if (!SpecificsNeedsEncryption(encrypted_types, specifics)) |
| 40 specifics)) { | |
| 41 continue; | 44 continue; |
| 42 } | 45 if (!UpdateEntryWithEncryption(trans, specifics, &entry)) |
| 43 if (!UpdateEntryWithEncryption(cryptographer, specifics, &entry)) { | |
| 44 NOTREACHED(); | |
| 45 return false; | 46 return false; |
| 46 } | |
| 47 } | 47 } |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool VerifyUnsyncedChangesAreEncrypted( | 51 bool VerifyUnsyncedChangesAreEncrypted( |
| 52 BaseTransaction* const trans, | 52 BaseTransaction* const trans, |
| 53 ModelTypeSet encrypted_types) { | 53 ModelTypeSet encrypted_types) { |
| 54 std::vector<int64> handles; | 54 std::vector<int64> handles; |
| 55 GetUnsyncedEntries(trans, &handles); | 55 GetUnsyncedEntries(trans, &handles); |
| 56 for (size_t i = 0; i < handles.size(); ++i) { | 56 for (size_t i = 0; i < handles.size(); ++i) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 86 if (type == PASSWORDS || type == NIGORI) | 86 if (type == PASSWORDS || type == NIGORI) |
| 87 return false; // These types have their own encryption schemes. | 87 return false; // These types have their own encryption schemes. |
| 88 if (!encrypted_types.Has(type)) | 88 if (!encrypted_types.Has(type)) |
| 89 return false; // This type does not require encryption | 89 return false; // This type does not require encryption |
| 90 return !specifics.has_encrypted(); | 90 return !specifics.has_encrypted(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Mainly for testing. | 93 // Mainly for testing. |
| 94 bool VerifyDataTypeEncryptionForTest( | 94 bool VerifyDataTypeEncryptionForTest( |
| 95 BaseTransaction* const trans, | 95 BaseTransaction* const trans, |
| 96 Cryptographer* cryptographer, | |
| 97 ModelType type, | 96 ModelType type, |
| 98 bool is_encrypted) { | 97 bool is_encrypted) { |
| 98 Cryptographer* cryptographer = trans->directory()->GetCryptographer(trans); |
| 99 if (type == PASSWORDS || type == NIGORI) { | 99 if (type == PASSWORDS || type == NIGORI) { |
| 100 NOTREACHED(); | 100 NOTREACHED(); |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 std::string type_tag = ModelTypeToRootTag(type); | 103 std::string type_tag = ModelTypeToRootTag(type); |
| 104 Entry type_root(trans, GET_BY_SERVER_TAG, type_tag); | 104 Entry type_root(trans, GET_BY_SERVER_TAG, type_tag); |
| 105 if (!type_root.good()) { | 105 if (!type_root.good()) { |
| 106 NOTREACHED(); | 106 NOTREACHED(); |
| 107 return false; | 107 return false; |
| 108 } | 108 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 return false; | 150 return false; |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 // Push the successor. | 153 // Push the successor. |
| 154 to_visit.push(child.Get(NEXT_ID)); | 154 to_visit.push(child.Get(NEXT_ID)); |
| 155 } | 155 } |
| 156 return true; | 156 return true; |
| 157 } | 157 } |
| 158 | 158 |
| 159 bool UpdateEntryWithEncryption( | 159 bool UpdateEntryWithEncryption( |
| 160 Cryptographer* cryptographer, | 160 BaseTransaction* const trans, |
| 161 const sync_pb::EntitySpecifics& new_specifics, | 161 const sync_pb::EntitySpecifics& new_specifics, |
| 162 syncable::MutableEntry* entry) { | 162 syncable::MutableEntry* entry) { |
| 163 NigoriHandler* nigori_handler = trans->directory()->GetNigoriHandler(); |
| 164 Cryptographer* cryptographer = trans->directory()->GetCryptographer(trans); |
| 163 ModelType type = GetModelTypeFromSpecifics(new_specifics); | 165 ModelType type = GetModelTypeFromSpecifics(new_specifics); |
| 164 DCHECK_GE(type, FIRST_REAL_MODEL_TYPE); | 166 DCHECK_GE(type, FIRST_REAL_MODEL_TYPE); |
| 165 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); | 167 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); |
| 166 const ModelTypeSet encrypted_types = cryptographer->GetEncryptedTypes(); | 168 const ModelTypeSet encrypted_types = nigori_handler->GetEncryptedTypes(trans); |
| 167 // It's possible the nigori lost the set of encrypted types. If the current | 169 // It's possible the nigori lost the set of encrypted types. If the current |
| 168 // specifics are already encrypted, we want to ensure we continue encrypting. | 170 // specifics are already encrypted, we want to ensure we continue encrypting. |
| 169 bool was_encrypted = old_specifics.has_encrypted(); | 171 bool was_encrypted = old_specifics.has_encrypted(); |
| 170 sync_pb::EntitySpecifics generated_specifics; | 172 sync_pb::EntitySpecifics generated_specifics; |
| 171 if (new_specifics.has_encrypted()) { | 173 if (new_specifics.has_encrypted()) { |
| 172 NOTREACHED() << "New specifics already has an encrypted blob."; | 174 NOTREACHED() << "New specifics already has an encrypted blob."; |
| 173 return false; | 175 return false; |
| 174 } | 176 } |
| 175 if ((!SpecificsNeedsEncryption(encrypted_types, new_specifics) && | 177 if ((!SpecificsNeedsEncryption(encrypted_types, new_specifics) && |
| 176 !was_encrypted) || | 178 !was_encrypted) || |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 encrypted_types.Put(APP_SETTINGS); | 308 encrypted_types.Put(APP_SETTINGS); |
| 307 if (nigori.encrypt_apps()) | 309 if (nigori.encrypt_apps()) |
| 308 encrypted_types.Put(APPS); | 310 encrypted_types.Put(APPS); |
| 309 if (nigori.encrypt_app_notifications()) | 311 if (nigori.encrypt_app_notifications()) |
| 310 encrypted_types.Put(APP_NOTIFICATIONS); | 312 encrypted_types.Put(APP_NOTIFICATIONS); |
| 311 return encrypted_types; | 313 return encrypted_types; |
| 312 } | 314 } |
| 313 | 315 |
| 314 } // namespace syncable | 316 } // namespace syncable |
| 315 } // namespace syncer | 317 } // namespace syncer |
| OLD | NEW |