Пример #1
0
 /// <summary>
 /// Generate user information.
 /// </summary>
 /// <param name="settings">DLMS settings.</param>
 /// <param name="cipher"></param>
 /// <param name="data">Generated user information.</param>
 static internal void GenerateUserInformation(GXDLMSSettings settings, GXICipher cipher, GXByteBuffer data)
 {
     data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.UserInformation);
     if (cipher == null || !cipher.IsCiphered())
     {
         //Length for AARQ user field
         data.SetUInt8(0x10);
         //Coding the choice for user-information (Octet STRING, universal)
         data.SetUInt8(BerType.OctetString);
         //Length
         data.SetUInt8(0x0E);
         GetInitiateRequest(settings, cipher, data);
     }
     else
     {
         GXByteBuffer tmp = new GXByteBuffer();
         GetInitiateRequest(settings, cipher, tmp);
         byte[] crypted = cipher.Encrypt(0x21, cipher.SystemTitle, tmp.Array());
         //Length for AARQ user field
         data.SetUInt8((byte)(2 + crypted.Length));
         //Coding the choice for user-information (Octet STRING, universal)
         data.SetUInt8(BerType.OctetString);
         data.SetUInt8((byte)crypted.Length);
         data.Set(crypted);
     }
 }
Пример #2
0
        /// <summary>
        /// Code application context name.
        /// </summary>
        /// <param name="settings">DLMS settings.</param>
        /// <param name="data">Byte buffer where data is saved.</param>
        /// <param name="cipher">Is ciphering settings.</param>
        private static void GenerateApplicationContextName(GXDLMSSettings settings, GXByteBuffer data, GXICipher cipher)
        {
            //Application context name tag
            data.SetUInt8(((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.ApplicationContextName));
            //Len
            data.SetUInt8(0x09);
            data.SetUInt8(BerType.ObjectIdentifier);
            //Len
            data.SetUInt8(0x07);
            bool ciphered = cipher != null && cipher.IsCiphered();

            if (settings.UseLogicalNameReferencing)
            {
                if (ciphered)
                {
                    data.Set(GXCommon.LogicalNameObjectIdWithCiphering);
                }
                else
                {
                    data.Set(GXCommon.LogicalNameObjectID);
                }
            }
            else
            {
                if (ciphered)
                {
                    data.Set(GXCommon.ShortNameObjectIdWithCiphering);
                }
                else
                {
                    data.Set(GXCommon.ShortNameObjectID);
                }
            }
            //Add system title if cipher or GMAC authentication is used..
            if (!settings.IsServer && (ciphered || settings.Authentication == Authentication.HighGMAC))
            {
                if (cipher.SystemTitle == null || cipher.SystemTitle.Length == 0)
                {
                    throw new ArgumentNullException("SystemTitle");
                }
                //Add calling-AP-title
                data.SetUInt8(((byte)BerType.Context | (byte)BerType.Constructed | 6));
                //LEN
                data.SetUInt8((byte)(2 + cipher.SystemTitle.Length));
                data.SetUInt8((byte)BerType.OctetString);
                //LEN
                data.SetUInt8((byte)cipher.SystemTitle.Length);
                data.Set(cipher.SystemTitle);
            }
        }
Пример #3
0
        private static byte[] GetUserInformation(GXDLMSSettings settings, GXICipher cipher)
        {
            GXByteBuffer data = new GXByteBuffer();

            data.SetUInt8(GXCommon.InitialResponce); // Tag for xDLMS-Initiate response
            // NegotiatedQualityOfService (not used)
            data.SetUInt8(0x1);
            data.SetUInt8(0x00);
            // DLMS Version Number
            data.SetUInt8(06);
            data.SetUInt8(0x5F);
            data.SetUInt8(0x1F);
            data.SetUInt8(0x04); // length of the conformance block
            data.SetUInt8(0x00); // encoding the number of unused bits in the bit string
            if (settings.UseLogicalNameReferencing)
            {
                data.Set(settings.LnSettings.ConformanceBlock);
            }
            else
            {
                data.Set(settings.SnSettings.ConformanceBlock);
            }
            data.SetUInt16(settings.MaxReceivePDUSize);
            //VAA Name VAA name (0x0007 for LN referencing and 0xFA00 for SN)
            if (settings.UseLogicalNameReferencing)
            {
                data.SetUInt16(0x0007);
            }
            else
            {
                data.SetUInt16(0xFA00);
            }
            if (cipher != null && cipher.IsCiphered())
            {
                return(cipher.Encrypt(0x28, cipher.SystemTitle, data.Array()));
            }
            return(data.Array());
        }
Пример #4
0
        ///<summary>
        ///Server generates AARE message.
        ///</summary>
        internal static void GenerateAARE(GXDLMSSettings settings, GXByteBuffer data,
                                          AssociationResult result, SourceDiagnostic diagnostic, GXICipher cipher)
        {
            int offset = data.Position;

            // Set AARE tag and length
            data.SetUInt8(((byte)BerType.Application | (byte)BerType.Constructed | (byte)PduType.ApplicationContextName)); //0x61
            // Length is updated later.
            data.SetUInt8(0);
            GenerateApplicationContextName(settings, data, cipher);
            //Result
            data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)BerType.Integer); //0xA2
            data.SetUInt8(3);                                                                         //len
            data.SetUInt8(BerType.Integer);                                                           //Tag
            //Choice for result (INTEGER, universal)
            data.SetUInt8(1);                                                                         //Len
            data.SetUInt8((byte)result);                                                              //ResultValue
            //SourceDiagnostic
            data.SetUInt8(0xA3);
            data.SetUInt8(5);                //len
            data.SetUInt8(0xA1);             //Tag
            data.SetUInt8(3);                //len
            data.SetUInt8(2);                //Tag
            //Choice for result (INTEGER, universal)
            data.SetUInt8(1);                //Len
            data.SetUInt8((byte)diagnostic); //diagnostic

            //SystemTitle
            if (cipher != null && (cipher.IsCiphered() || settings.Authentication == Authentication.HighGMAC))
            {
                data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.CalledApInvocationId);
                data.SetUInt8((byte)(2 + cipher.SystemTitle.Length));
                data.SetUInt8((byte)BerType.OctetString);
                data.SetUInt8((byte)cipher.SystemTitle.Length);
                data.Set(cipher.SystemTitle);
            }

            if (result != AssociationResult.PermanentRejected && diagnostic == SourceDiagnostic.AuthenticationRequired)
            {
                //Add server ACSE-requirenents field component.
                data.SetUInt8(0x88);
                data.SetUInt8(0x02);  //Len.
                data.SetUInt16(0x0780);
                //Add tag.
                data.SetUInt8(0x89);
                data.SetUInt8(0x07);//Len
                data.SetUInt8(0x60);
                data.SetUInt8(0x85);
                data.SetUInt8(0x74);
                data.SetUInt8(0x05);
                data.SetUInt8(0x08);
                data.SetUInt8(0x02);
                data.SetUInt8((byte)settings.Authentication);
                //Add tag.
                data.SetUInt8(0xAA);
                data.SetUInt8((byte)(2 + settings.StoCChallenge.Length));//Len
                data.SetUInt8((byte)BerType.Context);
                data.SetUInt8((byte)settings.StoCChallenge.Length);
                data.Set(settings.StoCChallenge);
            }
            //Add User Information
            //Tag 0xBE
            data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.UserInformation);
            byte[] tmp = GetUserInformation(settings, cipher);
            data.SetUInt8((byte)(2 + tmp.Length));
            //Coding the choice for user-information (Octet STRING, universal)
            data.SetUInt8(BerType.OctetString);
            //Length
            data.SetUInt8((byte)tmp.Length);
            data.Set(tmp);
            data.SetUInt8((UInt16)(offset + 1), (byte)(data.Size - offset - 2));
        }
Пример #5
0
        private static byte[] GetUserInformation(GXDLMSSettings settings, GXICipher cipher)
        {
            GXByteBuffer data = new GXByteBuffer();
            data.SetUInt8(Command.InitiateResponse); // Tag for xDLMS-Initiate response
                                                     // NegotiatedQualityOfService (not used)
            data.SetUInt8(0x1);
            data.SetUInt8(0x00);
            // DLMS Version Number
            data.SetUInt8(06);
            data.SetUInt8(0x5F);
            data.SetUInt8(0x1F);
            data.SetUInt8(0x04);// length of the conformance block
            data.SetUInt8(0x00);// encoding the number of unused bits in the bit string
            if (settings.UseLogicalNameReferencing)
            {
                data.Set(settings.LnSettings.ConformanceBlock);
            }
            else
            {
                data.Set(settings.SnSettings.ConformanceBlock);

            }
            data.SetUInt16(settings.MaxPduSize);
            //VAA Name VAA name (0x0007 for LN referencing and 0xFA00 for SN)
            if (settings.UseLogicalNameReferencing)
            {
                data.SetUInt16(0x0007);
            }
            else
            {
                data.SetUInt16(0xFA00);
            }
            if (cipher != null && cipher.IsCiphered())
            {
                return cipher.Encrypt((byte)Command.GloInitiateResponse, cipher.SystemTitle, data.Array());
            }
            return data.Array();
        }
Пример #6
0
 /// <summary>
 /// Generate user information.
 /// </summary>
 /// <param name="settings">DLMS settings.</param>
 /// <param name="cipher"></param>
 /// <param name="data">Generated user information.</param>
 static internal void GenerateUserInformation(GXDLMSSettings settings, GXICipher cipher, GXByteBuffer encryptedData, GXByteBuffer data)
 {
     data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.UserInformation);
     if (cipher == null || !cipher.IsCiphered())
     {
         //Length for AARQ user field
         data.SetUInt8(0x10);
         //Coding the choice for user-information (Octet STRING, universal)
         data.SetUInt8(BerType.OctetString);
         //Length
         data.SetUInt8(0x0E);
         GetInitiateRequest(settings, cipher, data);
     }
     else
     {
         if (encryptedData != null && encryptedData.Size != 0)
         {
             //Length for AARQ user field
             data.SetUInt8((byte)(4 + encryptedData.Size));
             //Tag
             data.SetUInt8(BerType.OctetString);
             data.SetUInt8((byte)(2 + encryptedData.Size));
             //Coding the choice for user-information (Octet STRING, universal)
             data.SetUInt8((byte)Command.GloInitiateRequest);
             data.SetUInt8((byte)encryptedData.Size);
             data.Set(encryptedData);
         }
         else
         {
             GXByteBuffer tmp = new GXByteBuffer();
             GetInitiateRequest(settings, cipher, tmp);
             byte[] crypted = cipher.Encrypt((byte)Command.GloInitiateRequest, cipher.SystemTitle, tmp.Array());
             //Length for AARQ user field
             data.SetUInt8((byte)(2 + crypted.Length));
             //Coding the choice for user-information (Octet STRING, universal)
             data.SetUInt8(BerType.OctetString);
             data.SetUInt8((byte)crypted.Length);
             data.Set(crypted);
         }
     }
 }
Пример #7
0
 /// <summary>
 /// Code application context name.
 /// </summary>
 /// <param name="settings">DLMS settings.</param>
 /// <param name="data">Byte buffer where data is saved.</param>
 /// <param name="cipher">Is ciphering settings.</param>
 private static void GenerateApplicationContextName(GXDLMSSettings settings, GXByteBuffer data, GXICipher cipher)
 {
     //Application context name tag
     data.SetUInt8(((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.ApplicationContextName));
     //Len
     data.SetUInt8(0x09);
     data.SetUInt8(BerType.ObjectIdentifier);
     //Len
     data.SetUInt8(0x07);
     bool ciphered = cipher != null && cipher.IsCiphered();
     if (settings.UseLogicalNameReferencing)
     {
         if (ciphered)
         {
             data.Set(GXCommon.LogicalNameObjectIdWithCiphering);
         }
         else
         {
             data.Set(GXCommon.LogicalNameObjectID);
         }
     }
     else
     {
         if (ciphered)
         {
             data.Set(GXCommon.ShortNameObjectIdWithCiphering);
         }
         else
         {
             data.Set(GXCommon.ShortNameObjectID);
         }
     }
     //Add system title if cipher or GMAC authentication is used..
     if (!settings.IsServer && (ciphered || settings.Authentication == Authentication.HighGMAC))
     {
         if (cipher.SystemTitle == null || cipher.SystemTitle.Length == 0)
         {
             throw new ArgumentNullException("SystemTitle");
         }
         //Add calling-AP-title
         data.SetUInt8(((byte)BerType.Context | (byte)BerType.Constructed | 6));
         //LEN
         data.SetUInt8((byte)(2 + cipher.SystemTitle.Length));
         data.SetUInt8((byte)BerType.OctetString);
         //LEN
         data.SetUInt8((byte)cipher.SystemTitle.Length);
         data.Set(cipher.SystemTitle);
     }
 }
Пример #8
0
        ///<summary>
        ///Server generates AARE message.
        ///</summary>
        internal static void GenerateAARE(GXDLMSSettings settings, GXByteBuffer data,
                                          AssociationResult result, SourceDiagnostic diagnostic, GXICipher cipher,
                                          GXByteBuffer encryptedData)
        {
            int offset = data.Size;
            // Set AARE tag and length
            data.SetUInt8(((byte)BerType.Application | (byte)BerType.Constructed | (byte)PduType.ApplicationContextName)); //0x61
                                                                                                                           // Length is updated later.
            data.SetUInt8(0);
            GenerateApplicationContextName(settings, data, cipher);
            //Result
            data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)BerType.Integer);//0xA2
            data.SetUInt8(3); //len
            data.SetUInt8(BerType.Integer); //Tag
                                            //Choice for result (INTEGER, universal)
            data.SetUInt8(1); //Len
            data.SetUInt8((byte)result); //ResultValue
                                         //SourceDiagnostic
            data.SetUInt8(0xA3);
            data.SetUInt8(5); //len
            data.SetUInt8(0xA1); //Tag
            data.SetUInt8(3); //len
            data.SetUInt8(2); //Tag
                              //Choice for result (INTEGER, universal)
            data.SetUInt8(1); //Len
            data.SetUInt8((byte)diagnostic); //diagnostic

            //SystemTitle
            if (cipher != null && (cipher.IsCiphered() || settings.Authentication == Authentication.HighGMAC))
            {
                data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.CalledApInvocationId);
                data.SetUInt8((byte)(2 + cipher.SystemTitle.Length));
                data.SetUInt8((byte)BerType.OctetString);
                data.SetUInt8((byte)cipher.SystemTitle.Length);
                data.Set(cipher.SystemTitle);
            }

            if (result != AssociationResult.PermanentRejected && diagnostic == SourceDiagnostic.AuthenticationRequired)
            {
                //Add server ACSE-requirenents field component.
                data.SetUInt8(0x88);
                data.SetUInt8(0x02);  //Len.
                data.SetUInt16(0x0780);
                //Add tag.
                data.SetUInt8(0x89);
                data.SetUInt8(0x07);//Len
                data.SetUInt8(0x60);
                data.SetUInt8(0x85);
                data.SetUInt8(0x74);
                data.SetUInt8(0x05);
                data.SetUInt8(0x08);
                data.SetUInt8(0x02);
                data.SetUInt8((byte)settings.Authentication);
                //Add tag.
                data.SetUInt8(0xAA);
                data.SetUInt8((byte)(2 + settings.StoCChallenge.Length));//Len
                data.SetUInt8((byte)BerType.Context);
                data.SetUInt8((byte)settings.StoCChallenge.Length);
                data.Set(settings.StoCChallenge);
            }
            byte[] tmp;
            //Add User Information
            //Tag 0xBE
            data.SetUInt8((byte)BerType.Context | (byte)BerType.Constructed | (byte)PduType.UserInformation);
            if (encryptedData != null && encryptedData.Size != 0)
            {
                GXByteBuffer tmp2 = new GXByteBuffer((UInt16)(2 + encryptedData.Size));
                tmp2.SetUInt8((byte)Command.GloInitiateResponse);
                GXCommon.SetObjectCount(encryptedData.Size, tmp2);
                tmp2.Set(encryptedData);
                tmp = tmp2.Array();
            }
            else
            {
                tmp = GetUserInformation(settings, cipher);
            }
            data.SetUInt8((byte)(2 + tmp.Length));
            //Coding the choice for user-information (Octet STRING, universal)
            data.SetUInt8(BerType.OctetString);
            //Length
            data.SetUInt8((byte)tmp.Length);
            data.Set(tmp);
            data.SetUInt8((UInt16)(offset + 1), (byte)(data.Size - offset - 2));
        }