Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor tweaks #102

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ online version:
schema: 2.0.0
---

# Get-YubikeyFIDO2Credentials
# Get-YubikeyFIDO2Credential

## SYNOPSIS
Read the FIDO2 discoverable credentials

## SYNTAX

```
Get-YubikeyFIDO2Credentials [<CommonParameters>]
Get-YubikeyFIDO2Credential [<CommonParameters>]
```

## DESCRIPTION
Expand All @@ -23,7 +23,7 @@ Get what FIDO2 credentials that have been saved in the Yubikey.

### Example 1
```powershell
PS C:\> Get-YubikeyFIDO2Credentials
PS C:\> Get-YubikeyFIDO2Credential

Site Name DisplayName
---- ---- -----------
Expand Down
6 changes: 4 additions & 2 deletions Module/Cmdlets/Fido/ConnectYubikeyFIDO2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected override void BeginProcessing()
}

// Check if Connect-YubikeyFIDO2 was called without a PIN (only possible with Yubikey that doesnt have a PIN configured)
/*
if (this.MyInvocation.BoundParameters.ContainsKey("PIN") == false)
{
WriteWarning("FIDO2 has no PIN, please set PIN before continuing:");
Expand All @@ -88,6 +89,7 @@ protected override void BeginProcessing()
}
myPowersShellInstance.Invoke();
}
*/


#if WINDOWS
Expand All @@ -106,12 +108,12 @@ protected override void ProcessRecord()
{
if (fido2Session.AuthenticatorInfo.GetOptionValue(AuthenticatorOptions.clientPin) == OptionValue.False)
{
WriteWarning("Client PIN is not set.");
WriteWarning("Client PIN is not set, see Set-YubiKeyFIDO2PIN.");
return;
}
else if (fido2Session.AuthenticatorInfo.ForcePinChange == true)
{
WriteWarning("YubiKey requires PIN change to continue, see Set-YubikeyFIDO2 -SetPIN.");
WriteWarning("YubiKey requires PIN change to continue, see Set-YubiKeyFIDO2PIN.");
return;
}
if (this.MyInvocation.BoundParameters["PIN"] is not null)
Expand Down
15 changes: 5 additions & 10 deletions Module/Cmdlets/Fido/GetYubikeyFIDO2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ public class GetYubikeyFIDO2Cmdlet : PSCmdlet
{
protected override void BeginProcessing()
{
// If no FIDO2 PIN exists, we need to connect to the FIDO2 application
if (YubiKeyModule._fido2PIN is null)
// Get-YubiKeyFIDO2, does not require authentication, so just make sure we have a YubiKey connected.
if (YubiKeyModule._yubikey is null)
{
WriteDebug("No FIDO2 session has been authenticated, calling Connect-YubikeyFIDO2...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-YubikeyFIDO2");
WriteDebug("No YubiKey selected, calling Connect-Yubikey...");
var myPowersShellInstance = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("Connect-Yubikey");
if (this.MyInvocation.BoundParameters.ContainsKey("InformationAction"))
{
myPowersShellInstance = myPowersShellInstance.AddParameter("InformationAction", this.MyInvocation.BoundParameters["InformationAction"]);
}
myPowersShellInstance.Invoke();
if (YubiKeyModule._fido2PIN is null)
{
throw new Exception("Connect-YubikeyFIDO2 failed to the FIDO2 applet!");
}
WriteDebug($"Successfully connected");
}


// Check if running as Administrator
if (Windows.IsRunningAsAdministrator() == false)
{
Expand All @@ -41,7 +37,6 @@ protected override void ProcessRecord()
{
using (var fido2Session = new Fido2Session((YubiKeyDevice)YubiKeyModule._yubikey!))
{
fido2Session.KeyCollector = YubiKeyModule._KeyCollector.YKKeyCollectorDelegate;

AuthenticatorInfo info = fido2Session.AuthenticatorInfo;
WriteObject(new Information(info));
Expand Down
33 changes: 8 additions & 25 deletions Module/Cmdlets/Fido/GetYubikeyFIDO2Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace powershellYK.Cmdlets.Fido
{
[Cmdlet(VerbsCommon.Get, "YubikeyFIDO2Credentials")]
[Alias("Get-YubikeyFIDO2Credentials")]
[Cmdlet(VerbsCommon.Get, "YubikeyFIDO2Credential")]

public class GetYubikeyFIDO2CredentialsCommand : PSCmdlet
{
Expand Down Expand Up @@ -41,44 +42,26 @@ protected override void ProcessRecord()
using (var fido2Session = new Fido2Session((YubiKeyDevice)YubiKeyModule._yubikey!))
{
fido2Session.KeyCollector = YubiKeyModule._KeyCollector.YKKeyCollectorDelegate;
var relyingParties = fido2Session.EnumerateRelyingParties();

var RelyingParties = fido2Session.EnumerateRelyingParties();

if (!RelyingParties.Any()) // Check if there are no relying parties
if (!relyingParties.Any()) // Check if there are no relying parties
{
WriteWarning("No credentials found on the YubiKey.");
return;
}
else
{
foreach (RelyingParty RelyingParty in RelyingParties)
foreach (RelyingParty relyingParty in relyingParties)
{
WriteDebug($"Enumerating credentials for {RelyingParty.Id}.");
IReadOnlyList<CredentialUserInfo> relayCredentials;
try
{
relayCredentials = fido2Session.EnumerateCredentialsForRelyingParty(RelyingParty);
}
catch (NotSupportedException e)
{
WriteWarning($"Failed to enumerate credentials for {RelyingParty.Id}: {e.Message}, SDK might not support algorithm.");
continue;
}
var relayCredentials = fido2Session.EnumerateCredentialsForRelyingParty(relyingParty);
foreach (CredentialUserInfo user in relayCredentials)
{
Credentials credentials = new Credentials
{
Site = RelyingParty.Id,
Name = user.User.Name,
DisplayName = user.User.DisplayName,
coseKey = user.CredentialPublicKey,
};
WriteObject(credentials);
Credential credential = new Credential(RPId: relyingParty.Id, UserName: user.User.Name, DisplayName: user.User.DisplayName, CredentialID: user.CredentialId);
WriteObject(credential);
}
}
}
}
}

}
}
2 changes: 1 addition & 1 deletion Module/Cmdlets/PIV/RemoveYubikeyPIVKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class RemoveYubiKeyPIVKeyCmdlet : Cmdlet
{
[ArgumentCompletions("\"PIV Authentication\"", "\"Digital Signature\"", "\"Key Management\"", "\"Card Authentication\"", "0x9a", "0x9c", "0x9d", "0x9e")]
[ValidateYubikeyPIVSlot(DontAllowAttestion = true)]
[Parameter(Mandatory = true, ValueFromPipeline = false, HelpMessage = "What slot to move a key from")]
[Parameter(Mandatory = true, ValueFromPipeline = false, HelpMessage = "What slot to remove a key from")]
public PIVSlot Slot { get; set; }
protected override void BeginProcessing()
{
Expand Down
2 changes: 1 addition & 1 deletion Module/powershellYK.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ CmdletsToExport = @(
'Connect-YubikeyFIDO2',
'Enable-YubikeyFIDO2EnterpriseAttestation',
'Get-YubikeyFIDO2',
'Get-YubikeyFIDO2Credentials',
'Get-YubikeyFIDO2Credential',
'Set-YubikeyFIDO2',
'Set-YubikeyFIDO2PIN',
'Reset-YubikeyFIDO2',
Expand Down
29 changes: 24 additions & 5 deletions Module/types/FIDO2-Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@
using System.Management.Automation;
using Yubico.YubiKey.Fido2;
using Yubico.YubiKey.Fido2.Cose;
using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext;

namespace powershellYK.FIDO2
{
public class Credentials
public class Credential
{
public string? Site { get; set; }
public string? Name { get; set; }
public string? DisplayName { get; set; }
// public CredentialId? CredentialID { get; set; }
public string? DisplayName { get; private set; }
public string? UserName { get; private set; }
public string? RPId { get; private set; }
public string? CredID
{
get
{
byte[] credentialIdBytes = CredentialID.Id.ToArray();

string credentialIdBase64 = Convert.ToBase64String(credentialIdBytes);
return credentialIdBase64;
}
}
[Hidden]
public CredentialId CredentialID { get; private set; }
[Hidden]
public CoseKey? coseKey { get; set; }

public Credential(string RPId, string? UserName, string? DisplayName, CredentialId CredentialID)
{
this.RPId = RPId;
this.UserName = UserName;
this.DisplayName = DisplayName;
this.CredentialID = CredentialID;
}
}
}
Loading