public void ReadFrom(ref SpanBufferReader bufferReader)
 {
     UserId        = bufferReader.ReadString();
     UserName      = bufferReader.ReadString();
     Random        = bufferReader.ReadBytes(32).ToArray();
     PublicKey     = bufferReader.ReadVarBytes().ToArray();
     Configuration = new GameplayServerConfiguration();
     Configuration.ReadFrom(ref bufferReader);
     Secret = bufferReader.ReadString();
 }
 public void ReadFrom(ref SpanBufferReader bufferReader)
 {
     UserId    = bufferReader.ReadString();
     UserName  = bufferReader.ReadString();
     Random    = bufferReader.ReadBytes(32).ToArray();
     PublicKey = bufferReader.ReadVarBytes().ToArray();
     Secret    = bufferReader.ReadString();
     Code      = bufferReader.ReadString();
     Password  = bufferReader.ReadString();
     UseRelay  = bufferReader.ReadBool();
 }
Exemple #3
0
        public override void ReadFrom(ref SpanBufferReader bufferReader)
        {
            UserId         = bufferReader.ReadString();
            UserName       = bufferReader.ReadString();
            RemoteEndPoint = bufferReader.ReadIPEndPoint();
            Random         = bufferReader.ReadBytes(32).ToArray();
            PublicKey      = bufferReader.ReadVarBytes().ToArray();
            var flags = bufferReader.ReadByte();

            IsConnectionOwner = (flags & 1) > 0;
            IsDedicatedServer = (flags & 2) > 0;
        }
        /// <inheritdoc cref="IEncryptedMessageReader.ReadFrom"/>
        public IEncryptedMessage ReadFrom(ref SpanBufferReader bufferReader, byte[] key, HMAC hmac, byte?packetProperty)
        {
            var sequenceId      = bufferReader.ReadUInt32();
            var iv              = bufferReader.ReadBytes(16).ToArray();
            var decryptedBuffer = bufferReader.RemainingData.ToArray();

            using (var cryptoTransform = _aesCryptoServiceProvider.CreateDecryptor(key, iv))
            {
                var bytesWritten = 0;
                for (var i = decryptedBuffer.Length; i >= cryptoTransform.InputBlockSize; i -= bytesWritten)
                {
                    var inputCount = cryptoTransform.CanTransformMultipleBlocks
                        ? (i / cryptoTransform.InputBlockSize * cryptoTransform.InputBlockSize)
                        : cryptoTransform.InputBlockSize;
                    bytesWritten = cryptoTransform.TransformBlock(
                        decryptedBuffer, bytesWritten, inputCount,
                        decryptedBuffer, bytesWritten
                        );
                }
            }

            var paddingByteCount    = decryptedBuffer[decryptedBuffer.Length - 1] + 1;
            var hmacStart           = decryptedBuffer.Length - paddingByteCount - 10;
            var decryptedBufferSpan = decryptedBuffer.AsSpan();
            var hash             = decryptedBufferSpan.Slice(hmacStart, 10);
            var hashBufferWriter = new SpanBufferWriter(stackalloc byte[decryptedBuffer.Length + 4]);

            hashBufferWriter.WriteBytes(decryptedBufferSpan.Slice(0, hmacStart));
            hashBufferWriter.WriteUInt32(sequenceId);
            Span <byte> computedHash = stackalloc byte[32];

            if (!hmac.TryComputeHash(hashBufferWriter.Data, computedHash, out _))
            {
                throw new Exception("Failed to compute message hash.");
            }
            if (!hash.SequenceEqual(computedHash.Slice(0, 10)))
            {
                throw new Exception("Message hash does not match the computed hash.");
            }

            bufferReader = new SpanBufferReader(decryptedBuffer);
            if (_messageReader.ReadFrom(ref bufferReader, packetProperty) is not IEncryptedMessage message)
            {
                throw new Exception(
                          "Successfully decrypted message but failed to cast to type " +
                          $"'{nameof(IEncryptedMessage)}'."
                          );
            }

            message.SequenceId = sequenceId;
            return(message);
        }
Exemple #5
0
 public override void ReadFrom(ref SpanBufferReader bufferReader)
 {
     MultipartMessageId = bufferReader.ReadUInt32();
     Offset             = bufferReader.ReadVarUInt();
     Length             = bufferReader.ReadVarUInt();
     TotalLength        = bufferReader.ReadVarUInt();
     if (Length > _maximumLength)
     {
         throw new InvalidDataContractException($"Length must not surpass {_maximumLength} bytes");
     }
     if (TotalLength > _maximumTotalLength)
     {
         throw new InvalidDataContractException($"Length must not surpass {_maximumTotalLength} bytes");
     }
     Data = bufferReader.ReadBytes((int)Length).ToArray();
 }
Exemple #6
0
 public void ReadFrom(ref SpanBufferReader bufferReader)
 {
     ServerName         = bufferReader.ReadString();
     UserId             = bufferReader.ReadString();
     UserName           = bufferReader.ReadString();
     Secret             = bufferReader.ReadString();
     Password           = bufferReader.ReadString();
     CurrentPlayerCount = bufferReader.ReadVarInt();
     MaximumPlayerCount = bufferReader.ReadVarInt();
     DiscoveryPolicy    = (DiscoveryPolicy)bufferReader.ReadByte();
     InvitePolicy       = (InvitePolicy)bufferReader.ReadByte();
     Configuration      = new GameplayServerConfiguration();
     Configuration.ReadFrom(ref bufferReader);
     Random    = bufferReader.ReadBytes(32).ToArray();
     PublicKey = bufferReader.ReadVarBytes().ToArray();
 }
        public override void ReadFrom(ref SpanBufferReader bufferReader)
        {
            Result = (ResultCode)bufferReader.ReadUInt8();
            if (!Success)
            {
                return;
            }

            UserId             = bufferReader.ReadString();
            UserName           = bufferReader.ReadString();
            Secret             = bufferReader.ReadString();
            DiscoveryPolicy    = (DiscoveryPolicy)bufferReader.ReadByte();
            InvitePolicy       = (InvitePolicy)bufferReader.ReadByte();
            MaximumPlayerCount = bufferReader.ReadVarInt();
            Configuration      = new GameplayServerConfiguration();
            Configuration.ReadFrom(ref bufferReader);
            var flags = bufferReader.ReadByte();

            IsConnectionOwner = (flags & 1) > 0;
            IsDedicatedServer = (flags & 2) > 0;
            RemoteEndPoint    = bufferReader.ReadIPEndPoint();
            Random            = bufferReader.ReadBytes(32).ToArray();
            PublicKey         = bufferReader.ReadVarBytes().ToArray();
        }
Exemple #8
0
 public override void ReadFrom(ref SpanBufferReader bufferReader)
 {
     Random    = bufferReader.ReadBytes(32).ToArray();
     PublicKey = bufferReader.ReadVarBytes().ToArray();
     Signature = bufferReader.ReadVarBytes().ToArray();
 }
Exemple #9
0
 public override void ReadFrom(ref SpanBufferReader bufferReader)
 {
     Random = bufferReader.ReadBytes(32).ToArray();
 }
Exemple #10
0
 public void ReadFrom(ref SpanBufferReader bufferReader)
 {
     CertificateResponseId = bufferReader.ReadUInt32();
     Random = bufferReader.ReadBytes(32).ToArray();
     Cookie = bufferReader.ReadBytes(32).ToArray();
 }
Exemple #11
0
 public void ReadFrom(ref SpanBufferReader bufferReader)
 {
     Cookie = bufferReader.ReadBytes(32).ToArray();
 }