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 "chrome/browser/sync/engine/nigori_util.h" | 5 #include "chrome/browser/sync/engine/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 "chrome/browser/sync/engine/syncer_util.h" | 11 #include "chrome/browser/sync/engine/syncer_util.h" |
12 #include "chrome/browser/sync/internal_api/write_node.h" | 12 #include "chrome/browser/sync/internal_api/write_node.h" |
13 #include "chrome/browser/sync/syncable/syncable.h" | 13 #include "chrome/browser/sync/syncable/syncable.h" |
14 #include "chrome/browser/sync/util/cryptographer.h" | 14 #include "chrome/browser/sync/util/cryptographer.h" |
15 | 15 |
16 namespace syncable { | 16 namespace syncable { |
17 | 17 |
18 bool ProcessUnsyncedChangesForEncryption( | 18 bool ProcessUnsyncedChangesForEncryption( |
19 WriteTransaction* const trans, | 19 WriteTransaction* const trans, |
20 browser_sync::Cryptographer* cryptographer) { | 20 browser_sync::Cryptographer* cryptographer) { |
21 DCHECK(cryptographer->is_ready()); | 21 DCHECK(cryptographer->is_ready()); |
22 syncable::ModelTypeSet encrypted_types = cryptographer->GetEncryptedTypes(); | |
23 | |
24 // Get list of all datatypes with unsynced changes. It's possible that our | 22 // Get list of all datatypes with unsynced changes. It's possible that our |
25 // local changes need to be encrypted if encryption for that datatype was | 23 // local changes need to be encrypted if encryption for that datatype was |
26 // just turned on (and vice versa). This should never affect passwords. | 24 // just turned on (and vice versa). This should never affect passwords. |
27 std::vector<int64> handles; | 25 std::vector<int64> handles; |
28 browser_sync::SyncerUtil::GetUnsyncedEntries(trans, &handles); | 26 browser_sync::SyncerUtil::GetUnsyncedEntries(trans, &handles); |
29 for (size_t i = 0; i < handles.size(); ++i) { | 27 for (size_t i = 0; i < handles.size(); ++i) { |
30 MutableEntry entry(trans, GET_BY_HANDLE, handles[i]); | 28 MutableEntry entry(trans, GET_BY_HANDLE, handles[i]); |
31 if (!sync_api::WriteNode::UpdateEntryWithEncryption(cryptographer, | 29 if (!sync_api::WriteNode::UpdateEntryWithEncryption(cryptographer, |
32 entry.Get(SPECIFICS), | 30 entry.Get(SPECIFICS), |
33 &entry)) { | 31 &entry)) { |
34 NOTREACHED(); | 32 NOTREACHED(); |
35 return false; | 33 return false; |
36 } | 34 } |
37 } | 35 } |
38 return true; | 36 return true; |
39 } | 37 } |
40 | 38 |
41 bool VerifyUnsyncedChangesAreEncrypted( | 39 bool VerifyUnsyncedChangesAreEncrypted( |
42 BaseTransaction* const trans, | 40 BaseTransaction* const trans, |
43 const ModelTypeSet& encrypted_types) { | 41 ModelEnumSet encrypted_types) { |
44 std::vector<int64> handles; | 42 std::vector<int64> handles; |
45 browser_sync::SyncerUtil::GetUnsyncedEntries(trans, &handles); | 43 browser_sync::SyncerUtil::GetUnsyncedEntries(trans, &handles); |
46 for (size_t i = 0; i < handles.size(); ++i) { | 44 for (size_t i = 0; i < handles.size(); ++i) { |
47 Entry entry(trans, GET_BY_HANDLE, handles[i]); | 45 Entry entry(trans, GET_BY_HANDLE, handles[i]); |
48 if (!entry.good()) { | 46 if (!entry.good()) { |
49 NOTREACHED(); | 47 NOTREACHED(); |
50 return false; | 48 return false; |
51 } | 49 } |
52 if (EntryNeedsEncryption(encrypted_types, entry)) | 50 if (EntryNeedsEncryption(encrypted_types, entry)) |
53 return false; | 51 return false; |
54 } | 52 } |
55 return true; | 53 return true; |
56 } | 54 } |
57 | 55 |
58 bool EntryNeedsEncryption(const ModelTypeSet& encrypted_types, | 56 bool EntryNeedsEncryption(ModelEnumSet encrypted_types, |
59 const Entry& entry) { | 57 const Entry& entry) { |
60 if (!entry.Get(UNIQUE_SERVER_TAG).empty()) | 58 if (!entry.Get(UNIQUE_SERVER_TAG).empty()) |
61 return false; // We don't encrypt unique server nodes. | 59 return false; // We don't encrypt unique server nodes. |
62 syncable::ModelType type = entry.GetModelType(); | 60 syncable::ModelType type = entry.GetModelType(); |
63 if (type == PASSWORDS || type == NIGORI) | 61 if (type == PASSWORDS || type == NIGORI) |
64 return false; | 62 return false; |
65 // Checking NON_UNIQUE_NAME is not necessary for the correctness of encrypting | 63 // Checking NON_UNIQUE_NAME is not necessary for the correctness of encrypting |
66 // the data, nor for determining if data is encrypted. We simply ensure it has | 64 // the data, nor for determining if data is encrypted. We simply ensure it has |
67 // been overwritten to avoid any possible leaks of sensitive data. | 65 // been overwritten to avoid any possible leaks of sensitive data. |
68 return SpecificsNeedsEncryption(encrypted_types, entry.Get(SPECIFICS)) || | 66 return SpecificsNeedsEncryption(encrypted_types, entry.Get(SPECIFICS)) || |
69 (encrypted_types.count(type) > 0 && | 67 (encrypted_types.Has(type) && |
70 entry.Get(NON_UNIQUE_NAME) != kEncryptedString); | 68 entry.Get(NON_UNIQUE_NAME) != kEncryptedString); |
71 } | 69 } |
72 | 70 |
73 bool SpecificsNeedsEncryption(const ModelTypeSet& encrypted_types, | 71 bool SpecificsNeedsEncryption(ModelEnumSet encrypted_types, |
74 const sync_pb::EntitySpecifics& specifics) { | 72 const sync_pb::EntitySpecifics& specifics) { |
75 ModelType type = GetModelTypeFromSpecifics(specifics); | 73 const ModelType type = GetModelTypeFromSpecifics(specifics); |
| 74 if (!syncable::IsRealDataType(type)) |
| 75 return false; |
76 if (type == PASSWORDS || type == NIGORI) | 76 if (type == PASSWORDS || type == NIGORI) |
77 return false; // These types have their own encryption schemes. | 77 return false; // These types have their own encryption schemes. |
78 if (encrypted_types.count(type) == 0) | 78 if (!encrypted_types.Has(type)) |
79 return false; // This type does not require encryption | 79 return false; // This type does not require encryption |
80 return !specifics.has_encrypted(); | 80 return !specifics.has_encrypted(); |
81 } | 81 } |
82 | 82 |
83 // Mainly for testing. | 83 // Mainly for testing. |
84 bool VerifyDataTypeEncryptionForTest( | 84 bool VerifyDataTypeEncryptionForTest( |
85 BaseTransaction* const trans, | 85 BaseTransaction* const trans, |
86 browser_sync::Cryptographer* cryptographer, | 86 browser_sync::Cryptographer* cryptographer, |
87 ModelType type, | 87 ModelType type, |
88 bool is_encrypted) { | 88 bool is_encrypted) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 return false; | 140 return false; |
141 } | 141 } |
142 } | 142 } |
143 // Push the successor. | 143 // Push the successor. |
144 to_visit.push(child.Get(NEXT_ID)); | 144 to_visit.push(child.Get(NEXT_ID)); |
145 } | 145 } |
146 return true; | 146 return true; |
147 } | 147 } |
148 | 148 |
149 } // namespace syncable | 149 } // namespace syncable |
OLD | NEW |