| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/sync/core_impl/syncapi_internal.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "components/sync/base/cryptographer.h" | |
| 13 #include "components/sync/protocol/attachments.pb.h" | |
| 14 #include "components/sync/protocol/password_specifics.pb.h" | |
| 15 #include "components/sync/protocol/sync.pb.h" | |
| 16 | |
| 17 namespace syncer { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 bool EndsWithSpace(const std::string& string) { | |
| 22 return !string.empty() && *string.rbegin() == ' '; | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 sync_pb::PasswordSpecificsData* DecryptPasswordSpecifics( | |
| 27 const sync_pb::EntitySpecifics& specifics, | |
| 28 Cryptographer* crypto) { | |
| 29 if (!specifics.has_password()) | |
| 30 return NULL; | |
| 31 const sync_pb::PasswordSpecifics& password_specifics = specifics.password(); | |
| 32 if (!password_specifics.has_encrypted()) | |
| 33 return NULL; | |
| 34 const sync_pb::EncryptedData& encrypted = password_specifics.encrypted(); | |
| 35 std::unique_ptr<sync_pb::PasswordSpecificsData> data( | |
| 36 new sync_pb::PasswordSpecificsData); | |
| 37 if (!crypto->CanDecrypt(encrypted)) | |
| 38 return NULL; | |
| 39 if (!crypto->Decrypt(encrypted, data.get())) | |
| 40 return NULL; | |
| 41 return data.release(); | |
| 42 } | |
| 43 | |
| 44 // The list of names which are reserved for use by the server. | |
| 45 static const char* kForbiddenServerNames[] = {"", ".", ".."}; | |
| 46 | |
| 47 // When taking a name from the syncapi, append a space if it matches the | |
| 48 // pattern of a server-illegal name followed by zero or more spaces. | |
| 49 void SyncAPINameToServerName(const std::string& syncer_name, std::string* out) { | |
| 50 *out = syncer_name; | |
| 51 if (IsNameServerIllegalAfterTrimming(*out)) | |
| 52 out->append(" "); | |
| 53 } | |
| 54 | |
| 55 // In the reverse direction, if a server name matches the pattern of a | |
| 56 // server-illegal name followed by one or more spaces, remove the trailing | |
| 57 // space. | |
| 58 void ServerNameToSyncAPIName(const std::string& server_name, std::string* out) { | |
| 59 CHECK(out); | |
| 60 int length_to_copy = server_name.length(); | |
| 61 if (IsNameServerIllegalAfterTrimming(server_name) && | |
| 62 EndsWithSpace(server_name)) { | |
| 63 --length_to_copy; | |
| 64 } | |
| 65 *out = server_name.substr(0, length_to_copy); | |
| 66 } | |
| 67 | |
| 68 // Checks whether |name| is a server-illegal name followed by zero or more space | |
| 69 // characters. The three server-illegal names are the empty string, dot, and | |
| 70 // dot-dot. Very long names (>255 bytes in UTF-8 Normalization Form C) are | |
| 71 // also illegal, but are not considered here. | |
| 72 bool IsNameServerIllegalAfterTrimming(const std::string& name) { | |
| 73 size_t untrimmed_count = name.find_last_not_of(' ') + 1; | |
| 74 for (size_t i = 0; i < arraysize(kForbiddenServerNames); ++i) { | |
| 75 if (name.compare(0, untrimmed_count, kForbiddenServerNames[i]) == 0) | |
| 76 return true; | |
| 77 } | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 // Compare the values of two EntitySpecifics, accounting for encryption. | |
| 82 bool AreSpecificsEqual(const Cryptographer* cryptographer, | |
| 83 const sync_pb::EntitySpecifics& left, | |
| 84 const sync_pb::EntitySpecifics& right) { | |
| 85 // Note that we can't compare encrypted strings directly as they are seeded | |
| 86 // with a random value. | |
| 87 std::string left_plaintext, right_plaintext; | |
| 88 if (left.has_encrypted()) { | |
| 89 if (!cryptographer->CanDecrypt(left.encrypted())) { | |
| 90 NOTREACHED() << "Attempting to compare undecryptable data."; | |
| 91 return false; | |
| 92 } | |
| 93 left_plaintext = cryptographer->DecryptToString(left.encrypted()); | |
| 94 } else { | |
| 95 left_plaintext = left.SerializeAsString(); | |
| 96 } | |
| 97 if (right.has_encrypted()) { | |
| 98 if (!cryptographer->CanDecrypt(right.encrypted())) { | |
| 99 NOTREACHED() << "Attempting to compare undecryptable data."; | |
| 100 return false; | |
| 101 } | |
| 102 right_plaintext = cryptographer->DecryptToString(right.encrypted()); | |
| 103 } else { | |
| 104 right_plaintext = right.SerializeAsString(); | |
| 105 } | |
| 106 if (left_plaintext == right_plaintext) { | |
| 107 return true; | |
| 108 } | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 bool AreAttachmentMetadataEqual(const sync_pb::AttachmentMetadata& left, | |
| 113 const sync_pb::AttachmentMetadata& right) { | |
| 114 if (left.SerializeAsString() == right.SerializeAsString()) { | |
| 115 return true; | |
| 116 } | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 } // namespace syncer | |
| OLD | NEW |