示例#1
0
        private void OnNonceReport(ActionCompletedUnit ou)
        {
            var encryptSucceeded = false;

            AddTraceLogItems(_requestNonce.SpecificResult.TraceLog);
            if (_requestNonce.Result.State == ActionStates.Completed)
            {
                COMMAND_CLASS_SECURITY_2.SECURITY_2_NONCE_REPORT cmd = _requestNonce.SpecificResult.Command;
                if (cmd.receiversEntropyInput != null && cmd.receiversEntropyInput.Count == 16 && cmd.properties1.sos == 1 /* SOS flag */)
                {
                    _spanTable.AddOrReplace(_peerNodeId,
                                            cmd.receiversEntropyInput.ToArray(), _spanTable.GetTxSequenceNumber(_peerNodeId), cmd.sequenceNumber);
                    _securityManagerInfo.InitializingNodeId = 0;
                    Extensions extensions = null;
                    if (cmd.properties1.mos == 1)
                    {
                        var groupId     = _securityS2CryptoProvider.LastSentMulticastGroupId;
                        var nodeGroupId = new NodeGroupId(_securityManagerInfo.Network.NodeId, groupId);
                        if (groupId != 0 && _mpanTable.CheckMpanExists(nodeGroupId))
                        {
                            extensions = new Extensions();
                            extensions.AddMpanExtension(
                                _mpanTable.GetContainer(nodeGroupId).MpanState,
                                groupId
                                );
                        }
                        ;
                    }
                    var cryptedData = _securityS2CryptoProvider.EncryptSinglecastCommand(
                        _sckey,
                        _spanTable, SrcNodeId, DestNodeId, _securityManagerInfo.Network.HomeId, CommandToSecureSend, extensions, new SubstituteSettings());
                    if (cryptedData != null)
                    {
                        if (SubstituteCallback != null)
                        {
                            SubstituteCallback();
                        }
                        encryptSucceeded  = true;
                        _sendEncData.Data = cryptedData;
                        //_sendEncData.OnHandledCallback = OnHandledCallback;

                        #region MessageEncapsulation
                        if (_securityManagerInfo.TestFramesS2.ContainsKey(SecurityS2TestFrames.MessageEncapsulation))
                        {
                            var testFrame = _securityManagerInfo.TestFramesS2[SecurityS2TestFrames.MessageEncapsulation];
                            if (testFrame.IsEncryptedSpecified)
                            {
                                if (testFrame.IsEncrypted)
                                {
                                    if (testFrame.IsTemp)
                                    {
                                        if (testFrame.NetworkKey != null)
                                        {
                                            _securityManagerInfo.ActivateNetworkKeyS2CustomForNode(_peerNodeId, testFrame.IsTemp, testFrame.NetworkKey);
                                        }
                                        else
                                        {
                                            _securityManagerInfo.ActivateNetworkKeyS2CustomForNode(_peerNodeId, testFrame.IsTemp, _securityManagerInfo.GetActualNetworkKeyS2Temp());
                                        }
                                    }
                                    else
                                    {
                                        if (testFrame.NetworkKey != null)
                                        {
                                            _securityManagerInfo.ActivateNetworkKeyS2CustomForNode(_peerNodeId, testFrame.IsTemp, testFrame.NetworkKey);
                                        }
                                    }
                                }
                                else
                                {
                                    var msgEncapCryptedData = (COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION)cryptedData;
                                    msgEncapCryptedData.ccmCiphertextObject = new List <byte>(CommandToSecureSend);
                                    _sendEncData.Data = msgEncapCryptedData;
                                }
                            }
                        }
                        #endregion
                    }
                }
            }

            if (!encryptSucceeded)
            {
                SetStateFailed(ou);
            }
        }
示例#2
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);
                }
            }
        }
示例#3
0
        private void OnNonceReport(ActionCompletedUnit ou)
        {
            AddTraceLogItems(_requestNonce.SpecificResult.TraceLog);
            SpecificResult.TransmitStatus = (_requestNonce.Result as TransmitResult).TransmitStatus;
            if (_requestNonce.Result)
            {
                COMMAND_CLASS_SECURITY_2.SECURITY_2_NONCE_REPORT cmd = _requestNonce.SpecificResult.Command;
                "NONCE REPORT {0}"._DLOG(_requestNonce.SpecificResult.Command.GetHex());
                if (cmd.receiversEntropyInput != null && cmd.receiversEntropyInput.Count == 16 && cmd.properties1.sos == 1 /* SOS flag */)
                {
                    _spanTable.AddOrReplace(_peerNodeId,
                                            cmd.receiversEntropyInput.ToArray(), _spanTable.GetTxSequenceNumber(_peerNodeId), cmd.sequenceNumber);
                    _securityManagerInfo.InitializingNodeId = 0;
                    if (cmd.properties1.mos == 1)
                    {
                        var groupId     = _securityS2CryptoProvider.LastSentMulticastGroupId;
                        var nodeGroupId = new NodeGroupId(_securityManagerInfo.Network.NodeId, groupId);
                        if (groupId != 0 && _mpanTable.CheckMpanExists(nodeGroupId))
                        {
                            if (ExtensionsToAdd == null)
                            {
                                ExtensionsToAdd = new Extensions();
                            }
                            ExtensionsToAdd.AddMpanExtension(
                                _mpanTable.GetContainer(nodeGroupId).MpanState,
                                groupId
                                );
                        }
                        ;
                    }

                    var cryptedData = _securityS2CryptoProvider.EncryptSinglecastCommand(_sckey, _spanTable, _securityManagerInfo.Network.NodeId, NodeId, _securityManagerInfo.Network.HomeId, CommandToSecureSend, ExtensionsToAdd, SubstituteSettingsForRetransmission);
                    if (cryptedData != null)
                    {
                        if (SubstituteCallback != null)
                        {
                            SubstituteCallback();
                        }
                        _securityManagerInfo.LastSendDataBuffer = cryptedData;
                        _sendEncData.Data = cryptedData;

                        #region MessageEncapsulation
                        _sendEncData.Data = _securityManagerInfo.TestOverrideMessageEncapsulation(_sckey, _spanTable, _securityS2CryptoProvider, SubstituteSettings, NodeId, CommandToSecureSend, _peerNodeId, ExtensionsToAdd, cryptedData, _sendEncData.Data);
                        #endregion
                    }
                    else
                    {
                        "No Data to Send"._DLOG();
                        SpecificResult.SubstituteStatus = SubstituteStatuses.Failed;
                        SetStateFailed(ou);
                    }
                }
                else
                {
                    "Invalid Nonce {0}"._DLOG(_requestNonce.SpecificResult.Command.GetHex());
                }
            }
            else
            {
                SpecificResult.SubstituteStatus = SubstituteStatuses.Failed;
                SetStateFailed(ou);
            }
        }