mirror of
https://github.com/espressif/openthread.git
synced 2026-08-13 06:07:48 +00:00
Require Thread Master Key to be 16 bytes. (#1724)
- Update code to use `otMasterKey` type.
This commit is contained in:
@@ -296,39 +296,31 @@ public:
|
||||
String^ get()
|
||||
{
|
||||
constexpr char hexmap[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
uint8_t keyLen;
|
||||
auto key = otThreadGetMasterKey(DeviceInstance, &keyLen);
|
||||
auto key = otThreadGetMasterKey(DeviceInstance);
|
||||
WCHAR szKey[OT_MASTER_KEY_SIZE * 2 + 1] = { 0 };
|
||||
for (uint8_t i = 0; i < keyLen; i++)
|
||||
for (uint8_t i = 0; i < OT_MASTER_KEY_SIZE; i++)
|
||||
{
|
||||
szKey[2 * i] = hexmap[(key[i] & 0xF0) >> 4];
|
||||
szKey[2 * i + 1] = hexmap[key[i] & 0x0F];
|
||||
szKey[2 * i] = hexmap[(key->m8[i] & 0xF0) >> 4];
|
||||
szKey[2 * i + 1] = hexmap[key->m8[i] & 0x0F];
|
||||
}
|
||||
otFreeMemory(key);
|
||||
return ref new String(szKey);
|
||||
}
|
||||
void set(String^ value)
|
||||
{
|
||||
uint8_t key[OT_MASTER_KEY_SIZE];
|
||||
otMasterKey key;
|
||||
uint8_t keyLen = 0;
|
||||
if (value->Length() % 2 == 0)
|
||||
for (uint32_t i = 0; i < value->Length() - 1; i+=2)
|
||||
{
|
||||
for (uint32_t i = 0; i < value->Length(); i+=2)
|
||||
{
|
||||
key[keyLen++] = (uint8_t)((charToValue(value->Data()[i]) << 4) |
|
||||
charToValue(value->Data()[i + 1]));
|
||||
}
|
||||
key.m8[keyLen++] = (uint8_t)((charToValue(value->Data()[i]) << 4) |
|
||||
charToValue(value->Data()[i + 1]));
|
||||
}
|
||||
else
|
||||
if (keyLen * 2 == value->Length() - 1)
|
||||
{
|
||||
key[keyLen++] = (uint8_t)(charToValue(value->Data()[0]));
|
||||
for (uint32_t i = 1; i < value->Length(); i += 2)
|
||||
{
|
||||
key[keyLen++] = (uint8_t)((charToValue(value->Data()[i]) << 4) |
|
||||
charToValue(value->Data()[i + 1]));
|
||||
}
|
||||
key.m8[keyLen++] = (uint8_t)(charToValue(value->Data()[value->Length()-1])) << 4;
|
||||
}
|
||||
ThrowOnFailure(otThreadSetMasterKey(DeviceInstance, key, keyLen));
|
||||
memset(key.m8 + keyLen, 0, sizeof(key) - keyLen);
|
||||
ThrowOnFailure(otThreadSetMasterKey(DeviceInstance, &key));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1598,34 +1598,22 @@ otThreadSetLinkMode(
|
||||
}
|
||||
|
||||
OTAPI
|
||||
const uint8_t *
|
||||
const otMasterKey *
|
||||
OTCALL
|
||||
otThreadGetMasterKey(
|
||||
_In_ otInstance *aInstance,
|
||||
_Out_ uint8_t *aKeyLength
|
||||
_In_ otInstance *aInstance
|
||||
)
|
||||
{
|
||||
if (aInstance == nullptr || aKeyLength == nullptr) return nullptr;
|
||||
if (aInstance == nullptr) return nullptr;
|
||||
|
||||
*aKeyLength = 0;
|
||||
|
||||
struct otMasterKeyAndLength
|
||||
{
|
||||
otMasterKey Key;
|
||||
uint8_t Length;
|
||||
};
|
||||
otMasterKeyAndLength *Result = (otMasterKeyAndLength*)malloc(sizeof(otMasterKeyAndLength));
|
||||
otMasterKey *Result = (otMasterKey*)malloc(sizeof(otMasterKey));
|
||||
if (Result == nullptr) return nullptr;
|
||||
if (QueryIOCTL(aInstance, IOCTL_OTLWF_OT_MASTER_KEY, Result) != ERROR_SUCCESS)
|
||||
{
|
||||
free(Result);
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
*aKeyLength = Result->Length;
|
||||
}
|
||||
return (uint8_t*)Result;
|
||||
return Result;
|
||||
}
|
||||
|
||||
OTAPI
|
||||
@@ -1633,18 +1621,12 @@ ThreadError
|
||||
OTCALL
|
||||
otThreadSetMasterKey(
|
||||
_In_ otInstance *aInstance,
|
||||
const uint8_t *aKey,
|
||||
uint8_t aKeyLength
|
||||
const otMasterKey *aKey
|
||||
)
|
||||
{
|
||||
if (aInstance == nullptr) return kThreadError_InvalidArgs;
|
||||
|
||||
BYTE Buffer[sizeof(GUID) + sizeof(otMasterKey) + sizeof(uint8_t)];
|
||||
memcpy_s(Buffer, sizeof(Buffer), &aInstance->InterfaceGuid, sizeof(GUID));
|
||||
memcpy_s(Buffer + sizeof(GUID), sizeof(Buffer) - sizeof(GUID), aKey, aKeyLength);
|
||||
memcpy_s(Buffer + sizeof(GUID) + sizeof(otMasterKey), sizeof(Buffer) - sizeof(GUID) - sizeof(otMasterKey), &aKeyLength, sizeof(aKeyLength));
|
||||
|
||||
return DwordToThreadError(SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_MASTER_KEY, Buffer, sizeof(Buffer), nullptr, 0));
|
||||
return DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_MASTER_KEY, aKey));
|
||||
}
|
||||
|
||||
OTAPI
|
||||
|
||||
@@ -1632,19 +1632,16 @@ otLwfIoCtl_otMasterKey(
|
||||
{
|
||||
NTSTATUS status = STATUS_INVALID_PARAMETER;
|
||||
|
||||
if (InBufferLength >= sizeof(otMasterKey) + sizeof(uint8_t))
|
||||
if (InBufferLength >= sizeof(otMasterKey))
|
||||
{
|
||||
uint8_t aKeyLength = *(uint8_t*)(InBuffer + sizeof(otMasterKey));
|
||||
status = ThreadErrorToNtstatus(otThreadSetMasterKey(pFilter->otCtx, InBuffer, aKeyLength));
|
||||
status = ThreadErrorToNtstatus(otThreadSetMasterKey(pFilter->otCtx, (otMasterKey*)InBuffer));
|
||||
*OutBufferLength = 0;
|
||||
}
|
||||
else if (*OutBufferLength >= sizeof(otMasterKey) + sizeof(uint8_t))
|
||||
else if (*OutBufferLength >= sizeof(otMasterKey))
|
||||
{
|
||||
uint8_t aKeyLength = 0;
|
||||
const uint8_t* aMasterKey = otThreadGetMasterKey(pFilter->otCtx, &aKeyLength);
|
||||
memcpy(OutBuffer, aMasterKey, aKeyLength);
|
||||
memcpy((PUCHAR)OutBuffer + sizeof(otMasterKey), &aKeyLength, sizeof(uint8_t));
|
||||
*OutBufferLength = sizeof(otMasterKey) + sizeof(uint8_t);
|
||||
const otMasterKey* aMasterKey = otThreadGetMasterKey(pFilter->otCtx);
|
||||
memcpy(OutBuffer, aMasterKey, sizeof(otMasterKey));
|
||||
*OutBufferLength = sizeof(otMasterKey);
|
||||
status = STATUS_SUCCESS;
|
||||
}
|
||||
else
|
||||
@@ -1668,10 +1665,8 @@ otLwfTunIoCtl_otMasterKey(
|
||||
{
|
||||
NTSTATUS status = STATUS_INVALID_PARAMETER;
|
||||
|
||||
if (InBufferLength >= sizeof(otMasterKey) + sizeof(uint8_t))
|
||||
if (InBufferLength >= sizeof(otMasterKey))
|
||||
{
|
||||
spinel_size_t aKeyLength = *(uint8_t*)(InBuffer + sizeof(otMasterKey));
|
||||
|
||||
status =
|
||||
otLwfTunSendCommandForIrp(
|
||||
pFilter,
|
||||
@@ -1682,9 +1677,9 @@ otLwfTunIoCtl_otMasterKey(
|
||||
sizeof(otMasterKey) + sizeof(uint16_t),
|
||||
SPINEL_DATATYPE_DATA_S,
|
||||
(otMasterKey*)InBuffer,
|
||||
aKeyLength);
|
||||
sizeof(otMasterKey));
|
||||
}
|
||||
else if (OutBufferLength >= sizeof(otMasterKey) + sizeof(uint8_t))
|
||||
else if (OutBufferLength >= sizeof(otMasterKey))
|
||||
{
|
||||
status =
|
||||
otLwfTunSendCommandForIrp(
|
||||
@@ -1717,11 +1712,10 @@ otLwfTunIoCtl_otMasterKey_Handler(
|
||||
uint8_t *data = NULL;
|
||||
spinel_size_t aKeyLength;
|
||||
if (try_spinel_datatype_unpack(Data, DataLength, SPINEL_DATATYPE_DATA_S, &data, &aKeyLength) && data != NULL &&
|
||||
aKeyLength <= sizeof(otMasterKey))
|
||||
aKeyLength == sizeof(otMasterKey))
|
||||
{
|
||||
memcpy(OutBuffer, data, aKeyLength);
|
||||
*(uint8_t*)((PUCHAR)OutBuffer + sizeof(otMasterKey)) = (uint8_t)aKeyLength;
|
||||
*OutBufferLength = sizeof(otMasterKey) + sizeof(uint8_t);
|
||||
memcpy(OutBuffer, data, sizeof(otMasterKey));
|
||||
*OutBufferLength = sizeof(otMasterKey);
|
||||
status = STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1200,14 +1200,14 @@ OTNODEAPI int32_t OTCALL otNodeSetMasterkey(otNode* aNode, const char *aMasterke
|
||||
printf("%d: masterkey %s\r\n", aNode->mId, aMasterkey);
|
||||
|
||||
int keyLength;
|
||||
uint8_t key[OT_MASTER_KEY_SIZE];
|
||||
if ((keyLength = Hex2Bin(aMasterkey, key, sizeof(key))) != OT_MASTER_KEY_SIZE)
|
||||
otMasterKey key;
|
||||
if ((keyLength = Hex2Bin(aMasterkey, key.m8, sizeof(key.m8))) != OT_MASTER_KEY_SIZE)
|
||||
{
|
||||
printf("invalid length key %d\r\n", keyLength);
|
||||
return kThreadError_Parse;
|
||||
}
|
||||
|
||||
auto error = otThreadSetMasterKey(aNode->mInstance, key, (uint8_t)keyLength);
|
||||
auto error = otThreadSetMasterKey(aNode->mInstance, &key);
|
||||
otLogFuncExit();
|
||||
return error;
|
||||
}
|
||||
@@ -1215,15 +1215,14 @@ OTNODEAPI int32_t OTCALL otNodeSetMasterkey(otNode* aNode, const char *aMasterke
|
||||
OTNODEAPI const char* OTCALL otNodeGetMasterkey(otNode* aNode)
|
||||
{
|
||||
otLogFuncEntryMsg("[%d]", aNode->mId);
|
||||
uint8_t aKeyLength = 0;
|
||||
auto aMasterKey = otThreadGetMasterKey(aNode->mInstance, &aKeyLength);
|
||||
uint8_t strLength = 2*aKeyLength + 1;
|
||||
auto aMasterKey = otThreadGetMasterKey(aNode->mInstance);
|
||||
uint8_t strLength = 2*sizeof(otMasterKey) + 1;
|
||||
char* str = (char*)malloc(strLength);
|
||||
if (str != nullptr)
|
||||
{
|
||||
aNode->mMemoryToFree.push_back(str);
|
||||
for (int i = 0; i < aKeyLength; i++)
|
||||
sprintf_s(str + i * 2, strLength - (2 * i), "%02x", aMasterKey[i]);
|
||||
for (int i = 0; i < sizeof(otMasterKey); i++)
|
||||
sprintf_s(str + i * 2, strLength - (2 * i), "%02x", aMasterKey->m8[i]);
|
||||
printf("%d: masterkey\r\n%s\r\n", aNode->mId, str);
|
||||
}
|
||||
otFreeMemory(aMasterKey);
|
||||
|
||||
Reference in New Issue
Block a user