| 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/internal_api/syncapi_internal.h" | 5 #include "sync/internal_api/syncapi_internal.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "sync/protocol/password_specifics.pb.h" | 8 #include "sync/protocol/password_specifics.pb.h" |
| 9 #include "sync/protocol/sync.pb.h" | 9 #include "sync/protocol/sync.pb.h" |
| 10 #include "sync/util/cryptographer.h" | 10 #include "sync/util/cryptographer.h" |
| 11 | 11 |
| 12 using csync::Cryptographer; | 12 using syncer::Cryptographer; |
| 13 | 13 |
| 14 namespace csync { | 14 namespace syncer { |
| 15 | 15 |
| 16 sync_pb::PasswordSpecificsData* DecryptPasswordSpecifics( | 16 sync_pb::PasswordSpecificsData* DecryptPasswordSpecifics( |
| 17 const sync_pb::EntitySpecifics& specifics, Cryptographer* crypto) { | 17 const sync_pb::EntitySpecifics& specifics, Cryptographer* crypto) { |
| 18 if (!specifics.has_password()) | 18 if (!specifics.has_password()) |
| 19 return NULL; | 19 return NULL; |
| 20 const sync_pb::PasswordSpecifics& password_specifics = specifics.password(); | 20 const sync_pb::PasswordSpecifics& password_specifics = specifics.password(); |
| 21 if (!password_specifics.has_encrypted()) | 21 if (!password_specifics.has_encrypted()) |
| 22 return NULL; | 22 return NULL; |
| 23 const sync_pb::EncryptedData& encrypted = password_specifics.encrypted(); | 23 const sync_pb::EncryptedData& encrypted = password_specifics.encrypted(); |
| 24 scoped_ptr<sync_pb::PasswordSpecificsData> data( | 24 scoped_ptr<sync_pb::PasswordSpecificsData> data( |
| 25 new sync_pb::PasswordSpecificsData); | 25 new sync_pb::PasswordSpecificsData); |
| 26 if (!crypto->Decrypt(encrypted, data.get())) | 26 if (!crypto->Decrypt(encrypted, data.get())) |
| 27 return NULL; | 27 return NULL; |
| 28 return data.release(); | 28 return data.release(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // The list of names which are reserved for use by the server. | 31 // The list of names which are reserved for use by the server. |
| 32 static const char* kForbiddenServerNames[] = { "", ".", ".." }; | 32 static const char* kForbiddenServerNames[] = { "", ".", ".." }; |
| 33 | 33 |
| 34 // When taking a name from the syncapi, append a space if it matches the | 34 // When taking a name from the syncapi, append a space if it matches the |
| 35 // pattern of a server-illegal name followed by zero or more spaces. | 35 // pattern of a server-illegal name followed by zero or more spaces. |
| 36 void SyncAPINameToServerName(const std::string& csync_name, | 36 void SyncAPINameToServerName(const std::string& syncer_name, |
| 37 std::string* out) { | 37 std::string* out) { |
| 38 *out = csync_name; | 38 *out = syncer_name; |
| 39 if (IsNameServerIllegalAfterTrimming(*out)) | 39 if (IsNameServerIllegalAfterTrimming(*out)) |
| 40 out->append(" "); | 40 out->append(" "); |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Checks whether |name| is a server-illegal name followed by zero or more space | 43 // Checks whether |name| is a server-illegal name followed by zero or more space |
| 44 // characters. The three server-illegal names are the empty string, dot, and | 44 // characters. The three server-illegal names are the empty string, dot, and |
| 45 // dot-dot. Very long names (>255 bytes in UTF-8 Normalization Form C) are | 45 // dot-dot. Very long names (>255 bytes in UTF-8 Normalization Form C) are |
| 46 // also illegal, but are not considered here. | 46 // also illegal, but are not considered here. |
| 47 bool IsNameServerIllegalAfterTrimming(const std::string& name) { | 47 bool IsNameServerIllegalAfterTrimming(const std::string& name) { |
| 48 size_t untrimmed_count = name.find_last_not_of(' ') + 1; | 48 size_t untrimmed_count = name.find_last_not_of(' ') + 1; |
| 49 for (size_t i = 0; i < arraysize(kForbiddenServerNames); ++i) { | 49 for (size_t i = 0; i < arraysize(kForbiddenServerNames); ++i) { |
| 50 if (name.compare(0, untrimmed_count, kForbiddenServerNames[i]) == 0) | 50 if (name.compare(0, untrimmed_count, kForbiddenServerNames[i]) == 0) |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 return false; | 53 return false; |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Compare the values of two EntitySpecifics, accounting for encryption. | 56 // Compare the values of two EntitySpecifics, accounting for encryption. |
| 57 bool AreSpecificsEqual(const csync::Cryptographer* cryptographer, | 57 bool AreSpecificsEqual(const syncer::Cryptographer* cryptographer, |
| 58 const sync_pb::EntitySpecifics& left, | 58 const sync_pb::EntitySpecifics& left, |
| 59 const sync_pb::EntitySpecifics& right) { | 59 const sync_pb::EntitySpecifics& right) { |
| 60 // Note that we can't compare encrypted strings directly as they are seeded | 60 // Note that we can't compare encrypted strings directly as they are seeded |
| 61 // with a random value. | 61 // with a random value. |
| 62 std::string left_plaintext, right_plaintext; | 62 std::string left_plaintext, right_plaintext; |
| 63 if (left.has_encrypted()) { | 63 if (left.has_encrypted()) { |
| 64 if (!cryptographer->CanDecrypt(left.encrypted())) { | 64 if (!cryptographer->CanDecrypt(left.encrypted())) { |
| 65 NOTREACHED() << "Attempting to compare undecryptable data."; | 65 NOTREACHED() << "Attempting to compare undecryptable data."; |
| 66 return false; | 66 return false; |
| 67 } | 67 } |
| 68 left_plaintext = cryptographer->DecryptToString(left.encrypted()); | 68 left_plaintext = cryptographer->DecryptToString(left.encrypted()); |
| 69 } else { | 69 } else { |
| 70 left_plaintext = left.SerializeAsString(); | 70 left_plaintext = left.SerializeAsString(); |
| 71 } | 71 } |
| 72 if (right.has_encrypted()) { | 72 if (right.has_encrypted()) { |
| 73 if (!cryptographer->CanDecrypt(right.encrypted())) { | 73 if (!cryptographer->CanDecrypt(right.encrypted())) { |
| 74 NOTREACHED() << "Attempting to compare undecryptable data."; | 74 NOTREACHED() << "Attempting to compare undecryptable data."; |
| 75 return false; | 75 return false; |
| 76 } | 76 } |
| 77 right_plaintext = cryptographer->DecryptToString(right.encrypted()); | 77 right_plaintext = cryptographer->DecryptToString(right.encrypted()); |
| 78 } else { | 78 } else { |
| 79 right_plaintext = right.SerializeAsString(); | 79 right_plaintext = right.SerializeAsString(); |
| 80 } | 80 } |
| 81 if (left_plaintext == right_plaintext) { | 81 if (left_plaintext == right_plaintext) { |
| 82 return true; | 82 return true; |
| 83 } | 83 } |
| 84 return false; | 84 return false; |
| 85 } | 85 } |
| 86 | 86 |
| 87 } // namespace csync | 87 } // namespace syncer |
| OLD | NEW |