Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/syncable/nigori_util.h" | |
| 6 | |
| 7 #include <queue> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "chrome/browser/sync/engine/syncer_util.h" | |
| 11 #include "chrome/browser/sync/syncable/syncable.h" | |
| 12 #include "chrome/browser/sync/util/cryptographer.h" | |
| 13 | |
| 14 namespace syncable { | |
| 15 | |
| 16 ModelTypeSet GetEncryptedDataTypesFromSyncer(BaseTransaction* const trans) { | |
|
tim (not reviewing)
2011/02/15 00:10:57
nit - ...FromDirectory or just GetEncryptedDataTyp
Nicolas Zea
2011/02/15 19:52:18
Done.
| |
| 17 std::string nigori_tag = ModelTypeToRootTag(syncable::NIGORI); | |
| 18 Entry entry(trans, GET_BY_SERVER_TAG, nigori_tag); | |
| 19 if (!entry.good()) { | |
| 20 VLOG(1) << "Nigori node not found, assuming no encrypted datatypes."; | |
| 21 return ModelTypeSet(); | |
| 22 } | |
| 23 if (NIGORI != entry.GetModelType()) { | |
| 24 // Can happen if we fail to apply the nigori node due to a conflict. | |
| 25 VLOG(1) << "Nigori node does not have nigori extension. Assuming no" | |
| 26 << " encrypted datatypes."; | |
| 27 return ModelTypeSet(); | |
| 28 } | |
| 29 const sync_pb::EntitySpecifics& specifics = entry.Get(SPECIFICS); | |
| 30 return GetEncryptedDataTypesFromNigori( | |
| 31 specifics.GetExtension(sync_pb::nigori)); | |
| 32 } | |
| 33 | |
| 34 ModelTypeSet GetEncryptedDataTypesFromNigori( | |
| 35 const sync_pb::NigoriSpecifics& nigori) { | |
| 36 // We don't check NIGORI datatype, it uses its own encryption scheme. | |
| 37 ModelTypeSet encrypted_types; | |
| 38 if(nigori.encrypt_bookmarks()) | |
|
Raghu Simha
2011/02/14 05:29:52
This file has a bunch of lint errors:
"Missing spa
Nicolas Zea
2011/02/14 21:18:41
Done.
| |
| 39 encrypted_types.insert(BOOKMARKS); | |
| 40 if(nigori.encrypt_preferences()) | |
| 41 encrypted_types.insert(PREFERENCES); | |
| 42 if(nigori.encrypt_passwords()) | |
| 43 encrypted_types.insert(PASSWORDS); | |
| 44 if(nigori.encrypt_autofill_profile()) | |
| 45 encrypted_types.insert(AUTOFILL_PROFILE); | |
| 46 if(nigori.encrypt_autofill()) | |
| 47 encrypted_types.insert(AUTOFILL); | |
| 48 if(nigori.encrypt_themes()) | |
| 49 encrypted_types.insert(THEMES); | |
| 50 if(nigori.encrypt_typed_urls()) | |
| 51 encrypted_types.insert(TYPED_URLS); | |
| 52 if(nigori.encrypt_extensions()) | |
| 53 encrypted_types.insert(EXTENSIONS); | |
| 54 if(nigori.encrypt_sessions()) | |
| 55 encrypted_types.insert(SESSIONS); | |
| 56 if(nigori.encrypt_apps()) | |
| 57 encrypted_types.insert(APPS); | |
| 58 return encrypted_types; | |
| 59 } | |
| 60 | |
| 61 void FillNigoriEncryptedTypes(const ModelTypeSet& types, | |
| 62 sync_pb::NigoriSpecifics* nigori) { | |
| 63 DCHECK(nigori); | |
| 64 nigori->set_encrypt_bookmarks(types.count(BOOKMARKS) > 0); | |
| 65 nigori->set_encrypt_preferences(types.count(PREFERENCES) > 0); | |
| 66 nigori->set_encrypt_passwords(types.count(PASSWORDS) > 0); | |
| 67 nigori->set_encrypt_autofill_profile(types.count(AUTOFILL_PROFILE) > 0); | |
| 68 nigori->set_encrypt_autofill(types.count(AUTOFILL) > 0); | |
| 69 nigori->set_encrypt_themes(types.count(THEMES) > 0); | |
| 70 nigori->set_encrypt_typed_urls(types.count(TYPED_URLS) > 0); | |
| 71 nigori->set_encrypt_extensions(types.count(EXTENSIONS) > 0); | |
| 72 nigori->set_encrypt_sessions(types.count(SESSIONS) > 0); | |
| 73 nigori->set_encrypt_apps(types.count(APPS) > 0); | |
| 74 } | |
| 75 | |
| 76 bool ProcessUnsyncedChangesForEncryption( | |
| 77 WriteTransaction* const trans, | |
| 78 const ModelTypeSet& encrypted_types, | |
| 79 browser_sync::Cryptographer* cryptographer) { | |
| 80 // Get list of all datatypes with unsynced changes. It's possible that our | |
| 81 // local changes need to be encrypted if encryption for that datatype was | |
| 82 // just turned on (and vice versa). This should never affect passwords. | |
| 83 std::vector<int64> handles; | |
| 84 browser_sync::SyncerUtil::GetUnsyncedEntries(trans, &handles); | |
| 85 for (size_t i = 0; i < handles.size(); ++i) { | |
| 86 MutableEntry entry(trans, GET_BY_HANDLE, handles[i]); | |
| 87 sync_pb::EntitySpecifics new_specifics; | |
| 88 const sync_pb::EntitySpecifics& entry_specifics = entry.Get(SPECIFICS); | |
| 89 ModelType type = entry.GetModelType(); | |
| 90 if (type == PASSWORDS) | |
| 91 continue; | |
| 92 if (encrypted_types.count(type) > 0 && | |
| 93 !entry_specifics.has_encrypted()) { | |
| 94 // This entry now requires encryption. | |
| 95 AddDefaultExtensionValue(type, &new_specifics); | |
| 96 if (!cryptographer->Encrypt( | |
| 97 entry_specifics, | |
| 98 new_specifics.mutable_encrypted())) { | |
| 99 LOG(ERROR) << "Could not encrypt data for newly encrypted type " << | |
| 100 ModelTypeToString(type); | |
| 101 NOTREACHED(); | |
| 102 return false; | |
| 103 } else { | |
| 104 VLOG(1) << "Encrypted change for newly encrypted type " << | |
| 105 ModelTypeToString(type); | |
| 106 entry.Put(SPECIFICS, new_specifics); | |
| 107 } | |
| 108 } else if (encrypted_types.count(type) == 0 && | |
| 109 entry_specifics.has_encrypted()) { | |
| 110 // This entry no longer requires encryption. | |
| 111 if (!cryptographer->Decrypt(entry_specifics.encrypted(), | |
| 112 &new_specifics)) { | |
| 113 LOG(ERROR) << "Could not decrypt data for newly unencrypted type " << | |
| 114 ModelTypeToString(type); | |
| 115 NOTREACHED(); | |
| 116 return false; | |
| 117 } else { | |
| 118 VLOG(1) << "Decrypted change for newly unencrypted type " << | |
| 119 ModelTypeToString(type); | |
| 120 entry.Put(SPECIFICS, new_specifics); | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 bool VerifyUnsyncedChangesAreEncrypted( | |
| 128 BaseTransaction* const trans, | |
| 129 const ModelTypeSet& encrypted_types) { | |
| 130 std::vector<int64> handles; | |
| 131 browser_sync::SyncerUtil::GetUnsyncedEntries(trans, &handles); | |
| 132 for (size_t i = 0; i < handles.size(); ++i) { | |
| 133 Entry entry(trans, GET_BY_HANDLE, handles[i]); | |
| 134 if (!entry.good()) { | |
| 135 NOTREACHED(); | |
| 136 return false; | |
| 137 } | |
| 138 const sync_pb::EntitySpecifics& entry_specifics = entry.Get(SPECIFICS); | |
| 139 ModelType type = entry.GetModelType(); | |
| 140 if (type == PASSWORDS) | |
| 141 continue; | |
| 142 if (encrypted_types.count(type) > 0 && | |
| 143 !entry_specifics.has_encrypted()) { | |
| 144 // This datatype requires encryption but this data is not encrypted. | |
| 145 return false; | |
| 146 } | |
| 147 } | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 // Mainly for testing. | |
| 152 bool VerifyDataTypeEncryption(BaseTransaction* const trans, | |
| 153 ModelType type, | |
| 154 bool is_encrypted) { | |
| 155 if(type == PASSWORDS || type == NIGORI) { | |
| 156 NOTREACHED(); | |
| 157 return true; | |
| 158 } | |
| 159 std::string type_tag = ModelTypeToRootTag(type); | |
| 160 Entry type_root(trans, GET_BY_SERVER_TAG, type_tag); | |
| 161 if (!type_root.good()) { | |
| 162 NOTREACHED(); | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 std::queue<Id> to_visit; | |
| 167 Id id_string = | |
| 168 trans->directory()->GetFirstChildId(trans, type_root.Get(ID)); | |
| 169 to_visit.push(id_string); | |
| 170 while (!to_visit.empty()) { | |
| 171 id_string = to_visit.front(); | |
| 172 to_visit.pop(); | |
| 173 if (id_string.IsRoot()) | |
| 174 continue; | |
| 175 | |
| 176 Entry child(trans, GET_BY_ID, id_string); | |
| 177 if (!child.good()) { | |
| 178 NOTREACHED(); | |
| 179 return false; | |
| 180 } | |
| 181 if (child.Get(IS_DIR)) { | |
| 182 // Traverse the children. | |
| 183 to_visit.push( | |
| 184 trans->directory()->GetFirstChildId(trans, child.Get(ID))); | |
| 185 } else { | |
| 186 const sync_pb::EntitySpecifics& specifics = child.Get(SPECIFICS); | |
| 187 DCHECK_EQ(type, child.GetModelType()); | |
| 188 DCHECK_EQ(type, GetModelTypeFromSpecifics(specifics)); | |
| 189 if (specifics.has_encrypted() != is_encrypted) | |
| 190 return false; | |
| 191 } | |
| 192 // Push the successor. | |
| 193 to_visit.push(child.Get(NEXT_ID)); | |
| 194 } | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 } // namespace syncable | |
| OLD | NEW |