Skip to content

Commit

Permalink
Merge pull request #167 from crocs-muni/devel
Browse files Browse the repository at this point in the history
Package AID support testing via custom cap file load
  • Loading branch information
petrs authored Jul 19, 2018
2 parents 8f7f47f + d3f1e7a commit 45c684c
Show file tree
Hide file tree
Showing 64 changed files with 74,718 additions and 552 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/!!!temp/
/AlgTest_JavaCard/nbproject/private/
/AlgTest_JClient/nbproject/private/
/AlgTest_Process/nbproject/private/
Expand Down
Binary file modified AlgTest_JClient/dist/AlgTestJClient.jar
Binary file not shown.
6 changes: 4 additions & 2 deletions AlgTest_JClient/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ endorsed.classpath=
excludes=
file.reference.AlgTest-AlgTest_JavaCard=../AlgTest_JavaCard
file.reference.AlgTest_JavaCard-src=../AlgTest_JavaCard/src
file.reference.AlgTest_JClient-libs=libs\\
file.reference.is-native-c.jar=libs/JNA/is-native-c.jar
file.reference.is-runtime.jar=libs/JNA/is-runtime.jar
file.reference.jcardsim-2.2.2-all.jar=test/jcardsim-2.2.2-all.jar
Expand All @@ -51,7 +52,8 @@ javac.classpath=\
${file.reference.jna-4.1.0.jar}:\
${file.reference.jna-platform-4.1.0.jar}:\
${file.reference.jna.jar}:\
${libs.testng.classpath}
${libs.testng.classpath}:\
${file.reference.AlgTest_JClient-libs}
${libs.JCardSim2.2.2.classpath}:/
${libs.JavaCard3.0.4.classpath}:/
${file.reference.jcardsim-2.2.2-all.jar}:/
Expand Down Expand Up @@ -111,7 +113,7 @@ run.classpath=
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.jvmargs=-enableassertions -noverify
run.test.classpath=
${javac.test.classpath}:/
${build.test.classes.dir}
Expand Down
2 changes: 2 additions & 0 deletions AlgTest_JClient/src/algtestjclient/CardMngr.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ public FileOutputStream establishConnection(Class ClassToTest, String cardName,
m_SystemOutLogger.println(message); file.write(message.getBytes());
message = "Card name; " + cardName + "\r\n";
m_SystemOutLogger.println(message); file.write(message.getBytes());
message = "Card provider; please insert link/description of shop where card was bought\r\n";
m_SystemOutLogger.println(message); file.write(message.getBytes());
message = "Used protocol; " + protocol + "\r\n";
m_SystemOutLogger.println(message); file.write(message.getBytes());

Expand Down
195 changes: 187 additions & 8 deletions AlgTest_JClient/src/algtestjclient/SingleModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ may be distributed under the terms of the GNU General Public License (GPL),
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -239,14 +241,100 @@ public class SingleModeTest {
public static final String RAWRSA_1024_STR[] = {"Variable RSA 1024 - support for variable public exponent. If supported, user-defined fast modular exponentiation can be executed on the smart card via cryptographic coprocessor. This is very specific feature and you will probably not need it",
"Allocate RSA 1024 objects", "Set random modulus", "Set random public exponent", "Initialize cipher with public key with random exponent", "Use random public exponent"};

public static final String PACKAGE_AID_STR[] = {"Package AID support test - a direct testing of supported packages from the standard JavaCard API including version. Not all constants from supported package are necessarily supported.",
"000107A0000000620001#2.1", // java.lang
"000107A0000000620002#2.2.0", // java.io
"000107A0000000620003#2.2.0", // java.rmi
// javacard.framework
"000107A0000000620101#2.1", "010107A0000000620101#2.2.0", "020107A0000000620101#2.2.1",
"030107A0000000620101#2.2.2", "040107A0000000620101#3.0.1", "050107A0000000620101#3.0.4",
"060107A0000000620101#3.0.5",
// javacard.framework.service
"000108A000000062010101#2.2.0",
// javacard.security
"000107A0000000620102#2.1", "010107A0000000620102#2.1.1", "020107A0000000620102#2.2.1",
"030107A0000000620102#2.2.2", "040107A0000000620102#3.0.1", "050107A0000000620102#3.0.4",
"060107A0000000620102#3.0.5",
// javacardx.crypto
"000107A0000000620201#2.1", "010107A0000000620201#2.1.1", "020107A0000000620201#2.2.1",
"030107A0000000620201#2.2.2", "040107A0000000620201#3.0.1", "050107A0000000620201#3.0.4",
"060107A0000000620201#3.0.5",
// javacardx.biometry (starting directly from version 1.2 - previous versions all from 2.2.2)
"000107A0000000620202#2.2.2", "010107A0000000620202#2.2.2", "020107A0000000620202#2.2.2",
"030107A0000000620202#3.0.5",
"000107A0000000620203#2.2.2", // javacardx.external
"000107A0000000620204#3.0.5", // javacardx.biometry1toN
"000107A0000000620205#3.0.5", // javacardx.security
// javacardx.framework.util
"000108A000000062020801#2.2.2", "010108A000000062020801#3.0.5",
"000109A00000006202080101#2.2.2", // javacardx.framework.util.intx
"000108A000000062020802#2.2.2", // javacardx.framework.math
"000108A000000062020803#2.2.2", // javacardx.framework.tlv
"000108A000000062020804#3.0.4", // javacardx.framework.string
"000107A0000000620209#2.2.2", // javacardx.apdu
"000108A000000062020901#3.0.5" // javacardx.apdu.util
};

public static final Map<String, String> PACKAGE_AID_NAMES_STR;
static {
PACKAGE_AID_NAMES_STR = new HashMap<>();
PACKAGE_AID_NAMES_STR.put("000107A0000000620001", "java.lang v1.0");
PACKAGE_AID_NAMES_STR.put("000107A0000000620002", "java.io v1.0");
PACKAGE_AID_NAMES_STR.put("000107A0000000620003", "java.rmi v1.0");
// javacard.framework
PACKAGE_AID_NAMES_STR.put("000107A0000000620101", "javacard.framework v1.0");
PACKAGE_AID_NAMES_STR.put("010107A0000000620101", "javacard.framework v1.1");
PACKAGE_AID_NAMES_STR.put("020107A0000000620101", "javacard.framework v1.2");
PACKAGE_AID_NAMES_STR.put("030107A0000000620101", "javacard.framework v1.3");
PACKAGE_AID_NAMES_STR.put("040107A0000000620101", "javacard.framework v1.4");
PACKAGE_AID_NAMES_STR.put("050107A0000000620101", "javacard.framework v1.5");
PACKAGE_AID_NAMES_STR.put("060107A0000000620101", "javacard.framework v1.6");
// javacard.framework.service
PACKAGE_AID_NAMES_STR.put("000108A000000062010101", "javacard.framework.service v1.0");
// javacard.security
PACKAGE_AID_NAMES_STR.put("000107A0000000620102", "javacard.security v1.0");
PACKAGE_AID_NAMES_STR.put("010107A0000000620102", "javacard.security v1.1");
PACKAGE_AID_NAMES_STR.put("020107A0000000620102", "javacard.security v1.2");
PACKAGE_AID_NAMES_STR.put("030107A0000000620102", "javacard.security v1.3");
PACKAGE_AID_NAMES_STR.put("040107A0000000620102", "javacard.security v1.4");
PACKAGE_AID_NAMES_STR.put("050107A0000000620102", "javacard.security v1.5");
PACKAGE_AID_NAMES_STR.put("060107A0000000620102", "javacard.security v1.6");
// javacardx.crypto
PACKAGE_AID_NAMES_STR.put("000107A0000000620201", "javacardx.crypto v1.0");
PACKAGE_AID_NAMES_STR.put("010107A0000000620201", "javacardx.crypto v1.1");
PACKAGE_AID_NAMES_STR.put("020107A0000000620201", "javacardx.crypto v1.2");
PACKAGE_AID_NAMES_STR.put("030107A0000000620201", "javacardx.crypto v1.3");
PACKAGE_AID_NAMES_STR.put("040107A0000000620201", "javacardx.crypto v1.4");
PACKAGE_AID_NAMES_STR.put("050107A0000000620201", "javacardx.crypto v1.5");
PACKAGE_AID_NAMES_STR.put("060107A0000000620201", "javacardx.crypto v1.6");
// javacardx.biometry (starting directly from version 1.2 - previous versions all from 2.2.2)
PACKAGE_AID_NAMES_STR.put("000107A0000000620202", "javacardx.biometry v1.0");
PACKAGE_AID_NAMES_STR.put("010107A0000000620202", "javacardx.biometry v1.1");
PACKAGE_AID_NAMES_STR.put("020107A0000000620202", "javacardx.biometry v1.2");
PACKAGE_AID_NAMES_STR.put("030107A0000000620202", "javacardx.biometry v1.3");

PACKAGE_AID_NAMES_STR.put("000107A0000000620203", "javacardx.external v1.0");
PACKAGE_AID_NAMES_STR.put("000107A0000000620204", "javacardx.biometry1toN v1.0");
PACKAGE_AID_NAMES_STR.put("000107A0000000620205", "javacardx.security v1.0");
// javacardx.framework.util
PACKAGE_AID_NAMES_STR.put("000108A000000062020801", "javacardx.framework.util v1.0");
PACKAGE_AID_NAMES_STR.put("010108A000000062020801", "javacardx.framework.util v1.1");
PACKAGE_AID_NAMES_STR.put("000109A00000006202080101", "javacardx.framework.util.intx v1.0");
PACKAGE_AID_NAMES_STR.put("000108A000000062020802", "javacardx.framework.math v1.0");
PACKAGE_AID_NAMES_STR.put("000108A000000062020803", "javacardx.framework.tlv v1.0");
PACKAGE_AID_NAMES_STR.put("000108A000000062020804", "javacardx.framework.string v1.0");
PACKAGE_AID_NAMES_STR.put("000107A0000000620209", "javacardx.apdu v1.0");
PACKAGE_AID_NAMES_STR.put("000108A000000062020901", "javacardx.apdu.util v1.0");
}

public static final String EXTENDEDAPDU_STR[] = {"javacardx.apdu.ExtendedLength", "Extended APDU#2.2.2"};

public static final String BASIC_INFO[] = {"Basic info", "JavaCard support version"};

public static final String[] ALL_CLASSES_STR[] = {
BASIC_INFO, JCSYSTEM_STR, EXTENDEDAPDU_STR, CIPHER_STR, SIGNATURE_STR, MESSAGEDIGEST_STR, RANDOMDATA_STR, KEYBUILDER_STR,
KEYPAIR_RSA_STR, KEYPAIR_RSACRT_STR, KEYPAIR_DSA_STR, KEYPAIR_EC_F2M_STR,
KEYPAIR_EC_FP_STR, KEYAGREEMENT_STR, CHECKSUM_STR, RAWRSA_1024_STR
KEYPAIR_EC_FP_STR, KEYAGREEMENT_STR, CHECKSUM_STR, RAWRSA_1024_STR, PACKAGE_AID_STR
};


Expand Down Expand Up @@ -407,6 +495,7 @@ public class SingleModeTest {
public final static byte[] RESET_APDU = {(byte) 0xb0, (byte) 0xe2, (byte) 0x00, (byte) 0x00, (byte) 0x00};

static DirtyLogger m_SystemOutLogger = null;
private Object ImmutableMap;
public SingleModeTest(DirtyLogger logger) {
m_SystemOutLogger = logger;
cardManager = new CardMngr(m_SystemOutLogger);
Expand Down Expand Up @@ -603,6 +692,96 @@ public void ClassSkipped(FileOutputStream file, String className) throws IOExcep
m_SystemOutLogger.println(message);
file.write(message.getBytes());
}

/**
* Convert error from card to text string
* @param swStatus
* @return
*/
public static String ErrorToString(int swStatus) {
// lower byte of exception is value as defined in JCSDK/api_classic/constant-values.htm
//https://docs.oracle.com/javacard/3.0.5/api/constant-values.html

short sw1 = (short) (swStatus & 0xff00);
short sw2 = (short) (swStatus & 0x00ff);
switch (sw1) {
case JCConsts.SW_Exception_prefix:
switch (sw2) {
case JCConsts.SW_Exception:
return "Exception";
case JCConsts.SW_ArrayIndexOutOfBoundsException:
return "ArrayIndexOutOfBoundsException";
case JCConsts.SW_ArithmeticException:
return "ArithmeticException";
case JCConsts.SW_ArrayStoreException:
return "ArrayStoreException";
case JCConsts.SW_NullPointerException:
return "NullPointerException";
case JCConsts.SW_NegativeArraySizeException:
return "NegativeArraySizeException";
}
case JCConsts.SW_CryptoException_prefix:
switch (sw2) {
case JCConsts.CryptoException_ILLEGAL_VALUE:
return "CryptoException_ILLEGAL_VALUE";
case JCConsts.CryptoException_UNINITIALIZED_KEY:
return "CryptoException_UNINITIALIZED_KEY";
case JCConsts.CryptoException_NO_SUCH_ALGORITHM:
return "CryptoException_NO_SUCH_ALGORITHM";
case JCConsts.CryptoException_INVALID_INIT:
return "CryptoException_INVALID_INIT";
case JCConsts.CryptoException_ILLEGAL_USE:
return "CryptoException_ILLEGAL_USE";
default: return "CryptoException_" + Integer.toHexString(sw2);
}
case JCConsts.SW_SystemException_prefix:
switch (sw2) {
case JCConsts.SystemException_ILLEGAL_VALUE:
return "SystemException_ILLEGAL_VALUE";
case JCConsts.SystemException_NO_TRANSIENT_SPACE:
return "SystemException_NO_TRANSIENT_SPACE";
case JCConsts.SystemException_ILLEGAL_TRANSIENT:
return "SystemException_ILLEGAL_TRANSIENT";
case JCConsts.SystemException_ILLEGAL_AID:
return "SystemException_ILLEGAL_AID";
case JCConsts.SystemException_NO_RESOURCE:
return "SystemException_NO_RESOURCE";
case JCConsts.SystemException_ILLEGAL_USE:
return "SystemException_ILLEGAL_USE";
default:
return "SystemException_" + Integer.toHexString(sw2);
}
case JCConsts.SW_PINException_prefix:
switch (sw2) {
case JCConsts.PINException_ILLEGAL_VALUE:
return "PINException_ILLEGAL_VALUE";
case JCConsts.PINException_ILLEGAL_STATE:
return "PINException_ILLEGAL_STATE";
default:
return "PINException_" + Integer.toHexString(sw2);
}
case JCConsts.SW_TransactionException_prefix:
switch (sw2) {
case JCConsts.TransactionException_IN_PROGRESS:
return "TransactionException_IN_PROGRESS";
case JCConsts.TransactionException_NOT_IN_PROGRESS:
return "TransactionException_NOT_IN_PROGRESS";
case JCConsts.TransactionException_BUFFER_FULL:
return "TransactionException_BUFFER_FULL";
case JCConsts.TransactionException_INTERNAL_FAILURE:
return "TransactionException_INTERNAL_FAILURE";
case JCConsts.TransactionException_ILLEGAL_USE:
return "TransactionException_ILLEGAL_USE";
default:
return "TransactionException_" + Integer.toHexString(sw2);
}
case JCConsts.SW_CardRuntimeException_prefix:
return "CardRuntimeException_" + Integer.toHexString(sw2);

default:
return Integer.toHexString(swStatus);
}
}

/**
* Checks result of algorithm testing on smart card.
Expand Down Expand Up @@ -673,7 +852,7 @@ public static void CheckResult (FileOutputStream file, String name, byte[] respo
}
}
else {
message += name + ";" + "error(0x" + Integer.toHexString(swStatus) + ");" + "\r\n";
message += name + ";" + ErrorToString(swStatus) + ";" + "\r\n";
}
m_SystemOutLogger.println(message);
file.write(message.getBytes());
Expand Down Expand Up @@ -952,12 +1131,12 @@ public static void TestClassChecksum (FileOutputStream file) throws IOException,
public static void TestClassKeyPair_ALG_RSA (FileOutputStream file) throws IOException, Exception{
long elapsedCard = 0;
byte[] apdu = new byte[8];
apdu[OFFSET_CLA] = Consts.CLA_CARD_ALGTEST; // for AlgTest applet
apdu[OFFSET_INS] = Consts.INS_CARD_TESTSUPPORTEDMODES_SINGLE; // for AlgTest applet switch to 'TestSupportedModeSingle'
apdu[OFFSET_P1] = Consts.CLASS_KEYPAIR; // 0x19
apdu[OFFSET_P2] = (byte)0x00;
apdu[OFFSET_LC] = (byte)0x03;
apdu[OFFSET_DATA] = ALG_RSA; // 1
apdu[OFFSET_CLA] = Consts.CLA_CARD_ALGTEST; // for AlgTest applet
apdu[OFFSET_INS] = Consts.INS_CARD_TESTSUPPORTEDMODES_SINGLE; // for AlgTest applet switch to 'TestSupportedModeSingle'
apdu[OFFSET_P1] = Consts.CLASS_KEYPAIR; // 0x19
apdu[OFFSET_P2] = (byte)0x00;
apdu[OFFSET_LC] = (byte)0x03;
apdu[OFFSET_DATA] = ALG_RSA; // 1

/* Creates message with class name and writes it in the output file and on the screen. */
String message = "\n" + cardManager.GetAlgorithmName(SingleModeTest.KEYPAIR_RSA_STR[0]) + "\r\n";
Expand Down
41 changes: 41 additions & 0 deletions AlgTest_JavaCard/src/AlgTest/JCConsts.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,45 @@ public class JCConsts {
public static final short SWAlgs_xor = 1;
public static final short SWAlgs_AES = 2;


// javacard.framework.SystemException
public final static short SW_SystemException_prefix = (short) 0xf200;
public static final short SystemException_ILLEGAL_VALUE = 1;
public static final short SystemException_NO_TRANSIENT_SPACE = 2;
public static final short SystemException_ILLEGAL_TRANSIENT = 3;
public static final short SystemException_ILLEGAL_AID = 4;
public static final short SystemException_NO_RESOURCE = 5;
public static final short SystemException_ILLEGAL_USE = 6;

// javacard.security.CryptoException
public static final short SW_CryptoException_prefix = (short) 0xf100;
public static final short CryptoException_ILLEGAL_VALUE = 1;
public static final short CryptoException_UNINITIALIZED_KEY = 2;
public static final short CryptoException_NO_SUCH_ALGORITHM = 3;
public static final short CryptoException_INVALID_INIT = 4;
public static final short CryptoException_ILLEGAL_USE = 5;

// javacard.framework.PINException
public static final short SW_PINException_prefix = (short) 0xf300;
public static final short PINException_ILLEGAL_VALUE = 1;
public static final short PINException_ILLEGAL_STATE = 2;

// javacard.framework.TransactionException
public static final short SW_TransactionException_prefix = (short) 0xf400;
public static final short TransactionException_IN_PROGRESS = 1;
public static final short TransactionException_NOT_IN_PROGRESS = 2;
public static final short TransactionException_BUFFER_FULL = 3;
public static final short TransactionException_INTERNAL_FAILURE = 4;
public static final short TransactionException_ILLEGAL_USE = 5;

// Named exceptions
public final static short SW_Exception_prefix = (short) 0xff00;
public final static short SW_Exception = (short) 0x0001;
public final static short SW_ArrayIndexOutOfBoundsException = (short) 0x0002;
public final static short SW_ArithmeticException = (short) 0x0003;
public final static short SW_ArrayStoreException = (short) 0x0004;
public final static short SW_NullPointerException = (short) 0x0005;
public final static short SW_NegativeArraySizeException = (short) 0x0006;

public final static short SW_CardRuntimeException_prefix = (short) 0xf500;
}
Binary file modified AlgTest_Process/dist/AlgTestProcess.jar
Binary file not shown.
Binary file modified AlgTest_Process/dist/lib/AlgTestJClient.jar
Binary file not shown.
Loading

0 comments on commit 45c684c

Please sign in to comment.