From 129afad2f5dc90f8286b91eb8944496868d176be Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 6 May 2026 21:03:07 -0700 Subject: [PATCH] [key-manager] add `ClearKek()` to remove KEK when no longer needed (#13072) This commit introduces the `KeyManager::ClearKek()` method, which clears the `Kek` and resets the `mIsKekSet` flag. The KEK is a temporary key used during the commissioning and entrust phases. To improve security and key hygiene, this commit updates the `Joiner` and `JoinerRouter` to explicitly clear the KEK once these operations have concluded. Specifically: - `Joiner::Finish()` clears the KEK when finishing in `kStateEntrust` or `kStateJoined`. - `JoinerRouter::HandleJoinerEntrustResponse()` clears the KEK immediately upon handling the entrust response, before scheduling any delayed entrusts (which set their own KEK from metadata). --- src/core/meshcop/joiner.cpp | 1 + src/core/meshcop/joiner_router.cpp | 2 ++ src/core/thread/key_manager.cpp | 6 ++++++ src/core/thread/key_manager.hpp | 5 +++++ 4 files changed, 14 insertions(+) diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 3455bb683..61a1fe119 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -184,6 +184,7 @@ void Joiner::Finish(Error aError) case kStateJoined: Get().Disconnect(); mTimer.Stop(); + Get().ClearKek(); OT_FALL_THROUGH; diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 21060bb48..243635826 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -302,6 +302,8 @@ exit: void JoinerRouter::HandleJoinerEntrustResponse(Coap::Msg *aMsg, Error aResult) { + Get().ClearKek(); + SendDelayedJoinerEntrust(); VerifyOrExit(aResult == kErrorNone && aMsg != nullptr); diff --git a/src/core/thread/key_manager.cpp b/src/core/thread/key_manager.cpp index e7b70dedf..d5e995309 100644 --- a/src/core/thread/key_manager.cpp +++ b/src/core/thread/key_manager.cpp @@ -516,6 +516,12 @@ void KeyManager::SetKek(const Kek &aKek) mIsKekSet = true; } +void KeyManager::ClearKek(void) +{ + mKek.Clear(); + mIsKekSet = false; +} + void KeyManager::SetSecurityPolicy(const SecurityPolicy &aSecurityPolicy) { SecurityPolicy newPolicy = aSecurityPolicy; diff --git a/src/core/thread/key_manager.hpp b/src/core/thread/key_manager.hpp index f3b4fe396..763d77109 100644 --- a/src/core/thread/key_manager.hpp +++ b/src/core/thread/key_manager.hpp @@ -485,6 +485,11 @@ public: */ void SetKek(const uint8_t *aKekBytes) { SetKek(*reinterpret_cast(aKekBytes)); } + /** + * Clears the KEK. + */ + void ClearKek(void); + /** * Returns the current KEK Frame Counter value. *