Пример #1
0
        public byte[] BuildAu2Packet()
        {
            var frame = new MaslAu2Frame(_rsspEndPoint.LocalEquipType, _rsspEndPoint.LocalID,
                                         EncryptionAlgorithm.TripleDES, _macCalc.RandomA);

            frame.MAC = this.CalcAu2MAC(frame, _rsspEndPoint.RemoteID);

            return(frame.GetBytes());
        }
Пример #2
0
        public override void HandleAu2Frame(MaslAu2Frame au2Frame)
        {
            if (au2Frame.EncryAlgorithm != EncryptionAlgorithm.TripleDES)
            {
                throw new SafetyFeatureNotSupportedException();
            }

            var expectedDir = MaslFrameDirection.Server2Client;

            if (au2Frame.Direction != expectedDir)
            {
                throw new DirectionFlagException(expectedDir);
            }

            var expectedId = this.Context.RsspEP.RemoteID & 0xFFFFFF;

            if (au2Frame.ServerID != expectedId)
            {
                throw new IdentifyInAu2Exception(string.Format("AU2中无效的CTCS ID,期望值={0}(0x{0:X2}),实际值={1}(0x{1:X2})。",
                                                               expectedId, au2Frame.ServerID));
            }

            // 更新远程设备(被动方)类型。
            this.Context.RsspEP.RemoteEquipType = au2Frame.DeviceType;

            // 更新RandomA
            this.Context.MacCalculator.RandomA = au2Frame.RandomA;

            // 初始化Mac计算器。
            this.Context.MacCalculator.UpdateSessionKeys();

            // 验证MAC。
            var actualMac   = au2Frame.MAC;
            var expectedMac = this.Context.AuMessageMacCalculator.CalcAu2MAC(au2Frame, this.Context.RsspEP.LocalID);

            if (!ArrayHelper.Equals(expectedMac, actualMac))
            {
                throw new MacInAu2Exception(string.Format("Au2消息Mac检验失败,期望值={0},实际值={1}",
                                                          HelperTools.ConvertToString(expectedMac),
                                                          HelperTools.ConvertToString(actualMac)));
            }

            // 发送AU3。
            var au3Pkt = this.Context.AuMessageBuilder.BuildAu3Packet();

            this.Context.AleConnection.SendUserData(au3Pkt);

            // 启动握手计时器,等待AR
            this.Context.StartHandshakeTimer();

            // 设置下一个状态。
            this.Context.CurrentState = new MaslWaitingforArState(this.Context);
        }
Пример #3
0
        public void Test1()
        {
            var randomA = new byte[] { 0x1f, 0xd3, 0xa1, 0xce, 0x7b, 0x87, 0xe9, 0xb0 };
            var mac     = new byte[] { 0xA6, 0x86, 0x33, 0x93, 0xb9, 0x3B, 0x51, 0x0D };
            var frame1  = new MaslAu2Frame(5, 0x8AC001, EncryptionAlgorithm.TripleDES, randomA);

            frame1.MAC = mac;

            var bytes = frame1.GetBytes();

            var frame2 = new MaslAu2Frame();

            frame2.ParseBytes(bytes, 0, bytes.Length);

            Assert.AreEqual(frame1.ServerID, frame2.ServerID);
            Assert.AreEqual(frame1.DeviceType, frame2.DeviceType);
            Assert.AreEqual(frame1.Direction, frame2.Direction);
            Assert.AreEqual(frame1.EncryAlgorithm, frame2.EncryAlgorithm);
            Assert.AreEqual(frame1.FrameType, frame2.FrameType);
            CollectionAssert.AreEqual(frame1.RandomA, frame2.RandomA);
            CollectionAssert.AreEqual(frame1.MAC, frame2.MAC);
        }
Пример #4
0
        public byte[] CalcAu2MAC(MaslAu2Frame frame, uint destAddress)
        {
            using (var memStream = new MemoryStream(30))
            {
                // L
                memStream.WriteByte(0x00);
                memStream.WriteByte(0x1B);

                // DA
                memStream.WriteByte((byte)((destAddress >> 16) & 0xFF));
                memStream.WriteByte((byte)((destAddress >> 8) & 0xFF));
                memStream.WriteByte((byte)((destAddress) & 0xFF));

                // ETY+MTI+DF
                memStream.WriteByte(frame.GetHeaderByte());

                // SA
                memStream.WriteByte((byte)((frame.ServerID >> 16) & 0xFF));
                memStream.WriteByte((byte)((frame.ServerID >> 8) & 0xFF));
                memStream.WriteByte((byte)((frame.ServerID) & 0xFF));

                // SaF
                memStream.WriteByte((byte)frame.EncryAlgorithm);

                // RA + RB
                memStream.Write(_macCalc.RandomA, 0, _macCalc.RandomA.Length);
                memStream.Write(_macCalc.RandomB, 0, _macCalc.RandomB.Length);

                // DA
                memStream.WriteByte((byte)((destAddress >> 16) & 0xFF));
                memStream.WriteByte((byte)((destAddress >> 8) & 0xFF));
                memStream.WriteByte((byte)((destAddress) & 0xFF));

                var mac = _macCalc.CalcMac(memStream.ToArray());

                return(mac);
            }
        }
Пример #5
0
 public override void HandleAu2Frame(MaslAu2Frame frame)
 {
     throw new SequenceIntegrityException();
 }
Пример #6
0
 public override void HandleAu2Frame(MaslAu2Frame frame)
 {
     throw new NotArObtainedAfterAu3Exception();
 }
Пример #7
0
 public override void HandleAu2Frame(MaslAu2Frame frame)
 {
     throw new MaslException(MaslErrorCode.SequenceIntegrityFailure);
 }
Пример #8
0
 public virtual void HandleAu2Frame(MaslAu2Frame frame)
 {
     LogUtility.Error(string.Format("{0}: {1}.{2} not implement!",
                                    this.Context.RsspEP.ID, this.GetType().Name,
                                    new StackFrame(0).GetMethod().Name.Split('.').Last()));
 }