Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.cc

Issue 554043003: cros: Create cryptohome keys for Easy sign-in. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update histograms.xml Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/values.h"
12
13 namespace chromeos {
14
15 namespace {
16
17 const char kKeyBluetoothAddress[] = "bluetoothAddress";
18 const char kKeyPermitRecord[] = "permitRecord";
19 const char kKeyId[] = "id";
20 const char kKeyPermitId[] = "permitRecord.id";
21 const char kKeyPsk[] = "psk";
22
23 const char kKeyLabelPrefix[] = "easy-unlock-";
24
25 } // namespace
26
27 EasyUnlockKeyManager::EasyUnlockKeyManager()
28 : operation_id_(0),
29 weak_ptr_factory_(this) {
30 }
31
32 EasyUnlockKeyManager::~EasyUnlockKeyManager() {
33 STLDeleteContainerPairSecondPointers(get_keys_ops_.begin(),
34 get_keys_ops_.end());
35 }
36
37 void EasyUnlockKeyManager::RefreshKeys(const UserContext& user_context,
38 const base::ListValue& remote_devices,
39 const RefreshKeysCallback& callback) {
40 // Must have the secret.
41 DCHECK(!user_context.GetKey()->GetSecret().empty());
42
43 EasyUnlockDeviceKeyDataList devices;
44 if (!RemoteDeviceListToDeviceDataList(remote_devices, &devices))
45 devices.clear();
46
47 // Only one pending request.
48 DCHECK(!HasPendingOperations());
49 create_keys_op_.reset(new EasyUnlockCreateKeysOperation(
50 user_context,
51 devices,
52 base::Bind(&EasyUnlockKeyManager::OnKeysCreated,
53 weak_ptr_factory_.GetWeakPtr(),
54 user_context,
55 devices,
56 callback)));
57 create_keys_op_->Start();
58 }
59
60 void EasyUnlockKeyManager::RemoveKeys(const UserContext& user_context,
61 size_t start_index,
62 const RemoveKeysCallback& callback) {
63 // Must have the secret.
64 DCHECK(!user_context.GetKey()->GetSecret().empty());
65 // Only one pending request.
66 DCHECK(!HasPendingOperations());
67
68 remove_keys_op_.reset(
69 new EasyUnlockRemoveKeysOperation(
70 user_context,
71 start_index,
72 base::Bind(&EasyUnlockKeyManager::OnKeysRemoved,
73 weak_ptr_factory_.GetWeakPtr(),
74 callback)));
75 remove_keys_op_->Start();
76 }
77
78 void EasyUnlockKeyManager::GetDeviceDataList(
79 const UserContext& user_context,
80 const GetDeviceDataListCallback& callback) {
81 // No write operations.
82 DCHECK(!create_keys_op_ && !remove_keys_op_);
83
84 const int op_id = GetNextOperationId();
85 scoped_ptr<EasyUnlockGetKeysOperation> op(new EasyUnlockGetKeysOperation(
86 user_context,
87 base::Bind(&EasyUnlockKeyManager::OnKeysFetched,
88 weak_ptr_factory_.GetWeakPtr(),
89 op_id,
90 callback)));
91 op->Start();
92 get_keys_ops_[op_id] = op.release();
93 }
94
95 // static
96 void EasyUnlockKeyManager::DeviceDataToRemoteDeviceDictionary(
97 const EasyUnlockDeviceKeyData& data,
98 base::DictionaryValue* dict) {
99 dict->SetString(kKeyBluetoothAddress, data.bluetooth_address);
100 dict->SetString(kKeyPsk, data.psk);
101 scoped_ptr<base::DictionaryValue> permit_record(new base::DictionaryValue);
102 permit_record->SetString(kKeyId, data.public_key);
103 dict->Set(kKeyPermitRecord, permit_record.release());
104 }
105
106 // static
107 bool EasyUnlockKeyManager::RemoteDeviceDictionaryToDeviceData(
108 const base::DictionaryValue& dict,
109 EasyUnlockDeviceKeyData* data) {
110 std::string bluetooth_address;
111 std::string public_key;
112 std::string psk;
113
114 if (!dict.GetString(kKeyBluetoothAddress, &bluetooth_address) ||
115 !dict.GetString(kKeyPermitId, &public_key) ||
116 !dict.GetString(kKeyPsk, &psk)) {
117 return false;
118 }
119
120 data->bluetooth_address.swap(bluetooth_address);
121 data->public_key.swap(public_key);
122 data->psk.swap(psk);
123 return true;
124 }
125
126 // static
127 void EasyUnlockKeyManager::DeviceDataListToRemoteDeviceList(
128 const EasyUnlockDeviceKeyDataList& data_list,
129 base::ListValue* device_list) {
130 device_list->Clear();
131 for (size_t i = 0; i < data_list.size(); ++i) {
132 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue);
133 DeviceDataToRemoteDeviceDictionary(data_list[i], device_dict.get());
134 device_list->Append(device_dict.release());
135 }
136 }
137
138 // static
139 bool EasyUnlockKeyManager::RemoteDeviceListToDeviceDataList(
140 const base::ListValue& device_list,
141 EasyUnlockDeviceKeyDataList* data_list) {
142 EasyUnlockDeviceKeyDataList parsed_devices;
143 for (base::ListValue::const_iterator it = device_list.begin();
144 it != device_list.end();
145 ++it) {
146 const base::DictionaryValue* dict;
147 if (!(*it)->GetAsDictionary(&dict) || !dict)
148 return false;
149
150 EasyUnlockDeviceKeyData data;
151 if (!RemoteDeviceDictionaryToDeviceData(*dict, &data))
152 return false;
153
154 parsed_devices.push_back(data);
155 }
156
157 data_list->swap(parsed_devices);
158 return true;
159 }
160
161 // static
162 std::string EasyUnlockKeyManager::GetKeyLabel(size_t key_index) {
163 return base::StringPrintf("%s%zu", kKeyLabelPrefix, key_index);
164 }
165
166 bool EasyUnlockKeyManager::HasPendingOperations() const {
167 return create_keys_op_ || remove_keys_op_ || !get_keys_ops_.empty();
168 }
169
170 int EasyUnlockKeyManager::GetNextOperationId() {
171 return ++operation_id_;
172 }
173
174 void EasyUnlockKeyManager::OnKeysCreated(
175 const UserContext& user_context,
176 const EasyUnlockDeviceKeyDataList& devices,
177 const RefreshKeysCallback& callback,
178 bool create_success) {
179 scoped_ptr<EasyUnlockCreateKeysOperation> op = create_keys_op_.Pass();
180 if (!callback.is_null())
181 callback.Run(create_success);
182
183 // Remove extra existing keys.
184 RemoveKeys(user_context, devices.size(), RemoveKeysCallback());
185 }
186
187 void EasyUnlockKeyManager::OnKeysRemoved(const RemoveKeysCallback& callback,
188 bool remove_success) {
189 scoped_ptr<EasyUnlockRemoveKeysOperation> op = remove_keys_op_.Pass();
190 if (!callback.is_null())
191 callback.Run(remove_success);
192 }
193
194 void EasyUnlockKeyManager::OnKeysFetched(
195 int op_id,
196 const GetDeviceDataListCallback& callback,
197 bool fetch_success,
198 const EasyUnlockDeviceKeyDataList& fetched_data) {
199 std::map<int, EasyUnlockGetKeysOperation*>::iterator it =
200 get_keys_ops_.find(op_id);
201 scoped_ptr<EasyUnlockGetKeysOperation> op;
202 if (it != get_keys_ops_.end()) {
203 op.reset(it->second);
204 get_keys_ops_.erase(it);
205 } else {
206 NOTREACHED();
207 }
208
209 if (!callback.is_null())
210 callback.Run(fetch_success, fetched_data);
211 }
212
213 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698