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