Esempio n. 1
0
        /// <inheritdoc cref="IEncryptedMessageWriter.WriteTo"/>
        public void WriteTo <T>(ref SpanBufferWriter bufferWriter, T message, byte[] key, HMAC hmac, byte?packetProperty)
            where T : class, IEncryptedMessage
        {
            var unencryptedBufferWriter = new SpanBufferWriter(stackalloc byte[412]);

            _messageWriter.WriteTo(ref unencryptedBufferWriter, message, packetProperty);

            var hashBufferWriter = new SpanBufferWriter(stackalloc byte[unencryptedBufferWriter.Size + 4]);

            hashBufferWriter.WriteBytes(unencryptedBufferWriter.Data);
            hashBufferWriter.WriteUInt32(message.SequenceId);
            Span <byte> hash = stackalloc byte[32];

            if (!hmac.TryComputeHash(hashBufferWriter.Data, hash, out _))
            {
                throw new Exception("Failed to compute message hash.");
            }
            unencryptedBufferWriter.WriteBytes(hash.Slice(0, 10));

            var iv = new byte[16];

            _rngCryptoServiceProvider.GetBytes(iv);

            var paddingByteCount = (byte)((16 - ((unencryptedBufferWriter.Size + 1) & 15)) & 15);

            for (var i = 0; i < paddingByteCount + 1; i++)
            {
                unencryptedBufferWriter.WriteUInt8(paddingByteCount);
            }

            var encryptedBuffer = unencryptedBufferWriter.Data.ToArray();

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

            bufferWriter.WriteUInt32(message.SequenceId);
            bufferWriter.WriteBytes(iv);
            bufferWriter.WriteBytes(encryptedBuffer);
        }
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteUInt32(MultipartMessageId);
     bufferWriter.WriteVarUInt(Offset);
     bufferWriter.WriteVarUInt(Length);
     bufferWriter.WriteVarUInt(TotalLength);
     bufferWriter.WriteBytes(Data);
 }
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteString(UserId);
     bufferWriter.WriteString(UserName);
     bufferWriter.WriteBytes(Random);
     bufferWriter.WriteVarBytes(PublicKey);
     Configuration.WriteTo(ref bufferWriter);
     bufferWriter.WriteString(Secret);
 }
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteString(UserId);
     bufferWriter.WriteString(UserName);
     bufferWriter.WriteIPEndPoint(RemoteEndPoint);
     bufferWriter.WriteBytes(Random);
     bufferWriter.WriteVarBytes(PublicKey);
     bufferWriter.WriteUInt8((byte)((IsConnectionOwner ? 1 : 0) | (IsDedicatedServer ? 2 : 0)));
 }
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteString(UserId);
     bufferWriter.WriteString(UserName);
     bufferWriter.WriteBytes(Random);
     bufferWriter.WriteVarBytes(PublicKey);
     bufferWriter.WriteString(Secret);
     bufferWriter.WriteString(Code);
     bufferWriter.WriteString(Password);
     bufferWriter.WriteBool(UseRelay);
 }
Esempio n. 6
0
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteString(ServerName);
     bufferWriter.WriteString(UserId);
     bufferWriter.WriteString(UserName);
     bufferWriter.WriteString(Secret);
     bufferWriter.WriteString(Password);
     bufferWriter.WriteVarInt(CurrentPlayerCount);
     bufferWriter.WriteVarInt(MaximumPlayerCount);
     bufferWriter.WriteUInt8((byte)DiscoveryPolicy);
     bufferWriter.WriteUInt8((byte)InvitePolicy);
     Configuration.WriteTo(ref bufferWriter);
     bufferWriter.WriteBytes(Random);
     bufferWriter.WriteVarBytes(PublicKey);
 }
Esempio n. 7
0
        /// <inheritdoc cref="IMessageWriter.WriteTo"/>
        public void WriteTo(ref SpanBufferWriter bufferWriter, IMessage message, byte?packetProperty)
        {
            var messageGroup = 0U;
            var messageId    = 0U;
            var messageType  = message.GetType();

            try
            {
                messageGroup = _messageRegistries
                               .First(kvp => kvp.Value.TryGetMessageId(messageType, out messageId))
                               .Key;
            }
            catch (InvalidOperationException)
            {
                throw new Exception(
                          "Failed to retrieve identifier for message of type " +
                          $"'{messageType.Name}'."
                          );
            }

            if (packetProperty.HasValue)
            {
                bufferWriter.WriteUInt8(packetProperty.Value);
            }
            bufferWriter.WriteUInt32(messageGroup);
            bufferWriter.WriteVarUInt(ProtocolVersion);

            var messageBufferWriter = new SpanBufferWriter(stackalloc byte[412]);

            messageBufferWriter.WriteVarUInt(messageId);
            if (message is IRequest request)
            {
                messageBufferWriter.WriteUInt32(request.RequestId);
            }
            if (message is IResponse response)
            {
                messageBufferWriter.WriteUInt32(response.ResponseId);
            }
            message.WriteTo(ref messageBufferWriter);
            bufferWriter.WriteVarUInt((uint)messageBufferWriter.Size);
            // TODO: Remove byte array allocation
            bufferWriter.WriteBytes(messageBufferWriter.Data.ToArray());
        }
Esempio n. 8
0
        public void WriteTo(ref SpanBufferWriter bufferWriter)
        {
            bufferWriter.WriteUInt8((byte)Result);
            if (!Success)
            {
                return;
            }

            bufferWriter.WriteString(UserId);
            bufferWriter.WriteString(UserName);
            bufferWriter.WriteString(Secret);
            bufferWriter.WriteUInt8((byte)DiscoveryPolicy);
            bufferWriter.WriteUInt8((byte)InvitePolicy);
            bufferWriter.WriteVarInt(MaximumPlayerCount);
            Configuration.WriteTo(ref bufferWriter);
            bufferWriter.WriteUInt8((byte)((IsConnectionOwner ? 1 : 0) | (IsDedicatedServer ? 2 : 0)));
            bufferWriter.WriteIPEndPoint(RemoteEndPoint);
            bufferWriter.WriteBytes(Random);
            bufferWriter.WriteVarBytes(PublicKey);
        }
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteBytes(Random);
     bufferWriter.WriteVarBytes(PublicKey);
     bufferWriter.WriteVarBytes(Signature);
 }
Esempio n. 10
0
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteUInt32(CertificateResponseId);
     bufferWriter.WriteBytes(Random);
     bufferWriter.WriteBytes(Cookie);
 }
Esempio n. 11
0
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteBytes(Random);
 }
Esempio n. 12
0
 public void WriteTo(ref SpanBufferWriter bufferWriter)
 {
     bufferWriter.WriteBytes(Cookie);
 }