fix commands generation

This commit is contained in:
Andrea Franz 2018-08-29 17:33:28 +02:00
parent fdb165ab62
commit d5301304eb
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
5 changed files with 29 additions and 26 deletions

View File

@ -76,7 +76,7 @@ public class Crypto {
}
}
public static byte[] macFull3des(byte[] keyData, byte[] data, byte[] iv) {
public static byte[] macFull3des(byte[] keyData, byte[] data, byte[] iv) {
try {
SecretKeySpec keyDes = new SecretKeySpec(resizeKey8(keyData), "DES");
Cipher cipherDes = Cipher.getInstance("DES/CBC/NoPadding");
@ -84,7 +84,7 @@ public class Crypto {
SecretKeySpec keyDes3 = new SecretKeySpec(resizeKey24(keyData), "DESede");
Cipher cipherDes3 = Cipher.getInstance("DESede/CBC/NoPadding");
byte[] des3Iv = iv;
byte[] des3Iv = iv.clone();
if (data.length > 8) {
byte[] tmp = cipherDes.doFinal(data, 0, data.length - 8);

View File

@ -5,6 +5,7 @@ import java.security.SecureRandom;
import im.status.applet_installer_test.appletinstaller.APDUCommand;
import im.status.applet_installer_test.appletinstaller.APDUException;
import im.status.applet_installer_test.appletinstaller.APDUResponse;
import im.status.applet_installer_test.appletinstaller.Crypto;
import im.status.applet_installer_test.appletinstaller.HexUtils;
public class InitializeUpdate {
@ -24,7 +25,7 @@ public class InitializeUpdate {
}
public APDUCommand getCommand() {
return new APDUCommand(CLA, INS, P1, P2, this.hostChallenge);
return new APDUCommand(CLA, INS, P1, P2, this.hostChallenge, true);
}
public static byte[] generateChallenge() {
@ -35,7 +36,7 @@ public class InitializeUpdate {
return challenge;
}
public void validateResponse(APDUResponse resp) throws APDUException {
public boolean validateResponse(byte[] encKeyData, APDUResponse resp) throws APDUException {
if (resp.getSw() == APDUResponse.SW_SECURITY_CONDITION_NOT_SATISFIED) {
throw new APDUException(resp.getSw(), "security confition not satisfied");
}
@ -50,23 +51,13 @@ public class InitializeUpdate {
throw new APDUException(resp.getSw(), String.format("bad data length, expected 28, got %d", data.length));
}
byte[] diversificationdData = new byte[10];
System.arraycopy(data, 0, diversificationdData, 0, 10);
byte[] cardChallenge = new byte[8];
System.arraycopy(data, 12, cardChallenge, 0, 8);
byte[] seq = new byte[2];
System.arraycopy(data, 12, seq, 0, 2);
byte[] cardCryptogram = new byte[8];
System.arraycopy(data, 20, cardCryptogram, 0, 8);
System.out.printf("diversification: %s, %n", HexUtils.byteArrayToHexString(diversificationdData));
System.out.printf("cardChallege: %s, %n", HexUtils.byteArrayToHexString(cardChallenge));
System.out.printf("ssc: %s, %n", HexUtils.byteArrayToHexString(seq));
System.out.printf("cardCryptogram: %s, %n", HexUtils.byteArrayToHexString(cardCryptogram));
return Crypto.verifyCryptogram(encKeyData, this.hostChallenge, cardChallenge, cardCryptogram);
//System.out.printf("key data: %s, %n", HexUtils.byteArrayToHexString(keyData));

View File

@ -15,10 +15,14 @@ public class APDUCommandTest {
int p2 = 0;
byte[] data = HexUtils.hexStringToByteArray("84762336c5187fe8");
APDUCommand c = new APDUCommand(cla, ins, p1, p2, (byte[])data);
APDUCommand c = new APDUCommand(cla, ins, p1, p2, (byte[])data, true);
String expected = "805000000884762336C5187FE800";
String actual = HexUtils.byteArrayToHexString(c.serialize());
assertEquals(expected, actual);
c = new APDUCommand(cla, ins, p1, p2, (byte[])data);
expected = "805000000884762336C5187FE8";
actual = HexUtils.byteArrayToHexString(c.serialize());
assertEquals(expected, actual);
}
}

View File

@ -9,14 +9,14 @@ import static org.junit.Assert.*;
public class APDUWrapperTest {
@Test
public void wrap() throws IOException {
byte[] macKeyData = HexUtils.hexStringToByteArray("904BA06BCE3037710556BE4057D1493C");
byte[] data = HexUtils.hexStringToByteArray("7af26ab1ba32b84f");
byte[] macKeyData = HexUtils.hexStringToByteArray("07EFCCEB0BB0CC01A22E0CE1E1E395F8");
byte[] data = HexUtils.hexStringToByteArray("3CE060483AACE927");
APDUCommand cmd = new APDUCommand(0x84, 0x82, 0x01, 0x00, data);
APDUWrapper w = new APDUWrapper(macKeyData);
APDUCommand wrapped = w.wrap(cmd);
byte[] result = wrapped.serialize();
String expected = "84820100107AF26AB1BA32B84FFE949381C7BC316C00";
String expected = "84820100103CE060483AACE927A3CDA954B0E88839";
assertEquals(expected, HexUtils.byteArrayToHexString(result));
}
}

View File

@ -2,6 +2,8 @@ package im.status.applet_installer_test.appletinstaller.apducommands;
import org.junit.Test;
import java.io.IOException;
import im.status.applet_installer_test.appletinstaller.APDUCommand;
import im.status.applet_installer_test.appletinstaller.APDUException;
import im.status.applet_installer_test.appletinstaller.APDUResponse;
@ -11,8 +13,8 @@ import static org.junit.Assert.*;
public class InitializeUpdateTest {
@Test
public void getCommand() {
byte[] challenge = InitializeUpdate.generateChallenge();
public void getCommand() throws IOException {
byte[] challenge = HexUtils.hexStringToByteArray("2d315d5ffc616d10");
InitializeUpdate init = new InitializeUpdate(challenge);
APDUCommand cmd = init.getCommand();
@ -21,6 +23,10 @@ public class InitializeUpdateTest {
assertEquals(0, cmd.getP1());
assertEquals(0, cmd.getP2());
assertEquals(challenge, cmd.getData());
String expectedAPDU = "80500000082D315D5FFC616D1000";
byte[] apdu = cmd.serialize();
assertEquals(expectedAPDU, HexUtils.byteArrayToHexString(apdu));
}
@Test
@ -32,7 +38,7 @@ public class InitializeUpdateTest {
InitializeUpdate init = new InitializeUpdate(challenge);
try {
init.validateResponse(resp);
init.validateResponse(new byte[]{}, resp);
fail("expected APDUException to be thrown");
} catch (APDUException e) {
assertEquals(0x6982, e.sw);
@ -41,12 +47,14 @@ public class InitializeUpdateTest {
@Test
public void validateResponse_GoodResponse() throws APDUException {
byte[] challenge = HexUtils.hexStringToByteArray("54676ea0043a2f49");
byte[] encKey = HexUtils.hexStringToByteArray("16B5867FF50BE7239C2BF1245B83A362");
byte[] challenge = HexUtils.hexStringToByteArray("f0467f908e5ca23f");
InitializeUpdate init = new InitializeUpdate(challenge);
byte[] apdu = HexUtils.hexStringToByteArray("000002650183039536622002003d2310f3cc9e6cca2551458b8bdb6e9000");
byte[] apdu = HexUtils.hexStringToByteArray("000002650183039536622002000de9c62ba1c4c8e55fcb91b6654ce49000");
APDUResponse resp = new APDUResponse(apdu);
init.validateResponse(resp);
init.validateResponse(encKey, resp);
}
}