Skip to content

Commit

Permalink
add part number property
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamVe committed May 14, 2024
1 parent 6042c64 commit 0f24762
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import com.yubico.yubikit.core.Transport;
import com.yubico.yubikit.core.Version;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Nullable;

Expand All @@ -38,6 +40,7 @@ public class DeviceInfo {
private static final int TAG_NFC_SUPPORTED = 0x0d;
private static final int TAG_NFC_ENABLED = 0x0e;
private static final int TAG_CONFIG_LOCKED = 0x0a;
private static final int TAG_PART_NUMBER = 0x13;
private static final int TAG_FIPS_CAPABLE = 0x14;
private static final int TAG_FIPS_APPROVED = 0x15;
private static final int TAG_PIN_COMPLEXITY = 0x16;
Expand All @@ -55,6 +58,7 @@ public class DeviceInfo {
private final boolean isLocked;
private final boolean isFips;
private final boolean isSky;
private final byte[] partNumber;
private final int fipsCapable;
private final int fipsApproved;
private final boolean pinComplexity;
Expand All @@ -73,6 +77,7 @@ private DeviceInfo(Builder builder) {
this.isLocked = builder.isLocked;
this.isFips = builder.isFips;
this.isSky = builder.isSky;
this.partNumber = builder.partNumber.clone();
this.fipsCapable = builder.fipsCapable;
this.fipsApproved = builder.fipsApproved;
this.pinComplexity = builder.pinComplexity;
Expand Down Expand Up @@ -191,6 +196,13 @@ public boolean isSky() {
return isSky;
}

/**
* Returns part number
*/
public byte[] getPartNumber() {
return partNumber.clone();
}

/**
* Returns FIPS capable flags
*/
Expand Down Expand Up @@ -241,6 +253,9 @@ static DeviceInfo parseTlvs(Map<Integer, byte[]> data, Version defaultVersion) {
int formFactorTagData = readInt(data.get(TAG_FORMFACTOR));
boolean isFips = (formFactorTagData & 0x80) != 0;
boolean isSky = (formFactorTagData & 0x40) != 0;
byte[] partNumber = data.containsKey(TAG_PART_NUMBER)
? data.get(TAG_PART_NUMBER)
: new byte[0];
int fipsCapable = fromFips(readInt(data.get(TAG_FIPS_CAPABLE)));
int fipsApproved = fromFips(readInt(data.get(TAG_FIPS_APPROVED)));
boolean pinComplexity = readInt(data.get(TAG_PIN_COMPLEXITY)) == 1;
Expand Down Expand Up @@ -307,6 +322,7 @@ static DeviceInfo parseTlvs(Map<Integer, byte[]> data, Version defaultVersion) {
.isLocked(isLocked)
.isFips(isFips)
.isSky(isSky)
.partNumber(partNumber)
.fipsCapable(fipsCapable)
.fipsApproved(fipsApproved)
.pinComplexity(pinComplexity)
Expand All @@ -326,6 +342,7 @@ public static class Builder {
private boolean isLocked = false;
private boolean isFips = false;
private boolean isSky = false;
private byte[] partNumber = new byte[0];
private int fipsCapable = 0;
private int fipsApproved = 0;
private boolean pinComplexity = false;
Expand Down Expand Up @@ -379,6 +396,12 @@ public Builder isSky(boolean sky) {
return this;
}

public Builder partNumber(byte[] partNumber) {
Arrays.fill(this.partNumber, (byte) 0x00);
this.partNumber = partNumber.clone();
return this;
}

public Builder fipsCapable(int fipsCapable) {
this.fipsCapable = fipsCapable;
return this;
Expand Down Expand Up @@ -436,6 +459,37 @@ private static int readInt(@Nullable byte[] data) {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DeviceInfo that = (DeviceInfo) o;
return isLocked == that.isLocked &&
isFips == that.isFips &&
isSky == that.isSky &&
fipsCapable == that.fipsCapable &&
fipsApproved == that.fipsApproved &&
pinComplexity == that.pinComplexity &&
resetBlocked == that.resetBlocked &&
Objects.equals(config, that.config) &&
Objects.equals(serialNumber, that.serialNumber) &&
Objects.equals(version, that.version) &&
formFactor == that.formFactor &&
Objects.equals(supportedCapabilities, that.supportedCapabilities) &&
Arrays.equals(partNumber, that.partNumber) &&
Objects.equals(fpsVersion, that.fpsVersion) &&
Objects.equals(stmVersion, that.stmVersion);
}

@Override
public int hashCode() {
int result = Objects.hash(config, serialNumber, version, formFactor,
supportedCapabilities, isLocked, isFips, isSky, fipsCapable, fipsApproved,
pinComplexity, resetBlocked, fpsVersion, stmVersion);
result = 31 * result + Arrays.hashCode(partNumber);
return result;
}

@Override
public String toString() {
return "DeviceInfo{" +
Expand All @@ -447,7 +501,13 @@ public String toString() {
", isLocked=" + isLocked +
", isFips=" + isFips +
", isSky=" + isSky +
", partNumber=" + Arrays.toString(partNumber) +
", fipsCapable=" + fipsCapable +
", fipsApproved=" + fipsApproved +
", pinComplexity=" + pinComplexity +
", resetBlocked=" + resetBlocked +
", fpsVersion=" + fpsVersion +
", stmVersion=" + stmVersion +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.yubico.yubikit.management;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.yubico.yubikit.core.Transport;
import com.yubico.yubikit.core.Version;
import com.yubico.yubikit.testing.Codec;

import org.junit.Test;

Expand Down Expand Up @@ -77,6 +79,19 @@ public void testConstruction() {
assertTrue(deviceInfo.hasTransport(Transport.NFC));
}

@Test
public void testPartNumber() {
assertArrayEquals(new byte[0], defaultInfo().getPartNumber());
assertArrayEquals(
new byte[0],
new DeviceInfo.Builder().partNumber(new byte[0]).build().getPartNumber());
assertArrayEquals(
Codec.fromHex("000102030405060708090A0B0C0D0E0F"),
new DeviceInfo.Builder().partNumber(new byte[]{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}).build().getPartNumber());
}

@Test
public void testFipsCapable() {
assertEquals(0, defaultInfo().getFipsCapable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import static com.yubico.yubikit.management.TestUtil.defaultVersion;
import static com.yubico.yubikit.management.TestUtil.emptyTlvs;
import static com.yubico.yubikit.management.TestUtil.tlvs;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.yubico.yubikit.core.Version;
import com.yubico.yubikit.testing.Codec;

import org.junit.Test;

Expand Down Expand Up @@ -89,6 +91,17 @@ public void testParseSky() {
assertFalse(infoOf(0x04, new byte[]{(byte) 0x80}).isSky());
}

@Test
public void testParsePartNumber() {
assertArrayEquals(new byte[0], defaultInfo().getPartNumber());
assertArrayEquals(new byte[0], infoOf(0x13, new byte[0]).getPartNumber());
assertArrayEquals(new byte[]{0x40}, infoOf(0x13, new byte[]{0x40}).getPartNumber());
assertArrayEquals(Codec.fromHex("000102030405060708090A0B0C0D0E0F"),
infoOf(0x13, new byte[]{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}).getPartNumber());
}

@Test
public void testParseFipsCapable() {
assertEquals(0, defaultInfo().getFipsCapable());
Expand Down

0 comments on commit 0f24762

Please sign in to comment.