CUST: - FwUpdateSw: - validate password file before installation

Common/../MeterPwdFile: - New CheckHashedPwdFile to validate password file with skeleton and password level 8 information
This commit is contained in:
Thomas Wiedebusch
2023-10-26 17:57:11 +02:00
parent 5abdbe7354
commit 79fa88c4e7
6 changed files with 94 additions and 20 deletions
@@ -223,6 +223,64 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile
return true;
}
/// <summary>
/// Check the hashed password file:
/// - Checks if password level 8 is unequal to skeleton key
/// - Checks if password level 3 is unequal to password level 8 (level 3 may be overwritten by level 8
/// password hash)
/// - Builds skeleton key password hash and checks if this is placed at password level 3,
/// - Builds password level 8 hash and checks if this is placed at level 8.
/// </summary>
/// <param name="pwdFile"></param>
/// <param name="skeletonKey">SkeletonKey for comparison </param>
/// <param name="pwdLevel8"></param>
/// <returns></returns>
/// <remarks date="2023-Oct-26" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean CheckHashedPwdFile(Byte[] pwdFile, String skeletonKey, String pwdLevel8)
{
var retVal = false;
// the skeletonKey has to be unequally to the passwordLvl8
if (skeletonKey.Equals(pwdLevel8))
{
return false;
}
// compare hashed skeletonKey and passwordLvl8
for (var c = 0; c < MeterPwdDb.HashedPwdLength; c++)
{
// hashed level 3 and level 8 have to be unequal on at least one byte position
if (pwdFile[MeterPwdDb.SkeletonKeyIdx * MeterPwdDb.HashedPwdLength + c] !=
pwdFile[MeterPwdDb.PwdLevel8Idx * MeterPwdDb.HashedPwdLength + c])
{
retVal = true;
break;
}
}
// hash the skeleton and check if equal to hashed level 3
// hash the passwordLvl8 and check if equal to hashed level 8
using (var sha1 = new System.Security.Cryptography.SHA1Managed())
{
var skeletonHash = sha1.ComputeHash(Encoding.UTF8.GetBytes(skeletonKey));
var passwordLvl8Hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(pwdLevel8));
for (var c = 0; c < MeterPwdDb.HashedPwdLength; c++)
{
// if any byte is different the passwords do not match
if (pwdFile[MeterPwdDb.SkeletonKeyIdx * MeterPwdDb.HashedPwdLength + c] != skeletonHash[c] ||
pwdFile[MeterPwdDb.PwdLevel8Idx * MeterPwdDb.HashedPwdLength + c] != passwordLvl8Hash[c])
{
return false;
}
}
}
return retVal;
}
/// <summary>
/// Build skeleton key out of unique processor Id
/// </summary>