From 5d679cccc1e678b2974d92457de3b40c5d713729 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 17 Oct 2023 19:37:04 +0200 Subject: [PATCH] Add scripts for managing real smartcards --- .gitignore | 4 ++ hwb1/card/c222.cmd | 3 ++ hwb1/card/delete.cmd | 1 + hwb1/card/e222.cmd | 5 ++ hwb1/card/install.cmd | 2 + hwb1/card/list.cmd | 1 + hwb1/card/test.cmd | 1 + hwb1/card/test.txt | 33 +++++++++++++ hwb1/src/hwb1/MyApplet.java | 95 ++++++++++++++++++------------------- 9 files changed, 95 insertions(+), 50 deletions(-) create mode 100644 hwb1/card/c222.cmd create mode 100644 hwb1/card/delete.cmd create mode 100644 hwb1/card/e222.cmd create mode 100644 hwb1/card/install.cmd create mode 100644 hwb1/card/list.cmd create mode 100644 hwb1/card/test.cmd create mode 100644 hwb1/card/test.txt diff --git a/.gitignore b/.gitignore index eb26d62..c028283 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,7 @@ local.properties .gen/ deliverables/ apdu_scripts/ + +# Build Output +*.class +javacard/ diff --git a/hwb1/card/c222.cmd b/hwb1/card/c222.cmd new file mode 100644 index 0000000..f600749 --- /dev/null +++ b/hwb1/card/c222.cmd @@ -0,0 +1,3 @@ +@rem compile applet with JcDK 2.2.2 +javac -g -target 1.1 -source 1.2 -cp %JC_HOME%\lib\api.jar -d . %~dp0../src/%PACKAGE%/*.java +converter -exportpath "%JC_HOME%\api_export_files" -applet 0x01:0x02:0x03:0x04:0x5:0x6:0x8:0x9 %PACKAGE%.%APPLET% %PACKAGE% 0x01:0x02:0x03:0x04:0x5:0x6:0x8 1.0 diff --git a/hwb1/card/delete.cmd b/hwb1/card/delete.cmd new file mode 100644 index 0000000..62727be --- /dev/null +++ b/hwb1/card/delete.cmd @@ -0,0 +1 @@ +gp --delete 01020304050608 diff --git a/hwb1/card/e222.cmd b/hwb1/card/e222.cmd new file mode 100644 index 0000000..a68d2ee --- /dev/null +++ b/hwb1/card/e222.cmd @@ -0,0 +1,5 @@ +@echo off +set PACKAGE=hwb1 +set APPLET=MyApplet +set "JAVA_HOME=%JAVA8_HOME%" +set "path=%JAVA_HOME%\bin;%JC_HOME%\bin;%JC_HOME%\..;%JC_HOME%\..\GPShell-1.4.4;%path%" diff --git a/hwb1/card/install.cmd b/hwb1/card/install.cmd new file mode 100644 index 0000000..2ae810b --- /dev/null +++ b/hwb1/card/install.cmd @@ -0,0 +1,2 @@ +@rem -f to force install over old version +gp --install %~dp0..\javacard\%PACKAGE%.cap -f diff --git a/hwb1/card/list.cmd b/hwb1/card/list.cmd new file mode 100644 index 0000000..637a7dd --- /dev/null +++ b/hwb1/card/list.cmd @@ -0,0 +1 @@ +gp --list diff --git a/hwb1/card/test.cmd b/hwb1/card/test.cmd new file mode 100644 index 0000000..31cd098 --- /dev/null +++ b/hwb1/card/test.cmd @@ -0,0 +1 @@ +gpshell %~dp0test.txt \ No newline at end of file diff --git a/hwb1/card/test.txt b/hwb1/card/test.txt new file mode 100644 index 0000000..2eff990 --- /dev/null +++ b/hwb1/card/test.txt @@ -0,0 +1,33 @@ +// test my applet +establish_context +enable_trace +enable_timer +card_connect +// select +send_apdu -sc 0 -APDU 00A40400080102030405060809 + +// Query Author Name +send_apdu -sc 0 -APDU 80000000 + +// Store Data +send_apdu -sc 0 -APDU 8002000002FFFE + +// Querying Data +send_apdu -sc 0 -APDU 800400000001 +send_apdu -sc 0 -APDU 800400000002 + +// Triggering Unsupported Instruction Error +send_apdu -sc 0 -APDU 8001000000 + +// Triggering Unsupported CLA Error +send_apdu -sc 0 -APDU 9001000000 + +// Sending Too Many Bytes (> 20) +send_apdu -sc 0 -APDU 80020000150102030405060708090A0B0C0D0E0F101112131415 + +// Querying Incorrect Amount of Bytes +send_apdu -sc 0 -APDU 8002000002FFFE +send_apdu -sc 0 -APDU 8004000003 + +card_disconnect +release_context diff --git a/hwb1/src/hwb1/MyApplet.java b/hwb1/src/hwb1/MyApplet.java index a5c4ee9..a43e9e6 100644 --- a/hwb1/src/hwb1/MyApplet.java +++ b/hwb1/src/hwb1/MyApplet.java @@ -6,21 +6,13 @@ package hwb1; import javacard.framework.*; -import javacardx.annotations.*; -import static hwb1.MyAppletStrings.*; - /** * Applet class * * @author */ -@StringPool(value = { - @StringDef(name = "Package", value = "hwb1"), - @StringDef(name = "AppletName", value = "MyApplet"), - @StringDef(name = "AuthorName", value = "Manuel") }, - // Insert your strings here - name = "MyAppletStrings") public class MyApplet extends Applet { + private byte[] authorName = new byte[] { 'M', 'a', 'n', 'u', 'e', 'l' }; private byte[] storage = new byte[] {}; /** @@ -47,52 +39,55 @@ public class MyApplet extends Applet { * @see APDU * @param apdu the incoming APDU */ - @Override public void process(APDU apdu) { short length; byte[] buffer = apdu.getBuffer(); - if (buffer[ISO7816.OFFSET_CLA] != (byte)0x80) { - ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); - } else { - byte instruction = buffer[ISO7816.OFFSET_INS]; - - switch (instruction) { - case 0x00: - case 0x04: - byte[] response; - length = apdu.setOutgoing(); - - if (instruction == 0x00) { - response = AuthorName; - length = (short)response.length; - } else { - response = storage; - - if (length > response.length) { - ISOException.throwIt((short)(ISO7816.SW_CORRECT_LENGTH_00 | response.length)); - } - } - - apdu.setOutgoingLength(length); - apdu.sendBytesLong(response, (short) 0, length); - break; - case 0x02: - length = apdu.setIncomingAndReceive(); - storage = new byte[length]; - - if (length > 20) { - ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); - } else { - Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, storage, (short) 0, length); - } - break; - default: - ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); - break; - } - + if (selectingApplet()) { ISOException.throwIt(ISO7816.SW_NO_ERROR); + } else { + if (buffer[ISO7816.OFFSET_CLA] != (byte)0x80) { + ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); + } else { + byte instruction = buffer[ISO7816.OFFSET_INS]; + + switch (instruction) { + case 0x00: + case 0x04: + byte[] response; + length = apdu.setOutgoing(); + + if (instruction == 0x00) { + response = authorName; + length = (short)response.length; + } else { + response = storage; + + if (length > response.length) { + ISOException.throwIt((short)(ISO7816.SW_CORRECT_LENGTH_00 | response.length)); + } + } + + apdu.setOutgoingLength(length); + apdu.sendBytesLong(response, (short) 0, length); + break; + case 0x02: + length = apdu.setIncomingAndReceive(); + storage = new byte[length]; + + if (length > 20) { + ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); + } else { + Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, storage, (short) 0, length); + } + break; + default: + ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); + break; + } + + ISOException.throwIt(ISO7816.SW_NO_ERROR); + } } } }