📄 zdsecmgr.c
字号:
// set control date
ctrl->parentAddr = device->parentAddr;
ctrl->secure = device->secure;
ctrl->entry = entry;
ctrl->state = ZDSECMGR_CTRL_INIT;
ctrl->cntr = 0;
// set device pointer
device->ctrl = ctrl;
}
#endif // defined ( ZDO_COORDINATOR )
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrCtrlAdd
*
* @brief Add control data.
*
* @param device - [in] valid device data
* @param entry - [in] valid entry data
*
* @return none
*/
ZStatus_t ZDSecMgrCtrlAdd( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry )
{
//---------------------------------------------------------------------------
#if defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
ZStatus_t status;
uint16 index;
// initialize results
status = ZNwkUnknownDevice;
// verify data is available
if ( ZDSecMgrCtrlData != NULL )
{
// look for an empty slot
for ( index = 0; index < ZDSECMGR_CTRL_MAX; index++ )
{
if ( ZDSecMgrCtrlData[index].state == ZDSECMGR_CTRL_NONE )
{
// return successful results
ZDSecMgrCtrlSet( device, entry, &ZDSecMgrCtrlData[index] );
status = ZSuccess;
// break from loop
index = ZDSECMGR_CTRL_MAX;
}
}
}
return status;
//---------------------------------------------------------------------------
#else // !defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
return ZSuccess;
//---------------------------------------------------------------------------
#endif // !defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrCtrlTerm
*
* @brief Terminate device control.
*
* @param entry - [in] valid entry data
*
* @return none
*/
void ZDSecMgrCtrlTerm( ZDSecMgrEntry_t* entry )
{
//---------------------------------------------------------------------------
#if defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
ZDSecMgrCtrl_t* ctrl;
// remove device from control data
ZDSecMgrCtrlLookup ( entry, &ctrl );
if ( ctrl != NULL )
{
ZDSecMgrCtrlRelease ( ctrl );
}
//---------------------------------------------------------------------------
#endif // defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrCtrlReset
*
* @brief Reset control data.
*
* @param device - [in] valid device data
* @param entry - [in] valid entry data
*
* @return none
*/
ZStatus_t ZDSecMgrCtrlReset( ZDSecMgrDevice_t* device, ZDSecMgrEntry_t* entry )
{
//---------------------------------------------------------------------------
#if defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
ZStatus_t status;
ZDSecMgrCtrl_t* ctrl;
// initialize results
status = ZNwkUnknownDevice;
// look for a match for the entry
ZDSecMgrCtrlLookup( entry, &ctrl );
if ( ctrl != NULL )
{
ZDSecMgrCtrlSet( device, entry, ctrl );
status = ZSuccess;
}
else
{
status = ZDSecMgrCtrlAdd( device, entry );
}
return status;
//---------------------------------------------------------------------------
#else // !defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
return ZSuccess;
//---------------------------------------------------------------------------
#endif // !defined ( ZDO_COORDINATOR )
//---------------------------------------------------------------------------
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrMasterKeyLoad
*
* @brief Load the MASTER key for device with specified EXT
* address.
*
* @param nwkAddr - [in] NWK address of Trust Center
* @param extAddr - [in] EXT address of Trust Center
* @param key - [in] MASTER key shared with Trust Center
*
* @return none
*/
void ZDSecMgrMasterKeyLoad( uint16 nwkAddr, uint8* extAddr, uint8* key )
{
AddrMgrEntry_t addr;
uint8* loaded;
// check if Trust Center address is configured and correct
// check if MASTER key has already been sent
// add address data
addr.user = ADDRMGR_USER_SECURITY;
addr.nwkAddr = nwkAddr;
AddrMgrExtAddrSet( addr.extAddr, extAddr );
if ( AddrMgrEntryUpdate( &addr ) == TRUE )
{
if ( ZDSecMgrMasterKeyLookup( addr.index, &loaded ) != ZSuccess )
{
ZDSecMgrMasterKeyStore( addr.index, key );
}
}
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrAppKeyGet
*
* @brief get an APP key - option APP(MASTER or LINK) key
*
* @param initNwkAddr - [in] NWK address of initiator device
* @param initExtAddr - [in] EXT address of initiator device
* @param partNwkAddr - [in] NWK address of partner device
* @param partExtAddr - [in] EXT address of partner device
* @param key - [out] APP(MASTER or LINK) key
* @param keyType - [out] APP(MASTER or LINK) key type
*
* @return pointer to MASTER key
*/
ZStatus_t ZDSecMgrAppKeyGet( uint16 initNwkAddr,
uint8* initExtAddr,
uint16 partNwkAddr,
uint8* partExtAddr,
uint8** key,
uint8* keyType )
{
//---------------------------------------------------------------------------
// note:
// should use a robust mechanism to generate keys, for example
// combine EXT addresses and call a hash function
//---------------------------------------------------------------------------
osal_memset( *key, 0, SEC_KEY_LEN );
*keyType = KEY_TYPE_APP_LINK;
//or KEY_TYPE_APP_MASTER;
return ZSuccess;
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrAppKeyReq
*
* @brief Process request for APP key between two devices.
*
* @param device - [in] ZDO_RequestKeyInd_t, request info
*
* @return none
*/
void ZDSecMgrAppKeyReq( ZDO_RequestKeyInd_t* ind )
{
APSME_TransportKeyReq_t req;
uint8 initExtAddr[Z_EXTADDR_LEN];
uint16 partNwkAddr;
uint8 key[SEC_KEY_LEN];
// validate initiator and partner
if ( ( APSME_LookupNwkAddr( ind->partExtAddr, &partNwkAddr ) == TRUE ) &&
( APSME_LookupExtAddr( ind->srcAddr, initExtAddr ) == TRUE ) )
{
// point the key to some memory
req.key = key;
// get an APP key - option APP (MASTER or LINK) key
if ( ZDSecMgrAppKeyGet( ind->srcAddr,
initExtAddr,
partNwkAddr,
ind->partExtAddr,
&req.key,
&req.keyType ) == ZSuccess )
{
// always secure
req.secure = TRUE;
// send key to initiator device
req.dstAddr = ind->srcAddr;
req.extAddr = ind->partExtAddr;
req.initiator = TRUE;
APSME_TransportKeyReq( &req );
// send key to partner device
req.dstAddr = partNwkAddr;
req.extAddr = initExtAddr;
req.initiator = FALSE;
APSME_TransportKeyReq( &req );
}
}
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrEstablishKey
*
* @brief Start SKKE with device joining network.
*
* @param device - [in] ZDSecMgrDevice_t, device info
*
* @return ZStatus_t
*/
ZStatus_t ZDSecMgrEstablishKey( ZDSecMgrDevice_t* device )
{
ZStatus_t status;
APSME_EstablishKeyReq_t req;
req.respExtAddr = device->extAddr;
req.method = APSME_SKKE_METHOD;
if ( device->parentAddr == NLME_GetShortAddr() )
{
req.dstAddr = device->nwkAddr;
req.secure = FALSE;
}
else
{
req.dstAddr = device->parentAddr;
req.secure = TRUE;
}
status = APSME_EstablishKeyReq( &req );
return status;
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrSendMasterKey
*
* @brief Send MASTER key to device joining network.
*
* @param device - [in] ZDSecMgrDevice_t, device info
*
* @return ZStatus_t
*/
ZStatus_t ZDSecMgrSendMasterKey( ZDSecMgrDevice_t* device )
{
ZStatus_t status;
APSME_TransportKeyReq_t req;
req.keyType = KEY_TYPE_TC_MASTER;
req.extAddr = device->extAddr;
ZDSecMgrMasterKeyLookup( device->ctrl->entry->ami, &req.key );
//check if using secure hop to to parent
if ( device->parentAddr != NLME_GetShortAddr() )
{
//send to parent with security
req.dstAddr = device->parentAddr;
req.secure = TRUE;
}
else
{
//direct with no security
req.dstAddr = device->nwkAddr;
req.secure = FALSE;
}
status = APSME_TransportKeyReq( &req );
return status;
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrSendNwkKey
*
* @brief Send NWK key to device joining network.
*
* @param device - [in] ZDSecMgrDevice_t, device info
*
* @return ZStatus_t
*/
ZStatus_t ZDSecMgrSendNwkKey( ZDSecMgrDevice_t* device )
{
ZStatus_t status;
APSME_TransportKeyReq_t req;
//---------------------------------------------------------------------------
#if defined ( ZDSECMGR_COMMERCIAL )
//---------------------------------------------------------------------------
{
// set values
req.extAddr = device->extAddr;
req.keyType = KEY_TYPE_NWK;
req.keySeqNum = _NIB.nwkActiveKey.keySeqNum;
req.key = _NIB.nwkActiveKey.key;
// check if using secure hop to to parent
if ( device->parentAddr == NLME_GetShortAddr() )
{
req.dstAddr = device->nwkAddr;
req.secure = FALSE;
}
else
{
req.dstAddr = device->parentAddr;
req.secure = TRUE;
}
}
//---------------------------------------------------------------------------
#else // defined( ZDSECMGR_RESIDENTIAL )
//---------------------------------------------------------------------------
{
// default values
req.dstAddr = device->nwkAddr;
req.secure = device->secure;
req.keyType = KEY_TYPE_NWK;
req.extAddr = device->extAddr;
// special cases
if ( device->secure == FALSE )
{
req.keySeqNum = _NIB.nwkActiveKey.keySeqNum;
req.key = _NIB.nwkActiveKey.key;
// check if using secure hop to to parent
if ( device->parentAddr != NLME_GetShortAddr() )
{
req.dstAddr = device->parentAddr;
req.secure = TRUE;
}
}
else
{
req.key = NULL;
req.keySeqNum = 0;
}
}
//-------------------------------------------------------------------------
#endif // defined( ZDSECMGR_RESIDENTIAL )
//-------------------------------------------------------------------------
status = APSME_TransportKeyReq( &req );
return status;
}
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrDeviceEntryRemove
*
* @brief Remove device entry.
*
* @param entry - [in] valid entry
*
* @return none
*/
void ZDSecMgrDeviceEntryRemove( ZDSecMgrEntry_t* entry )
{
// terminate device control
ZDSecMgrCtrlTerm( entry );
// remove device from entry data
ZDSecMgrEntryFree( entry );
// remove EXT address
//ZDSecMgrExtAddrRelease( aiOld );
}
#endif // defined ( ZDSECMGR_COMMERCIAL )
#if defined ( ZDSECMGR_COMMERCIAL )
/******************************************************************************
* @fn ZDSecMgrDeviceEntryAdd
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -