private void QueuePayload(ZWaveNode node, SecutiryPayload payload) { lock (secure_payload) { secure_payload.Add(payload); if (d_nonceTimer.ElapsedMilliseconds > 10000) { waitingForNonce = false; } if (!waitingForNonce) { sendRequestNonce(node); } } }
private void sendMsg(ZWaveNode node, byte[] message) { Utility.logMessage("In sendMsg - SecurityHandler"); if (message.Length < 7) { Utility.logMessage("Message too short"); } if (message[3] != 0x13) { Utility.logMessage("Invalid Message type"); } int length = message[5]; if (length > 28) { SecutiryPayload t_payload = new SecutiryPayload(); t_payload.length = 28; t_payload.part = 1; byte[] t_message = new byte[t_payload.length]; System.Array.Copy(message, 6, t_message, 0, t_payload.length); t_payload.message = t_message; QueuePayload(node, t_payload); SecutiryPayload t_payload2 = new SecutiryPayload(); t_payload2.length = length - 28; t_payload2.part = 2; byte[] t_message2 = new byte[t_payload.length]; System.Array.Copy(message, 34, t_message2, 0, t_payload2.length); t_payload2.message = t_message2; QueuePayload(node, t_payload2); } else { SecutiryPayload t_payload = new SecutiryPayload(); t_payload.length = length; t_payload.part = 0; byte[] t_message = new byte[t_payload.length]; System.Array.Copy(message, 6, t_message, 0, t_payload.length); t_payload.message = t_message; QueuePayload(node, t_payload); } }
// IN the mesage to be Encrypted // OUT - true - message processed and sent - proceed to next one // - false - we need to wait for the nonce report to come private bool EncryptMessage(ZWaveNode node, byte[] message) { if (enc == null) { init(node); } Utility.logMessage("In EncryptMessage - secure_payload [" + secure_payload.Count + "] - " + d_nonceTimer.ElapsedMilliseconds); // if we get true we need to wait for the new Nonce // if we get false we need to proceed // if (sendRequestNonce()) // return false; if (d_nonceTimer.ElapsedMilliseconds > 10000) { return(false); } SecutiryPayload payload = null; lock (secure_payload) { if (secure_payload.Count > 0) { payload = secure_payload.First(); secure_payload.Remove(payload); } } if (payload != null) { int len = payload.length + 20; byte[] t_message = new byte[len]; int i = 0; t_message[i] = (byte)CommandClass.Security; i++; t_message[i] = (byte)SecurityCommand.MessageEncap; byte[] initializationVector = new byte[16]; for (int a = 0; a < 8; a++) { initializationVector[a] = (byte)0xAA; i++; t_message[i] = initializationVector[a]; } Array.Copy(d_currentNonce, 0, initializationVector, 8, 8); int sequence = 0; if (payload.part == 1) { ++m_sequenceCounter; sequence = m_sequenceCounter & (byte)0x0f; sequence |= (byte)0x10; } else if (payload.part == 2) { ++m_sequenceCounter; sequence = m_sequenceCounter & (byte)0x0f; sequence |= (byte)0x30; } byte[] plaintextmsg = new byte[payload.length + 1]; plaintextmsg[0] = (byte)sequence; for (int a = 0; a < payload.length; a++) { plaintextmsg[a + 1] = payload.message[a]; } byte[] encryptedPayload = new byte[30]; encryptedPayload = enc.OFB_EncryptMessage(encryptKey, initializationVector, plaintextmsg); // Utility.logMessage("authKey " + Utility.ByteArrayToString(authKey)); // Utility.logMessage("EncryptKey " + Utility.ByteArrayToString(encryptKey)); Utility.logMessage("Input Packet: " + Utility.ByteArrayToString(plaintextmsg)); // Utility.logMessage("IV " + Utility.ByteArrayToString(initializationVector)); // Utility.logMessage("encryptedPayload " + Utility.ByteArrayToString(encryptedPayload)); for (int a = 0; a < payload.length + 1; ++a) { i++; t_message[i] = encryptedPayload[a]; } i++; t_message[i] = d_currentNonce[0]; //GenerateAuthentication int start = 1; byte[] mac = GenerateAuthentication(t_message, start, t_message.Length + 2 - start - 1, 0x01, node.Id, initializationVector, enc); for (int a = 0; a < 8; ++a) { i++; t_message[i] = mac[a]; } node.SendRequest(t_message); Utility.logMessage("In EncryptMessage - message sent"); if ((networkKeySet == false) && payload.message[0] == 0x98 && payload.message[1] == 0x06) { networkKeySet = true; adding_node = false; SetupNetworkKey(node); } return(true); } return(true); }