private void AddEdit() { this.timerUpdateTotp.Enabled = false; this.groupboxTotp.Text = "TOTP"; this.labelOtp.Text = "000000"; this.totp = null; var addEditForm = new OtpInformation(this.data, this.entry, this.host); var result = addEditForm.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { this.data = OtpAuthUtils.loadData(this.entry); if (this.data != null) { this.ShowCode(); } else { this.Close(); } } else if (this.data == null) { this.Close(); } else { this.ShowCode(); } }
void otpCopyToolStripItem_Click(object sender, EventArgs e) { PwEntry entry; if (this.GetSelectedSingleEntry(out entry)) { OtpAuthData data = OtpAuthUtils.loadData(entry); if (data == null) { if (MessageBox.Show("Must configure TOTP on this entry. Do you want to do this now?", "Not Configured", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ShowOneTimePasswords form = new ShowOneTimePasswords(entry, host); form.ShowDialog(); } } else { var totp = new Totp(data.Key, step: data.Step, mode: data.OtpHashMode, totpSize: data.Size); var text = totp.ComputeTotp().ToString().PadLeft(data.Size, '0'); if (ClipboardUtil.CopyAndMinimize(new KeePassLib.Security.ProtectedString(true, text), true, this.host.MainWindow, entry, this.host.Database)) { this.host.MainWindow.StartClipboardCountdown(); } } } }
private void ShowOneTimePasswords_FormClosing(object sender, FormClosingEventArgs e) { if (increaseHotpAfterClosing) { OtpAuthUtils.increaseHotpCounter(host, data, entry); } }
private void linkLabelLoadUriScanQR_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (scanQRMode) { if (MessageBox.Show(KeeOtp2Statics.MessageBoxScanQrCode, KeeOtp2Statics.OtpInformationScanQr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) { scanQRCode(); } } else { try { this.data = OtpAuthUtils.uriToOtpAuthData(new Uri(textBoxKey.Text)); loadData(); } catch (InvalidBase32FormatException ex) { MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidBase32Format, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (InvalidUriFormat ex) { MessageBox.Show(ex.Message, KeeOtp2Statics.InvalidUriFormat, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(String.Format(KeeOtp2Statics.MessageBoxException, ex.Message), KeeOtp2Statics.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void AddEdit() { this.timerUpdateOtp.Enabled = false; this.groupboxTotp.Text = KeeOtp2Statics.TOTP; this.labelOtp.Text = insertSpaceInMiddle("000000"); this.otp = null; OtpInformation addEditForm = new OtpInformation(this.data, this.entry, this.host); var result = addEditForm.ShowDialog(); if (result == DialogResult.OK) { this.data = OtpAuthUtils.loadData(this.entry); this.otp = OtpAuthUtils.getOtp(this.data); if (this.data != null) { this.ShowCode(); } else { this.Close(); } } else if (this.data == null) { this.Close(); } else { this.ShowCode(); } }
private void timerUpdateTotp_Tick(object sender, EventArgs e) { if (textBoxKey.Text.Length > 0) { try { OtpAuthData data = readData(); OtpBase otp = OtpAuthUtils.getOtp(data); if (data.Type == OtpType.Totp || data.Type == OtpType.Steam) { groupBoxKey.Text = String.Format(KeeOtp2Statics.OtpInformationKeyUriTotpPreview, otp.getTotpString(OtpTime.getTime()), otp.getRemainingSeconds(OtpTime.getTime())); } else if (data.Type == OtpType.Hotp) { groupBoxKey.Text = String.Format(KeeOtp2Statics.OtpInformationKeyUriHotpPreview, otp.getHotpString(data.Counter)); } } catch { groupBoxKey.Text = KeeOtp2Statics.OtpInformationKeyUriInvalid; } } else { groupBoxKey.Text = KeeOtp2Statics.OtpInformationKeyUri; } }
public static OtpAuthData uriToOtpAuthData(Uri uri) { if (uri.Scheme == "otpauth") { OtpAuthData data = new OtpAuthData(); data.Type = (OtpType)Enum.Parse(typeof(OtpType), uri.Host, true); string query = uri.Query; if (query.StartsWith("?")) { query = query.TrimStart('?'); } NameValueCollection parameters = ParseQueryString(query); if (parameters[uriSecretKey] != null) { if (data.Type == OtpType.Totp && parameters[KeeOtp1EncoderParameter] != null) { data.Type = (OtpType)Enum.Parse(typeof(OtpType), parameters[KeeOtp1EncoderParameter], true); } data.Encoding = OtpSecretEncoding.Base32; string secret = correctPlainSecret(parameters[uriSecretKey], data.Encoding); // Validate secret (catch) OtpAuthUtils.validatePlainSecret(secret, data.Encoding); data.SetPlainSecret(secret); if (parameters[uriAlgorithmKey] != null) { data.Algorithm = (OtpHashMode)Enum.Parse(typeof(OtpHashMode), parameters[uriAlgorithmKey], true); } if (data.Type == OtpType.Totp) { data.Period = GetIntOrDefault(parameters, uriPeriodKey, 30); } else if (data.Type == OtpType.Hotp) { data.Counter = GetIntOrDefault(parameters, uriCounterKey, 0); } data.Digits = GetIntOrDefault(parameters, uriDigitsKey, 6); return(data); } else { throw new InvalidUriFormat("The Uri does not contain a secret. A secret is required!"); } } else { throw new InvalidUriFormat("Given Uri does not start with 'otpauth://'!"); } }
private void ShowCode() { this.otp = OtpAuthUtils.getOtp(data); if (data.Type == OtpType.Hotp) { increaseHotpAfterClosing = true; } this.timerUpdateOtp.Enabled = true; }
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 static void NtpClient_ErrorOccurred(object sender, NtpNetworkErrorEventArgs e) { if (!OtpAuthUtils.CheckInternetConnection()) { retryPollCustomNtpServer(CUSTOM_NTP_SERVER_RETRY_DELAY); } else { MessageBox.Show("Polling the NTP Server failed. Please confirm your entered address!\n\nError message:\n" + e.Exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void otpConfigureToolStripItem_Click(object sender, EventArgs e) { PwEntry entry; if (GetSelectedSingleEntry(out entry)) { OtpAuthData data = OtpAuthUtils.loadData(entry); OtpInformation addEditForm = new OtpInformation(data, entry, host); addEditForm.ShowDialog(); } }
private void FormWasShown() { this.data = OtpAuthUtils.loadData(this.entry); if (this.data == null) { this.AddEdit(); } else { ShowCode(); } }
private void linkLabelIncorrectNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (data.Type == OtpType.Totp || data.Type == OtpType.Steam) { Troubleshooting troubleshooting = new Troubleshooting(this.host); troubleshooting.ShowDialog(); } else if (data.Type == OtpType.Hotp) { OtpAuthUtils.increaseHotpCounter(host, data, entry); this.data = OtpAuthUtils.loadData(this.entry); } }
public static bool checkEntryMigratable(PwEntry entry, MigrationMode migrateMode) { switch (migrateMode) { case MigrationMode.KeeOtp1ToBuiltIn: return(OtpAuthUtils.checkKeeOtp1Mode(entry)); case MigrationMode.BuiltInToKeeOtp1: return(OtpAuthUtils.checkBuiltInMode(entry)); default: return(false); } }
private void linkLabelMigrate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (MessageBox.Show(String.Format(KeeOtp2Statics.MessageBoxMigrationReplacePlaceholder, KeeOtp2Ext.KeeOtp1PlaceHolder, string.Format("{0}/{1}", KeeOtp2Ext.BuiltInTotpPlaceHolder, KeeOtp2Ext.BuiltInHotpPlaceHolder)), KeeOtp2Statics.Migration, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (data.Type == OtpType.Totp) { OtpAuthUtils.replacePlaceholder(this.entry, KeeOtp2Ext.KeeOtp1PlaceHolder, KeeOtp2Ext.BuiltInTotpPlaceHolder); } else if (data.Type == OtpType.Hotp) { OtpAuthUtils.replacePlaceholder(this.entry, KeeOtp2Ext.KeeOtp1PlaceHolder, KeeOtp2Ext.BuiltInHotpPlaceHolder); } } checkboxOldKeeOtp.Checked = false; this.DialogResult = DialogResult.OK; }
private void entryContextMenu_Opening(object sender, CancelEventArgs e) { PwEntry[] selectedEntries = this.host.MainWindow.GetSelectedEntries(); bool selectedOne = selectedEntries != null && selectedEntries.Length == 1; if (selectedOne) { bool configured = OtpAuthUtils.checkEntry(selectedEntries[0]); this.EntryContextMenuCopySubItem.Enabled = configured; this.EntryContextMenuShowOtpSubItem.Enabled = configured; } this.EntryContextMenuCopyItem.Enabled = this.EntryContextMenuMainItem.Enabled = this.EntryContextMenuConfigureSubItem.Enabled = this.EntryContextMenuCopySubItem.Enabled = this.EntryContextMenuShowOtpSubItem.Enabled = selectedOne; }
void SprEngine_FilterCompile(object sender, SprEventArgs e) { if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive) { if (e.Text.IndexOf(totpPlaceHolder, StringComparison.InvariantCultureIgnoreCase) >= 0) { OtpAuthData data = OtpAuthUtils.loadData(e.Context.Entry); if (data != null) { var totp = new Totp(data.Key, step: data.Step, mode: data.OtpHashMode, totpSize: data.Size); var text = totp.ComputeTotp().ToString().PadLeft(data.Size, '0'); e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, "{TOTP}", text); } } } }
private void scanQRCode() { Uri uri = null; IBarcodeReader reader = new BarcodeReader(); Bitmap bmpScreenshot; Graphics gfxScreenshot; this.Hide(); foreach (Screen sc in Screen.AllScreens) { bmpScreenshot = new Bitmap(sc.Bounds.Width, sc.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); gfxScreenshot = Graphics.FromImage(bmpScreenshot); gfxScreenshot.CopyFromScreen(sc.Bounds.X, sc.Bounds.Y, 0, 0, sc.Bounds.Size, CopyPixelOperation.SourceCopy); var result = reader.Decode(bmpScreenshot); if (result != null) { if (result.ToString().StartsWith("otpauth")) { uri = new Uri(result.ToString()); } } } this.Show(); if (uri != null) { MessageBox.Show(KeeOtp2Statics.MessageBoxQrCodeFound, KeeOtp2Statics.OtpInformationScanQr, MessageBoxButtons.OK, MessageBoxIcon.Information); this.data = OtpAuthUtils.uriToOtpAuthData(uri); loadData(); } else { if (MessageBox.Show(KeeOtp2Statics.MessageBoxQrCodeNotFound, KeeOtp2Statics.OtpInformationScanQr, MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry) { scanQRCode(); } } }
public ShowQrCode(OtpAuthData data, PwEntry entry, IPluginHost host) { InitializeComponent(); pictureBoxBanner.Image = KeePass.UI.BannerFactory.CreateBanner(pictureBoxBanner.Width, pictureBoxBanner.Height, KeePass.UI.BannerStyle.Default, Resources.qr_white, KeeOtp2Statics.ShowQr, KeeOtp2Statics.ShowQrSubline); this.Icon = host.MainWindow.Icon; this.TopMost = host.MainWindow.TopMost; this.data = data; this.uri = OtpAuthUtils.otpAuthDataToUri(entry, data); this.entry = entry; this.host = host; groupBoxQRCode.Text = KeeOtp2Statics.ShowQrDisclamer; buttonOk.Text = KeeOtp2Statics.OK; copyUriToolTip = new ToolTip(); copyUriToolTip.ToolTipTitle = KeeOtp2Statics.ShowQrCopyUri; copyUriToolTip.IsBalloon = true; string toolTipCopyUri = String.Format(KeeOtp2Statics.ToolTipShowQrCodeCopyUri, this.uri.AbsoluteUri); copyUriToolTip.SetToolTip(buttonCopyUriReload, toolTipCopyUri); reloadToolTip = new ToolTip(); reloadToolTip.ToolTipTitle = KeeOtp2Statics.ShowQr; reloadToolTip.IsBalloon = true; string toolTipReload = KeeOtp2Statics.ToolTipShowQrCodeReload; reloadToolTip.SetToolTip(buttonCopyUriReload, toolTipReload); }
// If built-in {TIMEOTP} placeholder is used, but KeeOtp1 Save Mode is used private void SprEngine_FilterCompilePre(object sender, SprEventArgs e) { if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive) { string currentPlaceHolder = null; if (e.Text.IndexOf(BuiltInTotpPlaceHolder, StringComparison.InvariantCultureIgnoreCase) >= 0) { currentPlaceHolder = BuiltInTotpPlaceHolder; } else if (e.Text.IndexOf(BuiltInHotpPlaceHolder, StringComparison.InvariantCultureIgnoreCase) >= 0) { currentPlaceHolder = BuiltInHotpPlaceHolder; } if (!string.IsNullOrEmpty(currentPlaceHolder)) { PwEntry entry = e.Context.Entry; OtpAuthData data = OtpAuthUtils.loadData(entry); if (data != null) { if (!OtpAuthUtils.checkBuiltInMode(entry) || (OtpAuthUtils.checkEntry(entry) && OtpTime.getOverrideBuiltInTime() && (OtpTime.getTimeType() == OtpTimeType.FixedOffset || OtpTime.getTimeType() == OtpTimeType.CustomNtpServer)) || !data.Proprietary) { OtpBase otp = OtpAuthUtils.getOtp(data); if (data.Type == OtpType.Totp || data.Type == OtpType.Steam) { e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, currentPlaceHolder, otp.getTotpString(OtpTime.getTime())); } else if (data.Type == OtpType.Hotp) { e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, currentPlaceHolder, otp.getHotpString(data.Counter)); OtpAuthUtils.increaseHotpCounter(host, data, entry); } } } } } }
private void otpCopyToolStripItem_Click(object sender, EventArgs e) { PwEntry entry; if (this.GetSelectedSingleEntry(out entry)) { OtpAuthData data = OtpAuthUtils.loadData(entry); if (data == null) { if (MessageBox.Show(KeeOtp2Statics.MessageBoxOtpNotConfigured, KeeOtp2Statics.PluginName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ShowOneTimePasswords form = new ShowOneTimePasswords(entry, host); form.ShowDialog(); } } else { OtpBase otp = OtpAuthUtils.getOtp(data); if (data.Type == OtpType.Totp) { if (ClipboardUtil.CopyAndMinimize(new ProtectedString(true, otp.getTotpString(OtpTime.getTime())), true, this.host.MainWindow, entry, this.host.Database)) { this.host.MainWindow.StartClipboardCountdown(); } } else if (data.Type == OtpType.Hotp) { if (ClipboardUtil.CopyAndMinimize(new ProtectedString(true, otp.getHotpString(data.Counter)), true, this.host.MainWindow, entry, this.host.Database)) { this.host.MainWindow.StartClipboardCountdown(); } OtpAuthUtils.increaseHotpCounter(host, data, entry); } } } }
private void SprEngine_FilterCompile(object sender, SprEventArgs e) { if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive) { if (e.Text.IndexOf(KeeOtp1PlaceHolder, StringComparison.InvariantCultureIgnoreCase) >= 0) { PwEntry entry = e.Context.Entry; OtpAuthData data = OtpAuthUtils.loadData(entry); if (data != null) { OtpBase otp = OtpAuthUtils.getOtp(data); if (data.Type == OtpType.Totp || data.Type == OtpType.Steam) { e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, KeeOtp1PlaceHolder, otp.getTotpString(OtpTime.getTime())); } else if (data.Type == OtpType.Hotp) { e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, KeeOtp1PlaceHolder, otp.getHotpString(data.Counter)); OtpAuthUtils.increaseHotpCounter(host, data, entry); } } } } }
private OtpAuthData readData(bool skipType = false) { if (OtpAuthUtils.checkUriString(textBoxKey.Text)) { OtpAuthData data = OtpAuthUtils.uriToOtpAuthData(new Uri(textBoxKey.Text)); if (this.data != null) { data.loadedFields = this.data.loadedFields; } return(data); } else { OtpAuthData data = new OtpAuthData(); if (this.data != null) { data = (OtpAuthData)this.data.Clone(); } string secret = textBoxKey.Text.Replace(" ", string.Empty).Replace("-", string.Empty); if (string.IsNullOrEmpty(this.textBoxKey.Text)) { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationMissingSecret); } if (checkBoxCustomSettings.Checked) { if (this.radioButtonBase32.Checked) { data.Encoding = OtpSecretEncoding.Base32; } else if (this.radioButtonBase64.Checked) { data.Encoding = OtpSecretEncoding.Base64; } else if (this.radioButtonHex.Checked) { data.Encoding = OtpSecretEncoding.Hex; } else if (this.radioButtonUtf8.Checked) { data.Encoding = OtpSecretEncoding.UTF8; } } // Secret validation, will throw an error if invalid secret = OtpAuthUtils.correctPlainSecret(secret, data.Encoding); OtpAuthUtils.validatePlainSecret(secret, data.Encoding); if (checkBoxCustomSettings.Checked) { if (!skipType) { data.Type = comboBoxTypeIndexValue[comboBoxType.SelectedIndex]; } if (data.Type == OtpType.Totp || data.Type == OtpType.Steam) { int period = 30; if (int.TryParse(this.textBoxPeriodCounter.Text, out period)) { if (period <= 0) { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } } else { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } data.Period = period; } else if (data.Type == OtpType.Hotp) { int counter = 0; if (int.TryParse(this.textBoxPeriodCounter.Text, out counter)) { if (counter < 0) { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } } else { throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger); } data.Counter = counter; } data.Digits = comboBoxLengthIndexValue[comboBoxLength.SelectedIndex]; if (this.radioButtonSha1.Checked) { data.Algorithm = OtpHashMode.Sha1; } else if (this.radioButtonSha256.Checked) { data.Algorithm = OtpHashMode.Sha256; } else if (this.radioButtonSha512.Checked) { data.Algorithm = OtpHashMode.Sha512; } data.KeeOtp1Mode = checkboxOldKeeOtp.Checked; } data.SetPlainSecret(secret); return(data); } }
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 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); } }
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; } }