summaryrefslogtreecommitdiffstats
path: root/components/proximity_auth/remote_device_loader.cc
blob: 59af64d4c2522a19cf9e6070c8bcdea205c05b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/proximity_auth/remote_device_loader.h"

#include <utility>

#include "base/base64url.h"
#include "base/bind.h"
#include "components/proximity_auth/cryptauth/secure_message_delegate.h"
#include "components/proximity_auth/logging/logging.h"
#include "components/proximity_auth/proximity_auth_pref_manager.h"

namespace proximity_auth {

RemoteDeviceLoader::RemoteDeviceLoader(
    const std::vector<cryptauth::ExternalDeviceInfo>& unlock_keys,
    const std::string& user_id,
    const std::string& user_private_key,
    scoped_ptr<SecureMessageDelegate> secure_message_delegate,
    ProximityAuthPrefManager* pref_manager)
    : remaining_unlock_keys_(unlock_keys),
      user_id_(user_id),
      user_private_key_(user_private_key),
      secure_message_delegate_(std::move(secure_message_delegate)),
      pref_manager_(pref_manager),
      weak_ptr_factory_(this) {}

RemoteDeviceLoader::~RemoteDeviceLoader() {}

void RemoteDeviceLoader::Load(const RemoteDeviceCallback& callback) {
  DCHECK(callback_.is_null());
  callback_ = callback;
  PA_LOG(INFO) << "Loading " << remaining_unlock_keys_.size()
               << " remote devices";

  if (remaining_unlock_keys_.empty()) {
    callback_.Run(remote_devices_);
    return;
  }

  std::vector<cryptauth::ExternalDeviceInfo> all_unlock_keys =
      remaining_unlock_keys_;

  for (const auto& unlock_key : all_unlock_keys) {
    secure_message_delegate_->DeriveKey(
        user_private_key_, unlock_key.public_key(),
        base::Bind(&RemoteDeviceLoader::OnPSKDerived,
                   weak_ptr_factory_.GetWeakPtr(), unlock_key));
  }
}

void RemoteDeviceLoader::OnPSKDerived(
    const cryptauth::ExternalDeviceInfo& unlock_key,
    const std::string& psk) {
  std::string public_key = unlock_key.public_key();
  auto iterator = std::find_if(
      remaining_unlock_keys_.begin(), remaining_unlock_keys_.end(),
      [&public_key](const cryptauth::ExternalDeviceInfo& unlock_key) {
        return unlock_key.public_key() == public_key;
      });

  DCHECK(iterator != remaining_unlock_keys_.end());
  remaining_unlock_keys_.erase(iterator);
  PA_LOG(INFO) << "Derived PSK for " << unlock_key.friendly_device_name()
               << ", " << remaining_unlock_keys_.size() << " keys remaining.";

  // TODO(tengs): We assume that devices without a |bluetooth_address| field are
  // BLE devices. Ideally, we should have a separate field for this information.
  RemoteDevice::BluetoothType bluetooth_type =
      unlock_key.bluetooth_address().empty() ? RemoteDevice::BLUETOOTH_LE
                                             : RemoteDevice::BLUETOOTH_CLASSIC;

  std::string bluetooth_address = unlock_key.bluetooth_address();
  if (bluetooth_address.empty() && pref_manager_) {
    std::string b64_public_key;
    base::Base64UrlEncode(unlock_key.public_key(),
                          base::Base64UrlEncodePolicy::INCLUDE_PADDING,
                          &b64_public_key);
    bluetooth_address = pref_manager_->GetDeviceAddress(b64_public_key);
    PA_LOG(INFO) << "The BLE address of " << unlock_key.friendly_device_name()
                 << " is " << bluetooth_address;
  }

  remote_devices_.push_back(RemoteDevice(
      user_id_, unlock_key.friendly_device_name(), unlock_key.public_key(),
      bluetooth_type, bluetooth_address, psk, std::string()));

  if (!remaining_unlock_keys_.size())
    callback_.Run(remote_devices_);
}

}  // namespace proximity_auth