Skip to content

Commit

Permalink
Softlock: Improvements
Browse files Browse the repository at this point in the history
You can now define how long Softlock will accept the QuickUnlock PIN.
When this timespan is exceeded, you need to provide the complete masterkey to deactivate Softlock.
This is an opt-in feature.

Improved display of information text "SoftLock active. Click topmost form to deactivate"

Closes #12
  • Loading branch information
Rookiestyle committed May 1, 2023
1 parent 66a1fe5 commit 7413e5f
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 14 deletions.
15 changes: 15 additions & 0 deletions src/Config/LockAssistConfig_SL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ internal partial class LockAssistConfig
private const string LockAssistSoftLockSeconds = "LockAssist.SoftLockSeconds";
private const string LockAssistSoftlockOnMinimize = "LockAssist.SoftlockOnMinimize";
private const string LockAssistSoftlockExcludeForms = "LockAssist.SoftlockExcludeForms";
private const string LockAssistSoftlockValidityActive = "LockAssist.SoftlockValidityActive";
private const string LockAssistSoftlockValiditySeconds = "LockAssist.SoftlockValiditySeconds";

public static bool SL_ValidityActive
{
get { return _config.GetBool(LockAssistSoftlockValidityActive, false); }
set { _config.SetBool(LockAssistSoftlockValidityActive, value); }
}

public static int SL_ValiditySeconds
{
get { return (int)_config.GetLong(LockAssistSoftlockValiditySeconds, 1800); }
set { _config.SetLong(LockAssistSoftlockValiditySeconds, value); }
}

public static bool SL_Active
{
get { return _config.GetBool(LockAssistSoftLockActive, true); }
Expand Down
2 changes: 2 additions & 0 deletions src/LockAssist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ private void OptionsFormClosed(object sender, Tools.OptionsFormsEventArgs e)
LockAssistConfig.SL_Active = slsResult.Active;
LockAssistConfig.SL_Seconds = slsResult.Seconds;
LockAssistConfig.SL_OnMinimize = slsResult.SoftLockOnMinimize;
LockAssistConfig.SL_ValidityActive = slsResult.ValidityActive;
LockAssistConfig.SL_ValiditySeconds = slsResult.ValiditySeconds;
_sl.CheckSoftlockMode();
}
#endregion
Expand Down
49 changes: 47 additions & 2 deletions src/OptionsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion src/OptionsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public OptionsForm()
cbSLInterval.Items.Add(PluginTranslate.Minutes);
cbSLInterval.SelectedIndex = 0;

cbSLValidityActive.Text = KPRes.ExpiryTime;

cbSLValidityInterval.Items.Add(PluginTranslate.Seconds);
cbSLValidityInterval.Items.Add(PluginTranslate.Minutes);
cbSLValidityInterval.SelectedIndex = 0;

}

internal void InitEx(LockAssistConfig options)
Expand Down Expand Up @@ -105,7 +111,19 @@ private void Init_SoftLock()
nSLSeconds.Value = LockAssistConfig.SL_Seconds / 60;
}
cbSLOnMinimize.Checked = LockAssistConfig.SL_OnMinimize;
}

if (LockAssistConfig.SL_ValiditySeconds < 60)
{
cbSLValidityInterval.SelectedIndex = 0;
nSLValiditySeconds.Value = LockAssistConfig.SL_ValiditySeconds;
}
else
{
cbSLValidityInterval.SelectedIndex = 1;
nSLValiditySeconds.Value = LockAssistConfig.SL_ValiditySeconds / 60;
}
cbSLValidityActive.Checked = LockAssistConfig.SL_ValidityActive;
}

internal LockAssistConfig GetQuickUnlockOptions()
{
Expand Down Expand Up @@ -137,6 +155,9 @@ internal SoftLockSettings GetSoftLockOptions()
slsResult.Seconds = (int)nSLSeconds.Value;
if (cbSLInterval.SelectedIndex == 1) slsResult.Seconds *= 60;
slsResult.SoftLockOnMinimize = cbSLOnMinimize.Checked;
slsResult.ValidityActive = cbSLValidityActive.Checked;
slsResult.ValiditySeconds = (int)nSLValiditySeconds.Value;
if (cbSLValidityInterval.SelectedIndex == 1) slsResult.ValiditySeconds *= 60;
return slsResult;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Rookiestyle")]
[assembly: AssemblyProduct("KeePass Plugin")]
[assembly: AssemblyCopyright("Copyright © 2021-2022")]
[assembly: AssemblyCopyright("Copyright © 2021-2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,5 +32,5 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.2.2")]
[assembly: AssemblyFileVersion("3.2.2")]
[assembly: AssemblyVersion("3.3")]
[assembly: AssemblyFileVersion("3.3")]
2 changes: 1 addition & 1 deletion src/QuickUnlockKeyProv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private static void RemoveDb(string ioc)
if (bRemoved) PluginDebug.AddInfo("Quick Unlock - Removed Quick Unlock data", 10, "Database: " + ioc);
}

private static ProtectedBinary CreateMasterKeyHash(CompositeKey mk)
internal static ProtectedBinary CreateMasterKeyHash(CompositeKey mk)
{
List<byte[]> keys = new List<byte[]>();
int keysLength = 0;
Expand Down
63 changes: 57 additions & 6 deletions src/SoftLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal struct SoftLockSettings
internal bool Active;
internal int Seconds;
internal bool SoftLockOnMinimize;
internal bool ValidityActive;
internal int ValiditySeconds;
}

internal class SoftLock : IMessageFilter
Expand Down Expand Up @@ -181,9 +183,22 @@ private void OnTimerTick(object sender, EventArgs e)
//if ((GlobalWindowManager.WindowCount > 0) && LockAssistConfig.SL_ExcludeForms.Contains(GlobalWindowManager.TopWindow.GetType().Name)) return;

Application.RemoveMessageFilter(this);

if (RequestFullPassword())
{
DisableSoftlockUsingFullPassword();
}
else
{
DisableSoftlockUsingQU();
}
Application.AddMessageFilter(this);
}

private void DisableSoftlockUsingQU()
{
m_UnlockForm = new UnlockForm();
m_UnlockForm.Text = PluginTranslate.PluginName + " - Softlock";

if (m_UnlockForm.ShowDialog(Program.MainForm) == DialogResult.OK)
{
ProtectedString CheckQuickUnlockKey = m_UnlockForm.QuickUnlockKey;
Expand All @@ -194,11 +209,37 @@ private void OnTimerTick(object sender, EventArgs e)
SetVisibility(true);
if (LockAssistConfig.SL_IsActive) m_SLTimer.Interval = LockAssistConfig.SL_Seconds * 1000;
}
else PluginDebug.AddError("Deactivate SoftLock", "Deactivation failed", "INvalid Quick Unlock key provided");
else PluginDebug.AddError("Deactivate SoftLock", "Deactivation failed", "Invalid Quick Unlock key provided");
}
if (m_UnlockForm != null) m_UnlockForm.Dispose();
m_UnlockForm = null;
Application.AddMessageFilter(this);
}

private void DisableSoftlockUsingFullPassword()
{
using (KeyPromptForm kpf = new KeyPromptForm())
{
kpf.InitEx(Program.MainForm.ActiveDatabase.IOConnectionInfo, false, false);
kpf.Load += (o, e1) => { kpf.Text = "Softlock - " + kpf.Text; };
if (kpf.ShowDialog(Program.MainForm) == DialogResult.OK)
{
ProtectedBinary pbKey = QuickUnlockKeyProv.CreateMasterKeyHash(kpf.CompositeKey);
ProtectedBinary pbKeyDB = QuickUnlockKeyProv.CreateMasterKeyHash(Program.MainForm.ActiveDatabase.MasterKey);
if (pbKey.Equals(pbKeyDB))
{
SetVisibility(true);
if (LockAssistConfig.SL_IsActive) m_SLTimer.Interval = LockAssistConfig.SL_Seconds * 1000;
}
else PluginDebug.AddError("Deactivate SoftLock", "Deactivation failed", "Invalid key provided");
}
}
}

private bool RequestFullPassword()
{
if (!LockAssistConfig.SL_ValidityActive) return false;
var dtCheck = m_dtSoftlockActivation + new TimeSpan(0, 0, LockAssistConfig.SL_ValiditySeconds);
return dtCheck < DateTime.Now;
}

internal void CheckSoftlockMode()
Expand Down Expand Up @@ -226,13 +267,15 @@ private void CheckSoftlockMode(object sender, EventArgs e)
}
}

private DateTime m_dtSoftlockActivation = DateTime.MaxValue;
internal void SetVisibility(bool bVisible)
{
List<string> lMsg = new List<string>();
m_SoftLocked = !bVisible;
lMsg.Add("SoftLock active: " + m_SoftLocked.ToString());
if (m_SoftLocked)
{
m_dtSoftlockActivation = DateTime.Now;
m_dHiddenForms.Clear();
foreach (Form f in Application.OpenForms)
{
Expand Down Expand Up @@ -260,7 +303,11 @@ internal void SetVisibility(bool bVisible)
try { HandleForm(kvp.Key, kvp.Value, !m_SoftLocked, lMsg); }
catch (Exception ex) { lMsg.Add("Ex:" + ex.Message); }
}
if (!m_SoftLocked) m_dHiddenForms.Clear();
if (!m_SoftLocked)
{
m_dHiddenForms.Clear();
m_dtSoftlockActivation = DateTime.MaxValue;
}
PluginDebug.AddInfo("Toggle SoftLock", lMsg.ToArray());
}

Expand Down Expand Up @@ -370,9 +417,13 @@ private void HandlePanel(bool bVisible, Control c)

bHide = new Button();
bHide.Name = buttonName;
//ONLY the button shown on the MainForm shall have a text
//Adjust the text based on the number of hidden forms
if (m_dHiddenForms.Count == 0)
bHide.Text = PluginTranslate.SoftlockModeUnhide; //Display button on MainForm only, set text
else if (Application.OpenForms.Count > 1 && c == Application.OpenForms[Application.OpenForms.Count - 1]) //Display button on topmost form, set text
bHide.Text = PluginTranslate.SoftlockModeUnhide; //Display button on MainForm only, set text
//else if (Application.OpenForms.Count > 1 && c == Application.OpenForms[Application.OpenForms.Count - 1]) //Display button on topmost form, set text
// bHide.Text = PluginTranslate.SoftlockModeUnhideForms;
else if (Application.OpenForms.Count > 1 && c == Program.MainForm) //Display button on topmost form, set text
bHide.Text = PluginTranslate.SoftlockModeUnhideForms;

bHide.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Expand Down
2 changes: 1 addition & 1 deletion version.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
:
LockAssist:3.2.2
LockAssist:3.3
LockAssist!de:5
LockAssist!pt:4
:

0 comments on commit 7413e5f

Please sign in to comment.