private async void RegisterButton_Click(object sender, RoutedEventArgs e) { if (String.IsNullOrEmpty(LoginBox.Text) || String.IsNullOrEmpty(PasswordBox.Password) || String.IsNullOrEmpty(ConfirmBox.Password)) { var errorDialog = new MessageDialog("Fields cannot be empty!"); await DialogHost.Show(errorDialog, "register"); return; } else if (PasswordBox.Password != ConfirmBox.Password) { var errorDialog = new MessageDialog("Passwords are not the same!"); await DialogHost.Show(errorDialog, "register"); return; } PasswordHashingServiceProvider phsp = new PasswordHashingServiceProvider(); UserRegisterPostModel registerModel = new UserRegisterPostModel { Username = LoginBox.Text, Password = await phsp.Client_ComputePasswordForLogin(Encoding.UTF8.GetBytes(LoginBox.Text), Encoding.UTF8.GetBytes(PasswordBox.Password)), PasswordRepeat = await phsp.Client_ComputePasswordForLogin(Encoding.UTF8.GetBytes(LoginBox.Text), Encoding.UTF8.GetBytes(ConfirmBox.Password)) }; try { var result = await UserData.ApiClient.ApiUsersRegisterAsync(registerModel); var dialogResult = await DialogHost.Show(new MessageDialog("You registered successfuly!\nYou can log in now."), "register"); WindowManager.LoginWindow = new LoginWindow(); WindowManager.LoginWindow.Show(); Close(); } catch (ApiException <ProblemDetails> exc) { await DialogHost.Show(new MessageDialog(ApiErrorsBuilder.GetErrorString(exc.Result.Errors)), "register"); } catch (Exception) { await DialogHost.Show(new MessageDialog("Unknown error"), "register"); } }
private async void LoginButton_Click(object sender, RoutedEventArgs e) { UserData.eccKeyPairs = new List <EccKeyPairGetModel>(); if (String.IsNullOrEmpty(LoginBox.Text) || String.IsNullOrEmpty(PasswordBox.Password)) { await DialogHost.Show(new MessageDialog("Fields cannot be empty!"), "login"); return; } PasswordHashingServiceProvider phsp = new PasswordHashingServiceProvider(); UserAuthenticatePostModel loginModel = new UserAuthenticatePostModel() { Username = LoginBox.Text, Password = await phsp.Client_ComputePasswordForLogin(Encoding.UTF8.GetBytes(LoginBox.Text), Encoding.UTF8.GetBytes(PasswordBox.Password)) }; try { var result = await UserData.ApiClient.ApiUsersAuthenticateAsync(loginModel); UserData.AuthToken = result.AuthenticationToken; UserData.UserName = LoginBox.Text; UserData.BytePassword = Encoding.UTF8.GetBytes(PasswordBox.Password); var checkResult = await UserData.ApiClient.ApiEcckeypairsGetAsync(20, 0); if (checkResult.Count == 0) { // Send new keypair EccKeyServiceProvider ecckey = new EccKeyServiceProvider(); var keypair = ecckey.CreateNew_secp256r1_ECKeyPair(); var masterKeyService = new KeyDerivationServiceProvider(); var masterKey = masterKeyService.Pbkdf2Sha256DeriveKeyFromPassword(UserData.BytePassword, 16, 16); var crypto = new SymmetricCryptographyServiceProvider(); var privateKeyEncrypted = crypto.Aes128GcmEncrypt(masterKey.MasterKey, keypair.PrivateKey); var keyPairPostResult = await UserData.ApiClient.ApiEcckeypairsPostAsync(new EccKeyPairPostModel { EncryptedPrivateKey = new EccEncryptedPrivateKeyModel() { Curve = keypair.Curve, AuthenticationTag = privateKeyEncrypted.AuthenticationTag, CipherDescription = privateKeyEncrypted.CipherDescription, Ciphertext = privateKeyEncrypted.Cipthertext, InitializationVector = privateKeyEncrypted.InitializationVector, DerivationDescription = masterKey.DerivationDescription, DerivationSalt = masterKey.DerivationSalt }, PublicKey = new EccPublicKeyModel() { Curve = keypair.Curve, PublicKey = keypair.PublicKey } }); checkResult = await UserData.ApiClient.ApiEcckeypairsGetAsync(20, 0); } // Get user keypairs and decrypt them foreach (var keyPair in checkResult) { UserData.eccKeyPairs.Add(keyPair); var masterKeyService = new KeyDerivationServiceProvider(); var masterKey = masterKeyService.DeriveKeyFromBlob(UserData.BytePassword, new KeyDerivationBlob( keyPair.EncryptedPrivateKey.DerivationDescription, keyPair.EncryptedPrivateKey.DerivationSalt, null )); var crypto = new SymmetricCryptographyServiceProvider(); var privateKeyDecrypted = crypto.DecryptFromSymmetricCipthertextBlob(masterKey.MasterKey, new SymmetricCipthertextBlob ( keyPair.EncryptedPrivateKey.CipherDescription, keyPair.EncryptedPrivateKey.InitializationVector, keyPair.EncryptedPrivateKey.Ciphertext, keyPair.EncryptedPrivateKey.AuthenticationTag ) ); UserData.PrivateKeyDecrypted = privateKeyDecrypted; //CngKey.Import(privateKeyDecrypted, CngKeyBlobFormat.EccPrivateBlob, new CngProvider()); } WindowManager.MainWindow = new MainWindow(); WindowManager.MainWindow.Show(); WindowManager.MainWindow.Notifier.ShowSuccess("You logged in successfully!"); Close(); } catch (ApiException <ProblemDetails> exc) { await DialogHost.Show(new MessageDialog(ApiErrorsBuilder.GetErrorString(exc.Result.Errors)), "login"); } catch (Exception) { await DialogHost.Show(new MessageDialog("Unknown error"), "login"); } }