From 19695b0f8ea9fec430ab87f88e3ecd516b720c3f Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 30 Oct 2019 16:54:10 -0700 Subject: [PATCH] [ncp] add support in spinel/NCP to generate PSKc from pass-phrase (#4294) --- src/ncp/ncp_base.cpp | 3 +++ src/ncp/ncp_base.hpp | 1 + src/ncp/ncp_base_ftd.cpp | 26 ++++++++++++++++++++++++++ src/ncp/spinel.c | 4 ++++ src/ncp/spinel.h | 23 +++++++++++++++++++++++ 5 files changed, 57 insertions(+) diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 7c155b833..a0ab110cf 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -936,6 +936,9 @@ bool NcpBase::HandlePropertySetForSpecialProperties(uint8_t aHeader, spinel_prop #endif #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE + case SPINEL_PROP_MESHCOP_COMMISSIONER_GENERATE_PSKC: + ExitNow(aError = HandlePropertySet_SPINEL_PROP_MESHCOP_COMMISSIONER_GENERATE_PSKC(aHeader)); + case SPINEL_PROP_THREAD_COMMISSIONER_ENABLED: ExitNow(aError = HandlePropertySet_SPINEL_PROP_THREAD_COMMISSIONER_ENABLED(aHeader)); #endif diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index b2fe3a829..7a0b810c9 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -413,6 +413,7 @@ protected: #endif #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE + otError HandlePropertySet_SPINEL_PROP_MESHCOP_COMMISSIONER_GENERATE_PSKC(uint8_t aHeader); otError HandlePropertySet_SPINEL_PROP_THREAD_COMMISSIONER_ENABLED(uint8_t aHeader); #endif // OPENTHREAD_FTD diff --git a/src/ncp/ncp_base_ftd.cpp b/src/ncp/ncp_base_ftd.cpp index 516b03f97..503a54ca6 100644 --- a/src/ncp/ncp_base_ftd.cpp +++ b/src/ncp/ncp_base_ftd.cpp @@ -759,6 +759,32 @@ exit: return error; } +otError NcpBase::HandlePropertySet_SPINEL_PROP_MESHCOP_COMMISSIONER_GENERATE_PSKC(uint8_t aHeader) +{ + otError error = OT_ERROR_NONE; + const char * passPhrase; + const char * networkName; + const uint8_t *extPanIdData; + uint16_t length; + otPskc pskc; + + SuccessOrExit(error = mDecoder.ReadUtf8(passPhrase)); + SuccessOrExit(error = mDecoder.ReadUtf8(networkName)); + SuccessOrExit(error = mDecoder.ReadDataWithLen(extPanIdData, length)); + VerifyOrExit(length == sizeof(spinel_net_xpanid_t), error = OT_ERROR_PARSE); + + SuccessOrExit(error = otCommissionerGeneratePskc(passPhrase, networkName, + reinterpret_cast(extPanIdData), &pskc)); + + SuccessOrExit( + error = mEncoder.BeginFrame(aHeader, SPINEL_CMD_PROP_VALUE_IS, SPINEL_PROP_MESHCOP_COMMISSIONER_GENERATE_PSKC)); + SuccessOrExit(error = mEncoder.WriteData(pskc.m8, sizeof(pskc))); + SuccessOrExit(error = mEncoder.EndFrame()); + +exit: + return error; +} + // SPINEL_PROP_THREAD_COMMISSIONER_ENABLED is replaced by SPINEL_PROP_MESHCOP_COMMISSIONER_STATE. Please use the new // property. The old property/implementation remains for backward compatibility. diff --git a/src/ncp/spinel.c b/src/ncp/spinel.c index 82e5954e6..0087d1757 100644 --- a/src/ncp/spinel.c +++ b/src/ncp/spinel.c @@ -1883,6 +1883,10 @@ const char *spinel_prop_key_to_cstr(spinel_prop_key_t prop_key) ret = "MESHCOP_COMMISSIONER_MGMT_SET"; break; + case SPINEL_PROP_MESHCOP_COMMISSIONER_GENERATE_PSKC: + ret = "MESHCOP_COMMISSIONER_GENERATE_PSKC"; + break; + case SPINEL_PROP_CHANNEL_MANAGER_NEW_CHANNEL: ret = "CHANNEL_MANAGER_NEW_CHANNEL"; break; diff --git a/src/ncp/spinel.h b/src/ncp/spinel.h index 6e99451e7..684af190d 100644 --- a/src/ncp/spinel.h +++ b/src/ncp/spinel.h @@ -3208,6 +3208,29 @@ typedef enum */ SPINEL_PROP_MESHCOP_COMMISSIONER_MGMT_SET = SPINEL_PROP_MESHCOP_EXT__BEGIN + 6, + // Thread Commissioner Generate PSKc + /** Format: `UUd` - Write only + * + * Required capability: SPINEL_CAP_THREAD_COMMISSIONER + * + * Writing to this property allows user to generate PSKc from a given commissioning pass-phrase, network name, + * extended PAN Id. + * + * Written value format is: + * + * `U` : The commissioning pass-phrase. + * `U` : Network Name. + * `d` : Extended PAN ID. + * + * The response on success would be a `VALUE_IS` command with the PSKc with format below: + * + * `D` : The PSKc + * + * On a failure a `LAST_STATUS` is emitted with the error status. + * + */ + SPINEL_PROP_MESHCOP_COMMISSIONER_GENERATE_PSKC = SPINEL_PROP_MESHCOP_EXT__BEGIN + 7, + SPINEL_PROP_MESHCOP_EXT__END = 0x1900, SPINEL_PROP_OPENTHREAD__BEGIN = 0x1900,