diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 0790d1825..b2227fd69 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -280,7 +280,7 @@ otError Commissioner::AddJoiner(const Mac::ExtAddress *aEui64, const char *aPskd VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE); - VerifyOrExit(MeshCoP::Joiner::IsPskdValid(aPskd), error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(IsPskdValid(aPskd), error = OT_ERROR_INVALID_ARGS); IgnoreError(RemoveJoiner(aEui64, 0, kJoinerOpFlagNotNotifyLeader)); // remove immediately diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index b6eb82caa..79f58a424 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -87,30 +87,6 @@ exit: return; } -bool Joiner::IsPskdValid(const char *aPskd) -{ - bool valid = false; - size_t pskdLength = StringLength(aPskd, kPskdMaxLength + 1); - - OT_STATIC_ASSERT(static_cast(kPskdMaxLength) <= static_cast(Dtls::kPskMaxLength), - "The maximum length of DTLS PSK is smaller than joiner PSKd"); - - VerifyOrExit(pskdLength >= kPskdMinLength && pskdLength <= kPskdMaxLength, OT_NOOP); - - for (size_t i = 0; i < pskdLength; i++) - { - char c = aPskd[i]; - - VerifyOrExit(isdigit(c) || isupper(c), OT_NOOP); - VerifyOrExit(c != 'I' && c != 'O' && c != 'Q' && c != 'Z', OT_NOOP); - } - - valid = true; - -exit: - return valid; -} - otError Joiner::Start(const char * aPskd, const char * aProvisioningUrl, const char * aVendorName, diff --git a/src/core/meshcop/joiner.hpp b/src/core/meshcop/joiner.hpp index fc12eff63..168edb9a0 100644 --- a/src/core/meshcop/joiner.hpp +++ b/src/core/meshcop/joiner.hpp @@ -109,29 +109,12 @@ public: */ void GetJoinerId(Mac::ExtAddress &aJoinerId) const; - /** - * This method validates the PSKd. - * - * Per Thread specification, a Joining Device Credential is encoded as - * uppercase alphanumeric characters (base32-thread: 0-9, A-Z excluding - * I, O, Q, and Z for readability) with a minimum length of 6 such - * characters and a maximum length of 32 such characters. - * - * param[in] aPskd The PSKd to validate. - * - * @retval A boolean indicates whether the given @p aPskd is valid. - * - */ - static bool IsPskdValid(const char *aPskd); - private: enum { kJoinerUdpPort = OPENTHREAD_CONFIG_JOINER_UDP_PORT, kConfigExtAddressDelay = 100, ///< [milliseconds] kReponseTimeout = 4000, ///< Maximum wait time to receive response [milliseconds]. - kPskdMinLength = 6, ///< Minimum PSKd length. - kPskdMaxLength = 32, ///< Maximum PSKd Length. }; struct JoinerRouter diff --git a/src/core/meshcop/meshcop.cpp b/src/core/meshcop/meshcop.cpp index fb60b092f..0be925845 100644 --- a/src/core/meshcop/meshcop.cpp +++ b/src/core/meshcop/meshcop.cpp @@ -44,6 +44,12 @@ namespace ot { namespace MeshCoP { +enum +{ + kPskdMinLength = 6, ///< Minimum PSKd length. + kPskdMaxLength = 32, ///< Maximum PSKd Length. +}; + void SteeringData::Init(uint8_t aLength) { OT_ASSERT(aLength <= kMaxLength); @@ -181,5 +187,31 @@ exit: } #endif // OPENTHREAD_FTD +#if OPENTHREAD_CONFIG_JOINER_ENABLE || OPENTHREAD_CONFIG_COMMISSIONER_ENABLE +bool IsPskdValid(const char *aPskd) +{ + bool valid = false; + size_t pskdLength = StringLength(aPskd, kPskdMaxLength + 1); + + OT_STATIC_ASSERT(static_cast(kPskdMaxLength) <= static_cast(Dtls::kPskMaxLength), + "The maximum length of DTLS PSK is smaller than joiner PSKd"); + + VerifyOrExit(pskdLength >= kPskdMinLength && pskdLength <= kPskdMaxLength, OT_NOOP); + + for (size_t i = 0; i < pskdLength; i++) + { + char c = aPskd[i]; + + VerifyOrExit(isdigit(c) || isupper(c), OT_NOOP); + VerifyOrExit(c != 'I' && c != 'O' && c != 'Q' && c != 'Z', OT_NOOP); + } + + valid = true; + +exit: + return valid; +} +#endif // OPENTHREAD_CONFIG_JOINER_ENABLE || OPENTHREAD_CONFIG_COMMISSIONER_ENABLE + } // namespace MeshCoP } // namespace ot diff --git a/src/core/meshcop/meshcop.hpp b/src/core/meshcop/meshcop.hpp index 99c42531e..dfebc2fcd 100644 --- a/src/core/meshcop/meshcop.hpp +++ b/src/core/meshcop/meshcop.hpp @@ -255,6 +255,23 @@ void ComputeJoinerId(const Mac::ExtAddress &aEui64, Mac::ExtAddress &aJoinerId); */ otError GetBorderAgentRloc(ThreadNetif &aNetIf, uint16_t &aRloc); +#if OPENTHREAD_CONFIG_JOINER_ENABLE || OPENTHREAD_CONFIG_COMMISSIONER_ENABLE +/** + * This method validates the PSKd. + * + * Per Thread specification, a Joining Device Credential is encoded as + * uppercase alphanumeric characters (base32-thread: 0-9, A-Z excluding + * I, O, Q, and Z for readability) with a minimum length of 6 such + * characters and a maximum length of 32 such characters. + * + * param[in] aPskd The PSKd to validate. + * + * @retval A boolean indicates whether the given @p aPskd is valid. + * + */ +bool IsPskdValid(const char *aPskd); +#endif // OPENTHREAD_CONFIG_JOINER_ENABLE || OPENTHREAD_CONFIG_COMMISSIONER_ENABLE + } // namespace MeshCoP } // namespace ot