private void backgroundWorkerMigrate_DoWork(object sender, DoWorkEventArgs e) { PwUuid RecycleBinUuid = this.host.Database.RecycleBinUuid; List <PwEntry> entries = new List <PwEntry>(); entries = this.host.MainWindow.ActiveDatabase.RootGroup.GetEntries(true).ToList(); int count = entries.Count; int counter = 0; int succeeded = 0; labelMigrationStatus.Text = String.Format("Loaded {0} entrie(s)!", count); foreach (PwEntry entry in entries) { if (entry.ParentGroup.Uuid != RecycleBinUuid) { if (OtpAuthUtils.checkKeeOtp1Mode(entry)) { if (this.migrateAutoType) { entry.AutoType.DefaultSequence = entry.AutoType.DefaultSequence.Replace("{TOTP}", "{TIMEOTP}"); foreach (KeePassLib.Collections.AutoTypeAssociation ata in entry.AutoType.Associations) { ata.Sequence = ata.Sequence.Replace("{TOTP}", "{TIMEOTP}"); } } OtpAuthData data = OtpAuthUtils.loadData(entry); if (data != null) { OtpAuthUtils.purgeLoadedFields(data, entry); OtpAuthUtils.migrateToBuiltInOtp(data, entry); entry.Touch(true); succeeded++; } else { MessageBox.Show(String.Format("Cant migrate \"{0}\" - \"{1}\"\n(Username: {2})\n\nJust check the format of the \"key\" string.", entry.ParentGroup.Name, entry.Strings.ReadSafe(PwDefs.TitleField), entry.Strings.ReadSafe(PwDefs.UserNameField)), "Migration Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } counter++; labelMigrationStatus.Text = String.Format("Done {0} of {1} entries!", counter, count); } if (succeeded > 0) { this.host.MainWindow.ActiveDatabase.Modified = true; this.host.MainWindow.UpdateUI(false, null, false, null, false, null, true); } }
private void OtpInformation_FormClosing(object sender, FormClosingEventArgs e) { if (this.DialogResult == DialogResult.Cancel) { return; } try { this.data = readData(); this.entry.CreateBackup(this.host.Database); if (this.data.loadedFields != null && !string.IsNullOrEmpty(OtpAuthUtils.getOtpString(this.data))) { OtpAuthUtils.purgeLoadedFields(this.data, this.entry); } if (this.data.KeeOtp1Mode) { OtpAuthUtils.migrateToKeeOtp1String(this.data, this.entry); } else { OtpAuthUtils.migrateToBuiltInOtp(this.data, this.entry); } this.entry.Touch(true); this.host.MainWindow.ActiveDatabase.Modified = true; this.host.MainWindow.UpdateUI(false, null, false, null, false, null, true); } catch (InvalidBase32FormatException ex) { MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidBase32Format, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } catch (InvalidBase64FormatException ex) { MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidBase64Format, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } catch (InvalidHexFormatException ex) { MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidHexFormat, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } catch (InvalidUriFormat ex) { MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidUriFormat, MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Cancel = true; return; } catch (InvalidOtpConfiguration ex) { MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidOtpConfiguration, MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Cancel = true; return; } catch (Exception ex) { MessageBox.Show(String.Format(KeeOtp2Statics.MessageBoxException, ex.Message), KeeOtp2Statics.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; return; } }
private void OtpInformation_FormClosing(object sender, FormClosingEventArgs e) { if (this.DialogResult == System.Windows.Forms.DialogResult.Cancel) { return; } try { int textBoxKeyLength = this.textBoxKey.Text.Length; if (string.IsNullOrEmpty(this.textBoxKey.Text)) { MessageBox.Show("A key must be set", "Missing key", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } if (radioButtonBase32.Checked && !Regex.IsMatch(this.textBoxKey.Text, @"^[A-Z2-7=]+$")) { MessageBox.Show("The key includes illegal characters.\n\nAllowed characters:\nABCDEFGHIJKLMNOPQRSTUVWXYZ234567=\n\nRegex:\n^[A-Z2-7=]+$", "Illegal character found!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } else if (radioButtonBase64.Checked && !Regex.IsMatch(this.textBoxKey.Text, @"^[a-zA-Z0-9\+/]*={0,2}$")) { MessageBox.Show("The key includes illegal characters.\n\nAllowed characters:\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789+/=\n\nRegex:\n^[a-zA-Z0-9\\+/]*={0,2}$", "Illegal character found!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } else if (radioButtonHex.Checked && !Regex.IsMatch(this.textBoxKey.Text, @"^[a-fA-F0-9]+$")) { MessageBox.Show("The key includes illegal characters.\n\nAllowed characters:\nabcdefABCDEF0123456789\n\nRegex:\n^[a-fA-F0-9]+$", "Illegal character found!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } // check Base32 format if (radioButtonBase32.Checked && (textBoxKeyLength % 8 == 2 || textBoxKeyLength % 8 == 4 || textBoxKeyLength % 8 == 5 || textBoxKeyLength % 8 == 7)) { this.textBoxKey.Text += new string('=', 8 - textBoxKeyLength % 8); } else if (radioButtonBase32.Checked && textBoxKeyLength % 8 != 0) { MessageBox.Show("The given format of your key is illegal. If you want to check the format, please read the RFC 4648.\n\nLink:\nhttps://tools.ietf.org/html/rfc4648", "Illegal Base32 format!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } // check Base64 format if (radioButtonBase64.Checked && (textBoxKeyLength % 4 == 2 || textBoxKeyLength % 4 == 3)) { this.textBoxKey.Text += new string('=', 4 - textBoxKeyLength % 4); } else if (radioButtonBase64.Checked && textBoxKeyLength % 8 != 0) { MessageBox.Show("The given format of your key is illegal. If you want to check the format, please read the RFC 4648.\n\nLink:\nhttps://tools.ietf.org/html/rfc4648", "Illegal Base64 format!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } // check Hex format if (radioButtonHex.Checked && textBoxKeyLength % 2 == 1) { MessageBox.Show("The given format of your key is illegal. If you want to check the format, please read the RFC 4648.\n\nLink:\nhttps://tools.ietf.org/html/rfc4648", "Illegal Hex format!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.Cancel = true; return; } if (this.Data == null) { this.Data = new OtpAuthData(); } int step = 30; if (int.TryParse(this.textBoxStep.Text, out step)) { if (step != 30) { if (step <= 0) { this.textBoxStep.Text = "30"; MessageBox.Show("The time step must be a non-zero positive integer. The standard value is 30.", "Invalid time step", MessageBoxButtons.OK, MessageBoxIcon.Information); e.Cancel = true; return; } else if (MessageBox.Show("You have selected a non-standard time step. You should only proceed if you were specifically told to use this time step size.\nDefault Value: 30\n\nDo you wish to proceed?", "Non-standard time step size", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) { e.Cancel = true; return; } } } else { this.textBoxStep.Text = "30"; MessageBox.Show("The time step must be a non-zero positive integer. The standard value is 30.", "Invalid time step", MessageBoxButtons.OK, MessageBoxIcon.Information); e.Cancel = true; return; } // size if (this.radioButtonSix.Checked) { this.Data.Size = 6; } else if (this.radioButtonEight.Checked) { this.Data.Size = 8; } else { this.Data.Size = 6; // default } // step this.Data.Step = Convert.ToInt32(this.textBoxStep.Text); // encoding if (this.radioButtonBase32.Checked) { this.Data.Encoding = OtpSecretEncoding.Base32; } else if (this.radioButtonBase64.Checked) { this.Data.Encoding = OtpSecretEncoding.Base64; } else if (this.radioButtonHex.Checked) { this.Data.Encoding = OtpSecretEncoding.Hex; } else if (this.radioButtonUtf8.Checked) { this.Data.Encoding = OtpSecretEncoding.UTF8; } else { this.Data.Encoding = OtpSecretEncoding.Base32; // default } // hashmode if (this.radioButtonSha1.Checked) { this.Data.OtpHashMode = OtpHashMode.Sha1; } else if (this.radioButtonSha256.Checked) { this.Data.OtpHashMode = OtpHashMode.Sha256; } else if (this.radioButtonSha512.Checked) { this.Data.OtpHashMode = OtpHashMode.Sha512; } else { this.Data.OtpHashMode = OtpHashMode.Sha1; // default } this.Data.SetPlainSecret(this.textBoxKey.Text.Replace(" ", string.Empty).Replace("-", string.Empty)); this.entry = OtpAuthUtils.purgeLoadedFields(this.Data, this.entry); if (checkboxOldKeeOtp.Checked) { this.entry = OtpAuthUtils.migrateToKeeOtp1String(this.Data, this.entry); } else { this.entry = OtpAuthUtils.migrateToBuiltInOtp(this.Data, this.entry); } this.entry.Touch(true); this.host.MainWindow.ActiveDatabase.Modified = true; this.host.MainWindow.UpdateUI(false, null, false, null, false, null, true); } catch { MessageBox.Show("There happened an error. Please check your entered key and your settings!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; return; } }
private void backgroundWorkerMigrate_DoWork(object sender, DoWorkEventArgs e) { PwUuid RecycleBinUuid = this.host.Database.RecycleBinUuid; List <PwEntry> entries = new List <PwEntry>(); entries = this.host.MainWindow.ActiveDatabase.RootGroup.GetEntries(true).ToList(); int count = entries.Count; int counter = 0; int succeeded = 0; labelStatus.Text = String.Format(KeeOtp2Statics.SettingsLoadedNEntries, count); foreach (PwEntry entry in entries) { if (entry.ParentGroup.Uuid != RecycleBinUuid) { if (checkEntryMigratable(entry, currentMigrationProfile.migrationMode)) { OtpAuthData data = OtpAuthUtils.loadData(entry); if (data != null) { entry.CreateBackup(this.host.Database); switch (currentMigrationProfile.migrationMode) { case MigrationMode.KeeOtp1ToBuiltIn: if (!data.isForcedKeeOtp1Mode()) { OtpAuthUtils.migrateToBuiltInOtp(data, entry); if (OtpAuthUtils.loadDataFromKeeOtp1String(entry) != null) { if (removeAfterMigration) { OtpAuthUtils.purgeLoadedFields(data, entry); } } } else { encounteredForcedKeeOtp1Entries = true; } break; case MigrationMode.BuiltInToKeeOtp1: OtpAuthUtils.migrateToKeeOtp1String(data, entry); if (OtpAuthUtils.loadDataFromKeeOtp1String(entry) != null) { if (removeAfterMigration) { OtpAuthUtils.purgeLoadedFields(data, entry); } } break; default: break; } if (migrateAutoType) { foreach (string currentPlaceholder in currentMigrationProfile.findPlaceholder.Values) { OtpAuthUtils.replacePlaceholder(entry, currentPlaceholder, currentMigrationProfile.replacePlaceholder[data.Type]); } } entry.Touch(true); succeeded++; } else { this.Invoke((Action)(() => { if (MessageBox.Show(String.Format(KeeOtp2Statics.MessageBoxCantParseEntry, entry.ParentGroup.Name, entry.Strings.ReadSafe(PwDefs.TitleField), entry.Strings.ReadSafe(PwDefs.UserNameField)), KeeOtp2Statics.Migration, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) { backgroundWorkerMigrate.CancelAsync(); } })); } } } counter++; labelStatus.Text = String.Format(KeeOtp2Statics.SettingsDoneNofNEntries, counter, count); } if (succeeded > 0) { this.host.MainWindow.ActiveDatabase.Modified = true; this.host.MainWindow.UpdateUI(false, null, false, null, false, null, true); } }