コード例 #1
0
        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");
            }
        }
コード例 #2
0
        private async void EditServiceButton_Click(object sender, RoutedEventArgs e)
        {
            var service = (Service)((Button)e.Source).DataContext;
            var dialog  = new EditServiceDialog(service, Notifier);
            var result  = await DialogHost.Show(dialog, "root");

            if (result == null)
            {
                return;
            }

            try
            {
                if ((bool)result)
                {
                    var eccService     = new EccKeyServiceProvider();
                    var ServiceKeyPair = eccService.CreateNew_secp256r1_ECKeyPair();

                    var userKeyPair      = UserData.eccKeyPairs[0];
                    var masterKeyService = new KeyDerivationServiceProvider();
                    var crypto           = new SymmetricCryptographyServiceProvider();

                    var derivedKey = eccService.EcdhDervieKey(
                        new EccKeyPairBlob(userKeyPair.PublicKey.Curve, userKeyPair.PublicKey.PublicKey, null),
                        ServiceKeyPair,
                        HashAlgorithmName.SHA256);

                    var masterKey = masterKeyService.Pbkdf2Sha256DeriveKeyFromPassword(derivedKey, 16, 16);

                    var encrypted = crypto.Aes128GcmEncrypt(masterKey.MasterKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(service)));

                    var putModel = new EccCredentialPutModel()
                    {
                        EccDerivationBlob = new EccDerivationBlobModel()
                        {
                            Curve     = ServiceKeyPair.Curve,
                            PublicKey = ServiceKeyPair.PublicKey
                        },
                        EccKeyPairId            = userKeyPair.Id,
                        SymmetricCiphertextBlob = new SymmetricCiphertextBlobModel()
                        {
                            AuthenticationTag     = encrypted.AuthenticationTag,
                            CipherDescription     = encrypted.CipherDescription,
                            Ciphertext            = encrypted.Cipthertext,
                            InitializationVector  = encrypted.InitializationVector,
                            DerivationDescription = masterKey.DerivationDescription,
                            DerivationSalt        = masterKey.DerivationSalt
                        },
                    };

                    await UserData.ApiClient.ApiEcccredentialsPutAsync(service.Id, putModel);
                }
            }
            catch (ApiException <ProblemDetails> exc)
            {
                foreach (var error in ApiErrorsBuilder.GetErrorList(exc.Result.Errors))
                {
                    Notifier.ShowError(error);
                }
            }
            catch (Exception)
            {
                Notifier.ShowError("Unknown error");
            }
        }