Ejemplo n.º 1
0
        public static byte[] _SRP_Salt; //session salt

        private byte[] handlePairStepOne(List <TypeValue> requestValue)
        {
            _SRP_Salt = SRP.s();

            _SRP_b = SRP.b();
            var vBytes    = Encoding.UTF8.GetBytes("Pair-Setup");
            var publicKey = SRP.B(_SRP_b, vBytes);


            var sequence = new HomeKit.Protocol.TypeLengthValue.TypeLengthValue.TypeValue()
            {
                Type = kTLVType_State.Type, Value = new byte[] { 0x02 }
            };
            var saltMessage = new HomeKit.Protocol.TypeLengthValue.TypeLengthValue.TypeValue()
            {
                Type = kTLVType_Salt.Type, Value = _SRP_Salt
            };
            var publicKeyMessage = new HomeKit.Protocol.TypeLengthValue.TypeLengthValue.TypeValue()
            {
                Type = kTLVType_PublicKey.Type, Value = publicKey
            };

            var result = HomeKit.Protocol.TypeLengthValue.TypeLengthValue.Encode(sequence, saltMessage, publicKeyMessage);

            return(result);
        }
Ejemplo n.º 2
0
        private byte[] handlePairStepTwo(List <TypeValue> requestValue)
        {
            //var newSalt = SRP.s();
            var a  = requestValue.FirstOrDefault(x => x.Type == kTLVType_PublicKey.Type); // "A is a public key that exists only for a single login session."
            var M1 = requestValue.FirstOrDefault(x => x.Type == kTLVType_Proof.Type);     // "M1 is the proof that you actually know your own password."

            var clientsA = SRP.A(a.Value);

            var session = SRP.K_Service(a.Value, Encoding.UTF8.GetBytes("Pair-Setup"), Encoding.UTF8.GetBytes("11111111"), _SRP_b);

            var _M2 = SRP.M2(clientsA, M1.Value, session);

            var sequence = new TypeValue()
            {
                Type = kTLVType_State.Type, Value = new byte[] { 0x04 }
            };
            var passowrdProof_M2 = new TypeValue()
            {
                Type = kTLVType_Proof.Type, Value = _M2
            };

            var result = HomeKit.Protocol.TypeLengthValue.TypeLengthValue.Encode(sequence, passowrdProof_M2);

            return(result);
        }
Ejemplo n.º 3
0
        private void OnCheckPasswordSettings(object sender, System.EventArgs e)
        {
            Execute.ShowDebugMessage("account.getPasswordSettings");

            MTProtoService.GetPasswordAsync(
                result => BeginOnUIThread(() =>
            {
                var password84 = result as TLPassword84;
                if (password84 != null && password84.HasRecovery.Value)
                {
                    var algo = password84.CurrentAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;
                    if (algo == null)
                    {
                        return;
                    }

                    password84.CurrentPasswordHash = SRP.GetX(new TLString(TempNewPassword), algo);
                    //password.CurrentPasswordHash = Utils.Password.GetHash(password.CurrentSalt, new TLString(TempNewPassword));

                    _password = password84;

                    RecoveryEmailUnconfirmedVisibility = !TLString.IsNullOrEmpty(_password.EmailUnconfirmedPattern) || (_password is TLPassword && ((TLPassword)_password).HasRecovery.Value)
                            ? Visibility.Visible
                            : Visibility.Collapsed;
                    NotifyOfPropertyChange(() => RecoveryEmailLabel);
                    NotifyOfPropertyChange(() => CompletePasswordLabel);
                    NotifyOfPropertyChange(() => ChangePasswordVisibility);
                    NotifyOfPropertyChange(() => CompletePasswordVisibility);

                    StopTimer();

                    if (_authorizationForm != null || _secureValues != null)
                    {
                        MTProtoService.GetPasswordSettingsAsync(SRP.GetCheck(password84.CurrentPasswordHash, password84.SRPId, password84.SRPB, algo),
                                                                result2 => BeginOnUIThread(() =>
                        {
                            Passport.EnterPasswordViewModel.NavigateToPassportCommon(
                                result2, password84, new TLString(TempNewPassword),
                                _authorizationForm, _secureValues,
                                MTProtoService, StateService, NavigationService);
                        }),
                                                                error => BeginOnUIThread(() =>
                        {
                            if (error.TypeEquals(ErrorType.PASSWORD_HASH_INVALID))
                            {
                                StateService.AuthorizationForm = _authorizationForm;
                                StateService.Password          = result;
                                NavigationService.UriFor <Passport.EnterPasswordViewModel>().WithParam(x => x.RandomParam, Guid.NewGuid().ToString()).Navigate();
                            }
                        }));
                    }
                }
            }),
                error =>
            {
                Execute.ShowDebugMessage("account.getPasswordSettings error " + error);
            });
        }
        private static void ResetSecureSecret(
            TLPasswordSettings passwordSettings, TLPassword passwordBase, TLString password,
            TLAuthorizationForm authorizationForm, IList <TLSecureValue> secureValues,
            IMTProtoService mtProtoService, IStateService stateService, INavigationService navigationService)
        {
            var passwordSettings83 = passwordSettings as TLPasswordSettings83;

            if (passwordSettings83 == null)
            {
                return;
            }

            var password84 = passwordBase as TLPassword84;

            if (password84 == null)
            {
                return;
            }

            var newSecureSettings = new TLSecureSecretSettings
            {
                SecureAlgo     = new TLSecurePasswordKdfAlgoUnknown(),
                SecureSecret   = TLString.Empty,
                SecureSecretId = new TLLong(0)
            };

            var newSettings = new TLPasswordInputSettings83
            {
                Flags             = new TLInt(0),
                NewSecureSettings = newSecureSettings,
            };

            mtProtoService.GetPasswordAsync(
                resul1 =>
            {
                var srpParams = resul1 as IPasswordSRPParams;
                if (srpParams == null)
                {
                    return;
                }

                var currentPasswordHash = passwordBase.CurrentPasswordHash ?? TLString.Empty;
                mtProtoService.UpdatePasswordSettingsAsync(SRP.GetCheck(currentPasswordHash, srpParams.SRPId, srpParams.SRPB, srpParams.CurrentAlgo), newSettings,
                                                           result2 =>
                {
                    passwordSettings83.SecureSettings = newSettings.NewSecureSettings;

                    NavigateToPassportCommon(
                        passwordSettings83, passwordBase, password,
                        authorizationForm, secureValues,
                        mtProtoService, stateService, navigationService);
                },
                                                           error2 => { });
            },
                error1 => { });
        }
Ejemplo n.º 5
0
    private void CompareNPC()
    {
        nPC = (SRP)Random.Range(0, (int)SRP.PAPER);

        if (nPC == sRP)
        {
            result = RESULT.DRAW;
            return;
        }

        switch (sRP)
        {
        case SRP.SCISSORS:
            if (nPC == SRP.ROCK)
            {
                result = RESULT.LOSE;
            }
            else
            {
                result = RESULT.WIN;
            }
            break;

        case SRP.ROCK:
            if (nPC == SRP.PAPER)
            {
                result = RESULT.LOSE;
            }
            else     //if(nPC == SRP.SCISSORS)
            {
                result = RESULT.WIN;
            }
            break;

        case SRP.PAPER:
            if (nPC == SRP.SCISSORS)
            {
                result = RESULT.LOSE;
            }
            else     //if(nPC == SRP.ROCK)
            {
                result = RESULT.WIN;
            }
            break;

        case SRP.COUNT:
            result = RESULT.COUNT;
            break;

        default:
            break;
        }
    }
Ejemplo n.º 6
0
 public static void CreateAccount(string username, string email, string password)
 {
     username = username.ToUpper();
     password = password.ToUpper();
     using (var db = new DBLogin())
         using (var sha = SHA1.Create())
         {
             var srp       = new SRP(username, password);
             var statement = db.Account
                             .Value(a => a.Username, username)
                             .Value(a => a.PasswordHash, sha.ComputeHash(Encoding.ASCII.GetBytes(password)))
                             .Value(a => a.Email, email)
                             .Value(a => a.SRPVerifier, srp.Verifier.ToProperByteArray())
                             .Value(a => a.SRPSalt, srp.Salt.ToProperByteArray());
             statement.Insert();
         }
 }
Ejemplo n.º 7
0
        public void HandleAuthLogonProof(AuthClass session, PacketReader data)
        {
            PacketWriter logonProof = new PacketWriter();

            BigInteger a  = data.ReadBytes(32);
            BigInteger m1 = data.ReadBytes(20);

            SRP.PublicEphemeralValueA = a;

            if (!SRP.IsClientProofValid(m1))
            {
                return;
            }

            foreach (var b in SRP.SessionKey.GetBytes())
            {
                if (b < 0x10)
                {
                    account.SessionKey += "0" + String.Format("{0:X}", b);
                }
                else
                {
                    account.SessionKey += String.Format("{0:X}", b);
                }
            }

            logonProof.WriteUInt8((byte)ClientLink.AuthLogonProof);
            logonProof.WriteUInt8(0);
            logonProof.WriteBytes(SRP.ServerSessionKeyProof.GetBytes(), 20);
            logonProof.WriteUInt32(0x00800000);
            logonProof.WriteUInt32(0);
            logonProof.WriteUInt16(0);

            //"UPDATE account SET sessionkey = ?, last_ip = ?, last_login = NOW(), locale = ?, failed_logins = 0 WHERE username = ?"
            PreparedStatement stmt = DB.Auth.GetPreparedStatement(LoginStatements.Upd_LogonProof);

            stmt.AddValue(0, account.SessionKey);
            stmt.AddValue(1, account.LastIP);
            stmt.AddValue(2, 0);//GetLocaleByName(_localizationName));
            stmt.AddValue(3, account.Name);
            DB.Auth.Execute(stmt);

            Log.outInfo("Accepted Client Connection IP: {0}", clientSocket.LocalEndPoint);
            session.Send(logonProof);
        }
Ejemplo n.º 8
0
    private IEnumerator CountDown(float duration)
    {
        sRP = SRP.COUNT;
        var beginTime = Time.time;
        var time      = 0f;

        for (; ;)
        {
            time = duration - (Time.time - beginTime);
            if (time >= 0f)
            {
                countTime.text = time.ToString("0.00");
            }
            else
            {
                countTime.text = "0.00f";
                break;
            }
            yield return(null);
        }
    }
Ejemplo n.º 9
0
        public override void Logon(IRpcController controller, LogonRequest request, Action <LogonResponse> done)
        {
            srp = new SRP(request.Email, "123");

            var message = srp.LogonChallenge;

            var moduleLoadRequest = ModuleLoadRequest.CreateBuilder()
                                    .SetModuleHandle(ContentHandle.CreateBuilder()
                                                     .SetRegion(Region)
                                                     .SetUsage(Usage)
                                                     .SetHash(ByteString.CopyFrom(ModuleHash)))
                                    .SetMessage(ByteString.CopyFrom(message))
                                    .Build();

            client.ListenerId = request.ListenerId;

            AuthenticationClient.CreateStub(client).ModuleLoad(controller, moduleLoadRequest, ClientServiceCallback);

            new Thread(() =>
            {
                wait.WaitOne();
                if (client.ErrorCode == AuthError.None)
                {
                    done(new LogonResponse.Builder
                    {
                        Account = new EntityId.Builder {
                            High = HighId.Account, Low = 0
                        }.Build(),
                        GameAccount = new EntityId.Builder {
                            High = HighId.GameAccount, Low = 0
                        }.Build(),
                    }.Build());
                }
                else
                {
                    done(new LogonResponse());
                }
            }).Start();
        }
Ejemplo n.º 10
0
        public override void Logon(IRpcController controller, LogonRequest request, Action<LogonResponse> done)
        {
            srp = new SRP(request.Email, "123");

            var message = srp.Response1;

            var moduleLoadRequest = ModuleLoadRequest.CreateBuilder()
                .SetModuleHandle(ContentHandle.CreateBuilder()
                    .SetRegion(Region)
                    .SetUsage(Usage)
                    .SetHash(ByteString.CopyFrom(ModuleHash)))
                .SetMessage(ByteString.CopyFrom(message))
                .Build();

            client.ListenerId = request.ListenerId;

            AuthenticationClient.CreateStub(client).ModuleLoad(controller, moduleLoadRequest, ClientServiceCallback);

            new Thread(() =>
                           {
                               wait.WaitOne();
                               if (client.ErrorCode == AuthError.None)
                               {
                                   done(new LogonResponse.Builder
                                            {
                                                Account = new EntityId.Builder { High = HighId.Account, Low = 0 }.Build(),
                                                GameAccount = new EntityId.Builder { High = HighId.GameAccount, Low = 0 }.Build(),
                                            }.Build());
                               }
                               else
                               {
                                   done(new LogonResponse());
                               }
                           }).Start();

        }
Ejemplo n.º 11
0
        private void ClearPassword()
        {
            var password = _password as TLPassword84;

            if (password == null)
            {
                return;
            }

            IsWorking = true;
            BeginOnThreadPool(() =>
            {
                var newSettings = new TLPasswordInputSettings83
                {
                    Flags           = new TLInt(0),
                    NewAlgo         = new TLPasswordKdfAlgoUnknown(),
                    NewPasswordHash = TLString.Empty,
                    Hint            = TLString.Empty,
                    Email           = TLString.Empty
                };

                MTProtoService.GetPasswordAsync(
                    result1 =>
                {
                    var srpParams = result1 as IPasswordSRPParams;
                    if (srpParams == null)
                    {
                        BeginOnUIThread(() => IsWorking = false);
                        return;
                    }

                    var currentPasswordHash = password.CurrentPasswordHash ?? TLString.Empty;
                    MTProtoService.UpdatePasswordSettingsAsync(SRP.GetCheck(currentPasswordHash, srpParams.SRPId, srpParams.SRPB, srpParams.CurrentAlgo), newSettings,
                                                               result => BeginOnUIThread(() =>
                    {
                        StopTimer();

                        IsWorking      = false;
                        var noPassword = new TLPassword84
                        {
                            Flags = new TLInt(0),
                            EmailUnconfirmedPattern = TLString.Empty,
                            NewSecureSalt           = TLString.Empty,
                            SecretRandom            = TLString.Empty
                        };

                        var password81 = _password as TLPassword84;
                        if (password81 != null)
                        {
                            noPassword.NewAlgo       = password81.NewAlgo;
                            noPassword.NewSecureSalt = password81.NewSecureSalt;
                            noPassword.SecretRandom  = password81.SecretRandom;
                        }

                        _password = noPassword;
                        if (_password != null)
                        {
                            PasswordEnabled = _password.IsAvailable;
                            RecoveryEmailUnconfirmedVisibility = !TLString.IsNullOrEmpty(_password.EmailUnconfirmedPattern) || (_password is TLPassword && ((TLPassword)_password).HasRecovery.Value)
                                        ? Visibility.Visible
                                        : Visibility.Collapsed;
                            NotifyOfPropertyChange(() => RecoveryEmailLabel);
                            NotifyOfPropertyChange(() => CompletePasswordLabel);
                            NotifyOfPropertyChange(() => ChangePasswordVisibility);
                            NotifyOfPropertyChange(() => CompletePasswordVisibility);
                        }
                        EventAggregator.Publish(_password);
                    }),
                                                               error => BeginOnUIThread(() =>
                    {
                        IsWorking          = false;
                        var messageBuilder = new StringBuilder();
                        //messageBuilder.AppendLine(AppResources.ServerErrorMessage);
                        //messageBuilder.AppendLine();
                        messageBuilder.AppendLine("Method: account.updatePasswordSettings");
                        messageBuilder.AppendLine("Result: " + error);

                        if (TLRPCError.CodeEquals(error, ErrorCode.FLOOD))
                        {
                            Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.FloodWaitString, AppResources.Error, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error, ErrorCode.INTERNAL))
                        {
                            Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.ServerError, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error, ErrorCode.BAD_REQUEST))
                        {
                            if (TLRPCError.TypeEquals(error, ErrorType.PASSWORD_HASH_INVALID))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.NEW_PASSWORD_BAD))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.NEW_SALT_INVALID))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.EMAIL_INVALID))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.EmailInvalid, AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error, ErrorType.EMAIL_UNCONFIRMED))
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else
                            {
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                        }
                        else
                        {
                            Execute.ShowDebugMessage("account.updatePasswordSettings error " + error);
                        }
                    }));
                },
                    error1 => BeginOnUIThread(() =>
                {
                    IsWorking = false;
                }));
            });
        }
        public void Validate()
        {
            if (_savedCredentialsCard == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(Password))
            {
                Error = "PASSWORD";
                NotifyOfPropertyChange(() => Error);
                return;
            }

            IsWorking = true;
            MTProtoService.GetPasswordAsync(
                result =>
            {
                var password84 = result as TLPassword84;
                if (password84 == null)
                {
                    return;
                }

                var algo = password84.CurrentAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;
                if (algo == null)
                {
                    return;
                }
                //var passwordHash = Utils.Password.GetHash(password.CurrentSalt, new TLString(Password));
                var passwordHash = SRP.GetX(new TLString(Password), algo);
                var password     = SRP.GetCheck(passwordHash, password84.SRPId, password84.SRPB, algo);

                MTProtoService.GetTmpPasswordAsync(
                    password, new TLInt(Constants.DefaultTmpPasswordLifetime),
                    result2 => Execute.BeginOnUIThread(() =>
                {
                    IsWorking = false;
                    PaymentInfo.Credentials = new TLInputPaymentCredentialsSaved
                    {
                        Id          = _savedCredentialsCard.Id,
                        TmpPassword = result2.TmpPassword
                    };
                    PaymentInfo.CredentialsTitle = Title;
                    StateService.SaveTmpPassword(result2);
                    NavigateToNextStep();
                }),
                    error2 => Execute.BeginOnUIThread(() =>
                {
                    IsWorking          = false;
                    var messageBuilder = new StringBuilder();
                    messageBuilder.AppendLine("Method: account.getTmpPassword");
                    messageBuilder.AppendLine("Result: " + error2);

                    if (TLRPCError.CodeEquals(error2, ErrorCode.FLOOD))
                    {
                        Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.FloodWaitString, AppResources.Error, MessageBoxButton.OK));
                    }
                    else if (TLRPCError.CodeEquals(error2, ErrorCode.INTERNAL))
                    {
                        Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.ServerError, MessageBoxButton.OK));
                    }
                    else if (TLRPCError.CodeEquals(error2, ErrorCode.BAD_REQUEST))
                    {
                        if (TLRPCError.TypeEquals(error2, ErrorType.PASSWORD_HASH_INVALID))
                        {
                            Error = string.Format("PASSWORD");
                            NotifyOfPropertyChange(() => Error);
                        }
                        else
                        {
                            Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                        }
                    }
                    else
                    {
                        Execute.ShowDebugMessage("account.getTmpPassword error " + error2);
                    }
                }));
            },
                error => Execute.BeginOnUIThread(() =>
            {
                IsWorking = false;
            }));
        }
        protected void UpdatePasswordSettings(TLPassword84 password, TLPasswordInputSettings newSettings)
        {
            IsWorking = true;
            BeginOnThreadPool(() =>
            {
                MTProtoService.GetPasswordAsync(
                    result1 =>
                {
                    var srpParams = result1 as IPasswordSRPParams;
                    if (srpParams == null)
                    {
                        BeginOnUIThread(() => IsWorking = false);
                        return;
                    }

                    var newSettings83 = newSettings as TLPasswordInputSettings83;
                    if (newSettings83 == null)
                    {
                        return;
                    }

                    // calculate new password hash if password will be changed
                    TLString newPasswordHash = null;
                    var newAlgo = newSettings83.NewAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;
                    if (newAlgo != null)
                    {
                        if (string.IsNullOrEmpty(newSettings83.NewPassword))
                        {
                            return;
                        }

                        newPasswordHash = SRP.GetX(new TLString(newSettings83.NewPassword), newAlgo);

                        newSettings83.NewPasswordHash = SRP.GetVBytes(new TLString(newSettings83.NewPassword), newAlgo);
                    }

                    var currentPasswordHash = password.CurrentPasswordHash ?? TLString.Empty;
                    MTProtoService.UpdatePasswordSettingsAsync(SRP.GetCheck(currentPasswordHash, srpParams.SRPId, srpParams.SRPB, srpParams.CurrentAlgo), newSettings,
                                                               result2 =>
                    {
                        IsWorking = false;
                        MTProtoService.GetPasswordAsync(
                            result3 => BeginOnUIThread(() =>
                        {
                            EventAggregator.Publish(result3);

                            var password84 = result3 as TLPassword84;
                            if (password84 != null)
                            {
                                password84.CurrentPasswordHash = newPasswordHash ?? password84.CurrentPasswordHash;
                            }

                            MessageBox.Show(AppResources.PasswordActive, AppResources.Success, MessageBoxButton.OK);

                            if (password84 != null && (_authorizationForm != null || _secureValues != null))
                            {
                                MTProtoService.GetPasswordSettingsAsync(SRP.GetCheck(password84.CurrentPasswordHash, password84.SRPId, password84.SRPB, password84.CurrentAlgo),
                                                                        result4 => BeginOnUIThread(() =>
                                {
                                    Passport.EnterPasswordViewModel.NavigateToPassportCommon(
                                        result4, password84, new TLString(PasswordViewModel.TempNewPassword),
                                        _authorizationForm, _secureValues,
                                        MTProtoService, StateService, NavigationService);
                                }),
                                                                        error4 => BeginOnUIThread(() =>
                                {
                                    if (error4.TypeEquals(ErrorType.PASSWORD_HASH_INVALID))
                                    {
                                        StateService.AuthorizationForm = _authorizationForm;
                                        StateService.Password          = password84;
                                        NavigationService.UriFor <Passport.EnterPasswordViewModel>().WithParam(x => x.RandomParam, Guid.NewGuid().ToString()).Navigate();
                                    }
                                }));
                            }
                            else
                            {
                                StateService.Password = result3;
                                NavigationService.GoBack();
                            }
                        }),
                            error3 => BeginOnUIThread(() =>
                        {
                            Execute.ShowDebugMessage("account.getPassword error " + error3);
                        }));
                    },
                                                               error2 => BeginOnUIThread(() =>
                    {
                        IsWorking          = false;
                        var messageBuilder = new StringBuilder();
                        //messageBuilder.AppendLine(AppResources.Error);
                        //messageBuilder.AppendLine();
                        messageBuilder.AppendLine("Method: account.updatePasswordSettings");
                        messageBuilder.AppendLine("Result: " + error2);

                        if (TLRPCError.CodeEquals(error2, ErrorCode.FLOOD))
                        {
                            HasError = true;
                            Error    = AppResources.FloodWaitString;
                            Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.FloodWaitString, AppResources.Error, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error2, ErrorCode.INTERNAL))
                        {
                            HasError = true;
                            Error    = AppResources.ServerError;
                            Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.ServerError, MessageBoxButton.OK));
                        }
                        else if (TLRPCError.CodeEquals(error2, ErrorCode.BAD_REQUEST))
                        {
                            if (TLRPCError.TypeEquals(error2, ErrorType.PASSWORD_HASH_INVALID))
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.NEW_PASSWORD_BAD))
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.NEW_SALT_INVALID))
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.EMAIL_INVALID))
                            {
                                HasError = true;
                                Error    = AppResources.EmailInvalid;
                                Telegram.Api.Helpers.Execute.BeginOnUIThread(() => MessageBox.Show(AppResources.EmailInvalid, AppResources.Error, MessageBoxButton.OK));
                            }
                            else if (TLRPCError.TypeEquals(error2, ErrorType.EMAIL_UNCONFIRMED))
                            {
                                HasError = false;
                                Error    = string.Empty;
                                MTProtoService.GetPasswordAsync(
                                    result3 => BeginOnUIThread(() =>
                                {
                                    IsWorking = false;
                                    password  = result3 as TLPassword84;
                                    if (password != null)
                                    {
                                        password.CurrentPasswordHash = currentPasswordHash;             //EMAIL_UNCONFIRMED - new settings are not active yet
                                    }

                                    MessageBox.Show(AppResources.CompletePasswordHint, AppResources.AlmostThere, MessageBoxButton.OK);

                                    if (_authorizationForm != null || _secureValues != null)
                                    {
                                        StateService.AuthorizationForm = _authorizationForm;
                                        StateService.SecureValues      = _secureValues;
                                        StateService.Password          = result3;
                                        StateService.RemoveBackEntry   = true;
                                        NavigationService.UriFor <PasswordViewModel>().Navigate();
                                    }
                                    else
                                    {
                                        StateService.Password = result3;
                                        NavigationService.GoBack();
                                    }
                                }),
                                    error3 => Execute.BeginOnUIThread(() =>
                                {
                                    IsWorking = false;
                                    Execute.ShowDebugMessage("account.getPassword error " + error3);
                                }));
                            }
                            else
                            {
                                HasError = true;
                                Error    = string.Format("{0} {1}", error2.Code, error2.Message);
                                Execute.BeginOnUIThread(() => MessageBox.Show(messageBuilder.ToString(), AppResources.Error, MessageBoxButton.OK));
                            }
                        }
                        else
                        {
                            HasError = true;
                            Error    = string.Empty;
                            Execute.ShowDebugMessage("account.updatePasswordSettings error " + error2);
                        }
                    }));
                },
                    error1 => BeginOnUIThread(() =>
                {
                    IsWorking = false;
                }));
            });
        }
        public void Confirm()
        {
            if (_password == null)
            {
                return;
            }
            if (!CanConfirm)
            {
                return;
            }

            var password84 = _password as TLPassword84;

            if (password84 == null)
            {
                return;
            }

            var algo = password84.CurrentAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;

            if (algo == null)
            {
                return;
            }

            IsWorking = true;
#if LOG_REGISTRATION
            TLUtils.WriteLog("auth.checkPassword");
#endif

            BeginOnThreadPool(() =>
            {
                //var passwordHash = Utils.Password.GetHash(_password.CurrentSalt, new TLString(Code));
                var passwordHash = SRP.GetX(new TLString(Code), algo);
                var password     = SRP.GetCheck(passwordHash, password84.SRPId, password84.SRPB, algo);

                MTProtoService.CheckPasswordAsync(
                    password,
                    auth => BeginOnUIThread(() =>
                {
#if LOG_REGISTRATION
                    TLUtils.WriteLog("auth.checkPassword result " + auth);
                    TLUtils.WriteLog("TLUtils.IsLogEnabled=false");
#endif

                    TLUtils.IsLogEnabled = false;
                    TLUtils.LogItems.Clear();

                    var result = MessageBox.Show(
                        AppResources.ConfirmPushMessage,
                        AppResources.ConfirmPushTitle,
                        MessageBoxButton.OKCancel);

                    if (result != MessageBoxResult.OK)
                    {
                        Notifications.Disable();
                    }
                    else
                    {
                        Notifications.Enable();
                    }

                    ConfirmViewModel.UpdateNotificationsAsync(MTProtoService, StateService);

                    MTProtoService.SetInitState();
                    StateService.CurrentUserId = auth.User.Index;
                    StateService.FirstRun      = true;
                    SettingsHelper.SetValue(Constants.IsAuthorizedKey, true);

                    ShellViewModel.Navigate(NavigationService);

                    IsWorking = false;
                }),
                    error => BeginOnUIThread(() =>
                {
#if LOG_REGISTRATION
                    TLUtils.WriteLog("auth.checkPassword error " + error);
#endif
                    IsWorking = false;
                    if (error.TypeEquals(ErrorType.PASSWORD_HASH_INVALID))
                    {
                        MTProtoService.GetPasswordAsync(
                            result1 => BeginOnUIThread(() =>
                        {
                            _password = result1 as TLPassword84;
                            MessageBox.Show(AppResources.PasswordInvalidString, AppResources.Error, MessageBoxButton.OK);
                        }),
                            error1 => BeginOnUIThread(() =>
                        {
                            MessageBox.Show(AppResources.PasswordInvalidString, AppResources.Error, MessageBoxButton.OK);
                        }));
                    }
                    else if (error.CodeEquals(ErrorCode.FLOOD))
                    {
                        MessageBox.Show(AppResources.FloodWaitString + Environment.NewLine + "(" + error.Message + ")", AppResources.Error, MessageBoxButton.OK);
                    }
                    else if (error.TypeEquals(ErrorType.SRP_ID_INVALID))
                    {
                        IsWorking = true;
                        MTProtoService.GetPasswordAsync(
                            result1 => BeginOnUIThread(() =>
                        {
                            IsWorking = false;
                            _password = result1 as TLPassword84;
                            Confirm();
                        }),
                            error1 => BeginOnUIThread(() =>
                        {
                            IsWorking = false;
                        }));
                    }
                    else
                    {
                        Execute.ShowDebugMessage("account.checkPassword error " + error);
                    }
                }));
            });
        }
Ejemplo n.º 15
0
        private byte[] handlePairStepThree(List <TypeValue> requestValue)
        {
            var newSalt = SRP.s();

            return(new byte[0]);
        }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            var c = new SRP();

            c.Run();
        }
        public void Done()
        {
            if (IsWorking)
            {
                return;
            }

            var password84 = PasswordBase as TLPassword84;

            if (password84 == null)
            {
                return;
            }

            var algo = password84.CurrentAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;

            if (algo == null)
            {
                return;
            }

            IsWorking = true;
            BeginOnThreadPool(() =>
            {
                var passwordHash = SRP.GetX(new TLString(_password), algo);

                MTProtoService.GetPasswordSettingsAsync(SRP.GetCheck(passwordHash, password84.SRPId, password84.SRPB, algo),
                                                        result => BeginOnUIThread(() =>
                {
                    IsWorking = false;

                    PasswordViewModel.Password = _password;

                    PasswordBase.CurrentPasswordHash = passwordHash;
                    PasswordBase.Settings            = result;

                    OnSucceded(result);
                }),
                                                        error => BeginOnUIThread(() =>
                {
                    IsWorking = false;
                    if (error.TypeEquals(ErrorType.PASSWORD_HASH_INVALID))
                    {
                        MTProtoService.GetPasswordAsync(
                            result1 => BeginOnUIThread(() =>
                        {
                            PasswordBase = result1 as TLPassword84;
                            MessageBox.Show(AppResources.PasswordInvalidString, AppResources.Error, MessageBoxButton.OK);
                        }),
                            error1 => BeginOnUIThread(() =>
                        {
                            MessageBox.Show(AppResources.PasswordInvalidString, AppResources.Error, MessageBoxButton.OK);
                        }));
                    }
                    else if (error.CodeEquals(ErrorCode.FLOOD))
                    {
                        MessageBox.Show(AppResources.FloodWaitString + Environment.NewLine + "(" + error.Message + ")", AppResources.Error, MessageBoxButton.OK);
                    }
                    else if (error.TypeEquals(ErrorType.SRP_ID_INVALID))
                    {
                        IsWorking = true;
                        MTProtoService.GetPasswordAsync(
                            result1 => BeginOnUIThread(() =>
                        {
                            IsWorking    = false;
                            PasswordBase = result1 as TLPassword84;
                            Done();
                        }),
                            error1 => BeginOnUIThread(() =>
                        {
                            IsWorking = false;
                        }));
                    }
                    else
                    {
                        Execute.ShowDebugMessage("account.checkPassword error " + error);
                    }
                }));
            });
        }
        public static void NavigateToPassportCommon(
            TLPasswordSettings passwordSettings, TLPassword passwordBase, TLString password,
            TLAuthorizationForm authorizationForm, IList <TLSecureValue> secureValues,
            IMTProtoService mtProtoService, IStateService stateService, INavigationService navigationService)
        {
            var passwordSettings83 = passwordSettings as TLPasswordSettings83;

            if (passwordSettings83 == null)
            {
                return;
            }

            var secureSettings = passwordSettings83.SecureSettings;

            passwordBase.Settings = passwordSettings83;

            var password84 = passwordBase as TLPassword84;

            if (password84 == null)
            {
                return;
            }

            var algo = password84.CurrentAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow;

            if (algo == null)
            {
                return;
            }

            //passwordBase.CurrentPasswordHash = Utils.Password.GetHash(passwordBase.CurrentSalt, password);
            var passwordHash = SRP.GetX(password, algo);

            passwordBase.CurrentPasswordHash = passwordHash;

            // create secure_secret
            if (secureSettings == null || TLString.IsNullOrEmpty(secureSettings.SecureSecret))
            {
                AddSecureSecret(passwordSettings, passwordBase, password, authorizationForm, secureValues, mtProtoService, stateService, navigationService);
                return;
            }

            var values = authorizationForm != null ? authorizationForm.Values : secureValues;

            if (values == null)
            {
                return;
            }

            var decrypted = SetSecretAndDecryptSecureValues(passwordSettings83, password, values);

            if (!decrypted)
            {
                Execute.BeginOnUIThread(() =>
                {
                    ShellViewModel.ShowCustomMessageBox(
                        AppResources.PassportDataCorrupted, AppResources.AppName,
                        AppResources.Reset, AppResources.Cancel,
                        dismissed =>
                    {
                        if (dismissed == CustomMessageBoxResult.RightButton)
                        {
                            ResetSecureSecret(passwordSettings, passwordBase, password, authorizationForm, secureValues, mtProtoService, stateService, navigationService);
                            values.Clear();
                        }
                    });
                });

                return;
            }

            stateService.RemoveBackEntry   = true;
            stateService.AuthorizationForm = authorizationForm;
            stateService.SecureValues      = secureValues;
            stateService.Password          = passwordBase;
            Execute.BeginOnUIThread(() =>
            {
                if (authorizationForm != null)
                {
                    navigationService.UriFor <PassportViewModel>().Navigate();
                }
                else if (secureValues != null)
                {
                    navigationService.UriFor <PassportSettingsViewModel>().Navigate();
                }
            });
        }