From b1ec563a8153ca974fa67764647f34f1987bbaa5 Mon Sep 17 00:00:00 2001 From: Rongli Sun Date: Thu, 16 Apr 2020 11:34:56 +0800 Subject: [PATCH] [key-manager] add helper method (#4838) --- src/core/mac/mac.cpp | 2 +- src/core/meshcop/dataset_manager.cpp | 6 +-- src/core/thread/key_manager.hpp | 56 ++++++++++++++++++++++++++++ src/core/thread/mle.cpp | 3 +- src/core/thread/mle_router.cpp | 2 +- 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 663f984a1..e63be4069 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -889,7 +889,7 @@ void Mac::PrepareBeacon(TxFrame &aFrame) beaconPayload = reinterpret_cast(beacon->GetPayload()); - if (Get().GetSecurityPolicyFlags() & OT_SECURITY_POLICY_BEACONS) + if (Get().IsThreadBeaconEnabled()) { beaconPayload->Init(); diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index abd9486b0..96cfd3e36 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -358,8 +358,7 @@ void DatasetManager::SendGetResponse(const Coap::Message & aRequest, while (cur < end) { - if (cur->GetType() != Tlv::kNetworkMasterKey || - (Get().GetSecurityPolicyFlags() & OT_SECURITY_POLICY_OBTAIN_MASTER_KEY)) + if (cur->GetType() != Tlv::kNetworkMasterKey || Get().IsObtainMasterKeyEnabled()) { SuccessOrExit(error = cur->AppendTo(*message)); } @@ -373,8 +372,7 @@ void DatasetManager::SendGetResponse(const Coap::Message & aRequest, { const Tlv *tlv; - if (aTlvs[index] == Tlv::kNetworkMasterKey && - !(Get().GetSecurityPolicyFlags() & OT_SECURITY_POLICY_OBTAIN_MASTER_KEY)) + if (aTlvs[index] == Tlv::kNetworkMasterKey && !Get().IsObtainMasterKeyEnabled()) { continue; } diff --git a/src/core/thread/key_manager.hpp b/src/core/thread/key_manager.hpp index 1318f534b..90aa14cd5 100644 --- a/src/core/thread/key_manager.hpp +++ b/src/core/thread/key_manager.hpp @@ -481,6 +481,62 @@ public: */ void SetSecurityPolicyFlags(uint8_t aSecurityPolicyFlags); + /** + * This method indicates whether or not obtaining Master key for out-of-band is enabled. + * + * @retval TRUE If obtaining Master key for out-of-band is enabled. + * @retval FALSE If obtaining Master key for out-of-band is not enabled. + * + */ + bool IsObtainMasterKeyEnabled(void) const + { + return (mSecurityPolicyFlags & OT_SECURITY_POLICY_OBTAIN_MASTER_KEY) != 0; + } + + /** + * This method indicates whether or not Native Commissioning using PSKc is allowed. + * + * @retval TRUE If Native Commissioning using PSKc is allowed. + * @retval FALSE If Native Commissioning using PSKc is not allowed. + * + */ + bool IsNativeCommissioningAllowed(void) const + { + return (mSecurityPolicyFlags & OT_SECURITY_POLICY_NATIVE_COMMISSIONING) != 0; + } + + /** + * This method indicates whether or not Thread 1.1 Routers are enabled. + * + * @retval TRUE If Thread 1.1 Routers are enabled. + * @retval FALSE If Thread 1.1 Routers are not enabled. + * + */ + bool IsRouterEnabled(void) const { return (mSecurityPolicyFlags & OT_SECURITY_POLICY_ROUTERS) != 0; } + + /** + * This method indicates whether or not external Commissioner authentication is allowed using PSKc. + * + * @retval TRUE If the commissioning sessions by an external Commissioner based on the PSKc are allowed + * to be established and that changes to the Commissioner Dataset by on-mesh nodes are allowed. + * @retval FALSE If the commissioning sessions by an external Commissioner based on the PSKc are not allowed + * to be established and that changes to the Commissioner Dataset by on-mesh nodes are not allowed. + * + */ + bool IsExternalCommissionerAllowed(void) const + { + return (mSecurityPolicyFlags & OT_SECURITY_POLICY_EXTERNAL_COMMISSIONER) != 0; + } + + /** + * This method indicates whether or not Thread Beacons are enabled. + * + * @retval TRUE If Thread Beacons are enabled. + * @retval FALSE If Thread Beacons are not enabled. + * + */ + bool IsThreadBeaconEnabled(void) const { return (mSecurityPolicyFlags & OT_SECURITY_POLICY_BEACONS) != 0; } + /** * This static method generates IEEE 802.15.4 nonce byte sequence. * diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index ac941a4cd..e7eb342a0 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1636,8 +1636,7 @@ void Mle::HandleStateChanged(otChangedFlags aFlags) if (aFlags & OT_CHANGED_SECURITY_POLICY) { - Get().AllowNativeCommissioner( - (Get().GetSecurityPolicyFlags() & OT_SECURITY_POLICY_NATIVE_COMMISSIONING) != 0); + Get().AllowNativeCommissioner(Get().IsNativeCommissioningAllowed()); } #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 77c4df741..15a3718fb 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -2886,7 +2886,7 @@ otError MleRouter::SendDiscoveryResponse(const Ip6::Address &aDestination, uint1 discoveryResponse.Init(); discoveryResponse.SetVersion(kThreadVersion); - if (Get().GetSecurityPolicyFlags() & OT_SECURITY_POLICY_NATIVE_COMMISSIONING) + if (Get().IsNativeCommissioningAllowed()) { SuccessOrExit( error = Tlv::AppendUint16Tlv(*message, MeshCoP::Tlv::kCommissionerUdpPort, MeshCoP::kBorderAgentUdpPort));