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

Commit 659141b2 authored by Chris Manton's avatar Chris Manton
Browse files

gd: Expand shim security api

Bug: 140778599
Test: Compiles

Change-Id: I4b31e126e938ab9f1904966c097526ed7d37c736
parent 64c28f28
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -25,8 +25,15 @@
namespace bluetooth {
namespace shim {

using SimplePairingCallback = std::function<bool(std::string address, uint32_t value, bool just_works)>;

struct ISecurity {
  virtual void CreateBond(std::string address, uint8_t address_type, uint8_t transport) = 0;
  virtual void CreateBond(std::string address) = 0;
  virtual void CreateBondLe(std::string address, uint8_t address_type) = 0;
  virtual void CancelBond(std::string address) = 0;
  virtual void RemoveBond(std::string address) = 0;

  virtual void SetSimplePairingCallback(SimplePairingCallback callback) = 0;

  virtual ~ISecurity() {}
};
+136 −15
Original line number Diff line number Diff line
@@ -34,17 +34,68 @@ namespace shim {

namespace {
constexpr char kModuleName[] = "shim::Security";

constexpr uint8_t kLegacyAddressTypePublic = 0;
constexpr uint8_t kLegacyAddressTypeRandom = 1;
constexpr uint8_t kLegacyAddressTypePublicIdentity = 2;
constexpr uint8_t kLegacyAddressTypeRandomIdentity = 3;

}  // namespace

struct Security::impl {
  void CreateBond(std::string address, uint8_t address_type, uint8_t transport);
struct Security::impl : public security::ISecurityManagerListener {
  void OnDisplayYesNoDialogWithValue(const bluetooth::hci::AddressWithType& address, uint32_t numeric_value,
                                     common::OnceCallback<void(bool)> input_callback) {
    std::move(input_callback).Run(simple_pairing_callback_(address.ToString(), numeric_value, false));
  }

  void OnDisplayYesNoDialog(const bluetooth::hci::AddressWithType& address,
                            common::OnceCallback<void(bool)> input_callback) {
    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
  }

  void OnDisplayPasskeyDialog(const bluetooth::hci::AddressWithType& address, uint32_t passkey) {
    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
  }

  void OnDisplayPasskeyInputDialog(const bluetooth::hci::AddressWithType& address,
                                   common::OnceCallback<void(uint32_t)> input_callback) {
    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
  }

  void OnDisplayCancelDialog(const bluetooth::hci::AddressWithType& address) {
    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
  }

  void OnDeviceBonded(bluetooth::hci::AddressWithType device) {
    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
  }

  void OnDeviceUnbonded(bluetooth::hci::AddressWithType device) {
    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
  }

  void OnDeviceBondFailed(bluetooth::hci::AddressWithType device) {
    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
  }

  void CreateBond(hci::AddressWithType bdaddr);
  void CreateBondLe(hci::AddressWithType bdaddr);
  void CancelBond(hci::AddressWithType bdaddr);
  void RemoveBond(hci::AddressWithType bdaddr);

  os::Handler* Handler() /*override*/;

  void SetSimplePairingCallback(SimplePairingCallback callback);

  impl(bluetooth::security::SecurityModule* security_module, os::Handler* handler);
  ~impl();

  void Start();
  void Stop();

 private:
  SimplePairingCallback simple_pairing_callback_;

  std::unique_ptr<bluetooth::security::SecurityManager> security_manager_{nullptr};
  os::Handler* handler_;
};
@@ -60,24 +111,92 @@ os::Handler* Security::impl::Handler() {
  return handler_;
}

void Security::impl::CreateBond(std::string address, uint8_t address_type, uint8_t transport) {
  hci::Address bdaddr;
  if (!hci::Address::FromString(address, bdaddr)) {
    LOG_ERROR("%s bad address: %s, aborting", __func__, address.c_str());
void Security::impl::CreateBond(hci::AddressWithType bdaddr) {
  security_manager_->CreateBond(bdaddr);
}

void Security::impl::CreateBondLe(hci::AddressWithType bdaddr) {
  security_manager_->CreateBondLe(bdaddr);
}

void Security::impl::CancelBond(hci::AddressWithType bdaddr) {
  security_manager_->CancelBond(bdaddr);
}

void Security::impl::RemoveBond(hci::AddressWithType bdaddr) {
  security_manager_->RemoveBond(bdaddr);
}

void Security::impl::SetSimplePairingCallback(SimplePairingCallback callback) {
  ASSERT(!simple_pairing_callback_);
  simple_pairing_callback_ = callback;
}

void Security::impl::Start() {
  LOG_DEBUG("Starting security manager shim");
  security_manager_->RegisterCallbackListener(this, handler_);
}

void Security::impl::Stop() {
  security_manager_->UnregisterCallbackListener(this);
  LOG_DEBUG("Stopping security manager shim");
}

void Security::CreateBond(std::string string_address) {
  hci::Address address;
  if (!hci::Address::FromString(string_address, address)) {
    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
    return;
  }
  pimpl_->CreateBond(hci::AddressWithType{address, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
}

void Security::CreateBondLe(std::string string_address, uint8_t type) {
  hci::AddressType address_type;
  switch (type) {
    case kLegacyAddressTypePublic:
    default:
      address_type = hci::AddressType::PUBLIC_DEVICE_ADDRESS;
      break;
    case kLegacyAddressTypeRandom:
      address_type = hci::AddressType::RANDOM_DEVICE_ADDRESS;
      break;
    case kLegacyAddressTypePublicIdentity:
      address_type = hci::AddressType::PUBLIC_IDENTITY_ADDRESS;
      break;
    case kLegacyAddressTypeRandomIdentity:
      address_type = hci::AddressType::RANDOM_IDENTITY_ADDRESS;
      break;
  }

  hci::Address address;
  if (!hci::Address::FromString(string_address, address)) {
    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
    return;
  }
  pimpl_->CreateBondLe(hci::AddressWithType{address, address_type});
}

void Security::CancelBond(std::string string_address) {
  hci::Address address;
  if (!hci::Address::FromString(string_address, address)) {
    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
    return;
  }
  pimpl_->CancelBond(hci::AddressWithType{address, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
}

  if (transport == 0x01 /* BT_TRANSPORT_BR_EDR */)
    security_manager_->CreateBond(hci::AddressWithType{bdaddr, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
  else if (transport == 0x02 /* BT_TRANSPORT_LE */)
    security_manager_->CreateBondLe(
        hci::AddressWithType{bdaddr, static_cast<bluetooth::hci::AddressType>(address_type)});
  else
    LOG_ALWAYS_FATAL("Bad transport in CreateBond %d", transport);
void Security::RemoveBond(std::string string_address) {
  hci::Address address;
  if (!hci::Address::FromString(string_address, address)) {
    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
    return;
  }
  pimpl_->RemoveBond(hci::AddressWithType{address, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
}

void Security::CreateBond(std::string address, uint8_t address_type, uint8_t transport) {
  pimpl_->CreateBond(address, address_type, transport);
void Security::SetSimplePairingCallback(SimplePairingCallback callback) {
  pimpl_->SetSimplePairingCallback(callback);
}

/**
@@ -89,9 +208,11 @@ void Security::ListDependencies(ModuleList* list) {

void Security::Start() {
  pimpl_ = std::make_unique<impl>(GetDependency<bluetooth::security::SecurityModule>(), GetHandler());
  pimpl_->Start();
}

void Security::Stop() {
  pimpl_->Stop();
  pimpl_.reset();
}

+6 −1
Original line number Diff line number Diff line
@@ -29,7 +29,12 @@ class Security : public bluetooth::Module, public bluetooth::shim::ISecurity {
  Security() = default;
  ~Security() = default;

  void CreateBond(std::string address, uint8_t address_type, uint8_t transport) override;
  void CreateBond(std::string address) override;
  void CreateBondLe(std::string address, uint8_t address_type) override;
  void CancelBond(std::string address) override;
  void RemoveBond(std::string address) override;

  void SetSimplePairingCallback(SimplePairingCallback callback) override;

  static const ModuleFactory Factory;

+48 −2
Original line number Diff line number Diff line
@@ -605,7 +605,53 @@ tBTM_STATUS bluetooth::shim::Btm::CreateBond(const RawAddress& bd_addr,
                                             tBT_TRANSPORT transport,
                                             uint8_t pin_len, uint8_t* p_pin,
                                             uint32_t trusted_mask[]) {
  bluetooth::shim::GetSecurity()->CreateBond(bd_addr.ToString(), addr_type,
                                             transport);
  switch (transport) {
    case BT_TRANSPORT_BR_EDR:
      bluetooth::shim::GetSecurity()->CreateBond(bd_addr.ToString());
      break;
    case BT_TRANSPORT_LE:
      bluetooth::shim::GetSecurity()->CreateBondLe(bd_addr.ToString(),
                                                   addr_type);
      break;
    default:
      return bluetooth::shim::BTM_ILLEGAL_VALUE;
  }
  return bluetooth::shim::BTM_SUCCESS;
}

bool bluetooth::shim::Btm::CancelBond(const RawAddress& bd_addr) {
  bluetooth::shim::GetSecurity()->CancelBond(bd_addr.ToString());
  return true;
}

bool bluetooth::shim::Btm::RemoveBond(const RawAddress& bd_addr) {
  // TODO(cmanton) Check if acl is connected
  bluetooth::shim::GetSecurity()->RemoveBond(bd_addr.ToString());
  return true;
}

void bluetooth::shim::Btm::SetSimplePairingCallback(
    tBTM_SP_CALLBACK* callback) {
  simple_pairing_callback_ = callback;

  bluetooth::shim::GetSecurity()->SetSimplePairingCallback(
      [this](std::string address_string, uint32_t value, bool just_works) {
        RawAddress address;
        RawAddress::FromString(address_string, address);

        tBTM_SP_EVT event = BTM_SP_CFM_REQ_EVT;
        tBTM_SP_EVT_DATA data;

        data.cfm_req.bd_addr = address;
        data.cfm_req.num_val = value;
        data.cfm_req.just_works = just_works;
        switch (simple_pairing_callback_(event, &data)) {
          case BTM_SUCCESS:
          case BTM_SUCCESS_NO_SECURITY:
            return true;
          default:
            break;
        }
        return false;
      });
}
+6 −0
Original line number Diff line number Diff line
@@ -239,6 +239,10 @@ class Btm {
  tBTM_STATUS CreateBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
                         tBT_TRANSPORT transport, uint8_t pin_len,
                         uint8_t* p_pin, uint32_t trusted_mask[]);
  bool CancelBond(const RawAddress& bd_addr);
  bool RemoveBond(const RawAddress& bd_addr);

  void SetSimplePairingCallback(tBTM_SP_CALLBACK* callback);

 private:
  ReadRemoteName le_read_remote_name_;
@@ -251,6 +255,8 @@ class Btm {

  LegacyInquiryCompleteCallback legacy_inquiry_complete_callback_{};

  tBTM_SP_CALLBACK* simple_pairing_callback_{nullptr};

  uint8_t active_inquiry_mode_ = 0;

  // TODO(cmanton) abort if there is no classic acl link up
Loading