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

Commit f01ffa2c authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Use AddressWithType in PairingHandlerLe

Bug: 142341141
Test: updated unittests
Change-Id: Ic543148ca2d36f44e64ffb00744adf6d557b84c3
parent 6298315f
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include "common/bidi_queue.h"
#include "common/callback.h"
#include "crypto_toolbox/crypto_toolbox.h"
#include "hci/address_with_type.h"
#include "hci/le_security_interface.h"
#include "os/handler.h"
#include "packet/base_packet_builder.h"
@@ -42,7 +43,7 @@ using DistributedKeys =

/* This class represents the result of pairing, as returned from Pairing Handler */
struct PairingResult {
  Address connection_address;
  hci::AddressWithType connection_address;
  DistributedKeys distributed_keys;
};

@@ -62,8 +63,7 @@ struct MyOobData {
/* This structure is filled and send to PairingHandlerLe to initiate the Pairing process with remote device */
struct InitialInformations {
  hci::Role my_role;
  Address my_connection_address;
  uint8_t my_connection_address_type;
  hci::AddressWithType my_connection_address;

  /* My capabilities, as in pairing request/response */
  struct {
@@ -78,8 +78,7 @@ struct InitialInformations {
  /* was it remote device that initiated the Pairing ? */
  bool remotely_initiated;
  uint16_t connection_handle;
  Address remote_connection_address;
  uint8_t remote_connection_address_type;
  hci::AddressWithType remote_connection_address;
  std::string remote_name;

  /* contains pairing request, if the pairing was remotely initiated */
+15 −11
Original line number Diff line number Diff line
@@ -130,9 +130,10 @@ StkOrFailure PairingHandlerLe::DoLegacyStage2(const InitialInformations& i, cons
    // LOG(INFO) << +(IAmMaster(i)) << " i.i.remote_connection_address.address = " << i.remote_connection_address;
    // LOG(INFO) << +(IAmMaster(i)) << " i.my_connection_address_type = " << +i.my_connection_address_type;
    // LOG(INFO) << +(IAmMaster(i)) << " i.i.my_connection_address.address = " << i.my_connection_address;
    Octet16 mconfirm = crypto_toolbox::c1(tk, mrand, preq.data(), pres.data(), i.my_connection_address_type,
                                          i.my_connection_address.address, i.remote_connection_address_type,
                                          i.remote_connection_address.address);
    Octet16 mconfirm = crypto_toolbox::c1(
        tk, mrand, preq.data(), pres.data(), (uint8_t)i.my_connection_address.GetAddressType(),
        i.my_connection_address.GetAddress().address, (uint8_t)i.remote_connection_address.GetAddressType(),
        i.remote_connection_address.GetAddress().address);

    LOG_INFO("Master sends Mconfirm");
    SendL2capPacket(i, PairingConfirmBuilder::Create(mconfirm));
@@ -162,9 +163,10 @@ StkOrFailure PairingHandlerLe::DoLegacyStage2(const InitialInformations& i, cons
    // LOG(INFO) << +(IAmMaster(i)) << " i.i.my_connection_address.address = " << i.my_connection_address;
    // LOG(INFO) << +(IAmMaster(i)) << " i.remote_connection_address_type = " << +i.remote_connection_address_type;
    // LOG(INFO) << +(IAmMaster(i)) << " i.i.remote_connection_address.address = " << i.remote_connection_address;
    Octet16 sconfirm_generated = crypto_toolbox::c1(tk, srand, preq.data(), pres.data(), i.my_connection_address_type,
                                                    i.my_connection_address.address, i.remote_connection_address_type,
                                                    i.remote_connection_address.address);
    Octet16 sconfirm_generated = crypto_toolbox::c1(
        tk, srand, preq.data(), pres.data(), (uint8_t)i.my_connection_address.GetAddressType(),
        i.my_connection_address.GetAddress().address, (uint8_t)i.remote_connection_address.GetAddressType(),
        i.remote_connection_address.GetAddress().address);

    if (sconfirm != sconfirm_generated) {
      LOG_INFO("sconfirm does not match generated value");
@@ -178,9 +180,10 @@ StkOrFailure PairingHandlerLe::DoLegacyStage2(const InitialInformations& i, cons
    std::vector<uint8_t> preq(pairing_request.begin(), pairing_request.end());
    std::vector<uint8_t> pres(pairing_response.begin(), pairing_response.end());

    Octet16 sconfirm = crypto_toolbox::c1(tk, srand, preq.data(), pres.data(), i.remote_connection_address_type,
                                          i.remote_connection_address.address, i.my_connection_address_type,
                                          i.my_connection_address.address);
    Octet16 sconfirm = crypto_toolbox::c1(
        tk, srand, preq.data(), pres.data(), (uint8_t)i.remote_connection_address.GetAddressType(),
        i.remote_connection_address.GetAddress().address, (uint8_t)i.my_connection_address.GetAddressType(),
        i.my_connection_address.GetAddress().address);

    LOG_INFO("Slave waits for the Mconfirm");
    auto mconfirm_pkt = WaitPairingConfirm();
@@ -208,8 +211,9 @@ StkOrFailure PairingHandlerLe::DoLegacyStage2(const InitialInformations& i, cons
    // LOG(INFO) << +(IAmMaster(i)) << " i.remote_connection_address_type = " << +i.remote_connection_address_type;
    // LOG(INFO) << +(IAmMaster(i)) << " i.i.remote_connection_address.address = " << i.remote_connection_address;
    Octet16 mconfirm_generated = crypto_toolbox::c1(
        tk, mrand, preq.data(), pres.data(), i.remote_connection_address_type, i.remote_connection_address.address,
        i.my_connection_address_type, i.my_connection_address.address);
        tk, mrand, preq.data(), pres.data(), (uint8_t)i.remote_connection_address.GetAddressType(),
        i.remote_connection_address.GetAddress().address, (uint8_t)i.my_connection_address.GetAddressType(),
        i.my_connection_address.GetAddress().address);

    if (mconfirm != mconfirm_generated) {
      LOG_INFO("mconfirm does not match generated value");
+8 −4
Original line number Diff line number Diff line
@@ -134,11 +134,15 @@ Stage2ResultOrFailure PairingHandlerLe::DoSecureConnectionsStage2(const InitialI
  uint8_t b[7];

  if (IAmMaster(i)) {
    memcpy(a, i.my_connection_address.address, 7);
    memcpy(b, i.remote_connection_address.address, 7);
    memcpy(a, i.my_connection_address.GetAddress().address, 6);
    a[6] = (uint8_t)i.my_connection_address.GetAddressType();
    memcpy(b, i.remote_connection_address.GetAddress().address, 6);
    b[6] = (uint8_t)i.remote_connection_address.GetAddressType();
  } else {
    memcpy(a, i.remote_connection_address.address, 7);
    memcpy(b, i.my_connection_address.address, 7);
    memcpy(a, i.remote_connection_address.GetAddress().address, 6);
    a[6] = (uint8_t)i.remote_connection_address.GetAddressType();
    memcpy(b, i.my_connection_address.GetAddress().address, 6);
    b[6] = (uint8_t)i.my_connection_address.GetAddressType();
  }

  Octet16 ltk, mac_key;
+10 −6
Original line number Diff line number Diff line
@@ -120,8 +120,7 @@ class PairingHandlerUnitTest : public testing::Test {

InitialInformations initial_informations{
    .my_role = hci::Role::MASTER,
    .my_connection_address = {},
    .my_connection_address_type = 0x00,
    .my_connection_address = {{}, hci::AddressType::PUBLIC_DEVICE_ADDRESS},

    .myPairingCapabilities = {.io_capability = IoCapability::NO_INPUT_NO_OUTPUT,
                              .oob_data_flag = OobDataFlag::NOT_PRESENT,
@@ -131,8 +130,7 @@ InitialInformations initial_informations{
                              .responder_key_distribution = 0x03},

    .remotely_initiated = false,
    .remote_connection_address = {},
    .remote_connection_address_type = 0x01,
    .remote_connection_address = {{}, hci::AddressType::RANDOM_DEVICE_ADDRESS},
    .ui_handler = &uiMock,
    .le_security_interface = &leSecurityMock,
    .OnPairingFinished = OnPairingFinished,
@@ -221,8 +219,10 @@ TEST_F(PairingHandlerUnitTest, test_secure_connections_just_works) {
  // Start of authentication stage 2
  uint8_t a[7];
  uint8_t b[7];
  memcpy(b, initial_informations.remote_connection_address.address, 7);
  memcpy(a, initial_informations.my_connection_address.address, 7);
  memcpy(b, initial_informations.remote_connection_address.GetAddress().address, 6);
  b[6] = (uint8_t)initial_informations.remote_connection_address.GetAddressType();
  memcpy(a, initial_informations.my_connection_address.GetAddress().address, 6);
  a[6] = (uint8_t)initial_informations.my_connection_address.GetAddressType();

  Octet16 ltk, mac_key;
  crypto_toolbox::f5(dhkey.data(), Na, Nb, a, b, &mac_key, &ltk);
@@ -254,6 +254,7 @@ TEST_F(PairingHandlerUnitTest, test_secure_connections_just_works) {

InitialInformations initial_informations_trsi{
    .my_role = hci::Role::MASTER,
    .my_connection_address = hci::AddressWithType(),

    .myPairingCapabilities = {.io_capability = IoCapability::NO_INPUT_NO_OUTPUT,
                              .oob_data_flag = OobDataFlag::NOT_PRESENT,
@@ -263,6 +264,7 @@ InitialInformations initial_informations_trsi{
                              .responder_key_distribution = 0x03},

    .remotely_initiated = true,
    .remote_connection_address = hci::AddressWithType(),
    .ui_handler = &uiMock,
    .le_security_interface = &leSecurityMock,
    .OnPairingFinished = OnPairingFinished,
@@ -289,6 +291,7 @@ TEST_F(PairingHandlerUnitTest, test_remote_slave_initiating) {

InitialInformations initial_informations_trmi{
    .my_role = hci::Role::SLAVE,
    .my_connection_address = hci::AddressWithType(),

    .myPairingCapabilities = {.io_capability = IoCapability::NO_INPUT_NO_OUTPUT,
                              .oob_data_flag = OobDataFlag::NOT_PRESENT,
@@ -298,6 +301,7 @@ InitialInformations initial_informations_trmi{
                              .responder_key_distribution = 0x03},

    .remotely_initiated = true,
    .remote_connection_address = hci::AddressWithType(),
    .pairing_request = PairingRequestView::Create(BuilderToView(
        PairingRequestBuilder::Create(IoCapability::NO_INPUT_NO_OUTPUT, OobDataFlag::NOT_PRESENT,
                                      AuthReqMaskBondingFlag | AuthReqMaskMitm | AuthReqMaskSc, 16, 0x03, 0x03))),
+2 −2
Original line number Diff line number Diff line
@@ -32,8 +32,8 @@ class UIMock : public UI {
  UIMock() {}
  ~UIMock() override = default;

  MOCK_METHOD2(DisplayPairingPrompt, void(const bluetooth::hci::Address&, std::string&));
  MOCK_METHOD1(CancelPairingPrompt, void(const bluetooth::hci::Address&));
  MOCK_METHOD2(DisplayPairingPrompt, void(const bluetooth::hci::AddressWithType&, std::string&));
  MOCK_METHOD1(CancelPairingPrompt, void(const bluetooth::hci::AddressWithType&));
  MOCK_METHOD1(DisplayConfirmValue, void(uint32_t));
  MOCK_METHOD0(DisplayEnterPasskeyDialog, void());
  MOCK_METHOD1(DisplayPasskey, void(uint32_t));
Loading