| 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 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 if (EntryNeedsEncryption(encrypted_types, entry)) | 62 if (EntryNeedsEncryption(encrypted_types, entry)) |
| 63 return false; | 63 return false; |
| 64 } | 64 } |
| 65 return true; | 65 return true; |
| 66 } | 66 } |
| 67 | 67 |
| 68 bool EntryNeedsEncryption(ModelTypeSet encrypted_types, | 68 bool EntryNeedsEncryption(ModelTypeSet encrypted_types, |
| 69 const Entry& entry) { | 69 const Entry& entry) { |
| 70 if (!entry.Get(UNIQUE_SERVER_TAG).empty()) | 70 if (!entry.Get(UNIQUE_SERVER_TAG).empty()) |
| 71 return false; // We don't encrypt unique server nodes. | 71 return false; // We don't encrypt unique server nodes. |
| 72 syncable::ModelType type = entry.GetModelType(); | 72 syncer::ModelType type = entry.GetModelType(); |
| 73 if (type == PASSWORDS || type == NIGORI) | 73 if (type == PASSWORDS || type == NIGORI) |
| 74 return false; | 74 return false; |
| 75 // Checking NON_UNIQUE_NAME is not necessary for the correctness of encrypting | 75 // Checking NON_UNIQUE_NAME is not necessary for the correctness of encrypting |
| 76 // the data, nor for determining if data is encrypted. We simply ensure it has | 76 // the data, nor for determining if data is encrypted. We simply ensure it has |
| 77 // been overwritten to avoid any possible leaks of sensitive data. | 77 // been overwritten to avoid any possible leaks of sensitive data. |
| 78 return SpecificsNeedsEncryption(encrypted_types, entry.Get(SPECIFICS)) || | 78 return SpecificsNeedsEncryption(encrypted_types, entry.Get(SPECIFICS)) || |
| 79 (encrypted_types.Has(type) && | 79 (encrypted_types.Has(type) && |
| 80 entry.Get(NON_UNIQUE_NAME) != kEncryptedString); | 80 entry.Get(NON_UNIQUE_NAME) != kEncryptedString); |
| 81 } | 81 } |
| 82 | 82 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 syncer::Cryptographer* cryptographer, | 160 syncer::Cryptographer* cryptographer, |
| 161 const sync_pb::EntitySpecifics& new_specifics, | 161 const sync_pb::EntitySpecifics& new_specifics, |
| 162 syncable::MutableEntry* entry) { | 162 syncable::MutableEntry* entry) { |
| 163 syncable::ModelType type = syncable::GetModelTypeFromSpecifics(new_specifics); | 163 syncer::ModelType type = syncer::GetModelTypeFromSpecifics(new_specifics); |
| 164 DCHECK_GE(type, syncable::FIRST_REAL_MODEL_TYPE); | 164 DCHECK_GE(type, syncer::FIRST_REAL_MODEL_TYPE); |
| 165 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); | 165 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); |
| 166 const syncable::ModelTypeSet encrypted_types = | 166 const syncer::ModelTypeSet encrypted_types = |
| 167 cryptographer->GetEncryptedTypes(); | 167 cryptographer->GetEncryptedTypes(); |
| 168 // It's possible the nigori lost the set of encrypted types. If the current | 168 // It's possible the nigori lost the set of encrypted types. If the current |
| 169 // specifics are already encrypted, we want to ensure we continue encrypting. | 169 // specifics are already encrypted, we want to ensure we continue encrypting. |
| 170 bool was_encrypted = old_specifics.has_encrypted(); | 170 bool was_encrypted = old_specifics.has_encrypted(); |
| 171 sync_pb::EntitySpecifics generated_specifics; | 171 sync_pb::EntitySpecifics generated_specifics; |
| 172 if (new_specifics.has_encrypted()) { | 172 if (new_specifics.has_encrypted()) { |
| 173 NOTREACHED() << "New specifics already has an encrypted blob."; | 173 NOTREACHED() << "New specifics already has an encrypted blob."; |
| 174 return false; | 174 return false; |
| 175 } | 175 } |
| 176 if ((!SpecificsNeedsEncryption(encrypted_types, new_specifics) && | 176 if ((!SpecificsNeedsEncryption(encrypted_types, new_specifics) && |
| 177 !was_encrypted) || | 177 !was_encrypted) || |
| 178 !cryptographer->is_initialized()) { | 178 !cryptographer->is_initialized()) { |
| 179 // No encryption required or we are unable to encrypt. | 179 // No encryption required or we are unable to encrypt. |
| 180 generated_specifics.CopyFrom(new_specifics); | 180 generated_specifics.CopyFrom(new_specifics); |
| 181 } else { | 181 } else { |
| 182 // Encrypt new_specifics into generated_specifics. | 182 // Encrypt new_specifics into generated_specifics. |
| 183 if (VLOG_IS_ON(2)) { | 183 if (VLOG_IS_ON(2)) { |
| 184 scoped_ptr<DictionaryValue> value(entry->ToValue()); | 184 scoped_ptr<DictionaryValue> value(entry->ToValue()); |
| 185 std::string info; | 185 std::string info; |
| 186 base::JSONWriter::WriteWithOptions(value.get(), | 186 base::JSONWriter::WriteWithOptions(value.get(), |
| 187 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 187 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 188 &info); | 188 &info); |
| 189 DVLOG(2) << "Encrypting specifics of type " | 189 DVLOG(2) << "Encrypting specifics of type " |
| 190 << syncable::ModelTypeToString(type) | 190 << syncer::ModelTypeToString(type) |
| 191 << " with content: " | 191 << " with content: " |
| 192 << info; | 192 << info; |
| 193 } | 193 } |
| 194 // Only copy over the old specifics if it is of the right type and already | 194 // Only copy over the old specifics if it is of the right type and already |
| 195 // encrypted. The first time we encrypt a node we start from scratch, hence | 195 // encrypted. The first time we encrypt a node we start from scratch, hence |
| 196 // removing all the unencrypted data, but from then on we only want to | 196 // removing all the unencrypted data, but from then on we only want to |
| 197 // update the node if the data changes or the encryption key changes. | 197 // update the node if the data changes or the encryption key changes. |
| 198 if (syncable::GetModelTypeFromSpecifics(old_specifics) == type && | 198 if (syncer::GetModelTypeFromSpecifics(old_specifics) == type && |
| 199 was_encrypted) { | 199 was_encrypted) { |
| 200 generated_specifics.CopyFrom(old_specifics); | 200 generated_specifics.CopyFrom(old_specifics); |
| 201 } else { | 201 } else { |
| 202 syncable::AddDefaultFieldValue(type, &generated_specifics); | 202 syncer::AddDefaultFieldValue(type, &generated_specifics); |
| 203 } | 203 } |
| 204 // Does not change anything if underlying encrypted blob was already up | 204 // Does not change anything if underlying encrypted blob was already up |
| 205 // to date and encrypted with the default key. | 205 // to date and encrypted with the default key. |
| 206 if (!cryptographer->Encrypt(new_specifics, | 206 if (!cryptographer->Encrypt(new_specifics, |
| 207 generated_specifics.mutable_encrypted())) { | 207 generated_specifics.mutable_encrypted())) { |
| 208 NOTREACHED() << "Could not encrypt data for node of type " | 208 NOTREACHED() << "Could not encrypt data for node of type " |
| 209 << syncable::ModelTypeToString(type); | 209 << syncer::ModelTypeToString(type); |
| 210 return false; | 210 return false; |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 // It's possible this entry was encrypted but didn't properly overwrite the | 214 // It's possible this entry was encrypted but didn't properly overwrite the |
| 215 // non_unique_name (see crbug.com/96314). | 215 // non_unique_name (see crbug.com/96314). |
| 216 bool encrypted_without_overwriting_name = (was_encrypted && | 216 bool encrypted_without_overwriting_name = (was_encrypted && |
| 217 entry->Get(syncable::NON_UNIQUE_NAME) != kEncryptedString); | 217 entry->Get(syncable::NON_UNIQUE_NAME) != kEncryptedString); |
| 218 | 218 |
| 219 // If we're encrypted but the name wasn't overwritten properly we still want | 219 // If we're encrypted but the name wasn't overwritten properly we still want |
| 220 // to rewrite the entry, irrespective of whether the specifics match. | 220 // to rewrite the entry, irrespective of whether the specifics match. |
| 221 if (!encrypted_without_overwriting_name && | 221 if (!encrypted_without_overwriting_name && |
| 222 old_specifics.SerializeAsString() == | 222 old_specifics.SerializeAsString() == |
| 223 generated_specifics.SerializeAsString()) { | 223 generated_specifics.SerializeAsString()) { |
| 224 DVLOG(2) << "Specifics of type " << syncable::ModelTypeToString(type) | 224 DVLOG(2) << "Specifics of type " << syncer::ModelTypeToString(type) |
| 225 << " already match, dropping change."; | 225 << " already match, dropping change."; |
| 226 return true; | 226 return true; |
| 227 } | 227 } |
| 228 | 228 |
| 229 if (generated_specifics.has_encrypted()) { | 229 if (generated_specifics.has_encrypted()) { |
| 230 // Overwrite the possibly sensitive non-specifics data. | 230 // Overwrite the possibly sensitive non-specifics data. |
| 231 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString); | 231 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString); |
| 232 // For bookmarks we actually put bogus data into the unencrypted specifics, | 232 // For bookmarks we actually put bogus data into the unencrypted specifics, |
| 233 // else the server will try to do it for us. | 233 // else the server will try to do it for us. |
| 234 if (type == syncable::BOOKMARKS) { | 234 if (type == syncer::BOOKMARKS) { |
| 235 sync_pb::BookmarkSpecifics* bookmark_specifics = | 235 sync_pb::BookmarkSpecifics* bookmark_specifics = |
| 236 generated_specifics.mutable_bookmark(); | 236 generated_specifics.mutable_bookmark(); |
| 237 if (!entry->Get(syncable::IS_DIR)) | 237 if (!entry->Get(syncable::IS_DIR)) |
| 238 bookmark_specifics->set_url(kEncryptedString); | 238 bookmark_specifics->set_url(kEncryptedString); |
| 239 bookmark_specifics->set_title(kEncryptedString); | 239 bookmark_specifics->set_title(kEncryptedString); |
| 240 } | 240 } |
| 241 } | 241 } |
| 242 entry->Put(syncable::SPECIFICS, generated_specifics); | 242 entry->Put(syncable::SPECIFICS, generated_specifics); |
| 243 DVLOG(1) << "Overwriting specifics of type " | 243 DVLOG(1) << "Overwriting specifics of type " |
| 244 << syncable::ModelTypeToString(type) | 244 << syncer::ModelTypeToString(type) |
| 245 << " and marking for syncing."; | 245 << " and marking for syncing."; |
| 246 syncable::MarkForSyncing(entry); | 246 syncable::MarkForSyncing(entry); |
| 247 return true; | 247 return true; |
| 248 } | 248 } |
| 249 | 249 |
| 250 } // namespace syncable | 250 } // namespace syncable |
| 251 } // namespace syncer | 251 } // namespace syncer |
| OLD | NEW |