diff --git a/Yubico.Core/src/Yubico/Core/CoreCompatSwitches.cs b/Yubico.Core/src/Yubico/Core/CoreCompatSwitches.cs index 82304f7f1..ad43e4126 100644 --- a/Yubico.Core/src/Yubico/Core/CoreCompatSwitches.cs +++ b/Yubico.Core/src/Yubico/Core/CoreCompatSwitches.cs @@ -25,5 +25,11 @@ public static class CoreCompatSwitches /// Default is false / shared. /// public const string OpenSmartCardHandlesExclusively = nameof(OpenSmartCardHandlesExclusively); + + /// + /// If set to true, Yubico.Core will attempt to open HID handles exclusively. False will open shared. + /// Default is false / shared. + /// + public const string OpenHidHandlesExclusively = nameof(OpenHidHandlesExclusively); } } diff --git a/Yubico.Core/src/Yubico/Core/Devices/Hid/MacOSHidIOReportConnection.cs b/Yubico.Core/src/Yubico/Core/Devices/Hid/MacOSHidIOReportConnection.cs index ffc644e29..ddf26b476 100644 --- a/Yubico.Core/src/Yubico/Core/Devices/Hid/MacOSHidIOReportConnection.cs +++ b/Yubico.Core/src/Yubico/Core/Devices/Hid/MacOSHidIOReportConnection.cs @@ -121,7 +121,14 @@ private void SetupConnection() throw new PlatformApiException(ExceptionMessages.IOKitCannotOpenDevice); } - int result = IOHIDDeviceOpen(_deviceHandle, 0x01); + int shareMode = + AppContext.TryGetSwitch( + CoreCompatSwitches.OpenHidHandlesExclusively, + out bool isEnabled) && isEnabled + ? IOKitHidConstants.kIOHIDOptionsTypeSeizeDevice + : IOKitHidConstants.kIOHIDOptionsTypeNone; + + int result = IOHIDDeviceOpen(_deviceHandle, shareMode); _log.IOKitApiCall(nameof(IOHIDDeviceOpen), (kern_return_t)result); if (result != 0) diff --git a/Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidFeatureReportConnection.cs b/Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidFeatureReportConnection.cs index 897c72c23..75cc1c13e 100644 --- a/Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidFeatureReportConnection.cs +++ b/Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidFeatureReportConnection.cs @@ -21,8 +21,9 @@ internal sealed class WindowsHidFeatureReportConnection : IHidConnection { // The SDK device instance that created this connection instance. private readonly WindowsHidDevice _device; + // The underlying Windows HID device used for communication. - private IHidDDevice HidDDevice { get; set; } + private readonly HidDDevice _hidDDevice; public int InputReportSize { get; private set; } public int OutputReportSize { get; private set; } @@ -30,20 +31,20 @@ internal sealed class WindowsHidFeatureReportConnection : IHidConnection internal WindowsHidFeatureReportConnection(WindowsHidDevice device, string path) { _device = device; - HidDDevice = new HidDDevice(path); + _hidDDevice = new HidDDevice(path); SetupConnection(); } private void SetupConnection() { - HidDDevice.OpenFeatureConnection(); - InputReportSize = HidDDevice.FeatureReportByteLength; - OutputReportSize = HidDDevice.FeatureReportByteLength; + _hidDDevice.OpenFeatureConnection(); + InputReportSize = _hidDDevice.FeatureReportByteLength; + OutputReportSize = _hidDDevice.FeatureReportByteLength; } public byte[] GetReport() { - byte[] data = HidDDevice.GetFeatureReport(); + byte[] data = _hidDDevice.GetFeatureReport(); _device.LogDeviceAccessTime(); @@ -52,23 +53,23 @@ public byte[] GetReport() public void SetReport(byte[] report) { - HidDDevice.SetFeatureReport(report); + _hidDDevice.SetFeatureReport(report); _device.LogDeviceAccessTime(); } #region IDisposable Support - private bool disposedValue; // To detect redundant calls + private bool _disposedValue; // To detect redundant calls private void Dispose(bool disposing) { - if (!disposedValue) + if (!_disposedValue) { if (disposing) { - HidDDevice.Dispose(); + _hidDDevice.Dispose(); } - disposedValue = true; + _disposedValue = true; } } diff --git a/Yubico.Core/src/Yubico/Core/Devices/SmartCard/DesktopSmartCardDevice.cs b/Yubico.Core/src/Yubico/Core/Devices/SmartCard/DesktopSmartCardDevice.cs index ea229e9a6..1abd37377 100644 --- a/Yubico.Core/src/Yubico/Core/Devices/SmartCard/DesktopSmartCardDevice.cs +++ b/Yubico.Core/src/Yubico/Core/Devices/SmartCard/DesktopSmartCardDevice.cs @@ -131,13 +131,12 @@ public override ISmartCardConnection Connect() try { - SCARD_SHARE shareMode = SCARD_SHARE.SHARED; - - if (AppContext.TryGetSwitch(CoreCompatSwitches.OpenSmartCardHandlesExclusively, out bool enabled) && - enabled) - { - shareMode = SCARD_SHARE.EXCLUSIVE; - } + SCARD_SHARE shareMode = + AppContext.TryGetSwitch( + CoreCompatSwitches.OpenSmartCardHandlesExclusively, + out bool isEnabled) && isEnabled + ? SCARD_SHARE.EXCLUSIVE + : SCARD_SHARE.SHARED; result = SCardConnect( context, diff --git a/Yubico.Core/src/Yubico/PlatformInterop/macOS/IOKitFramework/IOKitHidConstants.cs b/Yubico.Core/src/Yubico/PlatformInterop/macOS/IOKitFramework/IOKitHidConstants.cs index b272c07eb..80d724fb1 100644 --- a/Yubico.Core/src/Yubico/PlatformInterop/macOS/IOKitFramework/IOKitHidConstants.cs +++ b/Yubico.Core/src/Yubico/PlatformInterop/macOS/IOKitFramework/IOKitHidConstants.cs @@ -27,5 +27,20 @@ internal static class IOKitHidConstants public const int kIOHidReportTypeInput = 0; public const int kIOHidReportTypeOutput = 1; public const int kIOHidReportTypeFeature = 2; + + /// + /// No options specified. + /// + public const int kIOHIDOptionsTypeNone = 0x00; + + /// + /// Seize the device, preventing other applications from accessing it. + /// + public const int kIOHIDOptionsTypeSeizeDevice = 0x01; + + /// + /// Ignore the device, preventing it from being opened. + /// + public const int kIOHIDOptionsTypeIgnore = 0x02; } }