Пример #1
0
        public static Authenticator RequestAuthenticator(AuthenticatorRegion authRegion)
        {
            byte[] plainTextRequest;
            byte[] xorRequestKey = GetRandomBytes(20);

            using (MemoryStream plainTextStream = new MemoryStream())
            {
                plainTextStream.Write(xorRequestKey, 0, 20); // Write Randomized Padding

                // Force Blizzard to return an authenticator from a specific region if requested
                if (authRegion != null)
                {
                    string countryCode = (authRegion.RegionType == AuthenticatorRegion.RegionCode.NorthAmerica) ? "US" : "FR";
                    plainTextStream.Write(Encoding.UTF8.GetBytes(countryCode), 0, 2);
                }
                else
                {
                    plainTextStream.Write(new byte[] { 0x0, 0x0 }, 0, 2);
                }

                plainTextStream.Write(GetRandomBytes(16, true), 0, 16);

                plainTextRequest = plainTextStream.ToArray();
            }

            byte[] encryptedRequest = EncryptRSAPayload(plainTextRequest);
            byte[] responseData     = null;

            try
            {
                using (HttpWebResponse response = HTTPPost(ENDPOINT_ENROLL, encryptedRequest))
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new InvalidBlizzardAPIResponse(String.Format("HTTP {0}: {1}", response.StatusCode, response.StatusDescription));
                    }

                    using (MemoryStream memStream = new MemoryStream())
                    {
                        using (Stream binaryStream = response.GetResponseStream())
                        {
                            byte[] temp = new byte[RESPONSE_BUFFER_SIZE];

                            int read;
                            while ((read = binaryStream.Read(temp, 0, RESPONSE_BUFFER_SIZE)) != 0)
                            {
                                memStream.Write(temp, 0, read);
                            }
                            responseData = memStream.ToArray();

                            if (responseData.Length != ENROLL_RESPONSE_SIZE)
                            {
                                throw new InvalidBlizzardAPIResponse(string.Format("Invalid response size (expected {0} got {1})", ENROLL_RESPONSE_SIZE, responseData.Length));
                            }
                        }
                    }
                }
            }
            catch (WebException webEx)
            {
                HttpWebResponse response = webEx.Response as HttpWebResponse;

                throw new InvalidBlizzardAPIResponse(String.Format("HTTP {0}: {1}", response.StatusCode, response.StatusDescription));
            }

            // Server Time Offset
            byte[] serverTime = new byte[8];
            Array.Copy(responseData, serverTime, 8);

            Settings.SettingsDatabase.ServerTimeOffset = CalculateOffset(serverTime);

            // Serial
            string serial = Encoding.Default.GetString(responseData, 8, 17);

            // Token
            byte[] token = new byte[20];
            Array.Copy(responseData, 25, token, 0, 20);

            for (int i = xorRequestKey.Length - 1; i >= 0; i--)
            {
                token[i] ^= xorRequestKey[i];
            }

            return(new Authenticator(serial, token));
        }
Пример #2
0
        public static Authenticator RequestAuthenticator(AuthenticatorRegion authRegion)
        {
            byte[] plainTextRequest;
            byte[] xorRequestKey = GetRandomBytes(20);

            using (MemoryStream plainTextStream = new MemoryStream())
            {
                plainTextStream.Write(xorRequestKey, 0, 20); // Write Randomized Padding

                // Force Blizzard to return an authenticator from a specific region if requested
                if (authRegion != null)
                {
                    string countryCode = (authRegion.RegionType == AuthenticatorRegion.RegionCode.NorthAmerica) ? "US" : "FR";
                    plainTextStream.Write(Encoding.UTF8.GetBytes(countryCode), 0, 2);
                }
                else
                {
                    plainTextStream.Write(new byte[] { 0x0, 0x0 }, 0, 2);
                }

                plainTextStream.Write(GetRandomBytes(16, true), 0, 16);

                plainTextRequest = plainTextStream.ToArray();
            }

            byte[] encryptedRequest = EncryptRSAPayload(plainTextRequest);
            byte[] responseData = null;

            try
            {
                using (HttpWebResponse response = HTTPPost(ENDPOINT_ENROLL, encryptedRequest))
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new InvalidBlizzardAPIResponse(String.Format("HTTP {0}: {1}", response.StatusCode, response.StatusDescription));
                    }

                    using (MemoryStream memStream = new MemoryStream())
                    {
                        using (Stream binaryStream = response.GetResponseStream())
                        {
                            byte[] temp = new byte[RESPONSE_BUFFER_SIZE];

                            int read;
                            while ((read = binaryStream.Read(temp, 0, RESPONSE_BUFFER_SIZE)) != 0)
                            {
                                memStream.Write(temp, 0, read);
                            }
                            responseData = memStream.ToArray();

                            if (responseData.Length != ENROLL_RESPONSE_SIZE)
                            {
                                throw new InvalidBlizzardAPIResponse(string.Format("Invalid response size (expected {0} got {1})", ENROLL_RESPONSE_SIZE, responseData.Length));
                            }
                        }
                    }
                }
            }
            catch (WebException webEx)
            {
                HttpWebResponse response = webEx.Response as HttpWebResponse;

                throw new InvalidBlizzardAPIResponse(String.Format("HTTP {0}: {1}", response.StatusCode, response.StatusDescription));
            }

            // Server Time Offset
            byte[] serverTime = new byte[8];
            Array.Copy(responseData, serverTime, 8);

            Settings.SettingsDatabase.ServerTimeOffset = CalculateOffset(serverTime);

            // Serial
            string serial = Encoding.Default.GetString(responseData, 8, 17);

            // Token
            byte[] token = new byte[20];
            Array.Copy(responseData, 25, token, 0, 20);

            for (int i = xorRequestKey.Length - 1; i >= 0; i--)
            {
                token[i] ^= xorRequestKey[i];
            }

            return new Authenticator(serial, token);
        }