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

Commit e5c71dd3 authored by Pavlin Radoslavov's avatar Pavlin Radoslavov
Browse files

Fix the logic for removing bonded devices

* Removed btif_storage_is_device_bonded(), because it is not needed,
  and it was giving the wrong answer in use cases like Smart Setup
  with BR/EDR connections.

* Added a call to btif_storage_remove_ble_bonding_keys()
  within btif_storage_remove_bonded_device() so the bonded device
  state is properly removed.

* Don't save the BLE bonding keys if it is temporary bonding

Bug: 22233299
Change-Id: I33d9f76a124acc60173f0acaa517bc29ee6603e8
parent 0ee71847
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -133,18 +133,6 @@ bt_status_t btif_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
*******************************************************************************/
bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr);

/******************************************************************************
**
** Function         btif_storage_is_device_bonded
**
** Description      BTIF storage API - checks if device present in bonded list
**
** Returns          TRUE if the device is bonded,
**                  FALSE otherwise
**
*******************************************************************************/
BOOLEAN btif_storage_is_device_bonded(bt_bdaddr_t *remote_bd_addr);

/*******************************************************************************
**
** Function         btif_storage_remove_bonded_device
+19 −12
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@
#include "include/stack_config.h"
#include "osi/include/log.h"
#include "osi/include/allocator.h"
#include "stack/btm/btm_int.h"

/******************************************************************************
**  Constants & Macros
@@ -92,10 +93,6 @@

#define MAX_SDP_BL_ENTRIES 3

#define BOND_TYPE_UNKNOWN     0
#define BOND_TYPE_PERSISTENT  1
#define BOND_TYPE_TEMPORARY   2

#define ENCRYPTED_BREDR       2
#define ENCRYPTED_LE          4

@@ -104,7 +101,7 @@ typedef struct
    bt_bond_state_t state;
    bt_bdaddr_t static_bdaddr;
    BD_ADDR bd_addr;
    UINT8 bond_type;
    tBTM_BOND_TYPE bond_type;
    UINT8 pin_code_len;
    UINT8 is_ssp;
    UINT8 auth_req;
@@ -967,6 +964,8 @@ static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
    else
        pairing_cb.bond_type = BOND_TYPE_PERSISTENT;

    btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);

    pairing_cb.is_ssp = TRUE;

    /* If JustWorks auto-accept */
@@ -1083,9 +1082,7 @@ static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
            {
                BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
                        __FUNCTION__);
                if (btif_storage_is_device_bonded(&bd_addr) == TRUE) {
                btif_storage_remove_bonded_device(&bd_addr);
                }
                bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
                return;
            }
@@ -1702,6 +1699,7 @@ static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
            if (pairing_cb.state == BT_BOND_STATE_BONDING)
            {
                bdcpy(bd_addr.address, pairing_cb.bd_addr);
                btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
                bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
            }
            break;
@@ -1715,14 +1713,12 @@ static void btif_dm_upstreams_evt(UINT16 event, char* p_param)

        case BTA_DM_DEV_UNPAIRED_EVT:
            bdcpy(bd_addr.address, p_data->link_down.bd_addr);
            btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);

            /*special handling for HID devices */
            #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
            btif_hh_remove_device(bd_addr);
            #endif
            #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
            btif_storage_remove_ble_bonding_keys(&bd_addr);
            #endif
            btif_storage_remove_bonded_device(&bd_addr);
            bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
            break;
@@ -1763,6 +1759,7 @@ static void btif_dm_upstreams_evt(UINT16 event, char* p_param)

        case BTA_DM_LINK_DOWN_EVT:
            bdcpy(bd_addr.address, p_data->link_down.bd_addr);
            btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
            BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
            HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
                      &bd_addr, BT_ACL_STATE_DISCONNECTED);
@@ -2871,7 +2868,16 @@ static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
        bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
        if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
            btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);

        /* Test for temporary bonding */
        if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
            BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
                             __func__);
            btif_storage_remove_bonded_device(&bdaddr);
            state = BT_BOND_STATE_NONE;
        } else {
            btif_dm_save_ble_bonding_keys();
        }
        BTA_GATTC_Refresh(bd_addr.address);
        btif_dm_get_remote_services(&bd_addr);
    }
@@ -3055,6 +3061,7 @@ void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
    pairing_cb.is_le_only = TRUE;
    pairing_cb.is_le_nc = FALSE;
    pairing_cb.is_ssp = TRUE;
    btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);

    cod = COD_UNCLASSIFIED;

+5 −17
Original line number Diff line number Diff line
@@ -591,23 +591,6 @@ static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bd
 * the property->type.
 *******************************************************************************/

/*****************************************************************************
**
** Function         btif_storage_is_device_bonded
**
** Description      BTIF storage API - checks if device present in bonded list
**
** Returns          TRUE if the device is bonded,
**                  FALSE otherwise
**
*******************************************************************************/
BOOLEAN btif_storage_is_device_bonded(bt_bdaddr_t *remote_bd_addr)
{
    bdstr_t bdstr;
    bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
    return (btif_in_fetch_bonded_device(bdstr) == BT_STATUS_SUCCESS);
}

/*******************************************************************************
**
** Function         btif_storage_get_adapter_property
@@ -843,6 +826,11 @@ bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
    bdstr_t bdstr;
    bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
    BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);

#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
    btif_storage_remove_ble_bonding_keys(remote_bd_addr);
#endif

    int ret = 1;
    if(btif_config_exist(bdstr, "LinkKeyType"))
        ret &= btif_config_remove(bdstr, "LinkKeyType");
+45 −0
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ BOOLEAN BTM_SecAddDevice (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name,
            return(FALSE);
    }

    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;           /* Default value */
    p_dev_rec->timestamp = btm_cb.dev_rec_count++;

    if (dev_class)
@@ -288,6 +289,7 @@ tBTM_SEC_DEV_REC *btm_sec_alloc_dev (BD_ADDR bd_addr)

    }

    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;           /* Default value */
    p_dev_rec->sec_flags = BTM_SEC_IN_USE;

    /* Check with the BT manager if details about remote device are known */
@@ -336,6 +338,7 @@ tBTM_SEC_DEV_REC *btm_sec_alloc_dev (BD_ADDR bd_addr)
*******************************************************************************/
void btm_sec_free_dev (tBTM_SEC_DEV_REC *p_dev_rec)
{
    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
    p_dev_rec->sec_flags = 0;

#if BLE_INCLUDED == TRUE
@@ -499,8 +502,10 @@ void btm_consolidate_dev(tBTM_SEC_DEV_REC *p_target_rec)

                p_target_rec->new_encryption_key_is_p256 = temp_rec.new_encryption_key_is_p256;
                p_target_rec->no_smp_on_br = temp_rec.no_smp_on_br;
                p_target_rec->bond_type = temp_rec.bond_type;
                /* mark the combined record as unused */
                p_dev_rec->sec_flags &= ~BTM_SEC_IN_USE;
                p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
                break;
            }

@@ -512,6 +517,7 @@ void btm_consolidate_dev(tBTM_SEC_DEV_REC *p_target_rec)
                    p_target_rec->ble.ble_addr_type = p_dev_rec->ble.ble_addr_type;
                    p_target_rec->device_type |= p_dev_rec->device_type;
                    p_dev_rec->sec_flags &= ~BTM_SEC_IN_USE;
                    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
                }
                break;
            }
@@ -594,4 +600,43 @@ tBTM_SEC_DEV_REC *btm_find_oldest_dev (void)
    return(p_oldest);
}

/*******************************************************************************
**
** Function         btm_get_bond_type_dev
**
** Description      Get the bond type for a device in the device database
**                  with specified BD address
**
** Returns          The device bond type if known, otherwise BOND_TYPE_UNKNOWN
**
*******************************************************************************/
tBTM_BOND_TYPE btm_get_bond_type_dev(BD_ADDR bd_addr)
{
    tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);

    if (p_dev_rec == NULL)
        return BOND_TYPE_UNKNOWN;

    return p_dev_rec->bond_type;
}

/*******************************************************************************
**
** Function         btm_set_bond_type_dev
**
** Description      Set the bond type for a device in the device database
**                  with specified BD address
**
** Returns          TRUE on success, otherwise FALSE
**
*******************************************************************************/
BOOLEAN btm_set_bond_type_dev(BD_ADDR bd_addr, tBTM_BOND_TYPE bond_type)
{
    tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);

    if (p_dev_rec == NULL)
        return FALSE;

    p_dev_rec->bond_type = bond_type;
    return TRUE;
}
+13 −0
Original line number Diff line number Diff line
@@ -492,6 +492,15 @@ typedef struct

#endif  /* BLE_INCLUDED */

/* Peering bond type */
enum
{
    BOND_TYPE_UNKNOWN,
    BOND_TYPE_PERSISTENT,
    BOND_TYPE_TEMPORARY
};
typedef UINT8 tBTM_BOND_TYPE;

/*
** Define structure for Security Device Record.
** A record exists for each device authenticated with this device
@@ -586,6 +595,7 @@ typedef struct
    BOOLEAN no_smp_on_br;       /* if set to TRUE then SMP on BR/EDR doesn't */
                                /* work, i.e. link keys crosspairing */
                                /* SC BR/EDR->SC LE doesn't happen */
    tBTM_BOND_TYPE bond_type;   /* peering bond type */

#if BLE_INCLUDED == TRUE
    tBTM_SEC_BLE        ble;
@@ -1034,6 +1044,9 @@ extern void btm_sec_free_dev (tBTM_SEC_DEV_REC *p_dev_rec);
extern tBTM_SEC_DEV_REC  *btm_find_dev (BD_ADDR bd_addr);
extern tBTM_SEC_DEV_REC  *btm_find_or_alloc_dev (BD_ADDR bd_addr);
extern tBTM_SEC_DEV_REC  *btm_find_dev_by_handle (UINT16 handle);
extern tBTM_BOND_TYPE     btm_get_bond_type_dev(BD_ADDR bd_addr);
extern BOOLEAN            btm_set_bond_type_dev(BD_ADDR bd_addr,
                                                tBTM_BOND_TYPE bond_type);

/* Internal functions provided by btm_sec.c
**********************************************