• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 namespace cryptohome {
10 
Identification(const std::string & user_id)11 Identification::Identification(const std::string& user_id) : user_id(user_id) {
12 }
13 
operator ==(const Identification & other) const14 bool Identification::operator==(const Identification& other) const {
15   return user_id == other.user_id;
16 }
17 
Secret()18 KeyDefinition::AuthorizationData::Secret::Secret() : encrypt(false),
19                                                      sign(false),
20                                                      wrapped(false) {
21 }
22 
Secret(bool encrypt,bool sign,const std::string & symmetric_key,const std::string & public_key,bool wrapped)23 KeyDefinition::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 
operator ==(const Secret & other) const36 bool 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 
AuthorizationData()45 KeyDefinition::AuthorizationData::AuthorizationData() : type(TYPE_HMACSHA256) {
46 }
47 
AuthorizationData(bool encrypt,bool sign,const std::string & symmetric_key)48 KeyDefinition::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 
~AuthorizationData()60 KeyDefinition::AuthorizationData::~AuthorizationData() {
61 }
62 
operator ==(const AuthorizationData & other) const63 bool 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 
ProviderData()74 KeyDefinition::ProviderData::ProviderData() {
75 }
76 
ProviderData(const std::string & name)77 KeyDefinition::ProviderData::ProviderData(const std::string& name)
78     : name(name) {
79 }
80 
ProviderData(const ProviderData & other)81 KeyDefinition::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 
ProviderData(const std::string & name,int64 number)89 KeyDefinition::ProviderData::ProviderData(const std::string& name, int64 number)
90     : name(name),
91       number(new int64(number)) {
92 }
93 
ProviderData(const std::string & name,const std::string & bytes)94 KeyDefinition::ProviderData::ProviderData(const std::string& name,
95                                           const std::string& bytes)
96     : name(name),
97       bytes(new std::string(bytes)) {
98 }
99 
operator =(const ProviderData & other)100 void 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 
~ProviderData()106 KeyDefinition::ProviderData::~ProviderData() {
107 }
108 
operator ==(const ProviderData & other) const109 bool 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 
KeyDefinition()121 KeyDefinition::KeyDefinition() : type(TYPE_PASSWORD),
122                                  privileges(0),
123                                  revision(0) {
124 }
125 
KeyDefinition(const std::string & secret,const std::string & label,int privileges)126 KeyDefinition::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 
~KeyDefinition()136 KeyDefinition::~KeyDefinition() {
137 }
138 
operator ==(const KeyDefinition & other) const139 bool 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 
Authorization(const std::string & key,const std::string & label)160 Authorization::Authorization(const std::string& key, const std::string& label)
161     : key(key),
162       label(label) {
163 }
164 
Authorization(const KeyDefinition & key_def)165 Authorization::Authorization(const KeyDefinition& key_def)
166     : key(key_def.secret),
167       label(key_def.label) {
168 }
169 
operator ==(const Authorization & other) const170 bool Authorization::operator==(const Authorization& other) const {
171   return key == other.key && label == other.label;
172 }
173 
MountParameters(bool ephemeral)174 MountParameters::MountParameters(bool ephemeral) : ephemeral(ephemeral) {
175 }
176 
operator ==(const MountParameters & other) const177 bool MountParameters::operator==(const MountParameters& other) const {
178   return ephemeral == other.ephemeral && create_keys == other.create_keys;
179 }
180 
~MountParameters()181 MountParameters::~MountParameters() {
182 }
183 
184 }  // namespace cryptohome
185