Add scripts for managing real smartcards

This commit is contained in:
Manuel Thalmann 2023-10-17 19:37:04 +02:00
parent 9d5d03346a
commit 5d679cccc1
9 changed files with 95 additions and 50 deletions

4
.gitignore vendored
View file

@ -63,3 +63,7 @@ local.properties
.gen/ .gen/
deliverables/ deliverables/
apdu_scripts/ apdu_scripts/
# Build Output
*.class
javacard/

3
hwb1/card/c222.cmd Normal file
View file

@ -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

1
hwb1/card/delete.cmd Normal file
View file

@ -0,0 +1 @@
gp --delete 01020304050608

5
hwb1/card/e222.cmd Normal file
View file

@ -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%"

2
hwb1/card/install.cmd Normal file
View file

@ -0,0 +1,2 @@
@rem -f to force install over old version
gp --install %~dp0..\javacard\%PACKAGE%.cap -f

1
hwb1/card/list.cmd Normal file
View file

@ -0,0 +1 @@
gp --list

1
hwb1/card/test.cmd Normal file
View file

@ -0,0 +1 @@
gpshell %~dp0test.txt

33
hwb1/card/test.txt Normal file
View file

@ -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

View file

@ -6,21 +6,13 @@
package hwb1; package hwb1;
import javacard.framework.*; import javacard.framework.*;
import javacardx.annotations.*;
import static hwb1.MyAppletStrings.*;
/** /**
* Applet class * Applet class
* *
* @author <user> * @author <user>
*/ */
@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 { public class MyApplet extends Applet {
private byte[] authorName = new byte[] { 'M', 'a', 'n', 'u', 'e', 'l' };
private byte[] storage = new byte[] {}; private byte[] storage = new byte[] {};
/** /**
@ -47,52 +39,55 @@ public class MyApplet extends Applet {
* @see APDU * @see APDU
* @param apdu the incoming APDU * @param apdu the incoming APDU
*/ */
@Override
public void process(APDU apdu) { public void process(APDU apdu) {
short length; short length;
byte[] buffer = apdu.getBuffer(); byte[] buffer = apdu.getBuffer();
if (buffer[ISO7816.OFFSET_CLA] != (byte)0x80) { if (selectingApplet()) {
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); 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);
}
} }
} }
} }