Skip to content

Commit 7bc2730

Browse files
committed
fix openConnection
1 parent 04297db commit 7bc2730

File tree

7 files changed

+59
-49
lines changed

7 files changed

+59
-49
lines changed

desktop/src/main/java/com/yubico/yubikit/desktop/UsbPidGroup.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022 Yubico.
2+
* Copyright (C) 2022,2024 Yubico.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package com.yubico.yubikit.desktop;
1818

19+
import com.yubico.yubikit.core.Transport;
1920
import com.yubico.yubikit.core.UsbInterface;
2021
import com.yubico.yubikit.core.UsbPid;
2122
import com.yubico.yubikit.core.YubiKeyConnection;
@@ -37,6 +38,7 @@
3738

3839
public class UsbPidGroup {
3940
final UsbPid pid;
41+
4042
private final Map<String, DeviceInfo> infos = new HashMap<>();
4143
private final Map<String, Map<Integer, UsbYubiKeyDevice>> resolved = new HashMap<>();
4244
private final Map<Integer, List<UsbYubiKeyDevice>> unresolved = new HashMap<>();
@@ -51,20 +53,15 @@ public class UsbPidGroup {
5153
}
5254

5355
private String buildKey(DeviceInfo info) {
54-
// TODO
55-
/*
56-
return (
57-
info.serial,
58-
info.version,
59-
info.form_factor,
60-
str(info.supported_capabilities),
61-
info.config.get_bytes(False),
62-
info.is_locked,
63-
info.is_fips,
64-
info.is_sky,
65-
)
66-
*/
67-
return "" + info.getSerialNumber() + info.getVersion() + info.getFormFactor();
56+
return "" +
57+
info.getSerialNumber() +
58+
info.getVersion() +
59+
info.getFormFactor() +
60+
info.getSupportedCapabilities(Transport.USB) +
61+
info.getConfig() +
62+
info.isLocked() +
63+
info.isFips() +
64+
info.isSky();
6865
}
6966

7067
int getUsbInterface(Class<? extends YubiKeyConnection> connectionType) {
@@ -128,7 +125,8 @@ <T extends YubiKeyConnection> T openConnection(String key, Class<T> connectionTy
128125
while (!devices.isEmpty()) {
129126
device = devices.remove(0);
130127
Logger.debug(logger, "Candidate: {}", device);
131-
try (T connection = device.openConnection(connectionType)) {
128+
try {
129+
T connection = device.openConnection(connectionType);
132130
DeviceInfo info = DeviceUtil.readInfo(connection, pid);
133131
String deviceKey = buildKey(info);
134132
if (infos.containsKey(deviceKey)) {
@@ -137,14 +135,12 @@ <T extends YubiKeyConnection> T openConnection(String key, Class<T> connectionTy
137135
}
138136
resolved.get(deviceKey).put(usbInterface, device);
139137
if (deviceKey.equals(key)) {
140-
return device.openConnection(connectionType);
138+
return connection;
141139
} else if (pid.type == YubiKeyType.NEO && devices.isEmpty()) {
142140
Logger.debug(logger, "Resolved last NEO device without serial");
143-
return device.openConnection(connectionType);
141+
return connection;
144142
}
145143
}
146-
147-
return connection;
148144
} catch (IOException e) {
149145
Logger.error(logger, "Failed opening candidate device: ", e);
150146
failed.add(device);

desktop/src/main/java/com/yubico/yubikit/desktop/hid/HidFidoConnection.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2022 Yubico.
2+
* Copyright (C) 2020-2022,2024 Yubico.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,10 @@
1616
package com.yubico.yubikit.desktop.hid;
1717

1818
import com.yubico.yubikit.core.fido.FidoConnection;
19+
import com.yubico.yubikit.core.internal.Logger;
1920

2021
import org.hid4java.HidDevice;
22+
import org.slf4j.LoggerFactory;
2123

2224
import java.io.IOException;
2325

@@ -26,16 +28,24 @@ public class HidFidoConnection implements FidoConnection {
2628

2729
private final HidDevice hidDevice;
2830

31+
private final org.slf4j.Logger logger = LoggerFactory.getLogger(HidFidoConnection.class);
32+
2933
public HidFidoConnection(HidDevice hidDevice) throws IOException {
30-
if (hidDevice.isOpen()) {
34+
Logger.debug(logger, "Opening HID FIDO connection");
35+
36+
if (!hidDevice.isClosed()) {
3137
throw new IOException("Device already open");
3238
}
33-
hidDevice.open();
39+
40+
if (!hidDevice.open()) {
41+
throw new IOException("Failure opening device");
42+
}
3443
this.hidDevice = hidDevice;
3544
}
3645

3746
@Override
3847
public void close() {
48+
Logger.debug(logger, "Closing HID FIDO connection");
3949
hidDevice.close();
4050
}
4151

@@ -44,7 +54,7 @@ public void send(byte[] packet) throws IOException {
4454
int sent = hidDevice.write(packet, packet.length, (byte) 0);
4555
if (sent < 0) {
4656
throw new IOException(hidDevice.getLastErrorMessage());
47-
} else if (sent != PACKET_SIZE) {
57+
} else if (sent != PACKET_SIZE + 1) {
4858
throw new IOException("Unexpected amount of data sent: " + sent);
4959
}
5060
}

desktop/src/main/java/com/yubico/yubikit/desktop/hid/HidManager.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@
2626
import java.util.ArrayList;
2727
import java.util.List;
2828

29+
import javax.annotation.Nullable;
30+
2931
public class HidManager {
3032

33+
private static final int YUBICO_VENDOR_ID = 0x1050;
34+
private static final int HID_USAGE_PAGE_OTP = 0x0001;
35+
private static final int HID_USAGE_PAGE_FIDO = 0xf1d0;
36+
37+
3138
private final HidServices services;
3239

3340
private final org.slf4j.Logger logger = LoggerFactory.getLogger(HidManager.class);
@@ -36,34 +43,23 @@ public HidManager() {
3643
services = org.hid4java.HidManager.getHidServices();
3744
}
3845

39-
public List<HidDevice> getDevices() {
46+
public List<HidDevice> getHidDevices(int vendorId, @Nullable Integer usagePage) {
4047
List<HidDevice> yubikeys = new ArrayList<>();
4148
for (org.hid4java.HidDevice device: services.getAttachedHidDevices()) {
42-
if(device.getProductId() == 0x1050) {
49+
if(device.getProductId() == vendorId &&
50+
(usagePage != null && (device.getUsagePage() & 0xffff) == usagePage)) {
4351
yubikeys.add(new HidDevice(device));
4452
}
4553
}
4654
return yubikeys;
4755
}
4856

4957
public List<HidDevice> getOtpDevices() {
50-
List<HidDevice> yubikeys = new ArrayList<>();
51-
for (org.hid4java.HidDevice device: services.getAttachedHidDevices()) {
52-
if(device.getVendorId() == 0x1050 && (device.getUsagePage() & 0xffff) == 1) {
53-
yubikeys.add(new HidDevice(device));
54-
}
55-
}
56-
return yubikeys;
58+
return getHidDevices(YUBICO_VENDOR_ID, HID_USAGE_PAGE_OTP);
5759
}
5860

5961
public List<HidDevice> getFidoDevices() {
60-
List<HidDevice> yubikeys = new ArrayList<>();
61-
for (org.hid4java.HidDevice device: services.getAttachedHidDevices()) {
62-
if(device.getVendorId() == 0x1050 && (device.getUsagePage() & 0xffff) == 0xf1d0) {
63-
yubikeys.add(new HidDevice(device));
64-
}
65-
}
66-
return yubikeys;
62+
return getHidDevices(YUBICO_VENDOR_ID, HID_USAGE_PAGE_FIDO);
6763
}
6864

6965
public void setListener(HidSessionListener listener) {

desktop/src/main/java/com/yubico/yubikit/desktop/hid/HidOtpConnection.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022 Yubico.
2+
* Copyright (C) 2022,2024 Yubico.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,16 +28,21 @@
2828
public class HidOtpConnection implements OtpConnection {
2929
private final HidDevice hidDevice;
3030
private final byte interfaceId;
31+
private final static org.slf4j.Logger logger = LoggerFactory.getLogger(HidOtpConnection.class);
3132

3233
HidOtpConnection(HidDevice hidDevice, byte interfaceId) throws IOException {
33-
org.slf4j.Logger logger = LoggerFactory.getLogger(HidOtpConnection.class);
34-
if (hidDevice.isOpen()) {
34+
Logger.debug(logger, "Opening HID OTP connection");
35+
36+
if (!hidDevice.isClosed()) {
3537
throw new IOException("Device already open");
3638
}
37-
hidDevice.open();
39+
40+
if (!hidDevice.open()) {
41+
throw new IOException("Failure opening device");
42+
}
43+
3844
this.interfaceId = interfaceId;
3945
this.hidDevice = hidDevice;
40-
Logger.debug(logger, "usb connection opened");
4146
}
4247

4348
@Override
@@ -66,6 +71,7 @@ public void send(byte[] report) throws IOException {
6671

6772
@Override
6873
public void close() {
74+
Logger.debug(logger, "Closing HID OTP connection");
6975
hidDevice.close();
7076
}
7177
}

desktop/src/main/java/com/yubico/yubikit/desktop/pcsc/PcscSmartCardConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022 Yubico.
2+
* Copyright (C) 2022,2024 Yubico.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ public PcscSmartCardConnection(Card card) throws IOException {
4343
this.transport = (card.getATR()
4444
.getBytes()[1] & 0xf0) == 0xf0 ? Transport.USB : Transport.NFC;
4545
try {
46+
Logger.debug(logger, "Opening CCID connection");
4647
card.beginExclusive();
4748
this.cardChannel = card.getBasicChannel();
4849
} catch (CardException e) {

testing-desktop/src/test/resources/logback-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</appender>
2626

2727
<!-- Write TRACE (and higher-level) messages to logcat -->
28-
<root level="TRACE">
28+
<root level="DEBUG">
2929
<appender-ref ref="STDOUT" />
3030
</root>
3131
</configuration>

testing/src/main/java/com/yubico/yubikit/testing/TestState.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ protected YubiKeyConnection openConnection() throws IOException {
162162

163163
// common utils
164164
public DeviceInfo getDeviceInfo() {
165-
DeviceInfo deviceInfo = null;
165+
DeviceInfo deviceInfo;
166166
try (YubiKeyConnection connection = openConnection()) {
167167
deviceInfo = DeviceUtil.readInfo(connection, usbPid);
168-
} catch (IOException | UnsupportedOperationException ignoredException) {
168+
} catch (IOException | UnsupportedOperationException exception) {
169+
throw new RuntimeException("Failed to read info", exception);
169170
}
170171

171172
return deviceInfo;

0 commit comments

Comments
 (0)