Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c59a163b authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "RootCanal: Add RPA resolution for connections" am: 0814c43c am: 938d1a86 am: 3dd58129

Original change: https://android-review.googlesource.com/c/platform/system/bt/+/1865654

Change-Id: I65776545435fa9f65a542e3a116bef22413641f2
parents 13c0233d 3dd58129
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ cc_library_static {
        "net/posix/posix_async_socket_server.cc",
        ":BluetoothPacketSources",
        ":BluetoothHciClassSources",
        ":BluetoothCryptoToolboxSources",
    ],
    cflags: [
        "-Wall",
+3 −6
Original line number Diff line number Diff line
@@ -1998,16 +1998,13 @@ void DualModeController::LeAddDeviceToResolvingList(CommandView command) {
      gd_hci::LeSecurityCommandView::Create(command));
  ASSERT(command_view.IsValid());

  uint8_t addr_type =
  auto addr_type =
      static_cast<uint8_t>(command_view.GetPeerIdentityAddressType());
  Address address = command_view.GetPeerIdentityAddress();
  std::array<uint8_t, LinkLayerController::kIrk_size> peerIrk =
      command_view.GetPeerIrk();
  std::array<uint8_t, LinkLayerController::kIrk_size> localIrk =
      command_view.GetLocalIrk();

  auto status = link_layer_controller_.LeResolvingListAddDevice(
      address, addr_type, peerIrk, localIrk);
      address, addr_type, command_view.GetPeerIrk(),
      command_view.GetLocalIrk());
  auto packet =
      bluetooth::hci::LeAddDeviceToResolvingListCompleteBuilder::Create(
          kNumCommandPackets, status);
+33 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include <hci/hci_packets.h>

#include "crypto_toolbox/crypto_toolbox.h"
#include "include/le_advertisement.h"
#include "os/log.h"
#include "packet/raw_builder.h"
@@ -1143,6 +1144,19 @@ void LinkLayerController::IncomingKeypressNotificationPacket(
  }
}

static bool rpa_matches_irk(
    Address rpa, std::array<uint8_t, LinkLayerController::kIrkSize> irk) {
  // 1.3.2.3 Private device address resolution
  uint8_t hash[3] = {rpa.address[0], rpa.address[1], rpa.address[2]};
  uint8_t prand[3] = {rpa.address[3], rpa.address[4], rpa.address[5]};

  // generate X = E irk(R0, R1, R2) and R is random address 3 LSO
  auto x = bluetooth::crypto_toolbox::aes_128(irk, &prand[0], 3);

  // If the hashes match, this is the IRK
  return (memcmp(x.data(), &hash[0], 3) == 0);
}

void LinkLayerController::IncomingLeAdvertisementPacket(
    model::packets::LinkLayerPacketView incoming) {
  // TODO: Handle multiple advertisements per packet.
@@ -1226,13 +1240,27 @@ void LinkLayerController::IncomingLeAdvertisementPacket(
    SendLeLinkLayerPacket(std::move(to_send));
  }

  Address resolved_address = address;
  uint8_t resolved_address_type = static_cast<uint8_t>(address_type);
  bool resolved = false;
  for (const auto& entry : le_resolving_list_) {
    if (rpa_matches_irk(address, entry.peer_irk)) {
      resolved = true;
      resolved_address = entry.address;
      resolved_address_type = entry.address_type;
    }
  }

  // Connect
  if ((le_connect_ && le_peer_address_ == address &&
       le_peer_address_type_ == static_cast<uint8_t>(address_type) &&
       (adv_type == model::packets::AdvertisementType::ADV_IND ||
        adv_type == model::packets::AdvertisementType::ADV_DIRECT_IND)) ||
      (LeConnectListContainsDevice(address,
                                   static_cast<uint8_t>(address_type)))) {
                                   static_cast<uint8_t>(address_type))) ||
      (resolved &&
       LeConnectListContainsDevice(
           resolved_address, static_cast<uint8_t>(resolved_address_type)))) {
    if (!connections_.CreatePendingLeConnection(AddressWithType(
            address, static_cast<bluetooth::hci::AddressType>(address_type)))) {
      LOG_WARN(
@@ -2772,25 +2800,16 @@ ErrorCode LinkLayerController::LeConnectListAddDevice(Address addr,
}

ErrorCode LinkLayerController::LeResolvingListAddDevice(
    Address addr, uint8_t addr_type, std::array<uint8_t, kIrk_size> peerIrk,
    std::array<uint8_t, kIrk_size> localIrk) {
    Address addr, uint8_t addr_type, std::array<uint8_t, kIrkSize> peerIrk,
    std::array<uint8_t, kIrkSize> localIrk) {
  if (ResolvingListBusy()) {
    return ErrorCode::COMMAND_DISALLOWED;
  }
  std::tuple<Address, uint8_t, std::array<uint8_t, kIrk_size>,
             std::array<uint8_t, kIrk_size>>
      new_tuple = std::make_tuple(addr, addr_type, peerIrk, localIrk);
  for (size_t i = 0; i < le_connect_list_.size(); i++) {
    auto curr = le_connect_list_[i];
    if (std::get<0>(curr) == addr && std::get<1>(curr) == addr_type) {
      le_resolving_list_[i] = new_tuple;
      return ErrorCode::SUCCESS;
    }
  }
  if (LeResolvingListFull()) {
    return ErrorCode::MEMORY_CAPACITY_EXCEEDED;
  }
  le_resolving_list_.emplace_back(new_tuple);
  le_resolving_list_.emplace_back(
      ResolvingListEntry{addr, addr_type, peerIrk, localIrk});
  return ErrorCode::SUCCESS;
}

+10 −6
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ using ::bluetooth::hci::OpCode;

class LinkLayerController {
 public:
  static constexpr size_t kIrk_size = 16;
  static constexpr size_t kIrkSize = 16;

  LinkLayerController(const DeviceProperties& properties) : properties_(properties) {}
  ErrorCode SendCommandToRemoteByAddress(
@@ -176,8 +176,8 @@ class LinkLayerController {
  bool ResolvingListBusy();
  ErrorCode LeResolvingListClear();
  ErrorCode LeResolvingListAddDevice(Address addr, uint8_t addr_type,
                                     std::array<uint8_t, kIrk_size> peerIrk,
                                     std::array<uint8_t, kIrk_size> localIrk);
                                     std::array<uint8_t, kIrkSize> peerIrk,
                                     std::array<uint8_t, kIrkSize> localIrk);
  ErrorCode LeResolvingListRemoveDevice(Address addr, uint8_t addr_type);
  bool LeResolvingListContainsDevice(Address addr, uint8_t addr_type);
  bool LeResolvingListFull();
@@ -445,9 +445,13 @@ class LinkLayerController {
  std::vector<uint8_t> le_event_mask_;

  std::vector<std::tuple<Address, uint8_t>> le_connect_list_;
  std::vector<std::tuple<Address, uint8_t, std::array<uint8_t, kIrk_size>,
                         std::array<uint8_t, kIrk_size>>>
      le_resolving_list_;
  struct ResolvingListEntry {
    Address address;
    uint8_t address_type;
    std::array<uint8_t, kIrkSize> peer_irk;
    std::array<uint8_t, kIrkSize> local_irk;
  };
  std::vector<ResolvingListEntry> le_resolving_list_;

  std::array<LeAdvertiser, 7> advertisers_;