Skip to content

Commit

Permalink
Merge PR #124
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamVe committed Mar 22, 2024
2 parents 89c3a99 + fd2d3d1 commit b062eaf
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@

package com.yubico.yubikit.management;

import com.yubico.yubikit.core.application.BadResponseException;
import com.yubico.yubikit.core.internal.Logger;
import com.yubico.yubikit.core.Transport;
import com.yubico.yubikit.core.UsbInterface;
import com.yubico.yubikit.core.Version;
import com.yubico.yubikit.core.YubiKeyConnection;
import com.yubico.yubikit.core.YubiKeyDevice;
import com.yubico.yubikit.core.smartcard.AppId;
import com.yubico.yubikit.core.application.ApplicationNotAvailableException;
import com.yubico.yubikit.core.application.ApplicationSession;
import com.yubico.yubikit.core.application.BadResponseException;
import com.yubico.yubikit.core.application.CommandException;
import com.yubico.yubikit.core.application.Feature;
import com.yubico.yubikit.core.fido.FidoConnection;
import com.yubico.yubikit.core.fido.FidoProtocol;
import com.yubico.yubikit.core.internal.Logger;
import com.yubico.yubikit.core.otp.ChecksumUtils;
import com.yubico.yubikit.core.otp.OtpConnection;
import com.yubico.yubikit.core.otp.OtpProtocol;
import com.yubico.yubikit.core.smartcard.Apdu;
import com.yubico.yubikit.core.smartcard.ApduException;
import com.yubico.yubikit.core.smartcard.AppId;
import com.yubico.yubikit.core.smartcard.SmartCardConnection;
import com.yubico.yubikit.core.smartcard.SmartCardProtocol;
import com.yubico.yubikit.core.util.Callback;
Expand Down Expand Up @@ -149,6 +149,7 @@ void setMode(byte[] data) throws IOException, CommandException {
backend = new Backend<SmartCardProtocol>(protocol) {
@Override
byte[] readConfig(int page) throws IOException, CommandException {
Logger.debug(logger, "Reading config page {}...", page);
return delegate.sendAndReceive(new Apdu(0, INS_READ_CONFIG, page, 0, null));
}

Expand Down Expand Up @@ -182,7 +183,8 @@ public ManagementSession(OtpConnection connection) throws IOException, Applicati
backend = new Backend<OtpProtocol>(protocol) {
@Override
byte[] readConfig(int page) throws IOException, CommandException {
byte[] response = delegate.sendAndReceive(CMD_YK4_CAPABILITIES, int2bytes(page), null);
Logger.debug(logger, "Reading config page {}...", page);
byte[] response = delegate.sendAndReceive(CMD_YK4_CAPABILITIES, pagePayload(page), null);
if (ChecksumUtils.checkCrc(response, response[0] + 1 + 2)) {
return Arrays.copyOf(response, response[0] + 1);
}
Expand Down Expand Up @@ -214,8 +216,8 @@ public ManagementSession(FidoConnection connection) throws IOException {
backend = new Backend<FidoProtocol>(protocol) {
@Override
byte[] readConfig(int page) throws IOException {
Logger.debug(logger, "Reading fido config page {}...", page);
return delegate.sendAndReceive(CTAP_READ_CONFIG, int2bytes(page), null);
Logger.debug(logger, "Reading config page {}...", page);
return delegate.sendAndReceive(CTAP_READ_CONFIG, pagePayload(page), null);
}

@Override
Expand Down Expand Up @@ -379,15 +381,18 @@ byte[] readConfig() throws IOException, CommandException {
public void close() throws IOException {
delegate.close();
}

static byte[] int2bytes(int value) {
return ByteBuffer.allocate(4).putInt(value).array();
}
}

private void logCtor(YubiKeyConnection connection) {
Logger.debug(logger, "Management session initialized for connection={}, version={}",
connection.getClass().getSimpleName(),
getVersion());
}

private static byte[] pagePayload(int page) {
if (page > 255 || page < 0) {
throw new IllegalArgumentException("Invalid page value " + page);
}
return new byte[]{(byte) page};
}
}

0 comments on commit b062eaf

Please sign in to comment.