Skip to content

Commit

Permalink
Updated handling of Slots (#92)
Browse files Browse the repository at this point in the history
* Update the Slot parameter in the following cmdlets:
  - Remove-YubikeyOTP
  - Set-YubikeyOTP
  - Request-YubikeyOTPChallange
* Added Pester tests for Changed PIVSlot for the PIV Cmdlets
  • Loading branch information
virot authored Dec 25, 2024
1 parent cc82ca3 commit 8415e41
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 176 deletions.
5 changes: 3 additions & 2 deletions Docs/Commands/Remove-YubikeyOTP.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Remove YubiKey OTP slot.
## SYNTAX

```
Remove-YubikeyOTP -Slot <PSObject> [-WhatIf] [-Confirm] [<CommonParameters>]
Remove-YubikeyOTP -Slot <Slot> [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -42,9 +42,10 @@ Removes the OTP configuration from slot 1 (Short press)
Yubikey OTP Slot

```yaml
Type: PSObject
Type: Slot
Parameter Sets: (All)
Aliases:
Accepted values: None, ShortPress, LongPress

Required: True
Position: Named
Expand Down
7 changes: 4 additions & 3 deletions Docs/Commands/Request-YubikeyOTPChallange.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
---
external help file: powershellYK.dll-Help.xml
Module Name: powershellYK
online version:
Expand All @@ -13,7 +13,7 @@ Send Challaenge to YubiKey.
## SYNTAX

```
Request-YubikeyOTPChallange -Slot <PSObject> -Phrase <PSObject> [-YubikeyOTP <Boolean>] [<CommonParameters>]
Request-YubikeyOTPChallange -Slot <Slot> -Phrase <PSObject> [-YubikeyOTP <Boolean>] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -59,9 +59,10 @@ Accept wildcard characters: False
Yubikey OTP Slot
```yaml
Type: PSObject
Type: Slot
Parameter Sets: (All)
Aliases:
Accepted values: None, ShortPress, LongPress

Required: True
Position: Named
Expand Down
11 changes: 6 additions & 5 deletions Docs/Commands/Set-YubikeyOTP.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ Configure OTP slots

### Yubico OTP
```
Set-YubikeyOTP -Slot <PSObject> [-YubicoOTP] [-PublicID <Byte[]>] [-PrivateID <Byte[]>] [-SecretKey <Byte[]>]
Set-YubikeyOTP -Slot <Slot> [-YubicoOTP] [-PublicID <Byte[]>] [-PrivateID <Byte[]>] [-SecretKey <Byte[]>]
[-Upload] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### Static Password
```
Set-YubikeyOTP -Slot <PSObject> [-StaticPassword] -Password <SecureString> [-KeyboardLayout <KeyboardLayout>]
Set-YubikeyOTP -Slot <Slot> [-StaticPassword] -Password <SecureString> [-KeyboardLayout <KeyboardLayout>]
[-AppendCarriageReturn] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### Static Generated Password
```
Set-YubikeyOTP -Slot <PSObject> [-StaticGeneratedPassword] -PasswordLength <Int32>
Set-YubikeyOTP -Slot <Slot> [-StaticGeneratedPassword] -PasswordLength <Int32>
[-KeyboardLayout <KeyboardLayout>] [-AppendCarriageReturn] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### ChallengeResponse
```
Set-YubikeyOTP -Slot <PSObject> [-ChallengeResponse] [-SecretKey <Byte[]>]
Set-YubikeyOTP -Slot <Slot> [-ChallengeResponse] [-SecretKey <Byte[]>]
[-Algorithm <ChallengeResponseAlgorithm>] [-RequireTouch] [-WhatIf] [-Confirm] [<CommonParameters>]
```

Expand Down Expand Up @@ -240,9 +240,10 @@ Accept wildcard characters: False
Yubikey OTP Slot.
```yaml
Type: PSObject
Type: Slot
Parameter Sets: (All)
Aliases:
Accepted values: None, ShortPress, LongPress

Required: True
Position: Named
Expand Down
23 changes: 11 additions & 12 deletions Module/Cmdlets/OTP/RemoveYubikeyOTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ namespace powershellYK.Cmdlets.OTP
[Cmdlet(VerbsCommon.Remove, "YubikeyOTP", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class RemoveYubikeyOTPCommand : Cmdlet
{
[TransformOTPSlot()]
[ValidateOTPSlot()]
[ArgumentCompletions("ShortPress", "LongPress")]
//[ValidateOTPSlot()]
[Parameter(Mandatory = true, ValueFromPipeline = false, HelpMessage = "YubiOTP Slot", ParameterSetName = "Remove")]
public PSObject? Slot { get; set; }
private Slot _slot { get; set; }
public Slot Slot { get; set; }

protected override void BeginProcessing()
{
Expand All @@ -37,16 +34,18 @@ protected override void BeginProcessing()
}
protected override void ProcessRecord()
{
// Set an internal Slot variable to work with.
if (Slot!.BaseObject is Slot)
{
_slot = (Slot)Slot.BaseObject;
}
if (ShouldProcess($"Yubikey OTP {_slot}", "Set"))
if (ShouldProcess($"This will remove the OTP configuration in slot {Slot.ToString("d")} ({Slot}). Proceed?", $"This will remove the OTP configuration in slot {Slot.ToString("d")} ({Slot}). Proceed?", "Warning"))
{
using (var otpSession = new OtpSession((YubiKeyDevice)YubiKeyModule._yubikey!))
{
otpSession.DeleteSlot(_slot);
// Check if the slot is configured, if not, Write Warning and continue
if ((Slot == Slot.ShortPress && !otpSession.IsShortPressConfigured) || (Slot == Slot.LongPress && !otpSession.IsLongPressConfigured))
{
WriteWarning($"Slot {Slot.ToString("d")} ({Slot}) is not configured.");
return;
}
var deleteSlot = otpSession.DeleteSlotConfiguration(Slot);
deleteSlot.Execute();
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions Module/Cmdlets/OTP/RequestYubikeyOTPChallange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ namespace powershellYK.Cmdlets.OTP
[Cmdlet(VerbsLifecycle.Request, "YubikeyOTPChallange")]
public class RequestYubikeyOTPChallangeCommand : Cmdlet
{
[TransformOTPSlot()]
[ValidateOTPSlot()]
[ArgumentCompletions("ShortPress", "LongPress")]
[Parameter(Mandatory = true, ValueFromPipeline = false, HelpMessage = "YubiOTP Slot")]
public PSObject? Slot;
public Slot Slot;
[TransformHexInput()]
[Parameter(Mandatory = true, ValueFromPipeline = false, HelpMessage = "Phrase")]
public PSObject? Phrase;
Expand Down Expand Up @@ -50,7 +47,7 @@ protected override void ProcessRecord()
{
using (var otpSession = new OtpSession((YubiKeyDevice)YubiKeyModule._yubikey!))
{
CalculateChallengeResponse challange = otpSession.CalculateChallengeResponse((Slot)Slot!.BaseObject);
CalculateChallengeResponse challange = otpSession.CalculateChallengeResponse(Slot);
challange = challange.UseChallenge((byte[])Phrase!.BaseObject);
challange.UseYubiOtp(YubikeyOTP);
WriteObject(HexConverter.ByteArrayToString(challange.GetDataBytes().ToArray()));
Expand Down
24 changes: 6 additions & 18 deletions Module/Cmdlets/OTP/SetYubikeyOTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ namespace powershellYK.Cmdlets.OTP
[Cmdlet(VerbsCommon.Set, "YubikeyOTP", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class SetYubikeyOTPCommand : PSCmdlet
{
[TransformOTPSlot()]
[ValidateOTPSlot()]
[ArgumentCompletions("ShortPress", "LongPress")]
[Parameter(Mandatory = true, ValueFromPipeline = false, HelpMessage = "Yubikey OTP Slot")]
public PSObject? Slot { get; set; }
public Slot Slot { get; set; }
[Parameter(Mandatory = false, ValueFromPipeline = false, HelpMessage = "Allows configuration with all defaults", ParameterSetName = "Yubico OTP")]
public SwitchParameter YubicoOTP { get; set; }
[Parameter(Mandatory = false, ValueFromPipeline = false, HelpMessage = "Allows configuration with all defaults", ParameterSetName = "Static Password")]
Expand Down Expand Up @@ -56,11 +53,6 @@ public class SetYubikeyOTPCommand : PSCmdlet
[Parameter(Mandatory = false, ValueFromPipeline = false, HelpMessage = "Require Touch", ParameterSetName = "ChallengeResponse")]
public SwitchParameter RequireTouch { get; set; }


private Slot _slot { get; set; }



protected override void BeginProcessing()
{
if (YubiKeyModule._yubikey is null)
Expand All @@ -82,20 +74,16 @@ protected override void ProcessRecord()
{
using (var otpSession = new OtpSession((YubiKeyDevice)YubiKeyModule._yubikey!))
{
if (Slot!.BaseObject is Slot)
{
_slot = (Slot)Slot.BaseObject;
}
WriteDebug($"Working with {ParameterSetName}");
if ((_slot == Yubico.YubiKey.Otp.Slot.ShortPress && !otpSession.IsShortPressConfigured) || (_slot == Yubico.YubiKey.Otp.Slot.LongPress && !otpSession.IsLongPressConfigured) || ShouldProcess($"Yubikey OTP {_slot}", "Set"))
if ((Slot == Yubico.YubiKey.Otp.Slot.ShortPress && !otpSession.IsShortPressConfigured) || (Slot == Yubico.YubiKey.Otp.Slot.LongPress && !otpSession.IsLongPressConfigured) || ShouldProcess($"Yubikey OTP {Slot}", "Set"))
{
switch (ParameterSetName)
{
case "Yubico OTP":
Memory<byte> _publicID = new Memory<byte>(new byte[6]);
Memory<byte> _privateID = new Memory<byte>(new byte[6]);
Memory<byte> _secretKey = new Memory<byte>(new byte[16]);
ConfigureYubicoOtp configureyubicoOtp = otpSession.ConfigureYubicoOtp(_slot);
ConfigureYubicoOtp configureyubicoOtp = otpSession.ConfigureYubicoOtp(Slot);
int? serial = YubiKeyModule._yubikey!.SerialNumber;
if (PublicID is null)
{
Expand Down Expand Up @@ -139,7 +127,7 @@ protected override void ProcessRecord()
break;

case "Static Password":
ConfigureStaticPassword staticpassword = otpSession.ConfigureStaticPassword(_slot);
ConfigureStaticPassword staticpassword = otpSession.ConfigureStaticPassword(Slot);
staticpassword = staticpassword.WithKeyboard(KeyboardLayout);
staticpassword = staticpassword.SetPassword((Marshal.PtrToStringUni(Marshal.SecureStringToGlobalAllocUnicode(Password!))!).AsMemory());
if (AppendCarriageReturn.IsPresent)
Expand All @@ -149,7 +137,7 @@ protected override void ProcessRecord()
staticpassword.Execute();
break;
case "Static Generated Password":
ConfigureStaticPassword staticgenpassword = otpSession.ConfigureStaticPassword(_slot);
ConfigureStaticPassword staticgenpassword = otpSession.ConfigureStaticPassword(Slot);
Memory<char> generatedPassword = new Memory<char>(new char[PasswordLength]);
staticgenpassword = staticgenpassword.WithKeyboard(KeyboardLayout);
staticgenpassword = staticgenpassword.GeneratePassword(generatedPassword);
Expand All @@ -161,7 +149,7 @@ protected override void ProcessRecord()
break;
case "ChallengeResponse":
Memory<byte> _CRsecretKey = new Memory<byte>(new byte[20]);
ConfigureChallengeResponse configureCR = otpSession.ConfigureChallengeResponse(_slot);
ConfigureChallengeResponse configureCR = otpSession.ConfigureChallengeResponse(Slot);
if (SecretKey is null)
{
configureCR = configureCR.GenerateKey(_CRsecretKey);
Expand Down
36 changes: 0 additions & 36 deletions Module/support/transform/TransformOTPSlot.cs

This file was deleted.

72 changes: 0 additions & 72 deletions Module/support/transform/TransformPivSlot.cs

This file was deleted.

19 changes: 0 additions & 19 deletions Module/support/validators/ValidateOTPSlot.cs

This file was deleted.

5 changes: 1 addition & 4 deletions Module/types/PIVSlot.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using powershellYK.support;
using System.Management.Automation;
using System.Security.Cryptography.X509Certificates;
using Yubico.YubiKey.Piv;
using Yubico.YubiKey.Piv;

namespace powershellYK.PIV
{
Expand Down
Loading

0 comments on commit 8415e41

Please sign in to comment.