Skip to content

Commit a7da433

Browse files
committed
Add NFC ID (decimal and hex) to Get-YubiKey output to support PACS integration
1 parent a1cddb3 commit a7da433

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

Module/Cmdlets/Yubikey/GetYubikey.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
/// .EXAMPLE
77
/// Get-YubiKey
88
/// Returns information about the currently connected YubiKey
9+
///
10+
/// .EXAMPLE
11+
/// Get-YubiKey | Select-Object SerialNumber, NfcIdDec, NfcIdHex
12+
/// Returns information about select attributes of the YubiKey: serial number, NFC ID (decimal) and NFC ID (hex)
913
/// </summary>
1014

1115
using System.Management.Automation; // Windows PowerShell namespace.

Module/support/Converter.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
/// .EXAMPLE
1010
/// $bytes = [byte[]]@(0x1A, 0x2B, 0x3C, 0x4D)
1111
/// $hexString = [powershellYK.support.Converter]::ByteArrayToString($bytes)
12+
///
13+
/// .EXAMPLE
14+
/// $uid = [powershellYK.support.Converter]::SerialToNfcUid(12345678)
15+
/// $hex = [powershellYK.support.Converter]::ByteArrayToString($uid)
16+
/// Returns NFC ID in hex: "274E61BC00614E"
17+
///
18+
/// .EXAMPLE
19+
/// $uid = [powershellYK.support.Converter]::SerialToNfcUid(12345678)
20+
/// $dec = [powershellYK.support.Converter]::NfcUidToDecimal($uid)
21+
/// Returns NFC ID in decimal: 11161651036004942
22+
///
1223
/// </summary>
1324

1425
// Imports
@@ -96,6 +107,33 @@ internal static string AddMissingPadding(string base64)
96107
return base64;
97108
}
98109

110+
// Derive the 7-byte NFC UID from a YubiKey serial number
111+
public static byte[] SerialToNfcUid(uint serial)
112+
{
113+
byte b0 = (byte)(serial >> 24);
114+
byte b1 = (byte)(serial >> 16);
115+
byte b2 = (byte)(serial >> 8);
116+
byte b3 = (byte)serial;
117+
118+
return new byte[] { 0x27, b3, b2, b1, b0, b2, b3 };
119+
}
120+
121+
// Derive the decimal representation of a 7-byte NFC UID
122+
public static ulong NfcUidToDecimal(byte[] uid)
123+
{
124+
if (uid.Length != 7)
125+
{
126+
throw new ArgumentException("NFC UID must be 7 bytes long", nameof(uid));
127+
}
128+
129+
ulong value = 0;
130+
for (int i = 0; i < uid.Length; i++)
131+
{
132+
value = (value << 8) | uid[i];
133+
}
134+
return value;
135+
}
136+
99137
// Convert YubiKey public key to .NET asymmetric algorithm
100138
public static AsymmetricAlgorithm YubiKeyPublicKeyToDotNet(IPublicKey publicKey)
101139
{

Module/types/YubiKeyInformation.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ public class YubikeyInformation
7272
// YubiKey serial number
7373
public int? SerialNumber { get { return YubiKeyDevice.SerialNumber; } }
7474

75+
// NFC UID as decimal
76+
public string? NfcIdDec
77+
{
78+
get
79+
{
80+
if (SerialNumber is null) return null;
81+
byte[] uid = Converter.SerialToNfcUid((uint)SerialNumber.Value);
82+
return Converter.NfcUidToDecimal(uid).ToString();
83+
}
84+
}
85+
86+
// NFC UID as hex
87+
public string? NfcIdHex
88+
{
89+
get
90+
{
91+
if (SerialNumber is null) return null;
92+
byte[] uid = Converter.SerialToNfcUid((uint)SerialNumber.Value);
93+
return Converter.ByteArrayToString(uid);
94+
}
95+
}
96+
7597
// FIPS series status
7698
public bool IsFipsSeries { get { return YubiKeyDevice.IsFipsSeries; } }
7799

0 commit comments

Comments
 (0)