Skip to content

Commit

Permalink
Fix incorrect response format of some devices. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpaljak committed Mar 29, 2019
1 parent d60b574 commit 304c9fe
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/main/java/com/fidesmo/fdsm/FidesmoCard.java
Expand Up @@ -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();
Expand Down Expand Up @@ -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<byte[]> listApps() throws CardException {
// Fidesmo RID
final byte[] prefix = HexUtils.hex2bin("A00000061701");
Expand Down

0 comments on commit 304c9fe

Please sign in to comment.