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