Skip to content

Commit

Permalink
Add backwards compatible search for applet in reader app.
Browse files Browse the repository at this point in the history
  • Loading branch information
J08nY committed Mar 18, 2019
1 parent 9bd35df commit b99057b
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/cz/crcs/ectester/reader/ECTesterReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ public class ECTesterReader {
private static String CLI_FOOTER = "\n" + LICENSE;

private static final byte[] SELECT_PREFIX = {(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x0c};
private static final byte[] AID_221 = {(byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x33, (byte) 0x33, (byte) 0x62}; // VERSION v0.3.3
private static final byte[] AID_222 = {(byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x33, (byte) 0x33, (byte) 0x78}; // VERSION v0.3.3
private static final byte[] AID_PREFIX = {(byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72};
private static final byte[] AID_CURRENT_VERSION = {(byte) 0x30, (byte) 0x33, (byte) 0x33}; // VERSION v0.3.3
private static final byte[] AID_SUFFIX_221 = {(byte) 0x62};
private static final byte[] AID_SUFFIX_222 = {(byte) 0x78};
private static final byte[] INSTALL_DATA = new byte[10];
private static final int TRY_VERSIONS = 10;

static {
URLClassLoader cl = (URLClassLoader) ECTesterReader.class.getClassLoader();
Expand Down Expand Up @@ -136,7 +139,7 @@ private void run(String[] args) {

//connect or simulate connection
if (cfg.simulate) {
if (!cardManager.prepareLocalSimulatorApplet(AID_221, INSTALL_DATA, ECTesterApplet.class)) {
if (!cardManager.prepareLocalSimulatorApplet(ByteUtil.concatenate(AID_PREFIX, AID_CURRENT_VERSION, AID_SUFFIX_221), INSTALL_DATA, ECTesterApplet.class)) {
System.err.println(Colors.error("Failed to establish a simulator."));
System.exit(1);
} else {
Expand All @@ -147,17 +150,47 @@ private void run(String[] args) {
System.err.println(Colors.error("Failed to connect to card."));
System.exit(1);
}
ResponseAPDU selectResp = cardManager.send(ByteUtil.concatenate(SELECT_PREFIX, AID_222));
if ((short) selectResp.getSW() != ISO7816.SW_NO_ERROR) {
selectResp = cardManager.send(ByteUtil.concatenate(SELECT_PREFIX, AID_221));
//Try the highest known version first
byte[] versionByte = AID_CURRENT_VERSION.clone();
boolean selected = false;
for (int i = 0; i < TRY_VERSIONS; ++i) {
byte[] select222 = ByteUtil.concatenate(SELECT_PREFIX, AID_PREFIX, versionByte, AID_SUFFIX_222);
ResponseAPDU selectResp = cardManager.send(select222);
if ((short) selectResp.getSW() != ISO7816.SW_NO_ERROR) {
System.err.println(Colors.error("Failed to select ECTester applet, is it installed?"));
cardManager.disconnectFromCard();
System.exit(1);
byte[] select221 = ByteUtil.concatenate(SELECT_PREFIX, AID_PREFIX, versionByte, AID_SUFFIX_221);
selectResp = cardManager.send(select221);
if ((short) selectResp.getSW() == ISO7816.SW_NO_ERROR) {
cardManager.setChunking(true);
selected = true;
break;
}
} else {
selected = true;
break;
}
// Count down by versions
if (versionByte[2] == 0x30) {
if (versionByte[1] == 0x30) {
if (versionByte[0] == 0x30) {
break;
} else {
versionByte[0]--;
versionByte[1] = 0x39;
versionByte[2] = 0x39;
}
} else {
versionByte[1]--;
versionByte[2] = 0x39;
}
} else {
cardManager.setChunking(true);
versionByte[2]--;
}
}
if (!selected) {
System.err.println(Colors.error("Failed to select ECTester applet, is it installed?"));
cardManager.disconnectFromCard();
System.exit(1);
}
}

// Setup logger and respWriter
Expand Down

0 comments on commit b99057b

Please sign in to comment.