1// Copyright 2014 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 "chromeos/cryptohome/cryptohome_parameters.h"
6
7#include "chromeos/dbus/cryptohome/key.pb.h"
8
9namespace cryptohome {
10
11Identification::Identification(const std::string& user_id) : user_id(user_id) {
12}
13
14bool Identification::operator==(const Identification& other) const {
15  return user_id == other.user_id;
16}
17
18KeyDefinition::AuthorizationData::Secret::Secret() : encrypt(false),
19                                                     sign(false),
20                                                     wrapped(false) {
21}
22
23KeyDefinition::AuthorizationData::Secret::Secret(
24    bool encrypt,
25    bool sign,
26    const std::string& symmetric_key,
27    const std::string& public_key,
28    bool wrapped)
29    : encrypt(encrypt),
30      sign(sign),
31      symmetric_key(symmetric_key),
32      public_key(public_key),
33      wrapped(wrapped) {
34}
35
36bool KeyDefinition::AuthorizationData::Secret::operator==(
37    const Secret& other) const {
38  return encrypt == other.encrypt &&
39         sign == other.sign &&
40         symmetric_key == other.symmetric_key &&
41         public_key == other.public_key &&
42         wrapped == other.wrapped;
43}
44
45KeyDefinition::AuthorizationData::AuthorizationData() : type(TYPE_HMACSHA256) {
46}
47
48KeyDefinition::AuthorizationData::AuthorizationData(
49    bool encrypt,
50    bool sign,
51    const std::string& symmetric_key) : type(TYPE_HMACSHA256) {
52    secrets.push_back(Secret(encrypt,
53                             sign,
54                             symmetric_key,
55                             std::string() /* public_key */,
56                             false /* wrapped */));
57}
58
59
60KeyDefinition::AuthorizationData::~AuthorizationData() {
61}
62
63bool KeyDefinition::AuthorizationData::operator==(
64    const AuthorizationData& other) const {
65  if (type != other.type || secrets.size() != other.secrets.size())
66    return false;
67  for (size_t i = 0; i < secrets.size(); ++i) {
68    if (!(secrets[i] == other.secrets[i]))
69      return false;
70  }
71  return true;
72}
73
74KeyDefinition::ProviderData::ProviderData() {
75}
76
77KeyDefinition::ProviderData::ProviderData(const std::string& name)
78    : name(name) {
79}
80
81KeyDefinition::ProviderData::ProviderData(const ProviderData& other)
82    : name(other.name) {
83  if (other.number)
84    number.reset(new int64(*other.number));
85  if (other.bytes)
86    bytes.reset(new std::string(*other.bytes));
87}
88
89KeyDefinition::ProviderData::ProviderData(const std::string& name, int64 number)
90    : name(name),
91      number(new int64(number)) {
92}
93
94KeyDefinition::ProviderData::ProviderData(const std::string& name,
95                                          const std::string& bytes)
96    : name(name),
97      bytes(new std::string(bytes)) {
98}
99
100void KeyDefinition::ProviderData::operator=(const ProviderData& other) {
101  name = other.name;
102  number.reset(other.number ? new int64(*other.number) : NULL);
103  bytes.reset(other.bytes ? new std::string(*other.bytes) : NULL);
104}
105
106KeyDefinition::ProviderData::~ProviderData() {
107}
108
109bool KeyDefinition::ProviderData::operator==(const ProviderData& other) const {
110  const bool has_number = number;
111  const bool other_has_number = other.number;
112  const bool has_bytes = bytes;
113  const bool other_has_bytes = other.bytes;
114  return name == other.name &&
115         has_number == other_has_number &&
116         has_bytes == other_has_bytes &&
117         (!has_number || (*number == *other.number)) &&
118         (!has_bytes || (*bytes == *other.bytes));
119}
120
121KeyDefinition::KeyDefinition() : type(TYPE_PASSWORD),
122                                 privileges(0),
123                                 revision(0) {
124}
125
126KeyDefinition::KeyDefinition(const std::string& secret,
127                             const std::string& label,
128                             int /*AuthKeyPrivileges*/ privileges)
129    : type(TYPE_PASSWORD),
130      label(label),
131      privileges(privileges),
132      revision(0),
133      secret(secret) {
134}
135
136KeyDefinition::~KeyDefinition() {
137}
138
139bool KeyDefinition::operator==(const KeyDefinition& other) const {
140  if (type != other.type ||
141      label != other.label ||
142      privileges != other.privileges ||
143      revision != other.revision ||
144      authorization_data.size() != other.authorization_data.size() ||
145      provider_data.size() != other.provider_data.size()) {
146    return false;
147  }
148
149  for (size_t i = 0; i < authorization_data.size(); ++i) {
150    if (!(authorization_data[i] == other.authorization_data[i]))
151      return false;
152  }
153  for (size_t i = 0; i < provider_data.size(); ++i) {
154    if (!(provider_data[i] == other.provider_data[i]))
155      return false;
156  }
157  return true;
158}
159
160Authorization::Authorization(const std::string& key, const std::string& label)
161    : key(key),
162      label(label) {
163}
164
165Authorization::Authorization(const KeyDefinition& key_def)
166    : key(key_def.secret),
167      label(key_def.label) {
168}
169
170bool Authorization::operator==(const Authorization& other) const {
171  return key == other.key && label == other.label;
172}
173
174MountParameters::MountParameters(bool ephemeral) : ephemeral(ephemeral) {
175}
176
177bool MountParameters::operator==(const MountParameters& other) const {
178  return ephemeral == other.ephemeral && create_keys == other.create_keys;
179}
180
181MountParameters::~MountParameters() {
182}
183
184}  // namespace cryptohome
185