Fix GNUK version comparison. 1.2.5 already supports reset, use class to make 1.2.10 bigger as 1.2.9

This commit is contained in:
Dominik Schürmann
2017-11-02 13:54:56 +01:00
parent 3ff5470d10
commit bfce1cb4a9
2 changed files with 64 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
@@ -94,7 +95,7 @@ public abstract class SecurityTokenInfo implements Parcelable {
public enum TokenType { public enum TokenType {
YUBIKEY_NEO, YUBIKEY_4, FIDESMO, NITROKEY_PRO, NITROKEY_STORAGE, NITROKEY_START, YUBIKEY_NEO, YUBIKEY_4, FIDESMO, NITROKEY_PRO, NITROKEY_STORAGE, NITROKEY_START,
GNUK_OLD, GNUK_UNKNOWN, GNUK_NEWER_1_25, LEDGER_NANO_S, UNKNOWN GNUK_OLD, GNUK_UNKNOWN, GNUK_1_25_AND_NEWER, LEDGER_NANO_S, UNKNOWN
} }
private static final HashSet<TokenType> SUPPORTED_USB_TOKENS = new HashSet<>(Arrays.asList( private static final HashSet<TokenType> SUPPORTED_USB_TOKENS = new HashSet<>(Arrays.asList(
@@ -104,14 +105,14 @@ public abstract class SecurityTokenInfo implements Parcelable {
TokenType.NITROKEY_STORAGE, TokenType.NITROKEY_STORAGE,
TokenType.GNUK_OLD, TokenType.GNUK_OLD,
TokenType.GNUK_UNKNOWN, TokenType.GNUK_UNKNOWN,
TokenType.GNUK_NEWER_1_25 TokenType.GNUK_1_25_AND_NEWER
)); ));
private static final HashSet<TokenType> SUPPORTED_USB_RESET = new HashSet<>(Arrays.asList( private static final HashSet<TokenType> SUPPORTED_USB_RESET = new HashSet<>(Arrays.asList(
TokenType.YUBIKEY_NEO, TokenType.YUBIKEY_NEO,
TokenType.YUBIKEY_4, TokenType.YUBIKEY_4,
TokenType.NITROKEY_PRO, TokenType.NITROKEY_PRO,
TokenType.GNUK_NEWER_1_25 TokenType.GNUK_1_25_AND_NEWER
)); ));
private static final HashSet<TokenType> SUPPORTED_USB_PUT_KEY = new HashSet<>(Arrays.asList( private static final HashSet<TokenType> SUPPORTED_USB_PUT_KEY = new HashSet<>(Arrays.asList(
@@ -141,7 +142,7 @@ public abstract class SecurityTokenInfo implements Parcelable {
return isKnownSupported || isNfcTransport; return isKnownSupported || isNfcTransport;
} }
public static String parseGnukVersionString(String serialNo) { public static Version parseGnukVersionString(String serialNo) {
if (serialNo == null) { if (serialNo == null) {
return null; return null;
} }
@@ -150,6 +151,60 @@ public abstract class SecurityTokenInfo implements Parcelable {
if (!matcher.matches()) { if (!matcher.matches()) {
return null; return null;
} }
return matcher.group(1); return new Version(matcher.group(1));
}
public static class Version implements Comparable<Version> {
private String version;
public final String get() {
return this.version;
}
public Version(String version) {
if (version == null) {
throw new IllegalArgumentException("Version can not be null");
}
if (!version.matches("[0-9]+(\\.[0-9]+)*")) {
throw new IllegalArgumentException("Invalid version format");
}
this.version = version;
}
@Override
public int compareTo(@NonNull Version that) {
String[] thisParts = this.get().split("\\.");
String[] thatParts = that.get().split("\\.");
int length = Math.max(thisParts.length, thatParts.length);
for (int i = 0; i < length; i++) {
int thisPart = i < thisParts.length ?
Integer.parseInt(thisParts[i]) : 0;
int thatPart = i < thatParts.length ?
Integer.parseInt(thatParts[i]) : 0;
if (thisPart < thatPart) {
return -1;
}
if (thisPart > thatPart) {
return 1;
}
}
return 0;
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (this.getClass() != that.getClass()) {
return false;
}
return this.compareTo((Version) that) == 0;
}
} }
} }

View File

@@ -219,9 +219,10 @@ public class UsbTransport implements Transport {
} }
case VENDOR_FSIJ: { case VENDOR_FSIJ: {
String serialNo = usbConnection.getSerial(); String serialNo = usbConnection.getSerial();
String gnukVersion = SecurityTokenInfo.parseGnukVersionString(serialNo); SecurityTokenInfo.Version gnukVersion = SecurityTokenInfo.parseGnukVersionString(serialNo);
boolean versionBigger125 = gnukVersion != null && "1.2.5".compareTo(gnukVersion) < 0; boolean versionGreaterEquals125 = gnukVersion != null
return versionBigger125 ? TokenType.GNUK_NEWER_1_25 : TokenType.GNUK_OLD; && new SecurityTokenInfo.Version("1.2.5").compareTo(gnukVersion) <= 0;
return versionGreaterEquals125 ? TokenType.GNUK_1_25_AND_NEWER : TokenType.GNUK_OLD;
} }
case VENDOR_LEDGER: { case VENDOR_LEDGER: {
return TokenType.LEDGER_NANO_S; return TokenType.LEDGER_NANO_S;