Skip to content

Commit

Permalink
net: lwm2m_client_utils: Clear sec_tag content when overwriting
Browse files Browse the repository at this point in the history
When writing new content to security tag, clear existing
information first, that might cause problems.

For example, when writing PSK, delete all certificates.
When writing certificates, delete all PSK data.

Signed-off-by: Seppo Takalo <[email protected]>
  • Loading branch information
SeppoTakalo committed Nov 14, 2024
1 parent d839e3c commit 39c0d4b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
22 changes: 22 additions & 0 deletions subsys/net/lib/lwm2m_client_utils/lwm2m/lwm2m_security.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,30 @@ static int write_credential_type(int sec_obj_inst, int sec_tag, int res_id,
return 0;
}

static void delete_credential_if_exist(int sec_tag, enum modem_key_mgmt_cred_type type)
{
bool exist = false;
int ret;

ret = modem_key_mgmt_exists(sec_tag, type, &exist);
if (exist) {
ret = modem_key_mgmt_delete(sec_tag, type);
LOG_DBG("Deleted sec_tag %d, type %d", sec_tag, type);
}
if (ret < 0) {
LOG_ERR("Failed to delete credential %d", ret);
}
}

static int write_sec_obj_to_sec_tag(int sec_obj_inst, int sec_tag, int mode)
{
int ret;

if (mode == SEC_MODE_PSK) {
delete_credential_if_exist(sec_tag, MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN);
delete_credential_if_exist(sec_tag, MODEM_KEY_MGMT_CRED_TYPE_PUBLIC_CERT);
delete_credential_if_exist(sec_tag, MODEM_KEY_MGMT_CRED_TYPE_PRIVATE_CERT);

ret = write_credential_type(sec_obj_inst, sec_tag, SECURITY_CLIENT_PK_ID,
MODEM_KEY_MGMT_CRED_TYPE_IDENTITY);
if (ret) {
Expand All @@ -145,6 +164,9 @@ static int write_sec_obj_to_sec_tag(int sec_obj_inst, int sec_tag, int mode)
goto out;
}
} else if (mode == SEC_MODE_CERTIFICATE) {
delete_credential_if_exist(sec_tag, MODEM_KEY_MGMT_CRED_TYPE_PSK);
delete_credential_if_exist(sec_tag, MODEM_KEY_MGMT_CRED_TYPE_IDENTITY);

/* Don't fail if we already have a given data in the modem and we did not receive
* that as part of bootstrap. It might have been written as part of EST process.
*/
Expand Down
10 changes: 9 additions & 1 deletion tests/subsys/net/lib/lwm2m_client_utils/src/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,19 @@ ZTEST(lwm2m_client_utils_security, test_load_credentials_PSK)
modem_key_mgmt_write_fake.custom_fake = write_to_modem;
modem_key_mgmt_exists_fake.custom_fake = modem_key_mgmt_exists_custom_fake;
ctx.bootstrap_mode = false;
keys_exist = true;

rc = ctx.load_credentials(&ctx);
zassert_equal(rc, 0, "wrong return value");
zassert_equal(modem_key_mgmt_exists_fake.call_count, 5, "Did not check existing (%d)",
zassert_equal(modem_key_mgmt_exists_fake.call_count, 8, "Did not check existing (%d)",
modem_key_mgmt_exists_fake.call_count);
zassert_equal(modem_key_mgmt_delete_fake.call_count, 3, "Did not remove old keys");
zassert_equal(modem_key_mgmt_delete_fake.arg1_history[0], MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
"");
zassert_equal(modem_key_mgmt_delete_fake.arg1_history[1],
MODEM_KEY_MGMT_CRED_TYPE_PUBLIC_CERT, "");
zassert_equal(modem_key_mgmt_delete_fake.arg1_history[2],
MODEM_KEY_MGMT_CRED_TYPE_PRIVATE_CERT, "");
zassert_equal(modem_key_mgmt_write_fake.call_count, 2, "Did not write PSK");
zassert_equal(lte_lc_func_mode_set_fake.call_count, 1, "Did not set mode");
zassert_equal(lte_lc_func_mode_set_fake.arg0_val, LTE_LC_FUNC_MODE_OFFLINE,
Expand Down
1 change: 1 addition & 0 deletions tests/subsys/net/lib/lwm2m_client_utils/src/stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ DEFINE_FAKE_VALUE_FUNC(int, modem_key_mgmt_exists, nrf_sec_tag_t, enum modem_key
bool *);
DEFINE_FAKE_VALUE_FUNC(int, modem_key_mgmt_write, nrf_sec_tag_t, enum modem_key_mgmt_cred_type,
const void *, size_t);
DEFINE_FAKE_VALUE_FUNC(int, modem_key_mgmt_delete, nrf_sec_tag_t, enum modem_key_mgmt_cred_type);
DEFINE_FAKE_VALUE_FUNC(int, lte_lc_func_mode_set, enum lte_lc_func_mode);
DEFINE_FAKE_VALUE_FUNC(int, lte_lc_connect);
DEFINE_FAKE_VALUE_FUNC(int, lte_lc_offline);
Expand Down
2 changes: 2 additions & 0 deletions tests/subsys/net/lib/lwm2m_client_utils/src/stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ DECLARE_FAKE_VALUE_FUNC(int, modem_key_mgmt_exists, nrf_sec_tag_t, enum modem_ke
bool *);
DECLARE_FAKE_VALUE_FUNC(int, modem_key_mgmt_write, nrf_sec_tag_t, enum modem_key_mgmt_cred_type,
const void *, size_t);
DECLARE_FAKE_VALUE_FUNC(int, modem_key_mgmt_delete, nrf_sec_tag_t, enum modem_key_mgmt_cred_type);
DECLARE_FAKE_VALUE_FUNC(int, lte_lc_func_mode_set, enum lte_lc_func_mode);
DECLARE_FAKE_VALUE_FUNC(int, lte_lc_connect);
DECLARE_FAKE_VALUE_FUNC(int, lte_lc_offline);
Expand Down Expand Up @@ -140,6 +141,7 @@ DECLARE_FAKE_VOID_FUNC(boot_write_img_confirmed);
FUNC(lwm2m_rd_client_update) \
FUNC(modem_key_mgmt_exists) \
FUNC(modem_key_mgmt_write) \
FUNC(modem_key_mgmt_delete) \
FUNC(modem_info_init) \
FUNC(modem_info_params_init) \
FUNC(modem_info_params_get) \
Expand Down

0 comments on commit 39c0d4b

Please sign in to comment.