public override void OnClientData(ClientBase aClient, byte[] data)
        {
            RealmListClient client = aClient as RealmListClient;

            client.LastActivity = DateTime.Now;

            BinReader       read   = new BinReader(data);
            REALMLISTOPCODE opCode = (REALMLISTOPCODE)read.ReadByte();

            Console.WriteLine("REALMLIST: " + opCode);
            switch (opCode)
            {
            case REALMLISTOPCODE.CHALLENGE:                     // 0x00
            case REALMLISTOPCODE.RECODE_CHALLENGE:              //0x02
            {
                realm_challenge(read, client);
                break;
            }

            case REALMLISTOPCODE.PROOF:                     // 0x01
            case REALMLISTOPCODE.RECODE_PROOF:              // 0x03
            {
                realm_proof(read, client);
                break;
            }

            case REALMLISTOPCODE.REALMLIST_REQUEST:                     // 0x10
            {
                client.Send(realmList);
                client.Close("Done");
                break;
            }
            }
        }
 public void getPassword(RealmListClient client)
 {
     client.account = (DBAccount)DataServer.Database.FindObjectByKey(typeof(DBAccount), client.username);
     if (client.account != null)
     {
         client.password = client.account.Password.ToUpper();
     }
 }
示例#3
0
        public override void OnClientData(ClientBase aClient, byte[] data)
        {
            RealmListClient client = aClient as RealmListClient;

            client.LastActivity = DateTime.Now;
            BinReader       read   = new BinReader(data);
            REALMLISTOPCODE opCode = (REALMLISTOPCODE)read.ReadByte();

            switch (opCode)
            {
            case REALMLISTOPCODE.CHALLENGE:
            case REALMLISTOPCODE.RECODE_CHALLENGE:
            {
                if (opCode == REALMLISTOPCODE.CHALLENGE)
                {
                    client.Send(patch_challenge);
                }
                else
                {
                    client.Send(realm_challenge);
                }
                break;
            }

            case REALMLISTOPCODE.PROOF:
            case REALMLISTOPCODE.RECODE_PROOF:
            {
                client.Send(realm_proof);
                break;
            }

            case REALMLISTOPCODE.REALMLIST_REQUEST:
            {
                client.Send(realmList);
                client.Close("Done");
                break;
            }
            }
        }
 public void setHash(RealmListClient client, byte[] ss_hash)
 {
     client.account.SessionKey = BitConverter.ToString(ss_hash);
     DataServer.Database.SaveObject(client.account);
 }
        public void realm_proof(BinReader read, RealmListClient client)
        {
            int t;

            byte[] A   = read.ReadBytes(32);
            byte[] kM1 = read.ReadBytes(20);

            byte [] rA = Reverse(A);

            byte [] AB = Concat(A, Reverse(client.B.getBytes(32)));

            SHA1 shaM1 = new SHA1CryptoServiceProvider();

            byte [] U = shaM1.ComputeHash(AB);

            byte [] rU = Reverse(U);

            // SS_Hash
            BigInteger temp1 = client.v.modPow(new BigInteger(rU), new BigInteger(rN));
            BigInteger temp2 = temp1 * new BigInteger(rA);
            BigInteger temp3 = temp2.modPow(new BigInteger(client.rb), new BigInteger(rN));

            byte [] S1 = new byte[16];
            byte [] S2 = new byte[16];
            byte [] S  = temp3.getBytes(32);
            byte [] rS = Reverse(S);

            for (t = 0; t < 16; t++)
            {
                S1[t] = rS[t * 2];
                S2[t] = rS[(t * 2) + 1];
            }

            byte [] hashS1  = shaM1.ComputeHash(S1);
            byte [] hashS2  = shaM1.ComputeHash(S2);
            byte [] SS_Hash = new byte[hashS1.Length + hashS2.Length];
            for (t = 0; t < hashS1.Length; t++)
            {
                SS_Hash[t * 2]       = hashS1[t];
                SS_Hash[(t * 2) + 1] = hashS2[t];
            }

            // cal M1
            byte [] NHash    = shaM1.ComputeHash(N);
            byte [] GHash    = shaM1.ComputeHash(client.G.getBytes());
            byte [] userHash = shaM1.ComputeHash(client.Busername);
            byte [] NG_Hash  = new byte[20];
            for (t = 0; t < 20; t++)
            {
                NG_Hash[t] = (byte)(NHash[t] ^ GHash[t]);
            }
            byte [] Temp = Concat(NG_Hash, userHash);
            Temp = Concat(Temp, client.salt);
            Temp = Concat(Temp, A);
            Temp = Concat(Temp, Reverse(client.B.getBytes(32)));
            Temp = Concat(Temp, SS_Hash);

            byte [] M1 = shaM1.ComputeHash(Temp);

            BinWriter pkg;

            if (!Same(M1, kM1))
            {
                Console.WriteLine("{0} Failed authentication", client.username);

                pkg = new BinWriter();
                pkg.Write((byte)1);
                pkg.Write((byte)3);
                client.Send(pkg.GetBuffer(), 26);
                client.Close("Failed authentication");
                return;
            }

            Console.WriteLine("{0} authenticated", client.username);
            setHash(client, SS_Hash);             // sets session key in db

            // cal M2
            Temp = Concat(A, M1);
            Temp = Concat(Temp, SS_Hash);
            byte [] M2 = shaM1.ComputeHash(Temp);

            pkg = new BinWriter();
            pkg.Write((byte)1);
            pkg.Write((byte)0);
            pkg.Write(M2);
            pkg.Write(new byte[4]);
            client.Send(pkg.GetBuffer(), 26);
        }
        public void realm_challenge(BinReader read, RealmListClient client)
        {
            int       t;
            BinWriter pkg;

            byte[] useless = read.ReadBytes(32);
            int    namelen = read.ReadByte();

            client.Busername = read.ReadBytes(namelen);
            client.username  = System.Text.Encoding.ASCII.GetString(client.Busername);

            getPassword(client);

            if (client.password == null)
            {
                Console.WriteLine("Can not retreive password for: " + client.username);
                pkg = new BinWriter();
                pkg.Write((byte)1);
                pkg.Write((byte)3);
                client.Send(pkg.GetBuffer(), 26);
                client.Close("Accountless");
            }

            byte [] hash = GetUserPasswordHash(client.username, client.password);

            if (hash == null)
            {
                Console.WriteLine("Can not calculate hash for: " + client.username);
                pkg = new BinWriter();
                pkg.Write((byte)1);
                pkg.Write((byte)3);
                client.Send(pkg.GetBuffer(), 26);
                client.Close("Accountless");
            }

            byte [] res = new Byte[hash.Length + client.salt.Length];
            Const.rand.NextBytes(client.salt);

            t = 0;
            foreach (byte s in client.salt)
            {
                res[t++] = s;
            }
            foreach (byte s in hash)
            {
                res[t++] = s;
            }

            SHA1 sha = new SHA1CryptoServiceProvider();

            byte [] hash2 = sha.ComputeHash(res, 0, res.Length);
            byte [] x     = Reverse(hash2);

            rN = Reverse(N);
            Const.rand.NextBytes(client.b);

            client.rb = Reverse(client.b);

            BigInteger bi  = new BigInteger(x);
            BigInteger bi2 = new BigInteger(rN);

            client.G = new BigInteger(new byte[] { 7 });
            client.v = client.G.modPow(bi, bi2);

            client.K = new BigInteger(new Byte[] { 3 });
            BigInteger temp1 = client.K * client.v;
            BigInteger temp2 = client.G.modPow(new BigInteger(client.rb), new BigInteger(rN));
            BigInteger temp3 = temp1 + temp2;

            client.B = temp3 % new BigInteger(rN);

            pkg = new BinWriter();
            pkg.Write((byte)0);
            pkg.Write((byte)0);
            pkg.Write((byte)0);
            pkg.Write(Reverse(client.B.getBytes(32)));
            pkg.Write((byte)1);
            pkg.Write((byte)7);
            pkg.Write((byte)32);
            pkg.Write(N);
            pkg.Write(client.salt);
            pkg.Write((byte[])new byte[16]);

            client.Send(pkg.GetBuffer(), 118);
        }
        public override void OnAcceptSocket(Socket sock)
        {
            ClientBase client = new RealmListClient(sock);

            AddClient(client);
        }