Skip to content

Commit

Permalink
component/bt: support BLE Application Layer Encryption key size check
Browse files Browse the repository at this point in the history
  • Loading branch information
xiewenxiang committed Dec 3, 2020
1 parent 456d346 commit ea678c3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/bt/bluedroid/api/include/api/esp_gap_ble_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ typedef enum {
ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH,
/* Enable/Disable OOB support */
ESP_BLE_SM_OOB_SUPPORT,
/* Appl encryption key size */
ESP_BLE_APP_ENC_KEY_SIZE,
ESP_BLE_SM_MAX_PARAM,
} esp_ble_sm_param_t;

Expand Down
12 changes: 12 additions & 0 deletions components/bt/bluedroid/bta/dm/bta_dm_co.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tBTE_APPL_CFG bte_appl_cfg = {
BTM_BLE_MIN_KEY_SIZE,
BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE,
BTM_BLE_OOB_DISABLE,
BTM_BLE_APPL_ENC_KEY_SIZE,
};
#endif

Expand Down Expand Up @@ -427,6 +428,17 @@ void bta_dm_co_ble_set_min_key_size(UINT8 ble_key_size)
#endif ///SMP_INCLUDED == TRUE
}

void bta_dm_co_ble_set_appl_enc_key_size(UINT8 ble_key_size)
{
#if (SMP_INCLUDED == TRUE)
if(ble_key_size >= bte_appl_cfg.ble_min_key_size && ble_key_size <= bte_appl_cfg.ble_max_key_size) {
bte_appl_cfg.ble_appl_enc_key_size = ble_key_size;
} else {
APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size);
}
#endif ///SMP_INCLUDED == TRUE
}

void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable)
{
#if (SMP_INCLUDED == TRUE)
Expand Down
2 changes: 2 additions & 0 deletions components/bt/bluedroid/bta/include/bta/bta_dm_co.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,6 @@ extern UINT8 bta_dm_co_ble_get_accept_auth_enable(void);
extern UINT8 bta_dm_co_ble_get_auth_req(void);

extern void bta_dm_co_ble_oob_support(UINT8 enable);

extern void bta_dm_co_ble_set_appl_enc_key_size(UINT8 ble_key_size);
#endif
6 changes: 6 additions & 0 deletions components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,12 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
bta_dm_co_ble_oob_support(enable);
break;
}
case ESP_BLE_APP_ENC_KEY_SIZE: {
uint8_t key_size = 0;
STREAM_TO_UINT8(key_size, value);
bta_dm_co_ble_set_appl_enc_key_size(key_size);
break;
}
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions components/bt/bluedroid/common/include/common/bte_appl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct {
UINT8 ble_min_key_size;
UINT8 ble_accept_auth_enable;
UINT8 oob_support;
UINT8 ble_appl_enc_key_size;
#endif

} tBTE_APPL_CFG;
Expand Down
12 changes: 12 additions & 0 deletions components/bt/bluedroid/stack/gatt/gatt_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "gatt_int.h"
#include "stack/l2c_api.h"
#include "btm_int.h"
#include "common/bte_appl.h"

/********************************************************************************
** L O C A L F U N C T I O N P R O T O T Y P E S *
Expand Down Expand Up @@ -124,10 +125,15 @@ static tGATT_STATUS gatts_check_attr_readability(tGATT_ATTR16 *p_attr,
tGATT_PERM perm = p_attr->permission;

UNUSED(offset);

#if SMP_INCLUDED == TRUE
min_key_size = bte_appl_cfg.ble_appl_enc_key_size;
#else
min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
if (min_key_size != 0 ) {
min_key_size += 6;
}
#endif

if (!(perm & GATT_READ_ALLOWED)) {
GATT_TRACE_ERROR( "GATT_READ_NOT_PERMIT\n");
Expand Down Expand Up @@ -1073,10 +1079,16 @@ tGATT_STATUS gatts_write_attr_perm_check (tGATT_SVC_DB *p_db, UINT8 op_code,
while (p_attr != NULL) {
if (p_attr->handle == handle) {
perm = p_attr->permission;

#if SMP_INCLUDED == TRUE
min_key_size = bte_appl_cfg.ble_appl_enc_key_size;
#else
min_key_size = (((perm & GATT_ENCRYPT_KEY_SIZE_MASK) >> 12));
if (min_key_size != 0 ) {
min_key_size += 6;
}

#endif
GATT_TRACE_DEBUG( "gatts_write_attr_perm_check p_attr->permission =0x%04x min_key_size==0x%04x",
p_attr->permission,
min_key_size);
Expand Down
1 change: 1 addition & 0 deletions components/bt/bluedroid/stack/include/stack/btm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,7 @@ typedef UINT8 tBTM_IO_CAP;
#define BTM_BLE_RESPONDER_KEY_SIZE 15
#define BTM_BLE_MAX_KEY_SIZE 16
#define BTM_BLE_MIN_KEY_SIZE 7
#define BTM_BLE_APPL_ENC_KEY_SIZE 7

typedef UINT8 tBTM_AUTH_REQ;

Expand Down

0 comments on commit ea678c3

Please sign in to comment.