Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/board/rtl8730e: added BLE secure parameter setting, passkey display callback and passkey confirmation API #6620

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion apps/examples/ble_rmc/ble_rmc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ static void ble_server_mtu_update_cb(ble_conn_handle con_handle, uint16_t mtu_si
return;
}

static void ble_server_passkey_display_cb(uint32_t passkey, ble_conn_handle conn_handle)
{
printf("[%s : %d] passkey %ld, con_handle %d\n", __FUNCTION__, __LINE__, passkey, conn_handle);
return;
}

static void utc_cb_charact_a_1(ble_server_attr_cb_type_e type, ble_conn_handle conn_handle, ble_attr_handle attr_handle, void *arg)
{
char *arg_str = "None";
Expand Down Expand Up @@ -282,6 +288,7 @@ static ble_server_init_config server_config = {
ble_server_connected_cb,
ble_server_disconnected_cb,
ble_server_mtu_update_cb,
ble_server_passkey_display_cb,
true,
gatt_profile, sizeof(gatt_profile) / sizeof(ble_server_gatt_t)};

Expand Down Expand Up @@ -898,7 +905,43 @@ int ble_rmc_main(int argc, char *argv[])
}
}


if (strncmp(argv[1], "passkeycfm", 11) == 0) {
uint8_t conn_handle = 0;
uint8_t confirm = 0;
if (argc >= 4) {
conn_handle = atoi(argv[2]);
confirm = atoi(argv[3]);
}
ret = ble_manager_passkey_confirm(conn_handle, confirm);
if (ret != BLE_MANAGER_SUCCESS) {
RMC_LOG(RMC_SERVER_TAG, "Passkey confirm fail: [%d]\n", ret);
} else {
RMC_LOG(RMC_SERVER_TAG, "Passkey confirm OK\n");
}
}

if (strncmp(argv[1], "secureparam", 12) == 0) {
ble_sec_param sec_param;
// RTK_IO_CAP_DISPALY_ONLY = 0x00, /*!< 0x00 DisplayOnly */
// RTK_IO_CAP_DISPLAY_YES_NO = 0x01, /*!< 0x01 DisplayYesNo */
// RTK_IO_CAP_KEYBOARD_ONLY = 0x02, /*!< 0x02 KeyboardOnly */
// RTK_IO_CAP_NO_IN_NO_OUT = 0x03, /*!< 0x03 NoInputNoOutput */
// RTK_IO_CAP_KEYBOARD_DISPALY = 0x04, /*!< 0x04 KeyboardDisplay */
sec_param.io_cap = atoi(argv[2]);
sec_param.oob_data_flag = atoi(argv[3]);
sec_param.bond_flag = atoi(argv[4]);
sec_param.mitm_flag = atoi(argv[5]);
sec_param.sec_pair_flag = atoi(argv[6]);
sec_param.use_fixed_key = atoi(argv[7]);
sec_param.fixed_key = atoi(argv[8]);

ret = ble_manager_set_secure_param(&sec_param);
if (ret != BLE_MANAGER_SUCCESS) {
RMC_LOG(RMC_SERVER_TAG, "set secure param fail: [%d]\n", ret);
} else {
RMC_LOG(RMC_SERVER_TAG, "set secure param OK\n");
}
}
/* Server Test */
if (strncmp(argv[1], "server", 7) == 0) {
RMC_LOG(RMC_SERVER_TAG, " [ Server Control ]\n");
Expand Down
1 change: 1 addition & 0 deletions apps/examples/ble_tester/ble_tester_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ static ble_server_init_config server_config = {
ble_server_connected_cb,
ble_server_disconnected_cb,
ble_server_mtu_update_cb,
NULL,
true,
gatt_profile,
sizeof(gatt_profile) / sizeof(ble_server_gatt_t)
Expand Down
9 changes: 9 additions & 0 deletions framework/include/ble_manager/ble_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ typedef struct {
ble_conn_param_role role;
} ble_conn_param;

typedef struct {
uint8_t io_cap; /*!< IO capabilities */
uint8_t oob_data_flag; /*!< OOB data flag */
uint8_t bond_flag; /*!< Bonding flags */
uint8_t mitm_flag; /*!< MITM flag */
uint8_t sec_pair_flag; /*!< Secure connection pairing support flag */
uint8_t use_fixed_key; /*!< Pairing use fixed passkey */
uint32_t fixed_key; /*!< Fixed passkey value */
} ble_sec_param;
/**
* @brief Result types of BLE Manager APIs such as FAIL, SUCCESS, or INVALID ARGS
*/
Expand Down
2 changes: 2 additions & 0 deletions framework/include/ble_manager/ble_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ typedef struct {
typedef void (*ble_server_connected_t)(ble_conn_handle con_handle, ble_server_connection_type_e conn_type, uint8_t mac[BLE_BD_ADDR_MAX_LEN]);
typedef void (*ble_server_disconnected_t)(ble_conn_handle con_handle, uint16_t cause);
typedef void (*ble_server_mtu_update_t)(ble_conn_handle con_handle, uint16_t mtu_size);
typedef void (*ble_server_passkey_display_t)(uint32_t passkey, ble_conn_handle conn_handle);

typedef struct {
ble_server_connected_t connected_cb;
ble_server_disconnected_t disconnected_cb;
ble_server_mtu_update_t mtu_update_cb;
ble_server_passkey_display_t passkey_display_cb;
// true : Secure Manager is enabled. Bondable.
// false : Secure Manager is disabled. Requesting Pairing will be rejected. Non-Bondable.
bool is_secured_connect_allowed;
Expand Down
17 changes: 17 additions & 0 deletions framework/src/ble_manager/ble_manager_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ ble_result_e ble_manager_get_mac_addr(uint8_t mac[BLE_BD_ADDR_MAX_LEN])
RETURN_RESULT(res, msg);
}

ble_result_e ble_manager_passkey_confirm(uint8_t conn_handle, uint8_t confirm)
{
blemgr_msg_params param = {3, {(void *)&conn_handle, (void *)&confirm}};
blemgr_msg_s msg = {BLE_CMD_PASSKEY_CONFIRM, BLE_MANAGER_FAIL, (void *)(&param), NULL};
int res = blemgr_post_message(&msg);

RETURN_RESULT(res, msg);
}

ble_result_e ble_manager_set_secure_param(ble_sec_param *sec_param)
{
blemgr_msg_s msg = {BLE_CMD_SEC_PARAM_SET, BLE_MANAGER_FAIL, (void *)(sec_param), NULL};
int res = blemgr_post_message(&msg);

RETURN_RESULT(res, msg);
}

ble_result_e ble_manager_get_bonded_device(ble_bonded_device_list *device_list, uint16_t *device_count)
{
blemgr_msg_params param = {2, {(void *)device_list, (void *)device_count}};
Expand Down
2 changes: 2 additions & 0 deletions framework/src/ble_manager/ble_manager_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ typedef enum {
BLE_CMD_INIT,
BLE_CMD_DEINIT,
BLE_CMD_GET_MAC,
BLE_CMD_PASSKEY_CONFIRM,
BLE_CMD_SEC_PARAM_SET,
BLE_CMD_GET_BONDED_DEV,
BLE_CMD_DEL_BOND,
BLE_CMD_DEL_BOND_ALL,
Expand Down
21 changes: 21 additions & 0 deletions framework/src/ble_manager/ble_manager_lwnl.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ trble_result_e ble_drv_get_mac_addr(uint8_t mac[TRBLE_BD_ADDR_MAX_LEN])
return res;
}

trble_result_e ble_drv_passkey_confirm(uint8_t *conn_handle, uint8_t *confirm)
{
trble_result_e res = TRBLE_SUCCESS;
lwnl_msg_params msg_data = { 2, {(void *)conn_handle, (void *)confirm} };
lwnl_msg msg = {BLE_INTF_NAME, {LWNL_REQ_BLE_PASSKEY_CONFIRM}, sizeof(msg_data), (void *)&msg_data, (void *)&res};
if (_send_msg(&msg) < 0) {
res = TRBLE_FILE_ERROR;
}
return res;
}

trble_result_e ble_drv_get_bonded_device(trble_bonded_device_list_s *device_list, uint16_t *device_count)
{
trble_result_e res = TRBLE_SUCCESS;
Expand All @@ -95,6 +106,16 @@ trble_result_e ble_drv_get_bonded_device(trble_bonded_device_list_s *device_list
return res;
}

trble_result_e ble_drv_set_sec_param(trble_sec_param *sec_param)
{
trble_result_e res = TRBLE_SUCCESS;
lwnl_msg msg = {BLE_INTF_NAME, {LWNL_REQ_BLE_SEC_PARAM_SET}, sizeof(trble_sec_param), (void *)sec_param, (void *)&res};
if (_send_msg(&msg) < 0) {
res = TRBLE_FILE_ERROR;
}
return res;
}

trble_result_e ble_drv_delete_bonded(trble_addr *addr)
{
trble_result_e res = TRBLE_SUCCESS;
Expand Down
19 changes: 18 additions & 1 deletion framework/src/ble_manager/ble_manager_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,24 @@ ble_result_e blemgr_handle_request(blemgr_msg_s *msg)
}
ret = ble_drv_get_mac_addr(mac);
} break;


case BLE_CMD_PASSKEY_CONFIRM: {
BLE_STATE_CHECK;

blemgr_msg_params *param = (blemgr_msg_params *)msg->param;
uint8_t *conn_handle = (uint8_t *)param->param[0];
uint8_t *confirm = (uint8_t *)param->param[1];

ret = ble_drv_passkey_confirm(conn_handle, confirm);
} break;

case BLE_CMD_SEC_PARAM_SET: {
BLE_STATE_CHECK;

trble_sec_param *sec_param = (trble_sec_param *)msg->param;
ret = ble_drv_set_sec_param(sec_param);
} break;

case BLE_CMD_GET_BONDED_DEV: {
BLE_STATE_CHECK;

Expand Down
2 changes: 2 additions & 0 deletions os/board/rtl8721csm/src/component/os/tizenrt/rtk_blemgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ struct trble_ops g_trble_drv_ops = {
trble_netmgr_init,
trble_netmgr_deinit,
trble_netmgr_get_mac_addr,
NULL,
NULL,
trble_netmgr_get_bonded_device,
trble_netmgr_delete_bond,
trble_netmgr_delete_bond_all,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4054,6 +4054,18 @@ static uint16_t bt_stack_le_sm_passkey_confirm(void *param)
return RTK_BT_ERR_PARAM_INVALID;
}

if (key_cfm->confirm == 1)
{
confirm = GAP_CFM_CAUSE_ACCEPT;
}
else if (key_cfm->confirm == 0)
{
confirm = GAP_CFM_CAUSE_REJECT;
}
else{
return RTK_BT_ERR_PARAM_INVALID;
}

cause = le_bond_user_confirm(conn_id, confirm);
if (cause) {
return RTK_BT_ERR_LOWER_STACK_API;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,34 @@ trble_result_e rtw_ble_server_get_mac_address(uint8_t mac[TRBLE_BD_ADDR_MAX_LEN]
return TRBLE_SUCCESS;
}

trble_result_e rtw_ble_sm_set_security_param(trble_sec_param sec_param)
{
rtk_bt_le_security_param_t sec_param_input;
memcpy(&sec_param_input, &sec_param, sizeof(rtk_bt_le_security_param_t));

if(RTK_BT_OK != rtk_bt_le_sm_set_security_param(&sec_param_input))
{
debug_print("secure param set fail \n");
return TRBLE_FAIL;
}
return TRBLE_SUCCESS;
}

trble_result_e rtw_ble_pairing_passkey_confirm(uint8_t *conn_handle, uint8_t *confirm)
{
rtk_bt_le_auth_key_confirm_t pair_cfm_param = {0};

pair_cfm_param.conn_handle = *conn_handle;
pair_cfm_param.confirm = *confirm;

if(RTK_BT_OK != rtk_bt_le_sm_passkey_confirm(&pair_cfm_param))
{
debug_print("passkey confirm fail \n");
return TRBLE_FAIL;
}
return TRBLE_SUCCESS;
}

/* set data pointer of attribute value */
trble_result_e rtw_ble_server_att_set_data_ptr(trble_attr_handle attr_handle, uint8_t *new_data_ptr)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ trble_result_e rtw_ble_combo_init(trble_client_init_config* init_client, trble_s
server_init_parm.connected_cb = init_server->connected_cb;
server_init_parm.disconnected_cb = init_server->disconnected_cb;
server_init_parm.mtu_update_cb = init_server->mtu_update_cb;
server_init_parm.passkey_display_cb = init_server->passkey_display_cb;
server_init_parm.is_secured_connect_allowed = init_server->is_secured_connect_allowed;

ble_tizenrt_scatternet_main(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ static uint8_t scan_rsp_data[] = {
.duplicate_opt = 0,
};

//static rtk_bt_le_security_param_t sec_param = {
// .io_cap = RTK_IO_CAP_NO_IN_NO_OUT,
// .oob_data_flag = 0,
// .bond_flag = 1,
// .mitm_flag = 0,
// .sec_pair_flag = 0,
// .use_fixed_key = 0,
// .fixed_key = 000000,
//};
static rtk_bt_le_security_param_t sec_param = {
.io_cap = RTK_IO_CAP_NO_IN_NO_OUT,
.oob_data_flag = 0,
.bond_flag = 1,
.mitm_flag = 0,
.sec_pair_flag = 0,
.use_fixed_key = 0,
.fixed_key = 000000,
};

#if RTK_BLE_PRIVACY_SUPPORT
static bool privacy_enable = false;
Expand Down Expand Up @@ -519,6 +519,7 @@ static rtk_bt_evt_cb_ret_t ble_tizenrt_scatternet_gap_app_callback(uint8_t evt_c
case RTK_BT_LE_GAP_EVT_AUTH_PASSKEY_CONFIRM_IND: {
rtk_bt_le_auth_key_cfm_ind_t *key_cfm_ind =
(rtk_bt_le_auth_key_cfm_ind_t *)param;
server_init_parm.passkey_display_cb(key_cfm_ind->passkey, key_cfm_ind->conn_handle);
APP_PROMOTE("[APP] Auth passkey confirm: %ld, conn_handle: %d. " \
"Please comfirm if the passkeys are equal!\r\n",
key_cfm_ind->passkey,
Expand Down Expand Up @@ -877,6 +878,8 @@ int ble_tizenrt_scatternet_main(uint8_t enable)
memcpy(name,(const uint8_t*)RTK_BT_DEV_NAME,strlen((const char *)RTK_BT_DEV_NAME));
BT_APP_PROCESS(rtk_bt_le_gap_set_device_name((uint8_t *)name));
BT_APP_PROCESS(rtk_bt_le_gap_set_appearance(RTK_BT_LE_GAP_APPEARANCE_HEART_RATE_BELT));
BT_APP_PROCESS(rtk_bt_le_sm_set_security_param(&sec_param));

#if (RTK_BLE_5_0_AE_ADV_SUPPORT==0)
BT_APP_PROCESS(rtk_bt_le_gap_set_adv_data(adv_data,sizeof(adv_data)));
BT_APP_PROCESS(rtk_bt_le_gap_set_scan_rsp_data(scan_rsp_data,sizeof(scan_rsp_data)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#include <stdint.h>
#include <stdlib.h>

extern int TRNG_get_random_bytes(void *dst, uint32_t size);
uint32_t platform_random(uint32_t max)
{
return rand() % max;
uint32_t val;
TRNG_get_random_bytes(&val, sizeof(uint32_t));
return (val % max);
}
23 changes: 23 additions & 0 deletions os/board/rtl8730e/src/component/os/tizenrt/rtk_blemgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ static bool _check_mac_empty(uint8_t mac[TRBLE_BD_ADDR_MAX_LEN])
trble_result_e trble_netmgr_init(struct bledev *dev, trble_client_init_config *client, trble_server_init_config *server);
trble_result_e trble_netmgr_deinit(struct bledev *dev);
trble_result_e trble_netmgr_get_mac_addr(struct bledev *dev, uint8_t mac[TRBLE_BD_ADDR_MAX_LEN]);
trble_result_e trble_netmgr_set_sec_param(struct bledev *dev, trble_sec_param *sec_param);
trble_result_e trble_netmgr_get_bonded_device(struct bledev *dev, trble_bonded_device_list_s *device_list, uint16_t *device_count);
trble_result_e trble_netmgr_passkey_confirm(struct bledev *dev, uint8_t *conn_handle, uint8_t *confirm);
trble_result_e trble_netmgr_delete_bond(struct bledev *dev, trble_addr *addr);
trble_result_e trble_netmgr_delete_bond_all(struct bledev *dev);
trble_result_e trble_netmgr_conn_is_active(struct bledev *dev, trble_conn_handle con_handle, bool *is_active);
Expand Down Expand Up @@ -131,6 +133,8 @@ struct trble_ops g_trble_drv_ops = {
trble_netmgr_init,
trble_netmgr_deinit,
trble_netmgr_get_mac_addr,
trble_netmgr_set_sec_param,
trble_netmgr_passkey_confirm,
trble_netmgr_get_bonded_device,
trble_netmgr_delete_bond,
trble_netmgr_delete_bond_all,
Expand Down Expand Up @@ -270,6 +274,25 @@ trble_result_e trble_netmgr_get_bonded_device(struct bledev *dev, trble_bonded_d
return ret;
}

trble_result_e trble_netmgr_set_sec_param(struct bledev *dev, trble_sec_param *sec_param)
{
trble_sec_param sec_param_input;
sec_param_input.io_cap = sec_param->io_cap;
sec_param_input.oob_data_flag = sec_param->oob_data_flag;
sec_param_input.bond_flag = sec_param->bond_flag;
sec_param_input.mitm_flag = sec_param->mitm_flag;
sec_param_input.sec_pair_flag = sec_param->sec_pair_flag;
sec_param_input.use_fixed_key = sec_param->use_fixed_key;
sec_param_input.fixed_key = sec_param->fixed_key;

return rtw_ble_sm_set_security_param(sec_param_input);
}

trble_result_e trble_netmgr_passkey_confirm(struct bledev *dev, uint8_t *conn_handle, uint8_t *confirm)
{
return rtw_ble_pairing_passkey_confirm(conn_handle, confirm);
}

trble_result_e trble_netmgr_delete_bond(struct bledev *dev, trble_addr *addr)
{
trble_addr t_addr[1] = { 0, };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1288,4 +1288,30 @@ int SBOOT_Validate_Algorithm(u8 *AuthAlg, u8 *HashAlg, u8 ManiAuth, u8 ManiHash)
}
}
#endif

int TRNG_get_random_bytes(void *dst, u32 size)
{
unsigned int ranbuf;
unsigned int *lp;
int i, count;
count = size / sizeof(unsigned int);
lp = (unsigned int *) dst;

for (i = 0; i < count; i ++) {
lp[i] = _rand();
size -= sizeof(unsigned int);
}

if (size > 0) {
ranbuf = _rand();
_memcpy(&lp[i], &ranbuf, size);
}
return 0;
}

int TRNG_get_random_bytes_f_rng(void *p_rng, unsigned char *output, size_t output_size)
{
UNUSED(p_rng);
return TRNG_get_random_bytes(output, output_size);
}
/******************* (C) COPYRIGHT 2022 Realtek Semiconductor *****END OF FILE****/
Loading