Commit 413eb08d authored by Joakim Andersson's avatar Joakim Andersson Committed by Anas Nashif
Browse files

Bluetooth: host: Refactor update_keys_check to operate on keys as input



Refactor update_keys_check helper function to operate on input keys
input. This allows the function to be re-used on a keys structure that
is not the current connection keys.

This also avoids the helper function changing the connection state.
The conn->le.keys pointer should at this point always have been
assigned, as central when sending the pairing request, and as peripheral
when receiving the pairing request at the very latest.

Signed-off-by: default avatarJoakim Andersson <joakim.andersson@nordicsemi.no>
parent 6339812d
Loading
Loading
Loading
Loading
+9 −15
Original line number Diff line number Diff line
@@ -554,35 +554,29 @@ static u8_t get_encryption_key_size(struct bt_smp *smp)
/* Check that if a new pairing procedure with an existing bond will not lower
 * the established security level of the bond.
 */
bool update_keys_check(struct bt_smp *smp)
static bool update_keys_check(struct bt_smp *smp, struct bt_keys *keys)
{
	struct bt_conn *conn = smp->chan.chan.conn;

	if (!conn->le.keys) {
		conn->le.keys = bt_keys_get_addr(conn->id, &conn->le.dst);
	}

	if (!conn->le.keys ||
	    !(conn->le.keys->keys & (BT_KEYS_LTK_P256 | BT_KEYS_LTK))) {
	if (!keys ||
	    !(keys->keys & (BT_KEYS_LTK_P256 | BT_KEYS_LTK))) {
		return true;
	}

	if (conn->le.keys->enc_size > get_encryption_key_size(smp)) {
	if (keys->enc_size > get_encryption_key_size(smp)) {
		return false;
	}

	if ((conn->le.keys->keys & BT_KEYS_LTK_P256) &&
	if ((keys->keys & BT_KEYS_LTK_P256) &&
	    !atomic_test_bit(smp->flags, SMP_FLAG_SC)) {
		return false;
	}

	if ((conn->le.keys->flags & BT_KEYS_AUTHENTICATED) &&
	if ((keys->flags & BT_KEYS_AUTHENTICATED) &&
	     smp->method == JUST_WORKS) {
		return false;
	}

	if (!IS_ENABLED(CONFIG_BT_SMP_ALLOW_UNAUTH_OVERWRITE) &&
	    (!(conn->le.keys->flags & BT_KEYS_AUTHENTICATED)
	    (!(keys->flags & BT_KEYS_AUTHENTICATED)
	     && smp->method == JUST_WORKS)) {
		return false;
	}
@@ -2493,7 +2487,7 @@ static u8_t smp_pairing_req(struct bt_smp *smp, struct net_buf *buf)

	smp->method = get_pair_method(smp, req->io_capability);

	if (!update_keys_check(smp)) {
	if (!update_keys_check(smp, conn->le.keys)) {
		return BT_SMP_ERR_AUTH_REQUIREMENTS;
	}

@@ -2659,7 +2653,7 @@ static u8_t smp_pairing_rsp(struct bt_smp *smp, struct net_buf *buf)

	smp->method = get_pair_method(smp, rsp->io_capability);

	if (!update_keys_check(smp)) {
	if (!update_keys_check(smp, conn->le.keys)) {
		return BT_SMP_ERR_AUTH_REQUIREMENTS;
	}