Skip to content

Commit

Permalink
Merge PR #122
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamVe committed Mar 22, 2024
2 parents b062eaf + 703c578 commit 4512205
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ public boolean isSupportedBy(Version version) {
* Support for writing DeviceConfig data to the YubiKey.
*/
public static final Feature<ManagementSession> FEATURE_DEVICE_CONFIG = new Feature.Versioned<>("Device Config", 5, 0, 0);
/**
* Support for device-wide reset.
*/
public static final Feature<ManagementSession> FEATURE_DEVICE_RESET = new Feature.Versioned<>("Device Reset", 5, 6, 0);

// Smart card command constants
private static final byte OTP_INS_CONFIG = 0x01;
private static final byte INS_READ_CONFIG = 0x1d;
private static final byte INS_WRITE_CONFIG = 0x1c;
private static final byte INS_SET_MODE = 0x16;
private static final byte INS_DEVICE_RESET = 0x1f;
private static final byte P1_DEVICE_CONFIG = 0x11;

// OTP command constants
Expand Down Expand Up @@ -144,6 +149,11 @@ void writeConfig(byte[] config) {
void setMode(byte[] data) throws IOException, CommandException {
delegate.sendAndReceive(new Apdu(0, OTP_INS_CONFIG, CMD_DEVICE_CONFIG, 0, data));
}

@Override
void deviceReset() throws IOException, CommandException {
throw new UnsupportedOperationException("deviceReset not supported on YubiKey NEO");
}
};
} else {
backend = new Backend<SmartCardProtocol>(protocol) {
Expand All @@ -162,6 +172,11 @@ void writeConfig(byte[] config) throws IOException, CommandException {
void setMode(byte[] data) throws IOException, CommandException {
delegate.sendAndReceive(new Apdu(0, INS_SET_MODE, P1_DEVICE_CONFIG, 0, data));
}

@Override
void deviceReset() throws IOException, CommandException {
delegate.sendAndReceive(new Apdu(0, INS_DEVICE_RESET, 0, 0, null));
}
};
}
logCtor(connection);
Expand Down Expand Up @@ -200,6 +215,11 @@ void writeConfig(byte[] config) throws IOException, CommandException {
void setMode(byte[] data) throws IOException, CommandException {
delegate.sendAndReceive(CMD_DEVICE_CONFIG, data, null);
}

@Override
void deviceReset() {
throw new UnsupportedOperationException("deviceReset is only available over CCID");
}
};
logCtor(connection);
}
Expand Down Expand Up @@ -229,6 +249,11 @@ void writeConfig(byte[] config) throws IOException {
void setMode(byte[] data) throws IOException {
delegate.sendAndReceive(CTAP_YUBIKEY_DEVICE_CONFIG, data, null);
}

@Override
void deviceReset() {
throw new UnsupportedOperationException("deviceReset is only available over CCID");
}
};
logCtor(connection);
}
Expand Down Expand Up @@ -360,6 +385,20 @@ public void setMode(UsbInterface.Mode mode, byte chalrespTimeout, short autoejec
}
}

/**
* Perform a device-wide reset in Bio Multi-protocol Edition devices.
* <p>
* This functionality requires support for {@link #FEATURE_DEVICE_RESET}.
*
* @throws IOException in case of connection error
* @throws ApduException in case of communication or not supported operation error
*/
public void deviceReset() throws IOException, CommandException {
require(FEATURE_DEVICE_RESET);
backend.deviceReset();
Logger.info(logger, "Device reset");
}

private static abstract class Backend<T extends Closeable> implements Closeable {
protected final T delegate;

Expand All @@ -377,6 +416,8 @@ byte[] readConfig() throws IOException, CommandException {

abstract void setMode(byte[] data) throws IOException, CommandException;

abstract void deviceReset() throws IOException, CommandException;

@Override
public void close() throws IOException {
delegate.close();
Expand Down

0 comments on commit 4512205

Please sign in to comment.