From 36d16f1e59fd173df576a94b2c017ecc6de9e87c Mon Sep 17 00:00:00 2001 From: Sumeet Singh Date: Sat, 7 Sep 2024 17:08:40 +0800 Subject: [PATCH] feat(nimble): Added host config to enable or disable SC Only mode during runtime --- nimble/host/include/host/ble_hs.h | 8 ++++++++ nimble/host/src/ble_att_svr.c | 2 +- nimble/host/src/ble_hs_cfg.c | 1 + nimble/host/src/ble_sm.c | 4 ++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/nimble/host/include/host/ble_hs.h b/nimble/host/include/host/ble_hs.h index 4eba4b440..9aa6b88c7 100644 --- a/nimble/host/include/host/ble_hs.h +++ b/nimble/host/include/host/ble_hs.h @@ -315,6 +315,14 @@ struct ble_hs_cfg { */ unsigned sm_sc:1; + /** @brief Security Manager - Enable/Disable Secure Connections Only flag + * + * If set, this will enforce P-256 elliptic curve encryption algorithm + * during pairing. + * It will force the max key size to be used during pairing. + */ + unsigned sm_sc_only:1; + /** @brief Security Manager Key Press Notification flag * * Currently unsupported and should not be set. diff --git a/nimble/host/src/ble_att_svr.c b/nimble/host/src/ble_att_svr.c index 4190db9ca..f9f688f00 100644 --- a/nimble/host/src/ble_att_svr.c +++ b/nimble/host/src/ble_att_svr.c @@ -325,7 +325,7 @@ ble_att_svr_check_perms(uint16_t conn_handle, int is_read, /* In SC Only mode all characteristics requiring security * require it on level 4 */ - if (MYNEWT_VAL(BLE_SM_SC_ONLY)) { + if (ble_hs_cfg.sm_sc_only) { if (!sec_state.authenticated || !sec_state.encrypted) { *out_att_err = BLE_ATT_ERR_INSUFFICIENT_AUTHEN; diff --git a/nimble/host/src/ble_hs_cfg.c b/nimble/host/src/ble_hs_cfg.c index a46a604ac..b3cf33c74 100644 --- a/nimble/host/src/ble_hs_cfg.c +++ b/nimble/host/src/ble_hs_cfg.c @@ -27,6 +27,7 @@ struct ble_hs_cfg ble_hs_cfg = { .sm_bonding = MYNEWT_VAL(BLE_SM_BONDING), .sm_mitm = MYNEWT_VAL(BLE_SM_MITM), .sm_sc = MYNEWT_VAL(BLE_SM_SC), + .sm_sc_only = MYNEWT_VAL(BLE_SM_SC_ONLY), .sm_keypress = MYNEWT_VAL(BLE_SM_KEYPRESS), .sm_our_key_dist = MYNEWT_VAL(BLE_SM_OUR_KEY_DIST), .sm_their_key_dist = MYNEWT_VAL(BLE_SM_THEIR_KEY_DIST), diff --git a/nimble/host/src/ble_sm.c b/nimble/host/src/ble_sm.c index 40acbc25a..92caa3b7f 100644 --- a/nimble/host/src/ble_sm.c +++ b/nimble/host/src/ble_sm.c @@ -1930,7 +1930,7 @@ ble_sm_pair_req_rx(uint16_t conn_handle, struct os_mbuf **om, } else if (req->max_enc_key_size > BLE_SM_PAIR_KEY_SZ_MAX) { res->sm_err = BLE_SM_ERR_INVAL; res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_INVAL); - } else if (MYNEWT_VAL(BLE_SM_SC_ONLY)) { + } else if (ble_hs_cfg.sm_sc_only) { /* Fail if Secure Connections Only mode is on and remote does not * meet key size requirements - MITM was checked in last step. * Fail if SC is not supported by peer or key size is too small @@ -2005,7 +2005,7 @@ ble_sm_pair_rsp_rx(uint16_t conn_handle, struct os_mbuf **om, } else if (rsp->max_enc_key_size > BLE_SM_PAIR_KEY_SZ_MAX) { res->sm_err = BLE_SM_ERR_INVAL; res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_INVAL); - } else if (MYNEWT_VAL(BLE_SM_SC_ONLY) && (rsp->max_enc_key_size != BLE_SM_PAIR_KEY_SZ_MAX)) { + } else if (ble_hs_cfg.sm_sc_only && (rsp->max_enc_key_size != BLE_SM_PAIR_KEY_SZ_MAX)) { /* Fail if Secure Connections Only mode is on and remote does not meet * key size requirements - MITM was checked in last step */