OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/proximity_auth/remote_device_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "components/proximity_auth/cryptauth/secure_message_delegate.h" |
| 9 #include "components/proximity_auth/logging/logging.h" |
| 10 |
| 11 namespace proximity_auth { |
| 12 |
| 13 RemoteDeviceLoader::RemoteDeviceLoader( |
| 14 const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys, |
| 15 const std::string& user_id, |
| 16 const std::string& user_private_key, |
| 17 scoped_ptr<SecureMessageDelegate> secure_message_delegate) |
| 18 : remaining_unlock_keys_(unlock_keys), |
| 19 user_id_(user_id), |
| 20 user_private_key_(user_private_key), |
| 21 secure_message_delegate_(secure_message_delegate.Pass()), |
| 22 weak_ptr_factory_(this) {} |
| 23 |
| 24 RemoteDeviceLoader::~RemoteDeviceLoader() {} |
| 25 |
| 26 void RemoteDeviceLoader::Load(const RemoteDeviceCallback& callback) { |
| 27 DCHECK(callback_.is_null()); |
| 28 callback_ = callback; |
| 29 PA_LOG(INFO) << "Loading " << remaining_unlock_keys_.size() |
| 30 << " remote devices"; |
| 31 |
| 32 if (remaining_unlock_keys_.empty()) { |
| 33 callback_.Run(remote_devices_); |
| 34 return; |
| 35 } |
| 36 |
| 37 std::vector<cryptauth::ExternalDeviceInfo> all_unlock_keys = |
| 38 remaining_unlock_keys_; |
| 39 |
| 40 for (const auto& unlock_key : all_unlock_keys) { |
| 41 secure_message_delegate_->DeriveKey( |
| 42 user_private_key_, unlock_key.public_key(), |
| 43 base::Bind(&RemoteDeviceLoader::OnPSKDerived, |
| 44 weak_ptr_factory_.GetWeakPtr(), unlock_key)); |
| 45 } |
| 46 } |
| 47 |
| 48 void RemoteDeviceLoader::OnPSKDerived( |
| 49 const cryptauth::ExternalDeviceInfo& unlock_key, |
| 50 const std::string& psk) { |
| 51 std::string public_key = unlock_key.public_key(); |
| 52 auto iterator = std::find_if( |
| 53 remaining_unlock_keys_.begin(), remaining_unlock_keys_.end(), |
| 54 [&public_key](const cryptauth::ExternalDeviceInfo& unlock_key) { |
| 55 return unlock_key.public_key() == public_key; |
| 56 }); |
| 57 |
| 58 DCHECK(iterator != remaining_unlock_keys_.end()); |
| 59 remaining_unlock_keys_.erase(iterator); |
| 60 PA_LOG(INFO) << "Derived PSK for " << unlock_key.friendly_device_name() |
| 61 << ", " << remaining_unlock_keys_.size() << " keys remaining."; |
| 62 |
| 63 // TODO(tengs): We assume that devices without a |bluetooth_address| field are |
| 64 // BLE devices. Ideally, we should have a separate field for this information. |
| 65 RemoteDevice::BluetoothType bluetooth_type = |
| 66 unlock_key.bluetooth_address().empty() ? RemoteDevice::BLUETOOTH_LE |
| 67 : RemoteDevice::BLUETOOTH_CLASSIC; |
| 68 remote_devices_.push_back(RemoteDevice( |
| 69 user_id_, unlock_key.friendly_device_name(), unlock_key.public_key(), |
| 70 bluetooth_type, unlock_key.bluetooth_address(), psk, std::string())); |
| 71 |
| 72 if (!remaining_unlock_keys_.size()) |
| 73 callback_.Run(remote_devices_); |
| 74 } |
| 75 |
| 76 } // namespace proximity_auth |
OLD | NEW |