blob: 5e715d7f76871827713b50cc419bae7474b6de6d [file] [log] [blame]
[email protected]4557d222012-03-04 23:33:361// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]67192b42011-08-17 23:38:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]91835ca2012-04-21 09:59:425#include "sync/internal_api/syncapi_internal.h"
[email protected]67192b42011-08-17 23:38:306
avi4856ced02015-12-22 03:14:107#include <stddef.h>
8
9#include "base/macros.h"
[email protected]67192b42011-08-17 23:38:3010#include "base/memory/scoped_ptr.h"
[email protected]f3132452014-07-17 22:29:1611#include "sync/protocol/attachments.pb.h"
[email protected]1bcf30e2012-03-10 01:06:4112#include "sync/protocol/password_specifics.pb.h"
13#include "sync/protocol/sync.pb.h"
[email protected]c1c32c82012-03-15 09:35:4214#include "sync/util/cryptographer.h"
[email protected]67192b42011-08-17 23:38:3015
[email protected]65f173552012-06-28 22:43:5816namespace syncer {
[email protected]67192b42011-08-17 23:38:3017
[email protected]807c6592014-03-12 05:17:2918namespace {
19
20bool EndsWithSpace(const std::string& string) {
21 return !string.empty() && *string.rbegin() == ' ';
22}
23
24}
25
[email protected]67192b42011-08-17 23:38:3026sync_pb::PasswordSpecificsData* DecryptPasswordSpecifics(
27 const sync_pb::EntitySpecifics& specifics, Cryptographer* crypto) {
[email protected]4557d222012-03-04 23:33:3628 if (!specifics.has_password())
[email protected]67192b42011-08-17 23:38:3029 return NULL;
[email protected]4557d222012-03-04 23:33:3630 const sync_pb::PasswordSpecifics& password_specifics = specifics.password();
[email protected]67192b42011-08-17 23:38:3031 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);
zeadea3a922015-06-10 01:51:3036 if (!crypto->CanDecrypt(encrypted))
37 return NULL;
[email protected]67192b42011-08-17 23:38:3038 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.
44static const char* kForbiddenServerNames[] = { "", ".", ".." };
45
[email protected]9044359b2011-09-20 23:28:3246// 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]65f173552012-06-28 22:43:5848void SyncAPINameToServerName(const std::string& syncer_name,
[email protected]9044359b2011-09-20 23:28:3249 std::string* out) {
[email protected]65f173552012-06-28 22:43:5850 *out = syncer_name;
[email protected]9044359b2011-09-20 23:28:3251 if (IsNameServerIllegalAfterTrimming(*out))
52 out->append(" ");
53}
54
[email protected]807c6592014-03-12 05:17:2955// 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.
58void 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]67192b42011-08-17 23:38:3069// 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.
73bool 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]d45f0d92012-07-20 17:25:4183bool AreSpecificsEqual(const Cryptographer* cryptographer,
[email protected]67192b42011-08-17 23:38:3084 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]f3132452014-07-17 22:29:16113bool 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]65f173552012-06-28 22:43:58121} // namespace syncer