Skip to content

Commit

Permalink
Update apdu4j to get around OK 5022 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpaljak committed Jul 21, 2023
1 parent 6ce45b7 commit 1523e2f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

implementation 'com.github.martinpaljak:apdu4j-pcsc:23.03.21'
implementation 'com.github.martinpaljak:apdu4j-pcsc:23.03.22-SNAPSHOT'
implementation 'com.dustinredmond.fxtrayicon:FXTrayIcon:4.0.1'
implementation 'org.slf4j:slf4j-simple:2.0.7'
implementation 'net.sf.jopt-simple:jopt-simple:5.0.4'
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/pro/javacard/nfc4pc/NDEF.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ static boolean isNull(byte[] b) {
// Returns the NDEF message, if any
static Optional<byte[]> getType2(APDUBIBO b) throws BIBOException {
try {
// Read capability container
// Read capability container (4th block)
ResponseAPDU initial = b.transmit(new CommandAPDU(0xFF, 0xB0, 0x00, 3, 0x04));
if (initial.getSW() == 0x9000 && initial.getData().length == 4) {
// Cloud 3700F: returns 4 bytes even if asked for 16. OK 5022 returns 16 bytes even if asked for 4.
if (initial.getSW() == 0x9000 && initial.getData().length >= 4) {
var init = initial.getData();
log.debug("Capability container: {}", HexUtils.bin2hex(init));
if (init[0] == (byte) 0xE1 && init[1] == 0x10) {
int total = (init[2] & 0xFF) * 8;
log.info("NDEF payload of {} bytes", total);
ByteArrayOutputStream payload = new ByteArrayOutputStream();
for (int i = 4; i * 4 < total; i++) {
log.debug("Reading block {}", i);

for (int blocknum = 4; payload.toByteArray().length < total; ) {
log.debug("Reading block {}", blocknum);
log.debug("Current payload: ({} bytes) {}", payload.toByteArray().length, HexUtils.bin2hex(payload.toByteArray()));

var block = b.transmit(new CommandAPDU(0xFF, 0xB0, 0x00, i, 4));
var block = b.transmit(new CommandAPDU(0xFF, 0xB0, 0x00, blocknum, 4));
var bytes = block.getData();
if (block.getSW() == 0x9000) {
log.debug("Block: {}", HexUtils.bin2hex(bytes));
Expand All @@ -60,6 +62,7 @@ static Optional<byte[]> getType2(APDUBIBO b) throws BIBOException {
break;
}
payload.write(bytes);
blocknum += bytes.length / 4;
} else {
log.warn("Read returned {}", HexUtils.bin2hex(block.getBytes()));
return Optional.empty();
Expand Down Expand Up @@ -92,8 +95,15 @@ static String record2url(byte[] record) {
}

static byte[] type2_to_message(byte[] payload) {
// Type 2: tag 03 with lentgh, so offset 2
return Arrays.copyOfRange(payload, 2, payload.length);
log.debug("Parsing {}", HexUtils.bin2hex(payload));
int pos = 0;
if (payload[pos] == 0x01)
pos += payload[pos + 1] + 1;
if (payload[pos] == 0x02)
pos += payload[pos + 1] + 1;
byte[] msg = Arrays.copyOfRange(payload, pos + 2, pos + 2 + payload[pos + 1]);
log.debug("Message: {}", HexUtils.bin2hex(msg));
return msg;
}

static byte[] type4_to_message(byte[] payload) {
Expand All @@ -120,7 +130,7 @@ static String msg2url(byte[] payload) {
throw new IllegalArgumentException("Unsupported TNF");
int len = payload[2] & 0xFF;
record = Arrays.copyOfRange(payload, 4, 4 + len);
} else if ((payload[0] & 0x10) == 0x00){
} else if ((payload[0] & 0x10) == 0x00) {
if (payload[6] != 0x55)
throw new IllegalArgumentException("Unsupported TNF");
ByteBuffer buffer = ByteBuffer.wrap(payload);
Expand Down

0 comments on commit 1523e2f

Please sign in to comment.