public void AddExtension(List <COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1> extensionsList, List <COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1> encryptedExtensionsList) { byte extensionActualLength = 0; if (!ValueSpecified || Value == null) { extensionActualLength = Extensions.GetLengthByExtensionType(ExtensionTypeV); byte valueLength = (byte)(extensionActualLength - 2); if (valueLength > 0) { Value = new byte[valueLength]; } } else { extensionActualLength = (byte)(Value.Length + 2); } bool isEncryptedValue = IsEncryptedSpecified ? IsEncrypted : ExtensionTypeV == ExtensionTypes.Mpan; bool isCriticalValue = IsCriticalSpecified ? IsCritical : ExtensionTypeV != ExtensionTypes.Mos; var testExt = new COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1 { extensionLength = ExtensionLengthSpecified ? ExtensionLength : extensionActualLength, properties1 = new COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1.Tproperties1 { moreToFollow = (byte)((IsMoreToFollowSpecified && IsMoreToFollow) ? 0x01 : 0x00), critical = (byte)(isCriticalValue ? 0x01 : 0x00), type = (byte)ExtensionTypeV }, extension = Value }; if (isEncryptedValue) { if (encryptedExtensionsList.Count > 0 && !isPreviousEncryptedExtensionMoreToFollowDisabled) { encryptedExtensionsList[encryptedExtensionsList.Count - 1].properties1.moreToFollow = 1; } encryptedExtensionsList.Add(testExt); isPreviousEncryptedExtensionMoreToFollowDisabled = IsMoreToFollowSpecified ? !IsMoreToFollow : false; } else { if (extensionsList.Count > 0 && !isPreviousExtensionMoreToFollowDisabled) { extensionsList[extensionsList.Count - 1].properties1.moreToFollow = 1; } extensionsList.Add(testExt); isPreviousExtensionMoreToFollowDisabled = IsMoreToFollowSpecified ? !IsMoreToFollow : false; } }
public void AddMpanExtension(byte[] mpan, byte groupId) { if (EncryptedExtensionsList.Count > 0) { EncryptedExtensionsList[EncryptedExtensionsList.Count - 1].properties1.moreToFollow = 1; } var mpanExt = new COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1 { extensionLength = GetLengthByExtensionType(ExtensionTypes.Mpan), properties1 = new COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1.Tproperties1 { moreToFollow = 0, critical = 1, type = (byte)ExtensionTypes.Mpan } }; var extData = new List <byte>(); extData.Add(groupId); extData.AddRange(mpan); mpanExt.extension = extData; EncryptedExtensionsList.Add(mpanExt); }
public static bool DecryptS2( byte[] key, byte[] iv, byte senderNodeId, byte receiverNodeId, byte[] homeId, COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION cmd, out byte[] decryptedPayload, out List <COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1> decryptedExtensions) { var cipherData = cmd.ccmCiphertextObject.ToArray(); decryptedPayload = new byte[0]; decryptedExtensions = new List <COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1>(); if (cipherData.Length == SecurityS2Utils.AUTH_DATA_HEADER_LENGTH) { return(true); } var payloadLength = (ushort)((byte[])cmd).Length; var sequenceNumber = cmd.sequenceNumber; var statusByte = cmd.properties1; byte[] extensionData = new byte[0]; if (cmd.properties1.extension == 1) { var dataList = new List <byte>(); foreach (var vg1 in cmd.vg1) { dataList.AddRange(new byte[] { vg1.extensionLength, vg1.properties1 }); dataList.AddRange(vg1.extension); } extensionData = dataList.ToArray(); } var decryptedData = DecryptS2Internal(key, iv, senderNodeId, receiverNodeId, homeId, payloadLength, sequenceNumber, statusByte, extensionData, cipherData); var isDecryptSucceeded = decryptedData.Length > 0; if (isDecryptSucceeded) { if (cmd.properties1.encryptedExtension == 1) { try { int ind = 0; while (ind + 2 <= decryptedData.Length && ind + decryptedData[ind] <= decryptedData.Length) { COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1 ext = new COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION.TVG1 { extensionLength = decryptedData[ind], properties1 = decryptedData[ind + 1] }; if (ind + ext.extensionLength <= decryptedData.Length) { ext.extension = decryptedData.Skip(ind + 2).Take(ext.extensionLength - 2).ToList(); } else { ext.extension = new List <byte>(0); } decryptedExtensions.Add(ext); ind += ext.extensionLength; if (ext.properties1.moreToFollow == 0) { break; } } if (ind < decryptedData.Length) // Has payload. { decryptedPayload = decryptedData.Skip(ind).ToArray(); } } catch (IndexOutOfRangeException) { } catch (ArgumentOutOfRangeException) { } catch (ArgumentException) { } } else { decryptedPayload = decryptedData; } } return(isDecryptSucceeded); }