Lines Matching +full:restore +full:- +full:keys
1 /* Bluetooth CSIP - Coordinated Set Identification Profile */
5 * Copyright (c) 2020-2022 Nordic Semiconductor ASA
7 * SPDX-License-Identifier: Apache-2.0
42 #include "../host/keys.h"
94 &svc_inst->lock_client_addr); in is_last_client_to_write()
97 &svc_inst->lock_client_addr); in is_last_client_to_write()
122 for (size_t i = 0U; i < ARRAY_SIZE(svc_inst->clients); i++) { in notify_clients()
125 client = &svc_inst->clients[i]; in notify_clients()
127 if (atomic_test_bit(client->flags, FLAG_ACTIVE)) { in notify_clients()
129 bt_addr_le_eq(bt_conn_get_dst(excluded_client), &client->addr)) { in notify_clients()
133 atomic_set_bit(client->flags, flag); in notify_clients()
167 return -EINVAL; in sirk_encrypt()
170 k = conn->le.keys->ltk.val; in sirk_encrypt()
173 err = bt_csip_sef(k, sirk->value, enc_sirk->value); in sirk_encrypt()
179 enc_sirk->type = BT_CSIP_SIRK_TYPE_ENCRYPTED; in sirk_encrypt()
205 dest[BT_CSIP_CRYPTO_PRAND_SIZE - 1] &= 0x3F; in generate_prand()
206 dest[BT_CSIP_CRYPTO_PRAND_SIZE - 1] |= BIT(6); in generate_prand()
230 res = bt_csip_sih(svc_inst->sirk.value, prand, hash); in bt_csip_set_member_generate_rsi()
249 if (svc_inst->cb != NULL && svc_inst->cb->sirk_read_req != NULL) { in read_sirk()
254 cb_rsp = svc_inst->cb->sirk_read_req(conn, &svc_insts[0]); in read_sirk()
257 sirk = &svc_inst->sirk; in read_sirk()
262 err = sirk_encrypt(conn, &svc_inst->sirk, &enc_sirk); in read_sirk()
285 sirk = &svc_inst->sirk; in read_sirk()
288 LOG_DBG("SIRK %sencrypted", sirk->type == BT_CSIP_SIRK_TYPE_PLAIN ? "not " : ""); in read_sirk()
289 LOG_HEXDUMP_DBG(svc_inst->sirk.value, sizeof(svc_inst->sirk.value), "SIRK"); in read_sirk()
307 LOG_DBG("%u", svc_inst->set_size); in read_set_size()
310 &svc_inst->set_size, in read_set_size()
311 sizeof(svc_inst->set_size)); in read_set_size()
326 LOG_DBG("%u", svc_inst->set_lock); in read_set_lock()
329 &svc_inst->set_lock, in read_set_lock()
330 sizeof(svc_inst->set_lock)); in read_set_lock()
353 if (svc_inst->set_lock == BT_CSIP_LOCK_VALUE) { in set_lock()
365 notify = svc_inst->set_lock != val; in set_lock()
367 svc_inst->set_lock = val; in set_lock()
368 if (svc_inst->set_lock == BT_CSIP_LOCK_VALUE) { in set_lock()
370 bt_addr_le_copy(&svc_inst->lock_client_addr, in set_lock()
373 (void)k_work_reschedule(&svc_inst->set_lock_timer, in set_lock()
376 (void)memset(&svc_inst->lock_client_addr, 0, in set_lock()
377 sizeof(svc_inst->lock_client_addr)); in set_lock()
378 (void)k_work_cancel_delayable(&svc_inst->set_lock_timer); in set_lock()
381 LOG_DBG("%u", svc_inst->set_lock); in set_lock()
391 if (svc_inst->cb != NULL && svc_inst->cb->lock_changed != NULL) { in set_lock()
392 bool locked = svc_inst->set_lock == BT_CSIP_LOCK_VALUE; in set_lock()
394 svc_inst->cb->lock_changed(conn, svc_inst, locked); in set_lock()
437 LOG_DBG("%u", svc_inst->rank); in read_rank()
440 &svc_inst->rank, in read_rank()
441 sizeof(svc_inst->rank)); in read_rank()
455 svc_inst->set_lock = BT_CSIP_RELEASE_VALUE; in set_lock_timer_handler()
458 if (svc_inst->cb != NULL && svc_inst->cb->lock_changed != NULL) { in set_lock_timer_handler()
459 bool locked = svc_inst->set_lock == BT_CSIP_LOCK_VALUE; in set_lock_timer_handler()
461 svc_inst->cb->lock_changed(NULL, svc_inst, locked); in set_lock_timer_handler()
468 if (err != 0 || conn->encrypt == 0) { in csip_security_changed()
472 if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) { in csip_security_changed()
479 for (size_t j = 0U; j < ARRAY_SIZE(svc_inst->clients); j++) { in csip_security_changed()
482 client = &svc_inst->clients[i]; in csip_security_changed()
484 if (atomic_test_bit(client->flags, FLAG_NOTIFY_LOCK) && in csip_security_changed()
485 bt_addr_le_eq(bt_conn_get_dst(conn), &client->addr)) { in csip_security_changed()
496 LOG_DBG("Non-bonded device"); in handle_csip_disconnect()
498 (void)memset(&svc_inst->lock_client_addr, 0, in handle_csip_disconnect()
499 sizeof(svc_inst->lock_client_addr)); in handle_csip_disconnect()
500 svc_inst->set_lock = BT_CSIP_RELEASE_VALUE; in handle_csip_disconnect()
503 if (svc_inst->cb != NULL && svc_inst->cb->lock_changed != NULL) { in handle_csip_disconnect()
504 bool locked = svc_inst->set_lock == BT_CSIP_LOCK_VALUE; in handle_csip_disconnect()
506 svc_inst->cb->lock_changed(conn, svc_inst, locked); in handle_csip_disconnect()
513 for (size_t i = 0U; i < ARRAY_SIZE(svc_inst->clients); i++) { in handle_csip_disconnect()
516 client = &svc_inst->clients[i]; in handle_csip_disconnect()
518 if (bt_addr_le_eq(bt_conn_get_dst(conn), &client->addr)) { in handle_csip_disconnect()
529 if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) { in csip_disconnected()
540 for (size_t i = 0U; i < ARRAY_SIZE(svc_inst->clients); i++) { in handle_csip_auth_complete()
543 client = &svc_inst->clients[i]; in handle_csip_auth_complete()
545 if (atomic_test_bit(client->flags, FLAG_ACTIVE) && in handle_csip_auth_complete()
546 bt_addr_le_eq(bt_conn_get_dst(conn), &client->addr)) { in handle_csip_auth_complete()
552 for (size_t i = 0U; i < ARRAY_SIZE(svc_inst->clients); i++) { in handle_csip_auth_complete()
555 client = &svc_inst->clients[i]; in handle_csip_auth_complete()
557 if (!atomic_test_bit(client->flags, FLAG_ACTIVE)) { in handle_csip_auth_complete()
558 atomic_set_bit(client->flags, FLAG_ACTIVE); in handle_csip_auth_complete()
559 memcpy(&client->addr, bt_conn_get_dst(conn), sizeof(bt_addr_le_t)); in handle_csip_auth_complete()
600 for (size_t j = 0U; j < ARRAY_SIZE(svc_inst->clients); j++) { in csip_bond_deleted()
603 if (atomic_test_bit(svc_inst->clients[i].flags, FLAG_ACTIVE) && in csip_bond_deleted()
604 bt_addr_le_eq(peer, &svc_inst->clients[i].addr)) { in csip_bond_deleted()
605 atomic_clear(svc_inst->clients[i].flags); in csip_bond_deleted()
606 (void)memset(&svc_inst->clients[i].addr, 0, sizeof(bt_addr_le_t)); in csip_bond_deleted()
660 if (svc_inst == NULL || svc_inst->service_p == NULL) { in bt_csip_set_member_svc_decl_get()
664 return svc_inst->service_p->attrs; in bt_csip_set_member_svc_decl_get()
669 if (param->lockable && param->rank == 0) { in valid_register_param()
674 if (param->rank > 0 && param->set_size > 0 && param->rank > param->set_size) { in valid_register_param()
676 param->rank, param->set_size); in valid_register_param()
681 if (param->parent == NULL) { in valid_register_param()
702 for (size_t i = 4U; i < svc->attr_count; i++) { in remove_csis_char()
703 if (bt_uuid_cmp(svc->attrs[i].uuid, uuid) == 0) { in remove_csis_char()
705 * potentially the CCCD. The value declaration will be a i - 1, the in remove_csis_char()
713 if (i == (svc->attr_count - (attrs_to_rem - 1))) { in remove_csis_char()
720 for (size_t j = i - 1U; j < svc->attr_count - attrs_to_rem; j++) { in remove_csis_char()
721 svc->attrs[j] = svc->attrs[j + attrs_to_rem]; in remove_csis_char()
725 svc->attr_count -= attrs_to_rem; in remove_csis_char()
739 if (svc_inst->service_p == NULL) { in notify()
743 err = bt_gatt_notify_uuid(conn, uuid, svc_inst->service_p->attrs, data, len); in notify()
745 if (err == -ENOTCONN) { in notify()
771 struct csip_client *client = &svc_inst->clients[bt_conn_index(conn)]; in notify_cb()
773 if (atomic_test_and_clear_bit(client->flags, FLAG_NOTIFY_LOCK)) { in notify_cb()
774 notify(svc_inst, conn, BT_UUID_CSIS_SET_LOCK, &svc_inst->set_lock, in notify_cb()
775 sizeof(svc_inst->set_lock)); in notify_cb()
779 atomic_test_and_clear_bit(client->flags, FLAG_NOTIFY_SIRK)) { in notify_cb()
780 notify(svc_inst, conn, BT_UUID_CSIS_SIRK, &svc_inst->sirk, in notify_cb()
781 sizeof(svc_inst->sirk)); in notify_cb()
797 for (size_t j = 1U; j < ARRAY_SIZE(svc_inst->clients); i++) { in add_bonded_addr_to_client_list()
799 if (!atomic_test_bit(svc_inst->clients[j].flags, FLAG_ACTIVE)) { in add_bonded_addr_to_client_list()
802 atomic_set_bit(svc_inst->clients[j].flags, FLAG_ACTIVE); in add_bonded_addr_to_client_list()
803 memcpy(&svc_inst->clients[j].addr, &info->addr, in add_bonded_addr_to_client_list()
805 bt_addr_le_to_str(&svc_inst->clients[j].addr, addr_str, in add_bonded_addr_to_client_list()
824 return -ENOMEM; in bt_csip_set_member_register()
829 return -EINVAL; in bt_csip_set_member_register()
834 return -EINVAL; in bt_csip_set_member_register()
838 inst->service_p = &csip_set_member_service_list[instance_cnt]; in bt_csip_set_member_register()
845 /* Restore bonding list */ in bt_csip_set_member_register()
855 if (param->rank == 0U) { in bt_csip_set_member_register()
856 remove_csis_char(BT_UUID_CSIS_RANK, inst->service_p); in bt_csip_set_member_register()
859 if (param->set_size == 0U) { in bt_csip_set_member_register()
860 remove_csis_char(BT_UUID_CSIS_SET_SIZE, inst->service_p); in bt_csip_set_member_register()
863 if (!param->lockable) { in bt_csip_set_member_register()
864 remove_csis_char(BT_UUID_CSIS_SET_LOCK, inst->service_p); in bt_csip_set_member_register()
867 err = bt_gatt_service_register(inst->service_p); in bt_csip_set_member_register()
873 k_work_init_delayable(&inst->set_lock_timer, in bt_csip_set_member_register()
875 inst->rank = param->rank; in bt_csip_set_member_register()
876 inst->set_size = param->set_size; in bt_csip_set_member_register()
877 inst->set_lock = BT_CSIP_RELEASE_VALUE; in bt_csip_set_member_register()
878 inst->sirk.type = BT_CSIP_SIRK_TYPE_PLAIN; in bt_csip_set_member_register()
879 inst->cb = param->cb; in bt_csip_set_member_register()
887 (void)memcpy(inst->sirk.value, test_sirk, sizeof(test_sirk)); in bt_csip_set_member_register()
890 (void)memcpy(inst->sirk.value, param->sirk, sizeof(inst->sirk.value)); in bt_csip_set_member_register()
903 return -EINVAL; in bt_csip_set_member_unregister()
906 err = bt_gatt_service_unregister(svc_inst->service_p); in bt_csip_set_member_unregister()
912 (void)k_work_cancel_delayable(&svc_inst->set_lock_timer); in bt_csip_set_member_unregister()
923 return -EINVAL; in bt_csip_set_member_sirk()
928 return -EINVAL; in bt_csip_set_member_sirk()
931 memcpy(svc_inst->sirk.value, sirk, BT_CSIP_SIRK_SIZE); in bt_csip_set_member_sirk()
943 return -EINVAL; in bt_csip_set_member_get_sirk()
948 return -EINVAL; in bt_csip_set_member_get_sirk()
951 memcpy(sirk, svc_inst->sirk.value, BT_CSIP_SIRK_SIZE); in bt_csip_set_member_get_sirk()
969 svc_inst->set_lock = BT_CSIP_RELEASE_VALUE; in bt_csip_set_member_lock()
972 if (svc_inst->cb != NULL && svc_inst->cb->lock_changed != NULL) { in bt_csip_set_member_lock()
973 svc_inst->cb->lock_changed(NULL, &svc_insts[0], false); in bt_csip_set_member_lock()