Skip to content

Commit

Permalink
Refactoring of paged device info
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisDyallo committed May 16, 2024
1 parent c045a87 commit dff6fb6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 40 deletions.
12 changes: 4 additions & 8 deletions Yubico.YubiKey/src/Yubico/YubiKey/DeviceInfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ internal static class DeviceInfoHelper
/// paging through the data as needed until all configuration data is retrieved.
/// This method processes the responses, accumulating TLV-encoded data into a single dictionary.
/// </summary>
/// <typeparam name="T">The type of the YubiKey response which must include data.</typeparam>
/// <typeparam name="TCommand">The specific type of IGetPagedDeviceInfoCommand, e.g. GetPagedDeviceInfoCommand, which will then allow for returning the appropriate response.</typeparam>
/// <param name="connection">The connection interface to communicate with a YubiKey.</param>
/// <param name="command">The command to be sent to the YubiKey. This command should be capable of handling pagination.</param>
/// <returns>A YubiKeyDeviceInfo object containing all relevant device information.</returns>
/// <exception cref="InvalidOperationException">Thrown when the command fails to retrieve successful response statuses from the YubiKey.</exception>
public static YubiKeyDeviceInfo GetDeviceInfo<T>(
IYubiKeyConnection connection,
IPagedGetDeviceInfoCommand<T> command)
where T : IYubiKeyResponseWithData<Dictionary<int, ReadOnlyMemory<byte>>>
public static YubiKeyDeviceInfo GetDeviceInfo<TCommand>(IYubiKeyConnection connection)
where TCommand : IGetPagedDeviceInfoCommand<IYubiKeyResponseWithData<Dictionary<int, ReadOnlyMemory<byte>>>>, new()
{
Logger log = Log.GetLogger();

Expand All @@ -45,8 +42,7 @@ public static YubiKeyDeviceInfo GetDeviceInfo<T>(
bool hasMoreData = true;
while (hasMoreData)
{
command.Page = (byte)page++;
T response = connection.SendCommand(command);
IYubiKeyResponseWithData<Dictionary<int, ReadOnlyMemory<byte>>> response = connection.SendCommand(new TCommand {Page = (byte)page++});
if (response.Status == ResponseStatus.Success)
{
Dictionary<int, ReadOnlyMemory<byte>> tlvData = response.GetData();
Expand Down
4 changes: 2 additions & 2 deletions Yubico.YubiKey/src/Yubico/YubiKey/FidoDeviceInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using Yubico.Core.Devices.Hid;
using Yubico.Core.Logging;
using Yubico.YubiKey.DeviceExtensions;
using Yubico.YubiKey.Management.Commands;
using Yubico.YubiKey.U2f.Commands;

namespace Yubico.YubiKey
{
Expand Down Expand Up @@ -74,7 +74,7 @@ private static bool TryGetDeviceInfoFromFido(IHidDevice device,
{
log.LogInformation("Attempting to read device info via the FIDO interface management command.");
using var connection = new FidoConnection(device);
yubiKeyDeviceInfo = DeviceInfoHelper.GetDeviceInfo(connection, new GetPagedDeviceInfoCommand());
yubiKeyDeviceInfo = DeviceInfoHelper.GetDeviceInfo<GetPagedDeviceInfoCommand>(connection);

log.LogInformation("Successfully read device info via FIDO interface management command.");
return true;
Expand Down
13 changes: 13 additions & 0 deletions Yubico.YubiKey/src/Yubico/YubiKey/IGetPagedDeviceInfoCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;

namespace Yubico.YubiKey
{
public interface IGetPagedDeviceInfoCommand<out T> : IYubiKeyCommand<T>
where T : IYubiKeyResponseWithData<Dictionary<int, ReadOnlyMemory<byte>>>
{
public byte Page { get; set; }

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Yubico.Core.Devices.Hid;
using Yubico.Core.Logging;
using Yubico.YubiKey.DeviceExtensions;
using Yubico.YubiKey.Otp.Commands;

namespace Yubico.YubiKey
{
Expand Down Expand Up @@ -74,7 +75,7 @@ private static bool TryGetDeviceInfoFromKeyboard(IHidDevice device, [MaybeNullWh
{
log.LogInformation("Attempting to read device info via the management command over the keyboard interface.");
using var connection = new KeyboardConnection(device);
yubiKeyDeviceInfo = DeviceInfoHelper.GetDeviceInfo(connection, new Management.Commands.GetPagedDeviceInfoCommand());
yubiKeyDeviceInfo = DeviceInfoHelper.GetDeviceInfo<GetPagedDeviceInfoCommand>(connection);
//TODO Handle exceptions?
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Yubico.YubiKey.Management.Commands
/// <remarks>
/// This class has a corresponding partner class <see cref="GetDeviceInfoResponse"/>
/// </remarks>
public sealed class GetPagedDeviceInfoCommand : IPagedGetDeviceInfoCommand<GetPagedDeviceInfoResponse>
public sealed class GetPagedDeviceInfoCommand : IGetPagedDeviceInfoCommand<GetPagedDeviceInfoResponse>
{
private const byte GetDeviceInfoInstruction = 0x1D;
public byte Page { get; set; }
Expand Down Expand Up @@ -56,20 +56,5 @@ public GetPagedDeviceInfoCommand()
public GetPagedDeviceInfoResponse CreateResponseForApdu(ResponseApdu responseApdu) =>
new GetPagedDeviceInfoResponse(responseApdu);
}

/// <summary>
/// TODO
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IPagedGetDeviceInfoCommand<T> : IYubiKeyCommand<T>
where T : IYubiKeyResponseWithData<Dictionary<int, ReadOnlyMemory<byte>>>
{
public byte Page { get; set; }
}

// public interface IPagedGetDeviceInfoCommand : IYubiKeyCommand<IYubiKeyResponseWithData<Dictionary<int, ReadOnlyMemory<byte>>>>
// {
// public byte Page { get; set; }
// }
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
// limitations under the License.

using Yubico.Core.Iso7816;
using Yubico.YubiKey.Management.Commands;


namespace Yubico.YubiKey.Otp.Commands
{
/// <summary>
/// Gets detailed information about the YubiKey and its current configuration.
/// </summary>
/// <remarks>
/// This class has a corresponding partner class <see cref="GetDeviceInfoResponse"/>
/// This class has a corresponding partner class <see cref="GetPagedDeviceInfoResponse"/>
/// </remarks>
public class PagedGetPagedDeviceInfoCommand : IPagedGetDeviceInfoCommand<GetPagedDeviceInfoResponse>
public class GetPagedDeviceInfoCommand : IGetPagedDeviceInfoCommand<GetPagedDeviceInfoResponse>
{
/// <summary>
/// Gets the YubiKeyApplication to which this command belongs.
Expand All @@ -36,9 +36,9 @@ public class PagedGetPagedDeviceInfoCommand : IPagedGetDeviceInfoCommand<GetPage
public byte Page { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="PagedGetPagedDeviceInfoCommand"/> class.
/// Initializes a new instance of the <see cref="GetPagedDeviceInfoCommand"/> class.
/// </summary>
public PagedGetPagedDeviceInfoCommand()
public GetPagedDeviceInfoCommand()
{

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Yubico.Core.Devices.SmartCard;
using Yubico.Core.Logging;
using Yubico.YubiKey.DeviceExtensions;
using Yubico.YubiKey.Management.Commands;

namespace Yubico.YubiKey
{
Expand Down Expand Up @@ -77,17 +78,17 @@ public static YubiKeyDeviceInfo GetDeviceInfo(ISmartCardDevice device)
return ykDeviceInfo;
}

private static bool TryGetDeviceInfoFromManagement(ISmartCardDevice device,
[MaybeNullWhen(returnValue: false)]
out YubiKeyDeviceInfo yubiKeyDeviceInfo)
private static bool TryGetDeviceInfoFromManagement(
ISmartCardDevice device,
[MaybeNullWhen(returnValue: false)] out YubiKeyDeviceInfo yubiKeyDeviceInfo)
{
Logger log = Log.GetLogger();

try
{
log.LogInformation("Attempting to read device info via the management application.");
using var connection = new SmartcardConnection(device, YubiKeyApplication.Management);
yubiKeyDeviceInfo = DeviceInfoHelper.GetDeviceInfo(connection, new Management.Commands.GetPagedDeviceInfoCommand());
yubiKeyDeviceInfo = DeviceInfoHelper.GetDeviceInfo<GetPagedDeviceInfoCommand>(connection);

log.LogInformation("Successfully read device info via management application.");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
// limitations under the License.

using Yubico.Core.Iso7816;
using Yubico.YubiKey.Management.Commands;

namespace Yubico.YubiKey.U2f.Commands
{
/// <summary>
/// Gets detailed information about the YubiKey and its current configuration.
/// </summary>
public sealed class PagedGetPagedDeviceInfoCommand : IPagedGetDeviceInfoCommand<GetPagedDeviceInfoResponse>
public sealed class GetPagedDeviceInfoCommand : IGetPagedDeviceInfoCommand<GetPagedDeviceInfoResponse>
{
private const byte GetDeviceInfoInstruction = 0xC2;
public byte Page { get; set; }
Expand All @@ -34,9 +33,9 @@ public sealed class PagedGetPagedDeviceInfoCommand : IPagedGetDeviceInfoCommand<
public YubiKeyApplication Application => YubiKeyApplication.FidoU2f;

/// <summary>
/// Constructs an instance of the <see cref="PagedGetPagedDeviceInfoCommand" /> class.
/// Constructs an instance of the <see cref="GetPagedDeviceInfoCommand" /> class.
/// </summary>
public PagedGetPagedDeviceInfoCommand()
public GetPagedDeviceInfoCommand()
{

}
Expand Down

0 comments on commit dff6fb6

Please sign in to comment.