Exemplo n.º 1
0
        public void EncryptedReliableMessage(IChannelHandlerContext context, ProudSession session, EncryptedReliableMessage message)
        {
            Crypt crypt;
            // TODO Decrypt P2P
            //if (message.IsRelayed)
            //{
            //    //var remotePeer = (ServerRemotePeer)session.P2PGroup?.Members.GetValueOrDefault(message.TargetHostId);
            //    //if (remotePeer == null)
            //    //    return;

            //    //encryptContext = remotePeer.EncryptContext;
            //    //if (encryptContext == null)
            //    //    throw new ProudException($"Received encrypted message but the remote peer has no encryption enabled");
            //}
            //else
            {
                crypt = session.Crypt;
            }

            var buffer = context.Allocator.Buffer(message.Data.Length);

            using (var src = new MemoryStream(message.Data))
                using (var dst = new WriteOnlyByteBufferStream(buffer, false))
                    crypt.Decrypt(context.Allocator, message.EncryptMode, src, dst, true);

            context.Channel.Pipeline.Context <ProudFrameDecoder>().FireChannelRead(buffer);
        }
Exemplo n.º 2
0
        public void Encrypt(IByteBufferAllocator allocator, EncryptMode mode, Stream src, Stream dst, bool reliable)
        {
            using (var data = new BufferWrapper(allocator.Buffer().WithOrder(ByteOrder.LittleEndian)))
                using (var encryptor = GetAlgorithm(mode).CreateEncryptor())
                    using (var cs = new CryptoStream(new NonClosingStream(dst), encryptor, CryptoStreamMode.Write))
                        using (var w = cs.ToBinaryWriter(false))
                        {
                            var blockSize = AES.BlockSize / 8;
                            var padding   = blockSize - (src.Length + 1 + 4) % blockSize;
                            if (reliable)
                            {
                                padding = blockSize - (src.Length + 1 + 4 + 2) % blockSize;
                            }

                            if (reliable)
                            {
                                var counter = (ushort)(Interlocked.Increment(ref _encryptCounter) - 1);
                                data.Buffer.WriteShort(counter);
                            }

                            using (var dataStream = new WriteOnlyByteBufferStream(data.Buffer, false))
                                src.CopyTo(dataStream);

                            w.Write((byte)padding);
                            using (var dataStream = new ReadOnlyByteBufferStream(data.Buffer, false))
                            {
                                w.Write(Hash.GetUInt32 <CRC32>(dataStream));
                                dataStream.Position = 0;
                                dataStream.CopyTo(cs);
                            }
                            w.Fill((int)padding);
                        }
        }
Exemplo n.º 3
0
        protected override void Encode(IChannelHandlerContext context, SendContext message, List <object> output)
        {
            var type       = message.Message.GetType();
            var isInternal = RmiMessageFactory.Default.ContainsType(type);
            var factory    = isInternal
                ? RmiMessageFactory.Default
                : _userMessageFactories.FirstOrDefault(userFactory => userFactory.ContainsType(type));

            if (factory == null)
            {
                throw new ProudException($"No {nameof(MessageFactory)} found for message {type.FullName}");
            }

            var opCode = factory.GetOpCode(type);
            var buffer = context.Allocator.Buffer(2);

            using (var w = new WriteOnlyByteBufferStream(buffer, false).ToBinaryWriter(false))
            {
                w.Write(opCode);
                try
                {
                    _serializer.Serialize(w, message.Message);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Unable to serialize {message.Message.GetType().FullName}", ex);
                }
            }

            message.Message = buffer;
            output.Add(message);
        }
        public static void Encode(ICoreMessage message, IByteBuffer buffer)
        {
            var opCode = CoreMessageFactory.Default.GetOpCode(message.GetType());

            using (var w = new WriteOnlyByteBufferStream(buffer, false).ToBinaryWriter(false))
            {
                w.WriteEnum(opCode);
                Serializer.Serialize(w, (object)message);
            }
        }
Exemplo n.º 5
0
        public void EncryptedReliableMessage(IChannelHandlerContext context, ProudSession session, EncryptedReliableMessage message, RecvContext recvContext)
        {
            var crypt = session.Crypt;

            if (crypt != null)
            {
                var buffer = context.Allocator.Buffer(message.Data.Length);
                using (var src = new MemoryStream(message.Data))
                    using (var dst = new WriteOnlyByteBufferStream(buffer, false))
                    {
                        crypt.Decrypt(context.Allocator, message.EncryptMode, src, dst, true);
                    }
                recvContext.Message = buffer;
                context.Channel.Pipeline.Context <ProudFrameDecoder>().FireChannelRead(recvContext);
            }
        }
Exemplo n.º 6
0
        public void Decrypt(IByteBufferAllocator allocator, EncryptMode mode, Stream src, Stream dst, bool reliable)
        {
            if (RC4 == null || AES == null)
            {
                return;
            }
            //throw new ObjectDisposedException(GetType().FullName);

            using (var data = new BufferWrapper(allocator.Buffer().WithOrder(ByteOrder.LittleEndian)))
                using (var decryptor = GetAlgorithm(mode).CreateDecryptor())
                    using (var cs = new CryptoStream(src, decryptor, CryptoStreamMode.Read))
                    {
                        var padding  = cs.ReadByte();
                        var checksum = cs.ReadByte() | (cs.ReadByte() << 8) | (cs.ReadByte() << 16) | (cs.ReadByte() << 24);

                        using (var dataStream = new WriteOnlyByteBufferStream(data.Buffer, false))
                        {
                            cs.CopyTo(dataStream);
                        }

                        if (reliable)
                        {
                            var counter        = (ushort)(Interlocked.Increment(ref _decryptCounter) - 1);
                            var messageCounter = data.Buffer.GetShort(data.Buffer.ReaderIndex);

                            if (counter != messageCounter)
                            {
                                throw new ProudException($"Invalid decrypt counter! Remote: {messageCounter} Local: {counter}");
                            }
                        }

                        var slice = data.Buffer.ReadSlice(data.Buffer.ReadableBytes - padding);
                        using (var dataStream = new ReadOnlyByteBufferStream(slice, false))
                        {
                            if (Hash.GetUInt32 <CRC32>(dataStream) != (uint)checksum)
                            {
                                throw new ProudException("Invalid checksum");
                            }

                            dataStream.Position = reliable ? 2 : 0;
                            dataStream.CopyTo(dst);
                        }
                    }
        }
Exemplo n.º 7
0
        public void Encrypt(IByteBufferAllocator allocator, EncryptMode mode, Stream src, Stream dst, bool reliable)
        {
            if (RC4 == null || AES == null)
            {
                return;
            }
            //throw new ObjectDisposedException(GetType().FullName);

            using (var data = new BufferWrapper(allocator.Buffer()))
                using (var encryptor = GetAlgorithm(mode).CreateEncryptor())
                    using (var cs = new CryptoStream(new NonClosingStream(dst), encryptor, CryptoStreamMode.Write))
                        using (var w = cs.ToBinaryWriter(false))
                        {
                            var blockSize = AES.BlockSize / 8;
                            var padding   = blockSize - ((src.Length + 1 + 4) % blockSize);
                            if (reliable)
                            {
                                padding = blockSize - ((src.Length + 1 + 4 + 2) % blockSize);
                            }

                            if (reliable)
                            {
                                var counter = (ushort)(Interlocked.Increment(ref _encryptCounter) - 1);
                                data.Buffer.WriteShortLE(counter);
                            }

                            using (var dataStream = new WriteOnlyByteBufferStream(data.Buffer, false))
                            {
                                src.CopyTo(dataStream);
                            }

                            w.Write((byte)padding);
                            using (var dataStream = new ReadOnlyByteBufferStream(data.Buffer, false))
                            {
                                w.Write(Hash.GetUInt32 <CRC32>(dataStream));
                                dataStream.Position = 0;
                                dataStream.CopyTo(cs);
                            }

                            w.Fill((int)padding);
                        }
        }