Exemplo n.º 1
0
        protected override void OnHandledInternal(DataReceivedUnit ou)
        {
            var                 destNodeId = ReceivedAchData.DestNodeId > 0 ? ReceivedAchData.DestNodeId : _securityManagerInfo.Network.NodeId;
            SecuritySchemes     scheme     = SecuritySchemes.NONE;
            InvariantPeerNodeId peerNodeId = new InvariantPeerNodeId(destNodeId, ReceivedAchData.SrcNodeId);

            ou.SetNextActionItems();
            if (!ou.DataFrame.IsSkippedSecurity)
            {
                if (_securityManagerInfo.Network.HasSecurityScheme(SecuritySchemeSet.ALLS2) && _securityManagerInfo.IsActive)
                {
                    byte[]             command            = ReceivedAchData.Command;
                    bool               isNonceReport      = false;
                    bool               isSupportedReport  = false; // Only for test frame
                    Extensions         extensions         = null;
                    SubstituteSettings substituteSettings = null;
                    if (command != null && command.Length > 1)
                    {
                        bool   isSubstituteDenied = false;
                        byte[] dataToSend         = null;
                        bool   isMulticastFrame   = (ou.DataFrame.Data[2] & MULTICAST_MASK) == MULTICAST_MASK;
                        bool   isBroadcastFrame   = (ou.DataFrame.Data[2] & BROADCAST_MASK) == BROADCAST_MASK;

                        if (command[1] == COMMAND_CLASS_SECURITY_2.SECURITY_2_NONCE_GET.ID && (SecuritySchemes)ReceivedAchData.SecurityScheme == SecuritySchemes.NONE)
                        {
                            byte rxSequenceNumber = command[2];
                            if (!isMulticastFrame && !isBroadcastFrame)
                            {
                                if (handlingNonceGetFromNode != ReceivedAchData.SrcNodeId)
                                {
                                    handlingNonceGetFromNode = ReceivedAchData.SrcNodeId;
                                    var currentTxSequenceNumber = _spanTable.GetTxSequenceNumber(peerNodeId);
                                    _spanTable.SetNonceFree(peerNodeId);

                                    //reset MPAN for owner Id
                                    foreach (byte groupId in _mpanTable.SelectGroupIds(ReceivedAchData.SrcNodeId))
                                    {
                                        _mpanTable.RemoveRecord(new NodeGroupId(ReceivedAchData.SrcNodeId, groupId));
                                    }
                                    dataToSend         = _securityS2CryptoProvider.GenerateNonceReport(_spanTable, peerNodeId, ++currentTxSequenceNumber, rxSequenceNumber, true, false);
                                    isNonceReport      = true;
                                    isSubstituteDenied = true;
                                }
                            }
                        }
                        else if (command[1] == COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.ID)
                        {
                            byte rxSequenceNumber = command[2];
                            if (!isMulticastFrame && !isBroadcastFrame && ValidateS2MessageExtensions(command))
                            {
                                var currentTxSequenceNumber = _spanTable.GetTxSequenceNumber(peerNodeId);
                                _spanTable.SetNonceFree(peerNodeId);

                                var isMos = _securityS2CryptoProvider.CheckMpanMosForOwnerNode(_mpanTable, ReceivedAchData.SrcNodeId);
                                dataToSend         = _securityS2CryptoProvider.GenerateNonceReport(_spanTable, peerNodeId, ++currentTxSequenceNumber, rxSequenceNumber, true, isMos);
                                isNonceReport      = true;
                                isSubstituteDenied = true;
                            }
                        }
                        else if (command[1] == COMMAND_CLASS_SECURITY_2.SECURITY_2_NONCE_REPORT.ID && (SecuritySchemes)ReceivedAchData.SecurityScheme == SecuritySchemes.NONE)
                        {
                            if (!isMulticastFrame && !isBroadcastFrame)
                            {
                                COMMAND_CLASS_SECURITY_2.SECURITY_2_NONCE_REPORT nonceReportCmd = command;
                                if (_securityManagerInfo.InitializingNodeId != ReceivedAchData.SrcNodeId) // Node must be already initialized.
                                {
                                    if (nonceReportCmd.properties1.sos > 0 &&                             // Singlecast out of sync.
                                        nonceReportCmd.receiversEntropyInput != null &&
                                        nonceReportCmd.receiversEntropyInput.Count == 16
                                        )
                                    {
                                        var rTable = _securityManagerInfo.RetransmissionTableS2;
                                        if (rTable.ContainsKey(peerNodeId))
                                        {
                                            if (rTable[peerNodeId].Counter > 0)
                                            {
                                                _spanTable.AddOrReplace(peerNodeId,
                                                                        nonceReportCmd.receiversEntropyInput.ToArray(),
                                                                        _spanTable.GetTxSequenceNumber(peerNodeId),
                                                                        nonceReportCmd.sequenceNumber);

                                                dataToSend         = rTable[peerNodeId].Data;
                                                scheme             = rTable[peerNodeId].SecurityScheme;
                                                substituteSettings = rTable[peerNodeId].SubstituteSettings;
                                                rTable[peerNodeId].Counter--;
                                            }
                                            else
                                            {
                                                rTable.Remove(peerNodeId);
                                                _spanTable.SetNonceFree(peerNodeId);
                                            }
                                        }
                                        else
                                        {
                                            _spanTable.SetNonceFree(peerNodeId);
                                        }
                                    }
                                    if (nonceReportCmd.properties1.mos > 0) // Mutlicast out of sync.
                                    {
                                        var groupId = _securityS2CryptoProvider.LastSentMulticastGroupId;

                                        extensions = new Extensions();
                                        var nodeGroupId = new NodeGroupId(destNodeId, groupId);
                                        if (!_mpanTable.CheckMpanExists(nodeGroupId))
                                        {
                                            _mpanTable.AddOrReplace(nodeGroupId, 0x55, null, _securityS2CryptoProvider.GetRandomData());
                                        }
                                        extensions.AddMpanExtension(_mpanTable.GetContainer(nodeGroupId).MpanState, groupId);
                                    }
                                }
                            }
                        }
                        else if (command[1] == COMMAND_CLASS_SECURITY_2.SECURITY_2_COMMANDS_SUPPORTED_GET.ID)
                        {
                            if (!isMulticastFrame && !isBroadcastFrame)
                            {
                                scheme = (SecuritySchemes)ReceivedAchData.SecurityScheme;
                                if (scheme != SecuritySchemes.NONE && scheme != SecuritySchemes.S0 && _securityManagerInfo.Network.HasSecurityScheme(scheme))
                                {
                                    if (!_securityManagerInfo.Network.HasSecurityScheme(ReceivedAchData.SrcNodeId, SecuritySchemeSet.ALLS2) &&
                                        !_securityManagerInfo.Network.IsSecuritySchemesSpecified(ReceivedAchData.SrcNodeId))
                                    {
                                        _securityManagerInfo.Network.SetSecuritySchemes(ReceivedAchData.SrcNodeId, SecuritySchemeSet.ALL);
                                    }

                                    isSupportedReport = true;
                                    var ccReport = new COMMAND_CLASS_SECURITY_2.SECURITY_2_COMMANDS_SUPPORTED_REPORT();
                                    if (ReceivedAchData.CommandType == CommandTypes.CmdApplicationCommandHandler_Bridge &&
                                        ReceivedAchData.DestNodeId != _securityManagerInfo.Network.NodeId)
                                    {
                                        ccReport.commandClass = new List <byte>(_securityManagerInfo.Network.GetVirtualSecureCommandClasses());
                                    }
                                    else
                                    {
                                        var secureCommandClasses = _securityManagerInfo.Network.GetSecureCommandClasses();
                                        if (secureCommandClasses != null)
                                        {
                                            switch (scheme)
                                            {
                                            case SecuritySchemes.S2_UNAUTHENTICATED:
                                                if (!_securityManagerInfo.Network.HasSecurityScheme(SecuritySchemes.S2_ACCESS) &&
                                                    !_securityManagerInfo.Network.HasSecurityScheme(SecuritySchemes.S2_AUTHENTICATED))
                                                {
                                                    ccReport.commandClass = new List <byte>(_securityManagerInfo.Network.GetSecureCommandClasses());
                                                }
                                                break;

                                            case SecuritySchemes.S2_AUTHENTICATED:
                                                if (!_securityManagerInfo.Network.HasSecurityScheme(SecuritySchemes.S2_ACCESS))
                                                {
                                                    ccReport.commandClass = new List <byte>(_securityManagerInfo.Network.GetSecureCommandClasses());
                                                }
                                                break;

                                            case SecuritySchemes.S2_ACCESS:
                                                ccReport.commandClass = new List <byte>(secureCommandClasses);
                                                break;

                                            default:
                                                break;
                                            }
                                        }
                                    }
                                    dataToSend = ccReport;
                                }
                            }
                        }

                        if (dataToSend != null || extensions != null)
                        {
                            ApiOperation sendData = null;
                            if (SecuritySchemeSet.ALLS2.Contains(scheme))
                            {
                                sendData = new SendDataExOperation(ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, dataToSend, _securityManagerInfo.TxOptions, scheme);
                            }
                            else
                            {
                                if (ReceivedAchData.DestNodeId > 0)
                                {
                                    sendData = new SendDataBridgeOperation(ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, dataToSend, _securityManagerInfo.TxOptions);
                                    if (extensions != null)
                                    {
                                        ((SendDataBridgeOperation)sendData).Extensions = extensions;
                                    }
                                }
                                else
                                {
                                    sendData = new SendDataOperation(ReceivedAchData.SrcNodeId, dataToSend, _securityManagerInfo.TxOptions);
                                    if (extensions != null)
                                    {
                                        ((SendDataOperation)sendData).Extensions = extensions;
                                    }
                                }
                            }

                            if (substituteSettings != null)
                            {
                                sendData.SubstituteSettings = substituteSettings;
                            }

                            if (isSubstituteDenied)
                            {
                                sendData.SubstituteSettings.SetFlag(SubstituteFlags.DenySecurity);
                            }


                            sendData.CompletedCallback = (x) =>
                            {
                                var action = x as ActionBase;
                                if (action != null)
                                {
                                    handlingNonceGetFromNode = 0;
                                    SpecificResult.TotalCount++;
                                    if (action.Result.State != ActionStates.Completed)
                                    {
                                        SpecificResult.FailCount++;
                                    }
                                }
                            };
                            #region TestFrames
                            if (isNonceReport)
                            {
                                _securityTestSettingsService.ActivateTestPropertiesForFrame(SecurityS2TestFrames.NonceReport, sendData);
                            }
                            else if (isSupportedReport)
                            {
                                _securityTestSettingsService.ActivateTestPropertiesForFrame(SecurityS2TestFrames.CommandsSupportedReport, sendData);
                            }
                            #endregion

                            ou.SetNextActionItems(sendData);
                        }
                        else
                        {
                            ou.SetNextActionItems();
                        }
                    }
                }
                else
                {
                    "REJECT, {0}, {1} (IsNodeSecureS2={2}, IsActive={3}"._DLOG(
                        _securityManagerInfo.IsInclusion,
                        _securityManagerInfo.Network.HasSecurityScheme(ReceivedAchData.SrcNodeId, SecuritySchemeSet.ALLS2),
                        _securityManagerInfo.Network.HasSecurityScheme(SecuritySchemeSet.ALLS2),
                        _securityManagerInfo.IsActive);
                }
            }
        }
Exemplo n.º 2
0
        private byte[] UnboxAndGetResponce(byte[] encapedCommand)
        {
            byte[] data     = null;
            var    encapCmd = (COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CMD_ENCAP)encapedCommand;
            var    destEPId = encapCmd.properties2.destinationEndPoint;

            if (destEPId > 0 && destEPId <= END_POINTS_COUNT)
            {
                var command = new List <byte>();
                command.Add(encapCmd.commandClass);
                command.Add(encapCmd.command);
                command.AddRange(encapCmd.parameter);

                #region Security and Capability
                if (encapCmd.commandClass == COMMAND_CLASS_SECURITY_2.ID &&
                    encapCmd.command == COMMAND_CLASS_SECURITY_2.SECURITY_2_COMMANDS_SUPPORTED_GET.ID)
                {
                    var rpt = new COMMAND_CLASS_SECURITY_2.SECURITY_2_COMMANDS_SUPPORTED_REPORT()
                    {
                        commandClass = new List <byte>(_network.GetSecureFilteredCommandClasses(ENDPOINTS_COMMAND_CLASSES, true))
                    };
                    data = EncapData(rpt, destEPId);
                }
                else if (encapCmd.commandClass == COMMAND_CLASS_SECURITY.ID &&
                         encapCmd.command == COMMAND_CLASS_SECURITY.SECURITY_COMMANDS_SUPPORTED_GET.ID)
                {
                    var rpt = new COMMAND_CLASS_SECURITY.SECURITY_COMMANDS_SUPPORTED_REPORT()
                    {
                        commandClassSupport = new List <byte>(_network.GetSecureFilteredCommandClasses(ENDPOINTS_COMMAND_CLASSES, true))
                    };
                    data = EncapData(rpt, destEPId);
                }
                else if (encapCmd.commandClass == COMMAND_CLASS_MULTI_CHANNEL_V4.ID &&
                         encapCmd.command == COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_GET.ID)
                {
                    var rpt = new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_REPORT()
                    {
                        genericDeviceClass  = GENERIC_DEVICE_CLASS,
                        specificDeviceClass = SPECIFIC_DEVICE_CLASS,
                        properties1         = new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_CAPABILITY_REPORT.Tproperties1()
                        {
                            endPoint = destEPId
                        },
                        commandClass = GetCapability(destEPId)
                    };
                    data = EncapData(rpt, destEPId);
                }
                #endregion

                #region MULTI CHANNEL ASSOCIATIONS
                else if (encapCmd.commandClass == COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.ID)
                {
                    if (encapCmd.command == COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_GET.ID)
                    {
                        var unboxedCmd = (COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_GET)command.ToArray();
                        if (unboxedCmd.groupingIdentifier == _groupId)
                        {
                            var rpt = new COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_REPORT()
                            {
                                groupingIdentifier = _groupId,
                                nodeId             = _associatedNodeIds[destEPId - 1],
                                maxNodesSupported  = _maxNodesSupported
                            };
                            data = EncapData(rpt, destEPId);
                        }
                    }
                    else if (encapCmd.command == COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_GROUPINGS_GET.ID)
                    {
                        var unboxedCmd = (COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_GROUPINGS_GET)command.ToArray();
                        {
                            var rpt = new COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_GROUPINGS_REPORT()
                            {
                                supportedGroupings = 0x01
                            };
                        }
                    }
                    else if (encapCmd.command == COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_REMOVE.ID)
                    {
                        var unboxedCmd = (COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_REMOVE)command.ToArray();
                        if (unboxedCmd.groupingIdentifier == _groupId)
                        {
                            foreach (var associateNodeId in unboxedCmd.nodeId)
                            {
                                if (!_associatedNodeIds[destEPId - 1].Contains(associateNodeId))
                                {
                                    _associatedNodeIds[destEPId - 1].Remove(associateNodeId);
                                }
                            }
                        }
                    }
                    else if (encapCmd.command == COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_SET.ID)
                    {
                        var unboxedCmd = (COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.MULTI_CHANNEL_ASSOCIATION_SET)command.ToArray();
                        if (unboxedCmd.groupingIdentifier == _groupId)
                        {
                            foreach (var associateNodeId in unboxedCmd.nodeId)
                            {
                                if (!_associatedNodeIds[destEPId - 1].Contains(associateNodeId))
                                {
                                    _associatedNodeIds[destEPId - 1].Add(associateNodeId);
                                }
                            }
                        }
                    }
                }
                #endregion

                #region Association GRP
                else if (encapCmd.commandClass == COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ID)
                {
                    if (encapCmd.command == COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_NAME_GET.ID)
                    {
                        var associationNameGetCmd = (COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_NAME_GET)command.ToArray();
                        var requestedGroupId      = associationNameGetCmd.groupingIdentifier;
                        if (requestedGroupId == _groupId)
                        {
                            var rpt = new COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_NAME_REPORT()
                            {
                                groupingIdentifier = _groupId,
                                lengthOfName       = (byte)Encoding.UTF8.GetByteCount(_groupName),
                                name = Encoding.UTF8.GetBytes(_groupName)
                            };
                            data = EncapData(rpt, destEPId);
                        }
                    }
                    else if (encapCmd.command == COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_INFO_GET.ID)
                    {
                        var associationInfoGetCmd = (COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_INFO_GET)command.ToArray();
                        var requestedGroupId      = associationInfoGetCmd.groupingIdentifier;
                        var rpt = new COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_INFO_REPORT()
                        {
                            properties1 = new COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_INFO_REPORT.Tproperties1()
                            {
                                listMode   = associationInfoGetCmd.properties1.listMode,
                                groupCount = 0x01
                            },
                            vg1 = new List <COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_INFO_REPORT.TVG1>()
                            {
                                new COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_INFO_REPORT.TVG1()
                                {
                                    groupingIdentifier = _groupId,
                                    mode     = 0,
                                    profile1 = 0,
                                    profile2 = 1
                                }
                            }
                        };
                        data = EncapData(rpt, destEPId);
                    }
                    else if (encapCmd.command == COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_COMMAND_LIST_GET.ID)
                    {
                        var associationCommandListGetCmd = (COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_COMMAND_LIST_GET)command.ToArray();
                        var requestedGroupId             = associationCommandListGetCmd.groupingIdentifier;
                        var rpt = new COMMAND_CLASS_ASSOCIATION_GRP_INFO_V3.ASSOCIATION_GROUP_COMMAND_LIST_REPORT()
                        {
                            groupingIdentifier = _groupId,
                            listLength         = 0x02,
                            command            = new List <byte>()
                            {
                                COMMAND_CLASS_BASIC.ID,
                                COMMAND_CLASS_BASIC.BASIC_GET.ID,
                            }
                        };
                        data = EncapData(rpt, destEPId);
                    }
                }
                #endregion

                #region Multichannel Association
                else //if (encapCmd.commandClass == COMMAND_CLASS_MULTI_CHANNEL_ASSOCIATION_V3.ID)
                {
                }
                #endregion
            }
            return(data);
        }