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 "sync/internal_api/syncapi_internal.h"
6
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"
11
12namespace syncer {
13
14sync_pb::PasswordSpecificsData* DecryptPasswordSpecifics(
15    const sync_pb::EntitySpecifics& specifics, Cryptographer* crypto) {
16  if (!specifics.has_password())
17    return NULL;
18  const sync_pb::PasswordSpecifics& password_specifics = specifics.password();
19  if (!password_specifics.has_encrypted())
20    return NULL;
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()))
25    return NULL;
26  return data.release();
27}
28
29// The list of names which are reserved for use by the server.
30static const char* kForbiddenServerNames[] = { "", ".", ".." };
31
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.
34void SyncAPINameToServerName(const std::string& syncer_name,
35                             std::string* out) {
36  *out = syncer_name;
37  if (IsNameServerIllegalAfterTrimming(*out))
38    out->append(" ");
39}
40
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.
45bool 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)
49      return true;
50  }
51  return false;
52}
53
54// Compare the values of two EntitySpecifics, accounting for encryption.
55bool 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.";
64      return false;
65    }
66    left_plaintext = cryptographer->DecryptToString(left.encrypted());
67  } else {
68    left_plaintext = left.SerializeAsString();
69  }
70  if (right.has_encrypted()) {
71    if (!cryptographer->CanDecrypt(right.encrypted())) {
72      NOTREACHED() << "Attempting to compare undecryptable data.";
73      return false;
74    }
75    right_plaintext = cryptographer->DecryptToString(right.encrypted());
76  } else {
77    right_plaintext = right.SerializeAsString();
78  }
79  if (left_plaintext == right_plaintext) {
80    return true;
81  }
82  return false;
83}
84
85}  // namespace syncer
86