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

Commit 7f0b5eef authored by Hui Peng's avatar Hui Peng
Browse files

[Invisalign2] Add BTM_BleGetPeerLTK and BTM_BleGetPeerIRK

Use these APIs in csis_client.cc

Bug: 301661850
Test: m com.android.btservices
Change-Id: I7a49a0eceec9c6e17c7de89e69109286cdfab349
parent f00a822c
Loading
Loading
Loading
Loading
+8 −9
Original line number Original line Diff line number Diff line
@@ -46,6 +46,7 @@
#include "stack/btm/btm_sec.h"
#include "stack/btm/btm_sec.h"
#include "stack/crypto_toolbox/crypto_toolbox.h"
#include "stack/crypto_toolbox/crypto_toolbox.h"
#include "stack/gatt/gatt_int.h"
#include "stack/gatt/gatt_int.h"
#include "stack/include/btm_ble_sec_api.h"


using base::Closure;
using base::Closure;
using bluetooth::Uuid;
using bluetooth::Uuid;
@@ -1212,17 +1213,16 @@ class CsisClientImpl : public CsisClient {
   */
   */
  bool sdf(const RawAddress& address, const Octet16& encrypted_sirk,
  bool sdf(const RawAddress& address, const Octet16& encrypted_sirk,
           Octet16& sirk) {
           Octet16& sirk) {
    tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
    auto pltk = BTM_BleGetPeerLTK(address);
    if (!p_dev_rec) {
    if (!pltk.has_value()) {
      LOG_ERROR("No security for %s", ADDRESS_TO_LOGGABLE_CSTR(address));
      LOG_ERROR("No security for %s", ADDRESS_TO_LOGGABLE_CSTR(address));
      return false;
      return false;
    }
    }


#ifdef CSIS_DEBUG
#ifdef CSIS_DEBUG
    LOG_INFO("LTK %s",
    auto irk = BTM_BleGetPeerIRK(address);
             (base::HexEncode(p_dev_rec->ble.keys.pltk.data(), 16)).c_str());
    LOG_INFO("LTK %s", (base::HexEncode(*pltk.data(), 16)).c_str());
    LOG_INFO("IRK %s",
    LOG_INFO("IRK %s", base::HexEncode(*irk.data(), 16).c_str());
             base::HexEncode(p_dev_rec->ble.keys.irk.data(), 16).c_str());
#endif
#endif


    /* Calculate salt CSIS d1.0r05 4.3 */
    /* Calculate salt CSIS d1.0r05 4.3 */
@@ -1238,11 +1238,10 @@ class CsisClientImpl : public CsisClient {
#ifdef CSIS_DEBUG
#ifdef CSIS_DEBUG
    LOG_INFO("s1 (le) %s", base::HexEncode(s1.data(), 16).c_str());
    LOG_INFO("s1 (le) %s", base::HexEncode(s1.data(), 16).c_str());
    /* Create K = LTK */
    /* Create K = LTK */
    LOG_INFO("K (le) %s",
    LOG_INFO("K (le) %s", base::HexEncode(*pltk.data(), 16).c_str());
             base::HexEncode(p_dev_rec->ble.keys.pltk.data(), 16).c_str());
#endif
#endif


    Octet16 T = crypto_toolbox::aes_cmac(s1, p_dev_rec->ble_keys.pltk);
    Octet16 T = crypto_toolbox::aes_cmac(s1, *pltk);


#ifdef CSIS_DEBUG
#ifdef CSIS_DEBUG
    LOG_INFO("T (le) %s", base::HexEncode(T.data(), 16).c_str());
    LOG_INFO("T (le) %s", base::HexEncode(T.data(), 16).c_str());
+15 −1
Original line number Original line Diff line number Diff line
@@ -17,6 +17,10 @@


#include "btm_api_mock.h"
#include "btm_api_mock.h"


#include <optional>

#include "bt_octets.h"
#include "stack/include/btm_ble_sec_api.h"
#include "types/raw_address.h"
#include "types/raw_address.h"


static bluetooth::manager::MockBtmInterface* btm_interface = nullptr;
static bluetooth::manager::MockBtmInterface* btm_interface = nullptr;
@@ -103,3 +107,13 @@ tBTM_INQ_INFO* BTM_InqDbNext(tBTM_INQ_INFO* p_cur) {
  LOG_ASSERT(btm_interface) << "Mock btm interface not set!";
  LOG_ASSERT(btm_interface) << "Mock btm interface not set!";
  return btm_interface->BTM_InqDbNext(p_cur);
  return btm_interface->BTM_InqDbNext(p_cur);
}
}

std::optional<Octet16> BTM_BleGetPeerLTK(const RawAddress address) {
  LOG_ASSERT(btm_interface) << "Mock btm interface not set!";
  return btm_interface->BTM_BleGetPeerLTK(address);
}

std::optional<Octet16> BTM_BleGetPeerIRK(const RawAddress address) {
  LOG_ASSERT(btm_interface) << "Mock btm interface not set!";
  return btm_interface->BTM_BleGetPeerIRK(address);
}
+12 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,9 @@


#include <gmock/gmock.h>
#include <gmock/gmock.h>


#include <optional>

#include "bt_octets.h"
#include "btm_api.h"
#include "btm_api.h"
#include "stack/btm/security_device_record.h"
#include "stack/btm/security_device_record.h"
#include "types/raw_address.h"
#include "types/raw_address.h"
@@ -54,6 +57,11 @@ class BtmInterface {
  virtual void AclDisconnectFromHandle(uint16_t handle, tHCI_STATUS reason) = 0;
  virtual void AclDisconnectFromHandle(uint16_t handle, tHCI_STATUS reason) = 0;
  virtual tBTM_INQ_INFO* BTM_InqDbFirst() = 0;
  virtual tBTM_INQ_INFO* BTM_InqDbFirst() = 0;
  virtual tBTM_INQ_INFO* BTM_InqDbNext(tBTM_INQ_INFO* p_cur) = 0;
  virtual tBTM_INQ_INFO* BTM_InqDbNext(tBTM_INQ_INFO* p_cur) = 0;
  virtual std::optional<Octet16> BTM_BleGetPeerLTK(
      const RawAddress address) = 0;
  virtual std::optional<Octet16> BTM_BleGetPeerIRK(
      const RawAddress address) = 0;

  virtual ~BtmInterface() = default;
  virtual ~BtmInterface() = default;
};
};


@@ -95,6 +103,10 @@ class MockBtmInterface : public BtmInterface {
  MOCK_METHOD((tBTM_INQ_INFO*), BTM_InqDbFirst, (), (override));
  MOCK_METHOD((tBTM_INQ_INFO*), BTM_InqDbFirst, (), (override));
  MOCK_METHOD((tBTM_INQ_INFO*), BTM_InqDbNext, (tBTM_INQ_INFO * p_cur),
  MOCK_METHOD((tBTM_INQ_INFO*), BTM_InqDbNext, (tBTM_INQ_INFO * p_cur),
              (override));
              (override));
  MOCK_METHOD((std::optional<Octet16>), BTM_BleGetPeerLTK,
              (const RawAddress address), (override));
  MOCK_METHOD((std::optional<Octet16>), BTM_BleGetPeerIRK,
              (const RawAddress address), (override));
};
};


/**
/**
+20 −0
Original line number Original line Diff line number Diff line
@@ -15,6 +15,8 @@
 *
 *
 */
 */


#include <cstddef>
#include <optional>
#define LOG_TAG "ble_sec"
#define LOG_TAG "ble_sec"


#include <base/strings/stringprintf.h>
#include <base/strings/stringprintf.h>
@@ -1941,3 +1943,21 @@ bool btm_ble_get_acl_remote_addr(uint16_t hci_handle, RawAddress& conn_addr,
  }
  }
  return st;
  return st;
}
}

std::optional<Octet16> BTM_BleGetPeerLTK(const RawAddress address) {
  tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
  if (p_dev_rec == nullptr) {
    return std::nullopt;
  }

  return p_dev_rec->ble_keys.pltk;
}

std::optional<Octet16> BTM_BleGetPeerIRK(const RawAddress address) {
  tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
  if (p_dev_rec == nullptr) {
    return std::nullopt;
  }

  return p_dev_rec->ble_keys.irk;
}
+33 −0
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@
#include <hardware/bt_common_types.h>
#include <hardware/bt_common_types.h>


#include <cstdint>
#include <cstdint>
#include <optional>


#include "btm_ble_api_types.h"
#include "btm_ble_api_types.h"
#include "btm_ble_sec_api_types.h"
#include "btm_ble_sec_api_types.h"
@@ -202,3 +203,35 @@ bool BTM_BleVerifySignature(const RawAddress& bd_addr, uint8_t* p_orig,
 *
 *
 ******************************************************************************/
 ******************************************************************************/
void BTM_BleLoadLocalKeys(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key);
void BTM_BleLoadLocalKeys(uint8_t key_type, tBTM_BLE_LOCAL_KEYS* p_key);

/*******************************************************************************
 *
 * Function         BTM_BleGetPeerLTK
 *
 * Description      This function is used to get the long term key of
 *                  a bonded peer (LE) device.
 *
 * Parameters:      address: address of the peer device
 *
 * Returns          the ltk contained in std::optional if the remote device
 *                  is present in security database
 *                  std::nullopt if the device is not present
 *
 ******************************************************************************/
std::optional<Octet16> BTM_BleGetPeerLTK(const RawAddress address);

/*******************************************************************************
 *
 * Function         BTM_BleGetPeerIRK
 *
 * Description      This function is used to get the IRK of a bonded
 *                  peer (LE) device.
 *
 * Parameters:      address: address of the peer device
 *
 * Returns          the ltk contained in std::optional if the remote device
 *                  is present in security database
 *                  std::nullopt if the device is not present
 *
 ******************************************************************************/
std::optional<Octet16> BTM_BleGetPeerIRK(const RawAddress address);