From 24307f1ba94aa85ec185f7e0a5679a83fc6db264 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Wed, 17 Apr 2019 12:42:03 -0700 Subject: [PATCH] [pskc] set random PSKc when generating a new Active Dataset (#3769) When generating a new Active Dataset, the current PSKc value is used. Because the PSKc is initialized to all zeros, new Active Datasets have a default PSKc value of all zeros. This commit checks if the PSKc has been set (i.e. not all zeros) and, if not set, generates a random PSKc when creating a new Active Dataset. --- src/core/meshcop/dataset_manager_ftd.cpp | 16 +++++++++++++++- src/core/thread/key_manager.cpp | 4 +++- src/core/thread/key_manager.hpp | 12 ++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 7396a0aa0..00efdeed2 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -403,8 +403,22 @@ otError ActiveDataset::GenerateLocal(void) if (dataset.Get(Tlv::kPSKc) == NULL) { PSKcTlv tlv; + tlv.Init(); - tlv.SetPSKc(Get().GetPSKc()); + + if (Get().IsPSKcSet()) + { + // use configured PSKc + tlv.SetPSKc(Get().GetPSKc()); + } + else + { + // PSKc has not yet been configured, generate new PSKc at random + otPSKc pskc; + SuccessOrExit(error = otPlatRandomGetTrue(pskc.m8, sizeof(pskc))); + tlv.SetPSKc(pskc); + } + dataset.Set(tlv); } diff --git a/src/core/thread/key_manager.cpp b/src/core/thread/key_manager.cpp index 681c20996..7296ba06b 100644 --- a/src/core/thread/key_manager.cpp +++ b/src/core/thread/key_manager.cpp @@ -81,7 +81,9 @@ KeyManager::KeyManager(Instance &aInstance) , mKeyRotationTimer(aInstance, &KeyManager::HandleKeyRotationTimer, this) , mKekFrameCounter(0) , mSecurityPolicyFlags(0xff) + , mIsPSKcSet(false) { + memset(&mPSKc, 0, sizeof(mPSKc)); ComputeKey(mKeySequence, mKey); } @@ -104,7 +106,7 @@ void KeyManager::SetPSKc(const otPSKc &aPSKc) Get().Signal(OT_CHANGED_PSKC); exit: - return; + mIsPSKcSet = true; } #endif // OPENTHREAD_MTD || OPENTHREAD_FTD diff --git a/src/core/thread/key_manager.hpp b/src/core/thread/key_manager.hpp index 7452ba9b8..4cee91c62 100644 --- a/src/core/thread/key_manager.hpp +++ b/src/core/thread/key_manager.hpp @@ -103,6 +103,17 @@ public: otError SetMasterKey(const otMasterKey &aKey); #if OPENTHREAD_FTD || OPENTHREAD_MTD + /** + * This method indicates whether the PSKc is configured. + * + * A value of all zeros indicates that the PSKc is not configured. + * + * @retval TRUE if the PSKc is configured. + * @retval FALSE if the PSKc is not not configured. + * + */ + bool IsPSKcSet(void) const { return mIsPSKcSet; } + /** * This method returns a pointer to the PSKc. * @@ -368,6 +379,7 @@ private: uint32_t mKekFrameCounter; uint8_t mSecurityPolicyFlags; + bool mIsPSKcSet : 1; }; /**