///<summary> ///CodeData ///</summary> internal void CodeData(List <byte> data, InterfaceType interfaceType) { //AARQ APDU Tag data.Add(GXCommon.AARQTag); //Length int LenPos = data.Count; data.Add(0); /////////////////////////////////////////// // Add Application context name. ApplicationContextName.CodeData(data); GetAuthenticationString(data); UserInformation.CodeData(data); //Add extra tags... if (Tags != null) { for (int a = 0; a < Tags.Count; ++a) { GXDLMSTag tag = Tags[a]; if (tag != null) { //Add data ID. data.Add((byte)tag.ID); //Add data len. data.Add((byte)tag.Data.Length); //Add data. data.AddRange(tag.Data); } } } data[LenPos] = (byte)(data.Count() - LenPos - 1); }
///<summary> ///EncodeData ///</summary> internal void EncodeData(byte[] buff, ref int index) { // Get AARE tag and length int tag = buff[index++]; if (tag != 0x61 && tag != 0x60 && tag != 0x81 && tag != 0x80) { throw new Exception("Invalid tag."); } int len = buff[index++]; int size = buff.Length - index; if (len > size) { throw new Exception("Not enought data."); } while (index < buff.Length) { tag = buff[index]; if (tag == 0xA1) { ApplicationContextName.EncodeData(buff, ref index); } else if (tag == 0xBE) { if (ResultValue != AssociationResult.Accepted && ResultDiagnosticValue != SourceDiagnostic.None) { return; } UserInformation.EncodeData(buff, ref index); } else if (tag == 0xA2) //Result { tag = buff[index++]; len = buff[index++]; //Choice for result (INTEGER, universal) tag = buff[index++]; len = buff[index++]; ResultValue = (AssociationResult)buff[index++]; } else if (tag == 0xA3) //SourceDiagnostic { tag = buff[index++]; len = buff[index++]; // ACSE service user tag. tag = buff[index++]; len = buff[index++]; // Result source diagnostic component. tag = buff[index++]; len = buff[index++]; ResultDiagnosticValue = (SourceDiagnostic)buff[index++]; } else if (tag == 0x8A || tag == 0x88) //Authentication. { tag = buff[index++]; //Get sender ACSE-requirenents field component. if (buff[index++] != 2) { throw new Exception("Invalid tag."); } int val = GXCommon.GetUInt16(buff, ref index); if (val != 0x0780 && val != 0x0680) { throw new Exception("Invalid tag."); } } else if (tag == 0xAA) //Server Challenge. { tag = buff[index++]; len = buff[index++]; ++index; len = buff[index++]; //Get challenge index += len; } else if (tag == 0x8B || tag == 0x89) //Authentication. { tag = buff[index++]; len = buff[index++]; bool IsAuthenticationTag = len > 7; if (buff[index++] != 0x60) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x85) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x74) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x05) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x08) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x02) { throw new Exception("Invalid tag."); } int tmp = buff[index++]; if (tmp < 0 || tmp > 5) { throw new Exception("Invalid tag."); } if (IsAuthenticationTag) { Authentication = (Authentication)tmp; byte tag2 = buff[index++]; if (tag2 != 0xAC && tag2 != 0xAA) { throw new Exception("Invalid tag."); } len = buff[index++]; //Get authentication information. if (buff[index++] != 0x80) { throw new Exception("Invalid tag."); } len = buff[index++]; Password = new byte[len]; Array.Copy(buff, index, Password, 0, len); index += len; } else { Authentication = Authentication.None; } } //Unknown tags. else { tag = buff[index++]; len = buff[index++]; if (Tags != null) { GXDLMSTag tmp = new GXDLMSTag(); tmp.ID = tag; tmp.Data = new byte[len]; tmp.Data = GXCommon.Swap(buff, index, len); Tags.Add(tmp); } index += len; } } }
///<summary> ///EncodeData ///</summary> internal void EncodeData(byte[] buff, ref int index) { // Get AARE tag and length int tag = buff[index++]; if (tag != 0x61 && tag != 0x60 && tag != 0x81 && tag != 0x80) { throw new Exception("Invalid tag."); } int len = buff[index++]; int size = buff.Length - index; if (len > size) { throw new Exception("Not enough data."); } while (index < buff.Length) { tag = buff[index]; if (tag == 0xA1) { ApplicationContextName.EncodeData(buff, ref index); } else if (tag == 0xBE) { if (ResultValue != AssociationResult.Accepted && ResultDiagnosticValue != SourceDiagnostic.None) { return; } UserInformation.EncodeData(buff, ref index); } else if (tag == 0xA2) //Result { tag = buff[index++]; len = buff[index++]; //Choice for result (INTEGER, universal) tag = buff[index++]; len = buff[index++]; ResultValue = (AssociationResult)buff[index++]; } else if (tag == 0xA3) //SourceDiagnostic { tag = buff[index++]; len = buff[index++]; // ACSE service user tag. tag = buff[index++]; len = buff[index++]; // Result source diagnostic component. tag = buff[index++]; len = buff[index++]; ResultDiagnosticValue = (SourceDiagnostic)buff[index++]; } else if (tag == 0x8A || tag == 0x88) //Authentication. { tag = buff[index++]; //Get sender ACSE-requirements field component. if (buff[index++] != 2) { throw new Exception("Invalid tag."); } int val = GXCommon.GetUInt16(buff, ref index); if (val != 0x0780 && val != 0x0680) { throw new Exception("Invalid tag."); } } else if (tag == 0xAA) //Server Challenge. { tag = buff[index++]; len = buff[index++]; ++index; len = buff[index++]; //Get challenge and save it to the PW. Password = new byte[len]; Array.Copy(buff, index, Password, 0, len); index += len; } else if (tag == 0xAC) //Password. { tag = buff[index++]; len = buff[index++]; //Get authentication information. if (buff[index++] != 0x80) { throw new Exception("Invalid tag."); } len = buff[index++]; //Get password. Password = new byte[len]; Array.Copy(buff, index, Password, 0, len); index += len; } else if (tag == 0x8B || tag == 0x89) //Authentication. { tag = buff[index++]; len = buff[index++]; if (buff[index++] != 0x60) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x85) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x74) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x05) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x08) { throw new Exception("Invalid tag."); } if (buff[index++] != 0x02) { throw new Exception("Invalid tag."); } int tmp = buff[index++]; if (tmp < 0 || tmp > 5) { throw new Exception("Invalid tag."); } Authentication = (Authentication)tmp; } //Unknown tags. else { tag = buff[index++]; len = buff[index++]; if (Tags != null) { GXDLMSTag tmp = new GXDLMSTag(); tmp.ID = tag; tmp.Data = new byte[len]; tmp.Data = GXCommon.Swap(buff, index, len); Tags.Add(tmp); } index += len; } } }