예제 #1
0
        public static void EncodeAndPack(AESEngine aesEncrypt, H2NBinaryWriter writer, uint farId, int ignore = 0)
        {
            var s = writer.BaseStream;

            if (aesEncrypt.Type != AESEngine.AESType.EMPTY)
            {
                var paddingBytesLength = (0xFFFFFFFF - (int)s.Length + ignore + 5) & 0x0F;
                s.Position = s.Length;
                for (var i = 0; i < paddingBytesLength; i++)
                {
                    writer.Write((byte)0xFF);
                }
                //writer.Write(Enumerable.Repeat((byte)0xFF, (int) paddingBytesLength).ToArray());
            }
            //writeCRC
            s.Position = 6 + ignore;
            var sum = CheckSum(s);

            s.Position = 4 + ignore;
            writer.Write(sum);
            //writeCRC end
            s.Position = 4 + ignore;

            aesEncrypt.Process(s);
            //pack
            s.Position = 4 + ignore;
            var result = s.ReadUInt() ^ s.ReadUInt() ^ farId;

            s.Position = ignore;
            writer.Write(result);
        }
예제 #2
0
 public Session()
 {
     Writer = new RtmfpWriter(_outputBuffer)
     {
         BufferSize = RtmfpUtils.RTMFP_MAX_PACKET_LENGTH
     };
     AesEncrypt = new AESEngine(Defines.RTMFP_SYMETRIC_KEY, AESEngine.Direction.ENCRYPT);
     AesDecrypt = new AESEngine(Defines.RTMFP_SYMETRIC_KEY);
 }
예제 #3
0
 public static bool Decode(AESEngine aesDecrypt, N2HBinaryReader packet)
 {
     //var pos = packet.BaseStream.Position;
     //var buffer = packet.ReadBytes((int) packet.BaseStream.GetAvaliableByteCounts());
     // Decrypt
     // packet.BaseStream.Position = pos;
     aesDecrypt.Process(packet.BaseStream as MemoryStream);
     //packet.BaseStream.Write(buffer,0,buffer.Length);
     return(ReadCRC(packet));
 }
예제 #4
0
 public Session(Peer peer, byte[] decryptKey, byte[] encryptKey)
 {
     Writer = new RtmfpWriter(_outputBuffer)
     {
         BufferSize = RtmfpUtils.RTMFP_MAX_PACKET_LENGTH
     };
     AesEncrypt        = new AESEngine(encryptKey, AESEngine.Direction.ENCRYPT);
     AesDecrypt        = new AESEngine(decryptKey);
     Peer              = peer.Clone();
     Peer.Addresses[0] = peer.Address;
 }
예제 #5
0
        public static bool Decode(AESEngine aesDecrypt,N2HBinaryReader packet)
        {
            //var pos = packet.BaseStream.Position;
            //var buffer = packet.ReadBytes((int) packet.BaseStream.GetAvaliableByteCounts());
	// Decrypt
           // packet.BaseStream.Position = pos;
            aesDecrypt.Process(packet.BaseStream as MemoryStream);
            //packet.BaseStream.Write(buffer,0,buffer.Length);
	        return ReadCRC(packet);
        }
예제 #6
0
        public static void EncodeAndPack(AESEngine aesEncrypt, H2NBinaryWriter writer, uint farId,int ignore = 0)
        {
            var s = writer.BaseStream;
            if (aesEncrypt.Type != AESEngine.AESType.EMPTY)
            {
                var paddingBytesLength = (0xFFFFFFFF - (int)s.Length+ignore+ 5) & 0x0F;
                s.Position =s.Length;
                for (var i = 0; i < paddingBytesLength; i++)
                {
                    writer.Write((byte)0xFF);
                }
                //writer.Write(Enumerable.Repeat((byte)0xFF, (int) paddingBytesLength).ToArray());
            }
            //writeCRC
            s.Position = 6 + ignore;
            var sum = CheckSum(s);
            s.Position = 4 + ignore;
            writer.Write(sum);
            //writeCRC end
            s.Position = 4 + ignore;

            aesEncrypt.Process(s);
            //pack
            s.Position = 4 + ignore;
            var result = s.ReadUInt() ^ s.ReadUInt() ^ farId;
            s.Position = ignore;
            writer.Write(result);
        }
예제 #7
0
 public AESEngine(AESEngine other, AESType type)
 {
     Type = other.Type == AESType.EMPTY ? AESType.EMPTY : type;
     _key = other._key;
     _direction = other._direction;
 }
예제 #8
0
 public AESEngine(AESEngine other, AESType type)
 {
     Type       = other.Type == AESType.EMPTY ? AESType.EMPTY : type;
     _key       = other._key;
     _direction = other._direction;
 }
예제 #9
0
        public override void PacketHandler(N2HBinaryReader reader)
        {
            if (Checked)
            {
                lock (Writer)
                {
                    base.PacketHandler(reader);
                }
                return;
            }
            var marker = reader.ReadByte();

            if (marker != 0x0b)
            {
                Logger.FATAL("Marker hand shake wrong:should be 0b and not {0:X}", marker);
                return;
            }
            var time   = reader.ReadUInt16();
            var type   = reader.ReadByte();
            var length = reader.ReadUInt16();

            byte[] tag;
            Logger.Debug("handshake {0:X} len:{1}", type, length);
            Debug.WriteLine("handshake {0:X} len:{1}", type, length);
            switch (type)
            {
            case 0x70:

                tag = reader.ReadBytes(reader.ReadByte());
                var cookieBytes      = reader.ReadBytes(reader.ReadByte());
                var targetCertificat = reader.ReadBytes((int)reader.BaseStream.GetAvaliableByteCounts());
                var nonce            = new byte[0];
                _dh     = RtmfpUtils.BeginDiffieHellman(ref nonce, true);
                Peer.Id = Target.Sha256.ComputeHash(nonce, 0, nonce.Length);
                HandShake38(cookieBytes, nonce);
                _handshake = () => HandShake38(cookieBytes, nonce);
                break;

            case 0x71:
                tag = reader.ReadBytes(reader.ReadByte());
                var flag    = reader.ReadByte();
                var address = new IPEndPoint(new IPAddress(reader.ReadBytes(4)), reader.ReadInt16());

                Target.Address.Port = address.Port;
                Logger.Debug("redirect to {0}", address.ToString());
                Handler.FarProtocol.IOHandler.Socket.Connect(Target.Address);
                _handshake();
                break;

            case 0x78:

                FarId = reader.ReadUInt32();
                var targetNonce = reader.ReadBytes((int)reader.Read7BitLongValue());
                var must58      = reader.ReadByte();
                Debug.WriteLineIf(must58 != 0x58, $"must58!{must58}");
                var key = new byte[RtmfpUtils.KEY_SIZE];
                Buffer.BlockCopy(targetNonce, targetNonce.Length - RtmfpUtils.KEY_SIZE, key, 0, RtmfpUtils.KEY_SIZE);
                var    sharedSecret = _dh.CreateSharedKey(key);
                byte[] decryptKey;
                byte[] encryptKey;
                RtmfpUtils.ComputeAsymetricKeys(sharedSecret, _certificat, targetNonce, out encryptKey, out decryptKey);
                Checked = true;
                _handshakeTimeoutTimer.Stop();
                AesEncrypt  = new AESEngine(encryptKey, AESEngine.Direction.ENCRYPT);
                AesDecrypt  = new AESEngine(decryptKey);
                PrevAesType = AESEngine.AESType.DEFAULT;
                Application = Handler.Application;
                Handler.CreateSession(Peer, null);

                break;

            default:

                break;
            }
        }
예제 #10
0
        public override void PacketHandler(N2HBinaryReader reader)
        {
            if(Checked){
                lock (Writer)
                {
                    base.PacketHandler(reader);
                }
                return;
            }
            var marker = reader.ReadByte();
            if (marker != 0x0b)
            {
                Logger.FATAL("Marker hand shake wrong:should be 0b and not {0:X}", marker);
                return;
            }
            var time = reader.ReadUInt16();
            var type = reader.ReadByte();
            var length = reader.ReadUInt16();
            byte[] tag;
            Logger.Debug("handshake {0:X} len:{1}",type,length);
            switch (type)
            {
                case 0x70:
              
                    tag = reader.ReadBytes(reader.ReadByte());
                    var cookieBytes = reader.ReadBytes(reader.ReadByte());
                    var targetCertificat = reader.ReadBytes((int)reader.BaseStream.GetAvaliableByteCounts());
                    var nonce = new byte[0];
                    _dh = RtmfpUtils.BeginDiffieHellman(ref nonce, true);
                    Peer.Id = Target.Sha256.ComputeHash(nonce, 0, nonce.Length);
                    HandShake38(cookieBytes, nonce);
                    _handshake = () => HandShake38(cookieBytes, nonce);
                    break;
                case 0x71:
                    tag = reader.ReadBytes(reader.ReadByte());
                    var flag = reader.ReadByte();
                    var address = new IPEndPoint(new IPAddress(reader.ReadBytes(4)), reader.ReadInt16());
                    Target.Address.Port = address.Port;
                    Logger.Debug("redirect to {0}",address.ToString());
                    Handler.FarProtocol.IOHandler.Socket.Connect(Target.Address);
                    _handshake();
                    break;
                case 0x78:
                  
                    FarId = reader.ReadUInt32();
                    var targetNonce = reader.ReadBytes((int)reader.Read7BitLongValue());
                    var must58 = reader.ReadByte();
                    Debug.WriteLineIf(must58!=0x58,$"must58!{must58}");
                    var key = new byte[RtmfpUtils.KEY_SIZE];
                    Buffer.BlockCopy(targetNonce, targetNonce.Length - RtmfpUtils.KEY_SIZE, key, 0, RtmfpUtils.KEY_SIZE);
                    var  sharedSecret = _dh.CreateSharedKey(key);
                    byte[] decryptKey;
                    byte[] encryptKey;
                    RtmfpUtils.ComputeAsymetricKeys(sharedSecret, _certificat, targetNonce, out encryptKey, out decryptKey);
                    Checked = true;
                    _handshakeTimeoutTimer.Stop();
                    AesEncrypt = new AESEngine(encryptKey, AESEngine.Direction.ENCRYPT);
                    AesDecrypt = new AESEngine(decryptKey);
                    PrevAesType = AESEngine.AESType.DEFAULT;
                    Application = Handler.Application;
                    Handler.CreateSession(Peer, null);

                    break;
                default:

                    break;
            }

        }