Skip to content

Commit 6330d16

Browse files
committed
apps: Remove pair when security change fails
This allows us to re-pair on the next connection, which is necessary if, e.g., the peer has lost their bonding information. Additionally, do not initiate the Pouch sync until after confirming the security level was successfully changed. Signed-off-by: Sam Friedman <[email protected]>
1 parent 204af0e commit 6330d16

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

gateway/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ CONFIG_PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY=y
5555

5656
# Bluetooth secure connections
5757
CONFIG_BT_SMP=y
58+
CONFIG_BT_SECURITY_ERR_TO_STR=y
5859
CONFIG_MBEDTLS_ECP_C=y
5960
# This option is implied by BT_SMP and breaks secp384r1 support
6061
CONFIG_MBEDTLS_PSA_P256M_DRIVER_ENABLED=n

gateway/src/main.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ static void bt_connected(struct bt_conn *conn, uint8_t err)
168168
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
169169
return;
170170
}
171-
172-
pouch_gateway_bt_start(conn);
173171
}
174172

175173
static void bt_disconnected(struct bt_conn *conn, uint8_t reason)
@@ -188,7 +186,25 @@ static void bt_disconnected(struct bt_conn *conn, uint8_t reason)
188186

189187
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
190188
{
191-
LOG_INF("BT security changed to level %u, err %s(%u)", level, bt_security_err_to_str(err), err);
189+
if (err)
190+
{
191+
LOG_ERR("BT security change failed. Current level: %d, err: %s(%u)",
192+
level,
193+
bt_security_err_to_str(err),
194+
err);
195+
196+
struct bt_conn_info info;
197+
bt_conn_get_info(conn, &info);
198+
199+
bt_unpair(info.id, info.le.dst);
200+
bt_conn_disconnect(conn, BT_HCI_ERR_INSUFFICIENT_SECURITY);
201+
}
202+
else
203+
{
204+
LOG_INF("BT security changed to level %u", level);
205+
206+
pouch_gateway_bt_start(conn);
207+
}
192208
}
193209

194210
BT_CONN_CB_DEFINE(conn_callbacks) = {

samples/custom_connect/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ CONFIG_PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY=y
5555

5656
# Bluetooth secure connections
5757
CONFIG_BT_SMP=y
58+
CONFIG_BT_SECURITY_ERR_TO_STR=y
5859
CONFIG_MBEDTLS_ECP_C=y
5960
# This option is implied by BT_SMP and breaks secp384r1 support
6061
CONFIG_MBEDTLS_PSA_P256M_DRIVER_ENABLED=n

samples/custom_connect/src/main.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,6 @@ static void bt_connected(struct bt_conn *conn, uint8_t err)
140140
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
141141
return;
142142
}
143-
144-
sync_data.conn = conn;
145-
sync_data.counter = 0;
146-
147-
k_work_schedule(&sync_data.work, K_NO_WAIT);
148143
}
149144

150145
static void bt_disconnected(struct bt_conn *conn, uint8_t reason)
@@ -163,7 +158,28 @@ static void bt_disconnected(struct bt_conn *conn, uint8_t reason)
163158

164159
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
165160
{
166-
LOG_INF("BT security changed to level %u, err %s(%u)", level, bt_security_err_to_str(err), err);
161+
if (err)
162+
{
163+
LOG_ERR("BT security change failed. Current level: %d, err: %s(%u)",
164+
level,
165+
bt_security_err_to_str(err),
166+
err);
167+
168+
struct bt_conn_info info;
169+
bt_conn_get_info(conn, &info);
170+
171+
bt_unpair(info.id, info.le.dst);
172+
bt_conn_disconnect(conn, BT_HCI_ERR_INSUFFICIENT_SECURITY);
173+
}
174+
else
175+
{
176+
LOG_INF("BT security changed to level %u", level);
177+
178+
sync_data.conn = conn;
179+
sync_data.counter = 0;
180+
181+
k_work_schedule(&sync_data.work, K_NO_WAIT);
182+
}
167183
}
168184

169185
BT_CONN_CB_DEFINE(conn_callbacks) = {

0 commit comments

Comments
 (0)