Skip to content

Commit 1fa53aa

Browse files
committed
Added CreateKeyFile
1 parent 02ca07c commit 1fa53aa

File tree

3 files changed

+129
-12
lines changed

3 files changed

+129
-12
lines changed

KPCLib.xunit/pass_d_E8f4pEk.xyz

304 Bytes
Binary file not shown.

PassXYZLib/PxDatabase.cs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,74 @@ public void Open(PassXYZLib.User user)
326326
Open(ioc, cmpKey, logger);
327327
}
328328

329+
/// <summary>
330+
/// Create a database with user information.
331+
/// If the device lock is enabled, we need to set DefaultFolder first.
332+
/// </summary>
333+
/// <param name="user">an instance of PassXYZLib.User</param>
334+
public void New(PassXYZLib.User user)
335+
{
336+
if (user == null) { Debug.Assert(false); throw new ArgumentNullException("PassXYZLib.User"); }
337+
338+
if (user.IsDeviceLockEnabled)
339+
{
340+
if(!CreateKeyFile(user))
341+
{
342+
throw new KeePassLib.Keys.InvalidCompositeKeyException();
343+
}
344+
}
345+
346+
IOConnectionInfo ioc = IOConnectionInfo.FromPath(user.Path);
347+
CompositeKey cmpKey = new CompositeKey();
348+
cmpKey.AddUserKey(new KcpPassword(user.Password));
349+
350+
if (user.IsDeviceLockEnabled)
351+
{
352+
PassXYZ.Utils.Settings.DefaultFolder = PxDataFile.KeyFilePath;
353+
var pxKeyProvider = new PassXYZ.Services.PxKeyProvider(user.Username, false);
354+
if (pxKeyProvider.IsInitialized)
355+
{
356+
KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(new IOConnectionInfo(), false, false);
357+
byte[] pbProvKey = pxKeyProvider.GetKey(ctxKP);
358+
cmpKey.AddUserKey(new KcpCustomKey(pxKeyProvider.Name, pbProvKey, true));
359+
}
360+
else
361+
{
362+
throw new KeePassLib.Keys.InvalidCompositeKeyException();
363+
}
364+
}
365+
New(ioc, cmpKey);
366+
367+
// Set the database name to the current user name
368+
Name = user.Username;
369+
370+
// Set the name of root group to the user name
371+
RootGroup.Name = user.Username;
372+
}
373+
374+
/// <summary>
375+
/// Create a key file from an PxKeyProvider instance or from the system
376+
/// </summary>
377+
/// <param name="kp">a key provider instance. If it is null, the key file is created from the
378+
/// current system.</param>
379+
/// <returns>true - created key file, false - failed to create key file.</returns>
380+
private bool CreateKeyFile(PassXYZLib.User user, PassXYZ.Services.PxKeyProvider kp = null)
381+
{
382+
PassXYZ.Utils.Settings.DefaultFolder = PxDataFile.KeyFilePath;
383+
PassXYZ.Utils.Settings.User.Username = user.Username;
384+
PassXYZ.Services.PxKeyProvider pxKeyProvider;
385+
if (kp == null)
386+
{
387+
pxKeyProvider = new PassXYZ.Services.PxKeyProvider();
388+
return pxKeyProvider.CreateKeyFile(true);
389+
}
390+
else
391+
{
392+
pxKeyProvider = kp;
393+
return pxKeyProvider.CreateKeyFile(false);
394+
}
395+
}
396+
329397
private void EnsureRecycleBin(ref PwGroup pgRecycleBin)
330398
{
331399
if (pgRecycleBin == this.RootGroup)
@@ -688,7 +756,7 @@ public IEnumerable<PwEntry> GetAllEntries()
688756
/// <summary>
689757
/// PasswordDb is a sub-class of PxDatabase. It is a singleton class.
690758
/// </summary>
691-
public sealed class PasswordDb : PxDatabase
759+
public sealed class PasswordDb : PxDatabase
692760
{
693761
private static PasswordDb instance = null;
694762

PassXYZLib/User.cs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,28 @@ public static string GetUserName(string fileName)
7575
public class User
7676
{
7777
private string _username;
78-
public string Username
78+
public string Username
7979
{
8080
get => _username;
81-
set
81+
set
8282
{
8383
_username = value;
8484

85-
// Check whether Device Lock is enabled, but key file may not exist.
86-
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.DataFilePath, GetFileName(true))))
85+
if(_username == null)
8786
{
88-
IsDeviceLockEnabled = true;
87+
IsDeviceLockEnabled = false;
8988
}
90-
else
89+
else
9190
{
92-
IsDeviceLockEnabled = false;
91+
// Check whether Device Lock is enabled, but key file may not exist.
92+
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.DataFilePath, GetFileName(true))))
93+
{
94+
IsDeviceLockEnabled = true;
95+
}
96+
else
97+
{
98+
IsDeviceLockEnabled = false;
99+
}
93100
}
94101
}
95102
}
@@ -100,7 +107,31 @@ public string Username
100107
/// Check whether Device Lock is enabled for this user.
101108
/// <c>true</c> - key file is enabled, <c>false</c> - key file is not enabled
102109
/// </summary>
103-
public bool IsDeviceLockEnabled { get; set; }
110+
public bool IsDeviceLockEnabled { get; set; } = false;
111+
112+
/// <summary>
113+
/// Check whether the key file is existed.
114+
/// true - key file is available, false - key file is not available
115+
/// </summary>
116+
public bool IsUserExist
117+
{
118+
get
119+
{
120+
if (_username == null)
121+
{
122+
return false;
123+
}
124+
125+
if (System.IO.File.Exists(Path))
126+
{
127+
return true;
128+
}
129+
else
130+
{
131+
return false;
132+
}
133+
}
134+
}
104135

105136
/// <summary>
106137
/// Check whether the key file is existed.
@@ -110,10 +141,14 @@ public bool IsKeyFileExist
110141
{
111142
get
112143
{
144+
if (_username == null)
145+
{
146+
return false;
147+
}
148+
113149
if (IsDeviceLockEnabled)
114150
{
115-
var keyFilePath = System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName);
116-
if (System.IO.File.Exists(keyFilePath))
151+
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName)))
117152
{
118153
return true;
119154
}
@@ -141,6 +176,10 @@ public string Path
141176
{
142177
get
143178
{
179+
if (_username == null)
180+
{
181+
return null;
182+
}
144183
return System.IO.Path.Combine(PxDataFile.DataFilePath, FileName);
145184
}
146185
}
@@ -153,7 +192,12 @@ public string KeyFileName
153192
{
154193
get
155194
{
156-
if(IsDeviceLockEnabled)
195+
if (_username == null)
196+
{
197+
return string.Empty;
198+
}
199+
200+
if (IsDeviceLockEnabled)
157201
{
158202
return PxDefs.head_k4xyz + Base58CheckEncoding.ToBase58String(Username) + PxDefs.k4xyz;
159203
}
@@ -172,6 +216,11 @@ public string KeyFileName
172216
/// <returns>Data file name</returns>
173217
private string GetFileName(bool isDeviceLockEnabled = false)
174218
{
219+
if (_username == null)
220+
{
221+
return null;
222+
}
223+
175224
if (isDeviceLockEnabled)
176225
{
177226
return PxDefs.head_data + Base58CheckEncoding.ToBase58String(_username) + PxDefs.xyz;

0 commit comments

Comments
 (0)