diff --git a/src/main/java/com/fidesmo/fdsm/FidesmoCard.java b/src/main/java/com/fidesmo/fdsm/FidesmoCard.java index 064dd93..c435b9a 100644 --- a/src/main/java/com/fidesmo/fdsm/FidesmoCard.java +++ b/src/main/java/com/fidesmo/fdsm/FidesmoCard.java @@ -250,7 +250,7 @@ public boolean detect() throws CardException { if (response.getSW() != 0x9000) return false; BerTlvParser parser = new BerTlvParser(); - BerTlvs tlvs = parser.parse(response.getData()); + BerTlvs tlvs = parser.parse(fixup(response.getData())); BerTlv batchIdTag = tlvs.find(new BerTag(0x42)); if (batchIdTag != null) { batchId = batchIdTag.getBytesValue(); @@ -282,6 +282,20 @@ public boolean detect() throws CardException { return true; } + // Remove the trailing 0x00 if the format is not TLV + private byte[] fixup(byte[] v) { + if (v.length > 0 && v[v.length - 1] == 0x00) { + try { + BerTlvParser parser = new BerTlvParser(); + parser.parse(v); + return v; + } catch (ArrayIndexOutOfBoundsException e) { + return Arrays.copyOf(v, v.length - 1); + } + } + return v; + } + public List listApps() throws CardException { // Fidesmo RID final byte[] prefix = HexUtils.hex2bin("A00000061701");