private void HandleLoginCommand(MasterControlClient session, RouterMessage msg)
        {
            //Get details
            int loginId = BitConverter.ToInt16(msg.payload, 0);

            byte[] loginKey = new byte[16];
            Array.Copy(msg.payload, 2, loginKey, 0, 16);

            //Search for a server
            DeltaManagerServer server = null;

            lock (Program.servers)
            {
                foreach (var s in Program.servers)
                {
                    if (s.id == loginId)
                    {
                        //Authenticate key
                        if (BinaryTool.CompareBytes(s.key, loginKey))
                        {
                            server = s;
                        }
                    }
                }
            }

            //Check if it was successful
            if (server != null)
            {
                //Set
                server.SetTransport(session);
                session.authenticated = true;

                //Log
                logger.Log("HandleLoginCommand", $"Client {session.GetDebugName()} successfully logged in as server ID {server.id}.", DeltaLogLevel.Low);
            }
            else
            {
                //Failed
                logger.Log("HandleLoginCommand", $"Client {session.GetDebugName()} attempted login, but failed.", DeltaLogLevel.Low);
            }
        }
        /// <summary>
        /// Reads the data encoded by the CreateSteamIdTokenString function
        /// </summary>
        /// <param name="encoded"></param>
        public SteamIdToken ReadSteamIdTokenString(string encoded)
        {
            //Read as bytes
            byte[] data;
            try
            {
                data = Convert.FromBase64String(encoded);
            } catch
            {
                return(null);
            }

            //Validate
            if (data.Length < 16 + 1)
            {
                return(null);
            }

            //Read the hash
            byte[] hash = new byte[16];
            Array.Copy(data, 0, hash, 0, 16);

            //Get the HMAC of this
            steamTokenKey.CopyTo(data, 0);
            byte[] challengeHash = new HMACMD5(steamTokenKey).ComputeHash(data);

            //Validate
            if (!BinaryTool.CompareBytes(hash, challengeHash))
            {
                return(null);
            }

            //Data OK! Read it now
            return(new SteamIdToken
            {
                version = data[16],
                steam_id = Encoding.ASCII.GetString(data, 17, data.Length - 17)
            });
        }
Beispiel #3
0
        /// <summary>
        /// Handles the first binary message we get. It will always be for authentication.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private async Task HandleAuthentication(byte[] data, int length)
        {
            //This message follows the following format:
            //  256 bytes: Authentication key
            //  8 bytes: Program identification name
            //  4 bytes: System version

            //Verify
            if (length != 256 + 8 + 4)
            {
                Log("AUTHENTICATION", "Authentication length mismatch!");
                await DisconnectAsync(WebSocketCloseStatus.InvalidPayloadData, "DELTA_RPC_DISCONNECT_INVALID_AUTHENTICATION_PAYLOAD");

                return;
            }

            //Extract info
            byte[] key = BinaryTool.CopyFromArray(data, 0, 256);
            program_identity = Encoding.ASCII.GetString(data, 256, 8);
            version          = BinaryTool.ReadInt32(data, 264);

            //Validate the key
            if (!BinaryTool.CompareBytes(Program.master_key, key))
            {
                authenticated = false;
                Log("AUTHENTICATION", "Authentication failed!");
                await DisconnectAsync(WebSocketCloseStatus.PolicyViolation, "DELTA_RPC_DISCONNECT_INVALID_AUTHENTICATION_KEY");

                return;
            }

            //We're now ready for authentication!
            authenticated = true;
            Log("AUTHENTICATION", "Authentication OK!");
            await WriteResponse(0, 0);
        }