Move PSKc generation functionality to Commissioner role. (#1327)

This commit is contained in:
Jonathan Hui
2017-02-15 23:10:44 -08:00
committed by GitHub
parent bf21420cd0
commit 87bbf4a83a
14 changed files with 83 additions and 107 deletions
+19
View File
@@ -222,6 +222,25 @@ OTAPI ThreadError OTCALL otSendMgmtCommissionerSet(otInstance *, const otCommiss
*/
OTAPI uint16_t OTCALL otCommissionerGetSessionId(otInstance *);
/**
* This method generates PSKc.
*
* PSKc is used to establish the Commissioner Session.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aPassPhrase The commissioning passphrase.
* @param[in] aNetworkName The network name for PSKc computation.
* @param[in] aExtPanId The extended pan id for PSKc computation.
* @param[out] aPSKc A pointer to where the generated PSKc will be placed.
*
* @retval kThreadErrorNone Successfully generate PSKc.
* @retval kThreadError_InvalidArgs If any of the input arguments is invalid.
*
*/
OTAPI ThreadError OTCALL otCommissionerGeneratePSKc(otInstance *aInstance, const char *aPassPhrase,
const char *aNetworkName, const uint8_t *aExtPanId,
uint8_t *aPSKc);
/**
* @}
*
-1
View File
@@ -249,7 +249,6 @@ typedef struct otMeshLocalPrefix
#define OT_COMMISSIONING_PASSPHRASE_MIN_SIZE 6 ///< Minimum size of the Commissioning Passphrase
#define OT_COMMISSIONING_PASSPHRASE_MAX_SIZE 255 ///< Maximum size of the Commissioning Passphrase
#define OT_COMMISSIONING_PASSPHRASE_DEFAULT "OpenThreadPassphrase" ///< The default commissioning passphrase
/**
* This structure represents PSKc.
-18
View File
@@ -639,24 +639,6 @@ OTAPI const uint8_t *OTCALL otGetMasterKey(otInstance *aInstance, uint8_t *aKeyL
*/
OTAPI ThreadError OTCALL otSetMasterKey(otInstance *aInstance, const uint8_t *aKey, uint8_t aKeyLength);
/**
* This method generates PSKc.
*
* PSKc is used to establish the Commissioner Session.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aPassPhrase The commissioning passphrase.
* @param[in] aNetworkName The network name for PSKc computation.
* @param[in] aExtPanId The extended pan id for PSKc computation.
* @param[out] aPSKc A pointer to where the generated PSKc will be placed.
*
* @retval kThreadErrorNone Successfully generate PSKc.
* @retval kThreadError_InvalidArgs If any of the input arguments is invalid.
*
*/
OTAPI ThreadError OTCALL otGeneratePSKc(otInstance *aInstance, const char *aPassPhrase, const char *aNetworkName,
const uint8_t *aExtPanId, uint8_t *aPSKc);
/**
* This function returns the maximum transmit power setting in dBm.
*
-9
View File
@@ -655,15 +655,6 @@ Set pskc with hex format.
Done
```
### dataset pskc \[passphrase\] \[networkname\] \[extpanid\]
Generate pskc from passphrase, network name and extended panid.
```bash
> dataset pskc OpenThreadPassphrase OpenThread dead00beef00cafe
Done
```
### dataset userdata \[size\] \[data\]
Set user specific data for the command.
+6 -15
View File
@@ -666,25 +666,16 @@ exit:
ThreadError Dataset::ProcessPSKc(otInstance *aInstance, int argc, char *argv[])
{
ThreadError error = kThreadError_None;
uint8_t extPanId[OT_EXT_PAN_ID_SIZE];
uint16_t length;
VerifyOrExit((argc == 1) || (argc == 3), error = kThreadError_Parse);
if (argc == 1)
{
length = static_cast<uint16_t>((strlen(argv[0]) + 1) / 2);
VerifyOrExit(length <= OT_PSKC_MAX_SIZE, error = kThreadError_NoBufs);
VerifyOrExit(Interpreter::Hex2Bin(argv[0], sDataset.mPSKc.m8 + OT_PSKC_MAX_SIZE - length, length) == length,
error = kThreadError_Parse);
}
else
{
VerifyOrExit(Interpreter::Hex2Bin(argv[2], extPanId, sizeof(extPanId)) >= 0, error = kThreadError_Parse);
SuccessOrExit(error = otGeneratePSKc(aInstance, argv[0], argv[1], extPanId, sDataset.mPSKc.m8));
}
VerifyOrExit(argc > 0, error = kThreadError_Parse);
length = static_cast<uint16_t>((strlen(argv[0]) + 1) / 2);
VerifyOrExit(length <= OT_PSKC_MAX_SIZE, error = kThreadError_NoBufs);
VerifyOrExit(Interpreter::Hex2Bin(argv[0], sDataset.mPSKc.m8 + OT_PSKC_MAX_SIZE - length, length) == length,
error = kThreadError_Parse);
sDataset.mIsPSKcSet = true;
(void)aInstance;
exit:
return error;
+1 -1
View File
@@ -97,6 +97,7 @@ endif # OPENTHREAD_ENABLE_MAC_WHITELIST
if OPENTHREAD_ENABLE_COMMISSIONER
SOURCES_COMMON += \
coap/secure_coap_server.cpp \
crypto/pbkdf2_cmac.cpp \
meshcop/announce_begin_client.cpp \
meshcop/commissioner.cpp \
meshcop/energy_scan_client.cpp \
@@ -147,7 +148,6 @@ libopenthread_ftd_a_CPPFLAGS = \
libopenthread_ftd_a_SOURCES = \
$(SOURCES_COMMON) \
crypto/pbkdf2_cmac.cpp \
meshcop/dataset_manager_ftd.cpp \
meshcop/joiner_router.cpp \
meshcop/leader.cpp \
+29
View File
@@ -46,6 +46,7 @@
#include <common/crc16.hpp>
#include <common/encoding.hpp>
#include <common/logging.hpp>
#include <crypto/pbkdf2_cmac.h>
#include <meshcop/commissioner.hpp>
#include <meshcop/joiner_router.hpp>
#include <meshcop/tlvs.hpp>
@@ -926,5 +927,33 @@ exit:
return error;
}
ThreadError Commissioner::GeneratePSKc(const char *aPassPhrase, const char *aNetworkName, const uint8_t *aExtPanId,
uint8_t *aPSKc)
{
ThreadError error = kThreadError_None;
const char *saltPrefix = "Thread";
uint8_t salt[OT_PBKDF2_SALT_MAX_LEN];
uint16_t saltLen = 0;
VerifyOrExit((strlen(aPassPhrase) >= OT_COMMISSIONING_PASSPHRASE_MIN_SIZE) &&
(strlen(aPassPhrase) <= OT_COMMISSIONING_PASSPHRASE_MAX_SIZE), error = kThreadError_InvalidArgs);
memset(salt, 0, sizeof(salt));
memcpy(salt, saltPrefix, strlen(saltPrefix));
saltLen += static_cast<uint16_t>(strlen(saltPrefix));
memcpy(salt + saltLen, aExtPanId, OT_EXT_PAN_ID_SIZE);
saltLen += OT_EXT_PAN_ID_SIZE;
memcpy(salt + saltLen, aNetworkName, strlen(aNetworkName));
saltLen += static_cast<uint16_t>(strlen(aNetworkName));
otPbkdf2Cmac(reinterpret_cast<const uint8_t *>(aPassPhrase), static_cast<uint16_t>(strlen(aPassPhrase)),
reinterpret_cast<const uint8_t *>(salt), saltLen, 16384, OT_PSKC_MAX_SIZE, aPSKc);
exit:
return error;
}
} // namespace MeshCoP
} // namespace Thread
+17
View File
@@ -174,6 +174,23 @@ public:
ThreadError SendMgmtCommissionerSetRequest(const otCommissioningDataset &aDataset,
const uint8_t *aTlvs, uint8_t aLength);
/**
* This static method generates PSKc.
*
* PSKc is used to establish the Commissioner Session.
*
* @param[in] aPassPhrase The commissioning passphrase.
* @param[in] aNetworkName The network name for PSKc computation.
* @param[in] aExtPanId The extended pan id for PSKc computation.
* @param[out] aPSKc A pointer to where the generated PSKc will be placed.
*
* @retval kThreadErrorNone Successfully generate PSKc.
* @retval kThreadError_InvalidArgs If the length of passphrase is out of range.
*
*/
static ThreadError GeneratePSKc(const char *aPassPhrase, const char *aNetworkName, const uint8_t *aExtPanId,
uint8_t *aPSKc);
AnnounceBeginClient mAnnounceBegin;
EnergyScanClient mEnergyScan;
PanIdQueryClient mPanIdQuery;
+1 -6
View File
@@ -156,13 +156,8 @@ ThreadError ActiveDataset::GenerateLocal(void)
// PSKc
if (!IsTlvInitialized(Tlv::kPSKc))
{
const uint8_t pskc[OT_PSKC_MAX_SIZE] = {0};
PSKcTlv tlv;
uint8_t pskc[OT_PSKC_MAX_SIZE] = {0};
const char password[OT_COMMISSIONING_PASSPHRASE_MAX_SIZE] = OT_COMMISSIONING_PASSPHRASE_DEFAULT;
const char *networkName = mNetif.GetMac().GetNetworkName();
const uint8_t *extPanId = mNetif.GetMac().GetExtendedPanId();
mNetif.GetNetworkDataLeader().GeneratePSKc(password, networkName, extPanId, pskc);
tlv.Init();
tlv.SetPSKc(pskc);
mLocal.Set(tlv);
+6 -6
View File
@@ -270,12 +270,6 @@ ThreadError otSetMasterKey(otInstance *aInstance, const uint8_t *aKey, uint8_t a
return aInstance->mThreadNetif.GetKeyManager().SetMasterKey(aKey, aKeyLength);
}
ThreadError otGeneratePSKc(otInstance *aInstance, const char *aPassPhrase, const char *aNetworkName,
const uint8_t *aExtPanId, uint8_t *aPSKc)
{
return aInstance->mThreadNetif.GetNetworkDataLeader().GeneratePSKc(aPassPhrase, aNetworkName, aExtPanId, aPSKc);
}
int8_t otGetMaxTransmitPower(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetMac().GetMaxTransmitPower();
@@ -1821,6 +1815,12 @@ uint16_t otCommissionerGetSessionId(otInstance *aInstance)
{
return aInstance->mThreadNetif.GetCommissioner().GetSessionId();
}
ThreadError otCommissionerGeneratePSKc(otInstance *aInstance, const char *aPassPhrase, const char *aNetworkName,
const uint8_t *aExtPanId, uint8_t *aPSKc)
{
return aInstance->mThreadNetif.GetCommissioner().GeneratePSKc(aPassPhrase, aNetworkName, aExtPanId, aPSKc);
}
#endif // OPENTHREAD_ENABLE_COMMISSIONER
#if OPENTHREAD_ENABLE_JOINER
@@ -48,7 +48,6 @@
#include <thread/thread_tlvs.hpp>
#include <thread/thread_uris.hpp>
#include <thread/lowpan.hpp>
#include <crypto/pbkdf2_cmac.h>
using Thread::Encoding::BigEndian::HostSwap16;
@@ -1075,33 +1074,5 @@ void Leader::HandleTimer(void)
}
}
ThreadError Leader::GeneratePSKc(const char *aPassPhrase, const char *aNetworkName, const uint8_t *aExtPanId,
uint8_t *aPSKc)
{
ThreadError error = kThreadError_None;
const char *saltPrefix = "Thread";
uint8_t salt[OT_PBKDF2_SALT_MAX_LEN];
uint16_t saltLen = 0;
VerifyOrExit((strlen(aPassPhrase) >= OT_COMMISSIONING_PASSPHRASE_MIN_SIZE) &&
(strlen(aPassPhrase) <= OT_COMMISSIONING_PASSPHRASE_MAX_SIZE), error = kThreadError_InvalidArgs);
memset(salt, 0, sizeof(salt));
memcpy(salt, saltPrefix, strlen(saltPrefix));
saltLen += static_cast<uint16_t>(strlen(saltPrefix));
memcpy(salt + saltLen, aExtPanId, OT_EXT_PAN_ID_SIZE);
saltLen += OT_EXT_PAN_ID_SIZE;
memcpy(salt + saltLen, aNetworkName, strlen(aNetworkName));
saltLen += static_cast<uint16_t>(strlen(aNetworkName));
otPbkdf2Cmac(reinterpret_cast<const uint8_t *>(aPassPhrase), static_cast<uint16_t>(strlen(aPassPhrase)),
reinterpret_cast<const uint8_t *>(salt), saltLen, 16384, OT_PSKC_MAX_SIZE, aPSKc);
exit:
return error;
}
} // namespace NetworkData
} // namespace Thread
@@ -140,22 +140,6 @@ public:
*/
ThreadError SendServerDataNotification(uint16_t aRloc16);
/**
* This method generates PSKc.
*
* PSKc is used to establish the Commissioner Session.
*
* @param[in] aPassPhrase The commissioning passphrase.
* @param[in] aNetworkName The network name for PSKc computation.
* @param[in] aExtPanId The extended pan id for PSKc computation.
* @param[out] aPSKc A pointer to where the generated PSKc will be placed.
*
* @retval kThreadErrorNone Successfully generate PSKc.
* @retval kThreadError_InvalidArgs If the length of passphrase is out of range.
*
*/
ThreadError GeneratePSKc(const char *aPassPhrase, const char *aNetworkName, const uint8_t *aExtPanId, uint8_t *aPSKc);
private:
static void HandleServerData(void *aContext, otCoapHeader *aHeader, otMessage aMessage,
const otMessageInfo *aMessageInfo);
@@ -59,8 +59,6 @@ public:
void RemoveBorderRouter(uint16_t) { }
ThreadError SendServerDataNotification(uint16_t) { return kThreadError_NotImplemented; }
ThreadError GeneratePSKc(const char *, const char *, const uint8_t *, uint8_t *) { return kThreadError_NotImplemented; }
};
} // namespace NetworkData
+4 -4
View File
@@ -43,10 +43,6 @@ libmbedcrypto_a_CPPFLAGS = \
libmbedcrypto_a_SOURCES = \
hardware_entropy.c \
repo/library/aes.c \
repo/library/ccm.c \
repo/library/cipher.c \
repo/library/cipher_wrap.c \
repo/library/cmac.c \
repo/library/md.c \
repo/library/md_wrap.c \
repo/library/memory_buffer_alloc.c \
@@ -57,6 +53,10 @@ libmbedcrypto_a_SOURCES = \
if OPENTHREAD_ENABLE_DTLS
libmbedcrypto_a_SOURCES += \
repo/library/bignum.c \
repo/library/ccm.c \
repo/library/cipher.c \
repo/library/cipher_wrap.c \
repo/library/cmac.c \
repo/library/ctr_drbg.c \
repo/library/debug.c \
repo/library/ecjpake.c \