Truncate p and c before transforming strings to hex

This commit is contained in:
nicolas.dorier 2024-04-24 21:32:14 +09:00
parent dce8c3d066
commit d4bb214536
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
3 changed files with 7 additions and 7 deletions

View File

@ -68,9 +68,9 @@ public class AESKey
}
public bool CheckSunMac([NotNullWhen(true)] string? mac, PICCData piccData, byte[]? payload = null)
{
if (mac is null || !Regex.IsMatch(mac, "[a-f0-9A-F]{16}"))
if (!PICCData.ValidateC(mac))
return false;
return CheckSunMac(mac.HexToBytes(), piccData, payload);
return CheckSunMac(mac[0..16].HexToBytes(), piccData, payload);
}
public byte[] GetSunMac(PICCData piccData, byte[]? payload = null)

View File

@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<Version>1.0.22</Version>
<Version>1.0.23</Version>
</PropertyGroup>
<PropertyGroup>

View File

@ -58,7 +58,7 @@ public record BoltcardPICCData : PICCData
{
if (!ValidateP(p))
return null;
var bytes = encryptionKey.Decrypt(p.HexToBytes());
var bytes = encryptionKey.Decrypt(p[0..32].HexToBytes());
if (!ValidateBoltcardPICCData(bytes))
return null;
return new BoltcardPICCData(PICCData.Create(bytes));
@ -120,8 +120,8 @@ public record PICCData(byte[]? Uid, int? Counter)
return null;
return TryBoltcardDecryptCheck(encryptionKey, authenticationKey, p, c, payload);
}
internal static bool ValidateP(string p) => p != null && Regex.IsMatch(p, "[a-f0-9A-F]{32}");
internal static bool ValidateC(string c) => c != null && Regex.IsMatch(c, "[a-f0-9A-F]{16}");
internal static bool ValidateP([NotNullWhen(true)] string? p) => p is not null && Regex.IsMatch(p, "^[a-f0-9A-F]{32}");
internal static bool ValidateC([NotNullWhen(true)] string? c) => c is not null && Regex.IsMatch(c, "^[a-f0-9A-F]{16}");
/// <summary>
/// Decrypt the PICCData from the Boltcard and check the checksum.
@ -137,7 +137,7 @@ public record PICCData(byte[]? Uid, int? Counter)
if (!ValidateP(p) || !ValidateC(c))
return null;
var bytes = encryptionKey.Decrypt(p.HexToBytes());
var bytes = encryptionKey.Decrypt(p[0..32].HexToBytes());
PICCData piccData;
try
{