diff --git a/README.md b/README.md index 53a7981..08ec23f 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Typical output: [ant:verify] INFO: Verification completed with 0 warnings and 0 errors. ``` -## Installation on a card +## Installation on a (physical) card ```bash ./gradlew installJavaCard @@ -87,6 +87,16 @@ Or inspect already installed applets: ./gradlew listJavaCard ``` +## Running on simulator (jCardSim) + +As simple as: + +```bash +./gradlew run +``` + +By default the run task will run the main Java application implemented at: `main/java/main/Run.java`, using the `HelloWorldApplet` applet. + ## Running tests ``` diff --git a/applet/build.gradle b/applet/build.gradle index 8f2a977..d5a90f8 100644 --- a/applet/build.gradle +++ b/applet/build.gradle @@ -17,6 +17,7 @@ buildscript { apply plugin: 'com.klinec.gradle.javacard' apply plugin: 'idea' +apply plugin: 'application' sourceCompatibility = 1.8 // Common settings, definitions @@ -43,6 +44,8 @@ repositories { dependencies { jcardsim 'com.klinec:jcardsim:3.0.5.11' + implementation 'com.klinec:jcardsim:3.0.5.11' + testImplementation 'org.testng:testng:6.1.1' testImplementation 'org.slf4j:slf4j-api:1.7.33' testImplementation 'org.slf4j:slf4j-log4j12:1.7.33' @@ -71,6 +74,10 @@ task dumpClassPath(dependsOn: ['idea']) { } } +application { + mainClass = 'main.Run' +} + test { // useTestNG() useJUnitPlatform { diff --git a/applet/src/main/java/applet/HelloWorldApplet.java b/applet/src/main/java/applet/HelloWorldApplet.java new file mode 100755 index 0000000..715db3d --- /dev/null +++ b/applet/src/main/java/applet/HelloWorldApplet.java @@ -0,0 +1,32 @@ +package applet; + +import javacard.framework.*; + +public class HelloWorldApplet extends Applet +{ + + private static final byte[] helloWorld = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'}; + + public static void install(byte[] bArray, short bOffset, byte bLength) + { + new HelloWorldApplet(); + } + + public HelloWorldApplet() + { + register(); + } + + public void process(APDU apdu) + { + sendHelloWorld(apdu); + } + + // part of https://github.com/devrandom/javacard-helloworld/blob/master/src/main/java/org/gitian/javacard/HelloWorldApplet.java#L38 + private void sendHelloWorld(APDU apdu) { + byte[] buffer = apdu.getBuffer(); + short length = (short) helloWorld.length; + Util.arrayCopyNonAtomic(helloWorld, (short) 0, buffer, (short) 0, length); + apdu.setOutgoingAndSend((short) 0, length); + } +} diff --git a/applet/src/main/java/main/Run.java b/applet/src/main/java/main/Run.java new file mode 100644 index 0000000..f779d74 --- /dev/null +++ b/applet/src/main/java/main/Run.java @@ -0,0 +1,28 @@ +package main; + +import applet.HelloWorldApplet; +import com.licel.jcardsim.smartcardio.CardSimulator; +import com.licel.jcardsim.utils.AIDUtil; +import javacard.framework.AID; +import javax.smartcardio.*; + +public class Run { + public static void main(String[] args){ + // 1. create simulator + CardSimulator simulator = new CardSimulator(); + + // 2. install applet + AID appletAID = AIDUtil.create("F000000001"); + simulator.installApplet(appletAID, HelloWorldApplet.class); + + // 3. select applet + simulator.selectApplet(appletAID); + + // 4. send APDU + CommandAPDU commandAPDU = new CommandAPDU(0x00, 0x90, 0x00, 0x00); + ResponseAPDU response = simulator.transmitCommand(commandAPDU); + + System.out.println(new String(response.getData())); + } + +} \ No newline at end of file