File: remote_device_loader.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (96 lines) | stat: -rw-r--r-- 3,661 bytes parent folder | download
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
95
96
// 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 <algorithm>
#include <utility>

#include "base/base64url.h"
#include "base/bind.h"
#include "components/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,
    std::unique_ptr<cryptauth::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.
  cryptauth::RemoteDevice::BluetoothType bluetooth_type =
      unlock_key.bluetooth_address().empty()
          ? cryptauth::RemoteDevice::BLUETOOTH_LE
          : cryptauth::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(cryptauth::RemoteDevice(
      user_id_, unlock_key.friendly_device_name(), unlock_key.public_key(),
      bluetooth_type, bluetooth_address, psk, std::string()));

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

}  // namespace proximity_auth