private void znode_ParameterChanged(object sender, ZWaveEvent eventData) { if (sender is ZWaveNode) { ZWaveNode node = (ZWaveNode)sender; if (eventData.Parameter == EventParameter.SecurityDecriptedMessage && eventData.Value is byte[]) { node.MessageRequestHandler((byte[])eventData.Value); return; } else if (eventData.Parameter == EventParameter.SecurityGeneratedKey && eventData.Value is int) { SaveNodesConfig(); return; } else if (eventData.Parameter == EventParameter.SecurityNodeInformationFrame) { node.SecuredNodeInformationFrame = (byte[])eventData.Value; // we take them one a a time to make sure we keep the list with unique elements foreach (byte nodeInfo in node.SecuredNodeInformationFrame) { // if we found the COMMAND_CLASS_MARK we get out of the for loop if (nodeInfo == (byte)0xEF) { break; } node.NodeInformationFrame = Utility.AppendByteToArray(node.NodeInformationFrame, nodeInfo); } // we just send other events and save the node data gotNodeUpdateInformation(node); } } // Route node event RaiseUpdateParameterEvent(eventData); }
public bool DecryptMessage(ZWaveNode node, byte[] message, int start) { if (enc == null) { init(node); } Utility.logMessage("In DecryptMessage - SecurityHandler"); if (c_nonceTimer.ElapsedMilliseconds > 10000) { Utility.logMessage("Received the nonce too late'" + c_nonceTimer.ElapsedMilliseconds + "' > 10000"); return(false); } // Utility.logMessage("Message to be decrypted: " + Utility.ByteArrayToString(message)); byte[] iv = getVIFromPacket_inbound(message, start + 1); int _length = message.Length; int encryptedpackagesize = _length - 11 - 8; //19 + 11 + 8 byte[] encryptedpacket = new byte[encryptedpackagesize]; Array.Copy(message, 8 + start + 1, encryptedpacket, 0, encryptedpackagesize); byte[] decryptedpacket = enc.OFB_EncryptMessage(encryptKey, iv, encryptedpacket); // Utility.logMessage("Message " + Utility.ByteArrayToString(message)); // Utility.logMessage("IV " + Utility.ByteArrayToString(iv)); // Utility.logMessage("Encrypted Packet " + Utility.ByteArrayToString(encryptedpacket)); Utility.logMessage("Decrypted Packet " + Utility.ByteArrayToString(decryptedpacket)); byte[] mac = GenerateAuthentication(message, start, _length, node.Id, 0x01, iv, enc); byte[] e_mac = new byte[8]; Array.Copy(message, start + 8 + encryptedpackagesize + 2, e_mac, 0, 8); if (!Enumerable.SequenceEqual(mac, e_mac)) { Utility.logMessage("Computed mac " + Utility.ByteArrayToString(mac) + " does not match the provider mac " + Utility.ByteArrayToString(e_mac) + ". Dropping."); if (secure_payload.Count > 1) { sendRequestNonce(node); } return(false); } if (decryptedpacket[1] == (byte)CommandClass.Security && 1 == 0) { byte[] msg = new byte[decryptedpacket.Length - 1]; Array.Copy(decryptedpacket, 1, msg, 0, msg.Length); Utility.logMessage("Processing Internally: " + Utility.ByteArrayToString(msg)); } else { byte[] msg = new byte[decryptedpacket.Length - 2 + 8]; Array.Clear(msg, 0, 7); Array.Copy(decryptedpacket, 1, msg, 7, msg.Length - 7); msg[6] = (byte)(msg.Length - 7); Utility.logMessage("Forwarding: " + Utility.ByteArrayToString(msg)); /* send to the Command Class for Proecssing */ Utility.logMessage("Received External Command Class: " + Utility.ByteArrayToString(new byte[] { decryptedpacket[1] })); node.MessageRequestHandler(node.pController, msg); } Utility.logMessage("In DecryptMessage - Finished"); return(true); }