Example #1
0
        protected override void CreateInstance()
        {
            requestNonce = new RequestDataOperation(SrcNodeId, DestNodeId,
                                                    new COMMAND_CLASS_SECURITY.SECURITY_NONCE_GET(), TxOptions,
                                                    new COMMAND_CLASS_SECURITY.SECURITY_NONCE_REPORT(), 2, NONCE_REQUEST_TIMER);
            if (_securityManagerInfo.IsInclusion)
            {
                requestNonce.Token.Reset(NONCE_REQUEST_INCLUSION_TIMER);
            }
            requestNonce.SubstituteSettings.SetFlag(SubstituteFlags.DenySecurity);

            sendEncData = new SendDataBridgeOperation(SrcNodeId, DestNodeId, null, TxOptions);
            sendEncData.SubstituteSettings.SetFlag(SubstituteFlags.DenySecurity);
        }
Example #2
0
        protected override void CreateInstance()
        {
            _sendData = new SendDataOperation(DestNodeId, Data, TxOptions)
            {
                IsFollowup = IsFollowup
            };
            _sendData.DataDelay          = DataDelay;
            _sendData.SubstituteCallback = SendDataSubstituteCallback;
            _sendData.SubstituteSettings = SubstituteSettings;

            _sendDataBridge                    = new SendDataBridgeOperation(SrcNodeId, DestNodeId, Data, TxOptions);
            _sendDataBridge.DataDelay          = DataDelay;
            _sendDataBridge.SubstituteCallback = SendDataSubstituteCallback;
            _sendDataBridge.SubstituteSettings = SubstituteSettings;
            ExpectData = new ExpectDataOperation(SrcNodeId, DestNodeId, _dataToCompare, _timeoutMs);
        }
Example #3
0
        protected override void CreateInstance()
        {
            _spanTable.UpdateTxSequenceNumber(_peerNodeId);
            _requestNonce = new RequestDataOperation(SrcNodeId,
                                                     DestNodeId,
                                                     new COMMAND_CLASS_SECURITY_2.SECURITY_2_NONCE_GET()
            {
                sequenceNumber = _spanTable.GetTxSequenceNumber(_peerNodeId)
            },
                                                     TxOptions,
                                                     new COMMAND_CLASS_SECURITY_2.SECURITY_2_NONCE_REPORT(), 2,
                                                     NONCE_REQUEST_TIMER);
            if (_securityManagerInfo.IsInclusion)
            {
                _requestNonce.Token.Reset(NONCE_REQUEST_INCLUSION_TIMER);
            }
            _requestNonce.SubstituteSettings.SetFlag(SubstituteFlags.DenySecurity);
            _securityManagerInfo.InitializingNodeId = DestNodeId;

            _sendEncData = new SendDataBridgeOperation(SrcNodeId, TestNodeId ?? DestNodeId, null, TxOptions)
            {
                SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
            };
        }
Example #4
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);
                }
            }
        }
Example #5
0
        protected override void OnHandledInternal(DataReceivedUnit ou)
        {
            byte nodeId = ReceivedAchData.SrcNodeId;

            byte[] cmd = ReceivedAchData.Command;
            if (handlingRequestFromNode != nodeId || !handlingRequest.SequenceEqual(cmd))
            {
                handlingRequestFromNode = nodeId;
                handlingRequest         = cmd;
                if (ReceiveCallback != null)
                {
                    byte[] data = ReceiveCallback(ReceivedAchData.Options, ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, ReceivedAchData.Command);
                    if (data != null && data.Length > 0)
                    {
                        Data = new List <byte[]>();
                        Data.Add(data);
                    }
                }
                else if (ReceiveExCallback != null)
                {
                    Data = ReceiveExCallback(ReceivedAchData.Options, ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, ReceivedAchData.Command);
                }
                else if (ReceiveAchDataCallback != null)
                {
                    Data = ReceiveAchDataCallback(ReceivedAchData);
                }
                if (Data != null)
                {
                    if (ReceivedAchData.DestNodeId == 0)
                    {
                        ApiOperation[] operations = new ApiOperation[Data.Count];
                        for (int i = 0; i < Data.Count; i++)
                        {
                            operations[i] = new SendDataOperation(ReceivedAchData.SrcNodeId, Data[i], TxOptions);
                            operations[i].SubstituteSettings = SubstituteSettings;
                            operations[i].CompletedCallback  = (x) =>
                            {
                                var action = x as ActionBase;
                                if (action != null)
                                {
                                    handlingRequestFromNode = 0;
                                    handlingRequest         = emptyArray;
                                    SpecificResult.TotalCount++;
                                    if (action.Result.State != ActionStates.Completed)
                                    {
                                        SpecificResult.FailCount++;
                                    }
                                }
                            };
                        }
                        ou.SetNextActionItems(operations);
                    }
                    else
                    {
                        ApiOperation[] operations = new ApiOperation[Data.Count];
                        for (int i = 0; i < Data.Count; i++)
                        {
                            operations[i] = new SendDataBridgeOperation(ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, Data[i], TxOptions);
                            operations[i].SubstituteSettings = SubstituteSettings;
                            operations[i].CompletedCallback  = (x) =>
                            {
                                var action = x as ActionBase;
                                if (action != null)
                                {
                                    handlingRequestFromNode = 0;
                                    handlingRequest         = emptyArray;
                                    SpecificResult.TotalCount++;
                                    if (action.Result.State != ActionStates.Completed)
                                    {
                                        SpecificResult.FailCount++;
                                    }
                                }
                            };
                        }
                        ou.SetNextActionItems(operations);
                    }
                }
                else
                {
                    handlingRequestFromNode = 0;
                    handlingRequest         = emptyArray;
                    ou.SetNextActionItems();
                }
            }
        }
Example #6
0
        protected override void OnHandledInternal(DataReceivedUnit ou)
        {
            ou.SetNextActionItems();
            if (!ou.DataFrame.IsSkippedSecurity)
            {
                if (_securityManagerInfo.IsActive && _securityManagerInfo.Network.HasSecurityScheme(SecuritySchemes.S0))
                {
                    byte[] command = ReceivedAchData.Command;
                    if (command != null && command.Length > 1)
                    {
                        byte[] dataToSend         = null;
                        bool   isSubstituteDenied = false;
                        if ((command[1] == COMMAND_CLASS_SECURITY.SECURITY_NONCE_GET.ID ||
                             command[1] == COMMAND_CLASS_SECURITY.SECURITY_MESSAGE_ENCAPSULATION_NONCE_GET.ID) &&
                            handlingNonceGetFromNode != ReceivedAchData.SrcNodeId)
                        {
                            handlingNonceGetFromNode = ReceivedAchData.SrcNodeId;
                            var destNodeId = ReceivedAchData.DestNodeId > 0 ? ReceivedAchData.DestNodeId : _securityManagerInfo.Network.NodeId;
                            if (_securityManagerInfo.IsSenderNonceS0Disabled)
                            {
                                handlingNonceGetFromNode = 0;
                            }
                            else
                            {
                                dataToSend = _securityS0CryptoProvider.GenerateNonceReport(new OrdinalPeerNodeId(ReceivedAchData.SrcNodeId, destNodeId));
                            }
                            isSubstituteDenied = true;
                            if (_securityManagerInfo.DelaysS0.ContainsKey(SecurityS0Delays.NonceReport))
                            {
                                Thread.Sleep(_securityManagerInfo.DelaysS0[SecurityS0Delays.NonceReport]);
                            }
                        }
                        else if (command[1] == COMMAND_CLASS_SECURITY.SECURITY_COMMANDS_SUPPORTED_GET.ID && handlingNonceGetFromNode != ReceivedAchData.SrcNodeId)
                        {
                            handlingNonceGetFromNode = ReceivedAchData.SrcNodeId;
                            var scheme = (SecuritySchemes)ReceivedAchData.SecurityScheme;
                            if (scheme == SecuritySchemes.S0)
                            {
                                if (!_securityManagerInfo.Network.IsSecuritySchemesSpecified(ReceivedAchData.SrcNodeId) || _securityManagerInfo.Network.HasSecurityScheme(ReceivedAchData.SrcNodeId, SecuritySchemes.S0))
                                {
                                    var ccReport = new COMMAND_CLASS_SECURITY.SECURITY_COMMANDS_SUPPORTED_REPORT();
                                    if (!_securityManagerInfo.Network.HasSecurityScheme(SecuritySchemeSet.ALLS2))
                                    {
                                        if (ReceivedAchData.DestNodeId > 0 && ReceivedAchData.DestNodeId != _securityManagerInfo.Network.NodeId)
                                        {
                                            ccReport.commandClassSupport = new List <byte>(_securityManagerInfo.Network.GetVirtualSecureCommandClasses());
                                        }
                                        else
                                        {
                                            var secureCommandClasses = _securityManagerInfo.Network.GetSecureCommandClasses();
                                            if (secureCommandClasses != null)
                                            {
                                                ccReport.commandClassSupport = new List <byte>(secureCommandClasses);
                                            }
                                        }
                                        dataToSend = ccReport;
                                    }
                                    else
                                    {
                                        dataToSend = new byte[] { 0x98, 0x03, 0x00 };
                                    }
                                }
                            }
                        }

                        if (dataToSend != null)
                        {
                            ApiOperation sendData = null;

                            if (ReceivedAchData.DestNodeId > 0)
                            {
                                sendData = new SendDataBridgeOperation(ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, dataToSend, _securityManagerInfo.TxOptions);
                            }
                            else
                            {
                                sendData = new SendDataOperation(ReceivedAchData.SrcNodeId, dataToSend, _securityManagerInfo.TxOptions);
                            }


                            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++;
                                    }
                                }
                            };
                            ou.SetNextActionItems(sendData);
                        }
                        else
                        {
                            ou.SetNextActionItems();
                        }
                    }
                }
                else
                {
                    "REJECT, {0}, {1} (IsNodeSecure[S0]={2}, IsActive={3}"._DLOG(
                        _securityManagerInfo.IsInclusion,
                        _securityManagerInfo.Network.HasSecurityScheme(ReceivedAchData.SrcNodeId, SecuritySchemes.S0),
                        _securityManagerInfo.Network.HasSecurityScheme(SecuritySchemes.S0),
                        _securityManagerInfo.IsActive);
                }
            }
        }