From 171220bd799b04d1bc1ff86dca8532e3efd28804 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Fri, 14 Dec 2018 13:21:36 +0300 Subject: [PATCH] change AIDs --- .../im/status/keycard/applet/Identifiers.java | 30 +++++++++++++++---- .../keycard/applet/KeycardCommandSet.java | 18 +++++++++-- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/src/main/java/im/status/keycard/applet/Identifiers.java b/lib/src/main/java/im/status/keycard/applet/Identifiers.java index adaf606..5a20ca0 100644 --- a/lib/src/main/java/im/status/keycard/applet/Identifiers.java +++ b/lib/src/main/java/im/status/keycard/applet/Identifiers.java @@ -2,21 +2,39 @@ package im.status.keycard.applet; import org.bouncycastle.util.encoders.Hex; +import java.util.Arrays; + public class Identifiers { - public static final byte[] PACKAGE_AID = Hex.decode("53746174757357616C6C6574"); + public static final byte[] PACKAGE_AID = Hex.decode("A0000008040001"); - public static final byte[] KEYCARD_AID = Hex.decode("53746174757357616C6C6574417070"); + public static final byte[] KEYCARD_AID = Hex.decode("A000000804000101"); + public static final int KEYCARD_DEFAULT_INSTANCE_IDX = 1; - public static final byte[] NDEF_AID = Hex.decode("53746174757357616C6C65744E4643"); + public static final byte[] NDEF_AID = Hex.decode("A000000804000102"); public static final byte[] NDEF_INSTANCE_AID = Hex.decode("D2760000850101"); /** - * Gets the instance AID of the Keycard applet. Since multiple instances this is a method instead of a constant. - * Soon a method taking an additional instance index will be added. + * Gets the instance AID of the default instance of the Keycard applet. * * @return the instance AID of the Keycard applet */ public static byte[] getKeycardInstanceAID() { - return KEYCARD_AID; + return getKeycardInstanceAID(KEYCARD_DEFAULT_INSTANCE_IDX); + } + + /** + * Gets the instance AID of the Keycard applet with the given index. Since multiple instances of the Keycard applet + * could be installed in parallel, this method allows selecting a specific instance. The index is between 01 and ff + * + * @return the instance AID of the Keycard applet + */ + public static byte[] getKeycardInstanceAID(int instanceIdx) { + if (instanceIdx < 0x01 || instanceIdx > 0xff) { + throw new IllegalArgumentException("The instance index must be between 1 and 255"); + } + + byte[] instanceAID = Arrays.copyOf(KEYCARD_AID, KEYCARD_AID.length + 1); + instanceAID[KEYCARD_AID.length] = (byte) instanceIdx; + return instanceAID; } } diff --git a/lib/src/main/java/im/status/keycard/applet/KeycardCommandSet.java b/lib/src/main/java/im/status/keycard/applet/KeycardCommandSet.java index 80b929b..afabb3d 100644 --- a/lib/src/main/java/im/status/keycard/applet/KeycardCommandSet.java +++ b/lib/src/main/java/im/status/keycard/applet/KeycardCommandSet.java @@ -95,14 +95,26 @@ public class KeycardCommandSet { } /** - * Selects the applet. The applet is assumed to have been installed with its default AID. The returned data is a - * public key which must be used to initialize the secure channel. + * Selects the default instance of the Keycard applet. The applet is assumed to have been installed with its default + * AID. The returned data is a public key which must be used to initialize the secure channel. * * @return the raw card response * @throws IOException communication error */ public APDUResponse select() throws IOException { - APDUCommand selectApplet = new APDUCommand(0x00, 0xA4, 4, 0, Identifiers.getKeycardInstanceAID()); + return select(Identifiers.KEYCARD_DEFAULT_INSTANCE_IDX); + } + + /** + * Selects a Keycard instance. The applet is assumed to have been installed with its default AID. The returned data is + * a public key which must be used to initialize the secure channel. + * + * @param instanceIdx the instance index + * @return the raw card response + * @throws IOException communication error + */ + public APDUResponse select(int instanceIdx) throws IOException { + APDUCommand selectApplet = new APDUCommand(0x00, 0xA4, 4, 0, Identifiers.getKeycardInstanceAID(instanceIdx)); APDUResponse resp = apduChannel.send(selectApplet); if (resp.getSw() == 0x9000) {