Exemplo n.º 1
0
        public void DecryptMetadata(bool copyToClipboard, bool type)
        {
            var selectedFile = RequestPasswordFile();
            KeyedPasswordFile passFile;

            try
            {
                passFile = passwordManager.DecryptPassword(selectedFile, ConfigManager.Config.PasswordStore.FirstLineOnly);
            }
            catch (GpgError e)
            {
                notificationService.ShowErrorWindow("Password decryption failed: " + e.Message);
                return;
            }
            catch (GpgException e)
            {
                notificationService.ShowErrorWindow("Password decryption failed. " + e.Message);
                return;
            }
            catch (ConfigurationException e)
            {
                notificationService.ShowErrorWindow("Password decryption failed: " + e.Message);
                return;
            }
            catch (Exception e)
            {
                notificationService.ShowErrorWindow($"Password decryption failed: An error occurred: {e.GetType().Name}: {e.Message}");
                return;
            }

            if (copyToClipboard)
            {
                clipboard.Place(passFile.Metadata, TimeSpan.FromSeconds(ConfigManager.Config.Interface.ClipboardTimeout));
                if (ConfigManager.Config.Notifications.Types.PasswordCopied)
                {
                    notificationService.Raise($"The key has been copied to your clipboard.\nIt will be cleared in {ConfigManager.Config.Interface.ClipboardTimeout:0.##} seconds.", Severity.Info);
                }
            }
            if (type)
            {
                KeyboardEmulator.EnterText(passFile.Metadata, ConfigManager.Config.Output.DeadKeys);
            }
        }
Exemplo n.º 2
0
        public void GetKey(bool copyToClipboard, bool type, string key)
        {
            var selectedFile = RequestPasswordFile();

            if (selectedFile == null)
            {
                return;
            }

            KeyedPasswordFile passFile;

            try
            {
                passFile = passwordManager.DecryptPassword(selectedFile, ConfigManager.Config.PasswordStore.FirstLineOnly);
            }
            catch (GpgError e)
            {
                notificationService.ShowErrorWindow("Password decryption failed: " + e.Message);
                return;
            }
            catch (GpgException e)
            {
                notificationService.ShowErrorWindow("Password decryption failed. " + e.Message);
                return;
            }
            catch (ConfigurationException e)
            {
                notificationService.ShowErrorWindow("Password decryption failed: " + e.Message);
                return;
            }
            catch (Exception e)
            {
                notificationService.ShowErrorWindow($"Password decryption failed: An error occurred: {e.GetType().Name}: {e.Message}");
                return;
            }

            if (string.IsNullOrWhiteSpace(key))
            {
                var keys      = passFile.Keys.Select(k => k.Key).Distinct();
                var selection = ShowPasswordMenu(keys);
                if (selection == null)
                {
                    return;
                }
                key = selection;
            }

            var values = passFile.Keys.Where(k => k.Key == key).ToList();

            if (values.Count == 0)
            {
                return;
            }

            string chosenValue;

            if (values.Count > 1)
            {
                chosenValue = ShowPasswordMenu(values.Select(v => v.Value));
                if (chosenValue == null)
                {
                    return;
                }
            }
            else
            {
                chosenValue = values[0].Value;
            }


            if (copyToClipboard)
            {
                clipboard.Place(chosenValue, TimeSpan.FromSeconds(ConfigManager.Config.Interface.ClipboardTimeout));
                if (ConfigManager.Config.Notifications.Types.PasswordCopied)
                {
                    notificationService.Raise($"The key has been copied to your clipboard.\nIt will be cleared in {ConfigManager.Config.Interface.ClipboardTimeout:0.##} seconds.", Severity.Info);
                }
            }
            if (type)
            {
                KeyboardEmulator.EnterText(chosenValue, ConfigManager.Config.Output.DeadKeys);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asks the user to choose a password file, decrypts it, and copies the resulting value to the clipboard.
        /// </summary>
        public void DecryptPassword(bool copyToClipboard, bool typeUsername, bool typePassword)
        {
            var selectedFile = RequestPasswordFile();

            // If the user cancels their selection, the password decryption should be cancelled too.
            if (selectedFile == null)
            {
                return;
            }

            KeyedPasswordFile passFile;

            try
            {
                passFile = passwordManager.DecryptPassword(selectedFile, ConfigManager.Config.PasswordStore.FirstLineOnly);
            }
            catch (GpgError e)
            {
                notificationService.ShowErrorWindow("Password decryption failed: " + e.Message);
                return;
            }
            catch (GpgException e)
            {
                notificationService.ShowErrorWindow("Password decryption failed. " + e.Message);
                return;
            }
            catch (ConfigurationException e)
            {
                notificationService.ShowErrorWindow("Password decryption failed: " + e.Message);
                return;
            }
            catch (Exception e)
            {
                notificationService.ShowErrorWindow($"Password decryption failed: An error occurred: {e.GetType().Name}: {e.Message}");
                return;
            }

            if (copyToClipboard)
            {
                clipboard.Place(passFile.Password, TimeSpan.FromSeconds(ConfigManager.Config.Interface.ClipboardTimeout));
                if (ConfigManager.Config.Notifications.Types.PasswordCopied)
                {
                    notificationService.Raise($"The password has been copied to your clipboard.\nIt will be cleared in {ConfigManager.Config.Interface.ClipboardTimeout:0.##} seconds.", Severity.Info);
                }
            }
            var usernameEntered = false;

            if (typeUsername)
            {
                var username = new PasswordFileParser().GetUsername(passFile);
                if (username != null)
                {
                    KeyboardEmulator.EnterText(username, ConfigManager.Config.Output.DeadKeys);
                    usernameEntered = true;
                }
            }
            if (typePassword)
            {
                // If a username has also been entered, press Tab to switch to the password field.
                if (usernameEntered)
                {
                    KeyboardEmulator.EnterRawText("{TAB}");
                }

                KeyboardEmulator.EnterText(passFile.Password, ConfigManager.Config.Output.DeadKeys);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Asks the user to choose a password file, decrypts it,
        /// generates an OTP code from the secret in the totp field, and copies the resulting value to the clipboard.
        /// </summary>
        public void GenerateTotpCode(bool copyToClipboard, bool typeTotpCode)
        {
            var selectedFile = RequestPasswordFile();

            // If the user cancels their selection, the password decryption should be cancelled too.
            if (selectedFile == null)
            {
                return;
            }

            KeyedPasswordFile passFile;

            try
            {
                passFile = passwordManager.DecryptPassword(selectedFile, true);
            }
            catch (Exception e) when(e is GpgError || e is GpgException || e is ConfigurationException)
            {
                notificationService.ShowErrorWindow("TOTP decryption failed: " + e.Message);
                return;
            }
            catch (Exception e)
            {
                notificationService.ShowErrorWindow($"TOTP decryption failed: An error occurred: {e.GetType().Name}: {e.Message}");
                return;
            }

            String secretKey = null;

            foreach (var k in passFile.Keys)
            {
                // totp: Xxxx4
                if (k.Key == "totp")
                {
                    secretKey = k.Value;
                }

                // otpauth: //totp/account?secret=FxxxX&digits=6
                if (k.Key == "otpauth")
                {
                    var regex   = new Regex("secret=([a-zA-Z0-9]+)&", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    var matches = regex.Match(k.Value);
                    if (matches.Success)
                    {
                        secretKey = matches.Groups[1].Value;
                    }
                }
            }

            var foo = String.Join(",", passFile.Keys.Select(f => f.Key));

            if (secretKey == null)
            {
                notificationService.ShowErrorWindow($"TOTP decryption failed: Failed to find an OTP secret. Keys = ${foo}");
                return;
            }

            var secretKeyBytes = Base32Encoding.ToBytes(secretKey);
            var totp           = new Totp(secretKeyBytes);
            var totpCode       = totp.ComputeTotp();

            if (copyToClipboard)
            {
                ClipboardHelper.Place(totpCode, TimeSpan.FromSeconds(ConfigManager.Config.Interface.ClipboardTimeout));
                if (ConfigManager.Config.Notifications.Types.TotpCopied)
                {
                    notificationService.Raise($"The totp code has been copied to your clipboard.\nIt will be cleared in {ConfigManager.Config.Interface.ClipboardTimeout:0.##} seconds.", Severity.Info);
                }
            }

            if (typeTotpCode)
            {
                KeyboardEmulator.EnterText(totpCode, ConfigManager.Config.Output.DeadKeys);
            }
        }