Ejemplo n.º 1
0
 public void SerializeEmberKeyData(EmberKeyData keyData)
 {
     // If we pass in null, then send a null key.
     // Note that ember will not accept this key, so this check is here to prevent the system
     // throwing an NPE - the application needs to resolve this so it sends a valid key.
     if (keyData == null || keyData.GetContents() == null)
     {
         SerializeUInt8Array(new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });
         return;
     }
     SerializeUInt8Array(keyData.GetContents());
 }
Ejemplo n.º 2
0
        /**
         * This function updates an existing entry in the key table or adds a new one. It first searches the table for an
         * existing entry that matches the passed EUI64 address. If no entry is found, it searches for the first free entry.
         * If successful, it updates the key data and resets the associated incoming frame counter. If it fails to find an
         * existing entry and no free one exists, it returns a failure.
         *
         * @param address the {@link IeeeAddress}
         * @param key the {@link ZigBeeKey}
         * @param linkKey This indicates whether the key is a Link or a Master Key
         * @return the returned {@link EmberStatus} of the request
         */
        public EmberStatus AddOrUpdateKeyTableEntry(IeeeAddress address, ZigBeeKey key, bool linkKey)
        {
            EmberKeyData keyData = new EmberKeyData();

            keyData.SetContents(Array.ConvertAll(key.Key, c => (int)c));

            EzspAddOrUpdateKeyTableEntryRequest request = new EzspAddOrUpdateKeyTableEntryRequest();

            request.SetAddress(address);
            request.SetKeyData(keyData);
            request.SetLinkKey(linkKey);
            IEzspTransaction transaction = _protocolHandler.SendEzspTransaction(new EzspSingleResponseTransaction(request, typeof(EzspAddOrUpdateKeyTableEntryResponse)));
            EzspAddOrUpdateKeyTableEntryResponse response = (EzspAddOrUpdateKeyTableEntryResponse)transaction.GetResponse();

            return(response.GetStatus());
        }
Ejemplo n.º 3
0
        /**
         * Adds a transient link key to the NCP
         *
         * @param partner the {@link IeeeAddress} to set
         * @param transientKey the {@link ZigBeeKey} to set
         * @return the {@link EmberStatus} of the response
         */
        public EmberStatus AddTransientLinkKey(IeeeAddress partner, ZigBeeKey transientKey)
        {
            EmberKeyData emberKey = new EmberKeyData();

            emberKey.SetContents(Array.ConvertAll(transientKey.Key, c => (int)c));
            EzspAddTransientLinkKeyRequest request = new EzspAddTransientLinkKeyRequest();

            request.SetPartner(partner);
            request.SetTransientKey(emberKey);
            IEzspTransaction transaction             = _protocolHandler.SendEzspTransaction(new EzspSingleResponseTransaction(request, typeof(EzspAddTransientLinkKeyResponse)));
            EzspAddTransientLinkKeyResponse response = (EzspAddTransientLinkKeyResponse)transaction.GetResponse();

            _lastStatus = response.GetStatus();
            Log.Debug(response.ToString());
            if (response.GetStatus() != EmberStatus.EMBER_SUCCESS)
            {
                Log.Debug("Error setting transient key: {}", response);
            }

            return(response.GetStatus());
        }
Ejemplo n.º 4
0
        private bool ConfigureSecurityState()
        {
            Random r = new Random();
            EmberInitialSecurityBitmask bitmask = (EmberInitialSecurityBitmask.EMBER_STANDARD_SECURITY_MODE
                                                   | EmberInitialSecurityBitmask.EMBER_HAVE_PRECONFIGURED_KEY
                                                   | EmberInitialSecurityBitmask.EMBER_GLOBAL_LINK_KEY
                                                   | EmberInitialSecurityBitmask.EMBER_REQUIRE_ENCRYPTED_KEY
                                                   | EmberInitialSecurityBitmask.EMBER_HAVE_NETWORK_KEY);

            EmberKeyData preconfigKey = new Centralite.Ember.DataTypes.EmberKeyData(new byte[] { 0x5A, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6C, 0x6C, 0x69, 0x61, 0x6E, 0x63, 0x65, 0x30, 0x39 });

            byte[] key = new byte[16];
            r.NextBytes(key);
            EmberKeyData networkKey = new EmberKeyData(key);

            var res = Ezsp.SetInitialSecurityState(new EmberInitialSecurityState(bitmask, preconfigKey, networkKey, 0)) ?? new EmberStatusResponse(EmberStatus.EMBER_ERR_FATAL);

            if (res?.Status != EmberStatus.EMBER_SUCCESS)
            {
                errorProducerService.AddMessage(new ErrorMessage("Unable to configure security state of USB stick", ErrorType.Exception));
            }

            return(res?.Status == EmberStatus.EMBER_SUCCESS);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// The keyStruct to set as <see cref="EmberKeyData"/> </summary>
 public void SetKeyStruct(EmberKeyData keyStruct)
 {
     _keyStruct = keyStruct;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// The gpdKey to set as <see cref="EmberKeyData"/> </summary>
 public void SetGpdKey(EmberKeyData gpdKey)
 {
     _gpdKey = gpdKey;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// The newNetworkKey to set as <see cref="EmberKeyData"/> </summary>
 public void SetNewNetworkKey(EmberKeyData newNetworkKey)
 {
     _newNetworkKey = newNetworkKey;
 }
 /// <summary>
 /// The keyData to set as <see cref="EmberKeyData"/> </summary>
 public void SetKeyData(EmberKeyData keyData)
 {
     _keyData = keyData;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// The transientKey to set as <see cref="EmberKeyData"/> </summary>
 public void SetTransientKey(EmberKeyData transientKey)
 {
     _transientKey = transientKey;
 }
        /**
         * Sets the initial security state
         *
         * @param linkKey the initial {@link ZigBeeKey}
         * @param networkKey the initial {@link ZigBeeKey}
         * @return true if the security state was set successfully
         */
        private bool SetSecurityState(ZigBeeKey linkKey, ZigBeeKey networkKey)
        {
            EzspSetInitialSecurityStateRequest securityState = new EzspSetInitialSecurityStateRequest();
            EmberInitialSecurityState          state         = new EmberInitialSecurityState();

            state.AddBitmask(EmberInitialSecurityBitmask.EMBER_TRUST_CENTER_GLOBAL_LINK_KEY);

            EmberKeyData networkKeyData = new EmberKeyData();

            if (networkKey != null)
            {
                networkKeyData.SetContents(Array.ConvertAll(networkKey.Key, c => (int)c));
                state.AddBitmask(EmberInitialSecurityBitmask.EMBER_HAVE_NETWORK_KEY);
                if (networkKey.SequenceNumber.HasValue)
                {
                    state.SetNetworkKeySequenceNumber(networkKey.SequenceNumber.Value);
                }
            }
            state.SetNetworkKey(networkKeyData);

            EmberKeyData linkKeyData = new EmberKeyData();

            if (linkKey != null)
            {
                linkKeyData.SetContents(Array.ConvertAll(linkKey.Key, c => (int)c));
                state.AddBitmask(EmberInitialSecurityBitmask.EMBER_HAVE_PRECONFIGURED_KEY);
                state.AddBitmask(EmberInitialSecurityBitmask.EMBER_REQUIRE_ENCRYPTED_KEY);
            }
            state.SetPreconfiguredKey(linkKeyData);

            state.SetPreconfiguredTrustCenterEui64(new IeeeAddress());

            securityState.SetState(state);
            EzspSingleResponseTransaction transaction = new EzspSingleResponseTransaction(securityState, typeof(EzspSetInitialSecurityStateResponse));

            _protocolHandler.SendEzspTransaction(transaction);
            EzspSetInitialSecurityStateResponse securityStateResponse = (EzspSetInitialSecurityStateResponse)transaction.GetResponse();

            Log.Debug(securityStateResponse.ToString());
            if (securityStateResponse.GetStatus() != EmberStatus.EMBER_SUCCESS)
            {
                Log.Debug("Error during retrieval of network parameters: {Response}", securityStateResponse);
                return(false);
            }

            EmberNcp ncp = new EmberNcp(_protocolHandler);

            if (networkKey != null && networkKey.OutgoingFrameCounter.HasValue)
            {
                EzspSerializer serializer = new EzspSerializer();
                serializer.SerializeUInt32(networkKey.OutgoingFrameCounter.Value);
                if (ncp.SetValue(EzspValueId.EZSP_VALUE_NWK_FRAME_COUNTER, serializer.GetPayload()) != EzspStatus.EZSP_SUCCESS)
                {
                    return(false);
                }
            }
            if (linkKey != null && linkKey.OutgoingFrameCounter.HasValue)
            {
                EzspSerializer serializer = new EzspSerializer();
                serializer.SerializeUInt32(linkKey.OutgoingFrameCounter.Value);
                if (ncp.SetValue(EzspValueId.EZSP_VALUE_APS_FRAME_COUNTER, serializer.GetPayload()) != EzspStatus.EZSP_SUCCESS)
                {
                    return(false);
                }
            }

            return(true);
        }