Skip to content

Commit

Permalink
feat: expanded debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-sekanina committed Oct 10, 2022
1 parent c403483 commit e2e1cad
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 44 deletions.
16 changes: 9 additions & 7 deletions applet/src/main/java/applet/AppletInstructions.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package applet;

import org.graalvm.compiler.graph.Node;

/**
* Class of applet instructions to make testing and changing instructions easier
* both MainApplet and MyTests are getting instructions from this class
Expand Down Expand Up @@ -36,21 +34,25 @@ public AppletInstructions() {

public static final short CLASS_POLICY_UPLOAD = 1;
// simple scenario where applet returns "HAND SHAKE" in bytes
public static final short HAND_SHAKE = 3;
public static final short CLASS_HAND_SHAKE = 3;
public static final short CLASS_SECRETandTIME_UPLOAD = 2; // p1 will determine reference, where to store it
public static final short CLASS_DEBUG_DOWNLOAD = 4;

/**
* Below are debug classes
*/
public static final short CLASS_DOWNLOAD_GLOBAL_MAP = 5; // p1 which global map, here always 0
public static final short INS_DOWNLOAD_NUM_OUTPUT = 4;
public static final short INS_DOWNLOAD_NUM_INPUT = 5;
public static final short INS_DOWNLOAD_VERSION = 6;
public static final short INS_DOWNLOAD_SIZE = 7;
public static final short CLASS_DOWNLOAD_INPUT_MAP = 6; // p1 is position of map, map must be present, first is 0
public static final short CLASS_DOWNLOAD_OUTPUT_MAP = 7; // --above--

public static final short CLASSS_DOWNLOAD_GLOBAL_ALL = 8;
public static final short CLASSS_DOWNLOAD_INPUT_ALL = 9;
public static final short CLASSS_DOWNLOAD_OUTPUT_ALL = 10;

public static final short CLASS_DOWNLOAD_GLOBAL_ALL = 8;
public static final short CLASS_DOWNLOAD_INPUT_ALL = 9;
public static final short CLASS_DOWNLOAD_OUTPUT_ALL = 10;
public static final short CLASS_DOWNLOAD_GLOBAL_MAP_KEYPAIR = 11;


public static final short CLASS_PSBT_UPLOAD_AND_BACK = 4;
Expand Down
21 changes: 19 additions & 2 deletions applet/src/main/java/applet/FromApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
public class FromApplet {
/**
* should be used for small amount of data of size of one packet max
* @param apdu apdu used for communication
*
* @param apdu apdu used for communication
* @param array array of data to send
*/

Expand All @@ -27,8 +28,24 @@ static void send_data(APDU apdu, GeneralMap map) {
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, KeyPair keyPair) {
apdu.getBuffer()[0] = (byte) (keyPair.key.start << 8);
apdu.getBuffer()[1] = (byte) (keyPair.key.start);
apdu.getBuffer()[2] = (byte) (keyPair.getSize() << 8);
apdu.getBuffer()[3] = (byte) (keyPair.getSize());
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, short data) {
apdu.getBuffer()[0] = (byte) (data << 8);
apdu.getBuffer()[1] = (byte) (data);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}
}

/**
static void send_data(APDU apdu, KeyPair key_pair) {
//TODO rewrite this to take a byte of KeyPair type instead of itself
send_data(apdu, PSBTdata, key_pair.start, key_pair.getSize());
}
}
**/
2 changes: 1 addition & 1 deletion applet/src/main/java/applet/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static applet.MainApplet.PSBTdata;

public class Key {
public Short start = null;
public short start = -1;
public Short key_len = null;
public short key_len_bytes = 1;
public Short key_type = null;
Expand Down
49 changes: 43 additions & 6 deletions applet/src/main/java/applet/MainApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void process(APDU apdu) {
}

if (cla == AppletInstructions.CLASS_DEBUG_DOWNLOAD && ins == AppletInstructions.INS_DOWNLOAD_ARRAY) {
short from = Tools.toShort(apduBuffer[ISO7816.OFFSET_CDATA], apduBuffer[ISO7816.OFFSET_CDATA+ 1]);
short to = Tools.toShort(apduBuffer[ISO7816.OFFSET_CDATA + 2], apduBuffer[ISO7816.OFFSET_CDATA + 3]);
short from = (short) (apduBuffer[ISO7816.OFFSET_CDATA] << 8 | apduBuffer[ISO7816.OFFSET_CDATA + 1]);
short to = (short) (apduBuffer[ISO7816.OFFSET_CDATA + 2] << 8 | apduBuffer[ISO7816.OFFSET_CDATA + 3]);
System.out.print("from: " + from + System.lineSeparator());
System.out.print("to: " + to + System.lineSeparator());
if (from < 0 || to > PSBTdata.length) {
Expand All @@ -129,23 +129,60 @@ public void process(APDU apdu) {
FromApplet.send_data(apdu, PSBTdata, from, to);
}

if (cla == AppletInstructions.CLASSS_DOWNLOAD_GLOBAL_ALL) {
if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_VERSION){
if (psbt.global_map.PSBTversion != null) {
FromApplet.send_data(apdu, psbt.global_map.PSBTversion);
}
else {
FromApplet.send_data(apdu, (short) (-1));
}
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_SIZE){
FromApplet.send_data(apdu, offset);
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_NUM_INPUT){
if (psbt.global_map.input_maps_total != null) {
FromApplet.send_data(apdu, psbt.global_map.input_maps_total);
}
if (psbt.global_map.globalUnsignedTX.input_count != null) {
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.input_count);
}
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_NUM_OUTPUT){
if (psbt.global_map.input_maps_total != null) {
FromApplet.send_data(apdu, psbt.global_map.output_maps_total);
}
if (psbt.global_map.globalUnsignedTX.output_count != null) {
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.output_count);
}
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_ALL) {
FromApplet.send_data(apdu, psbt.global_map);
}

if (cla == AppletInstructions.CLASSS_DOWNLOAD_INPUT_ALL) { // TODO: change asserts to exception warning
if (cla == AppletInstructions.CLASS_DOWNLOAD_INPUT_ALL) { // TODO: change asserts to exception warning
assert p1 >= 0;
assert p1 <= psbt.current_input_map;
FromApplet.send_data(apdu, psbt.input_maps[p1]);
}

if (cla == AppletInstructions.CLASSS_DOWNLOAD_OUTPUT_ALL) { // TODO: same
if (cla == AppletInstructions.CLASS_DOWNLOAD_OUTPUT_ALL) { // TODO: same
assert p1 >= 0;
assert p1 <= psbt.current_output_map;
FromApplet.send_data(apdu, psbt.output_maps[p1]);
}

if (cla == AppletInstructions.HAND_SHAKE) {
if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP_KEYPAIR) { // TODO: same
assert p1 >= 0;
assert p1 <= psbt.global_map.current_key_pair;
FromApplet.send_data(apdu, psbt.global_map.key_pairs[p1]);
}

if (cla == AppletInstructions.CLASS_HAND_SHAKE) {
byte[] HAND_SHAKE = {'H', 'A', 'N', 'D', ' ', 'S', 'H', 'A', 'K', 'E'};
Util.arrayCopyNonAtomic(HAND_SHAKE, (short) 0, apduBuffer, (short) 0, (short) HAND_SHAKE.length);
apdu.setOutgoingAndSend((short) 0, (short) HAND_SHAKE.length);
Expand Down
2 changes: 0 additions & 2 deletions applet/src/main/java/applet/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class Tools {
*/

public static short toShort(byte zero, byte first) {
System.out.print("toShort returns: " + (short) ((zero & 0xff) * 256 + (first & 0xff))
+ System.lineSeparator());
return (short) ((zero & 0xff) * 256 + (first & 0xff)); // TODO: have to check overflow
}

Expand Down
2 changes: 1 addition & 1 deletion applet/src/main/java/applet/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static applet.MainApplet.PSBTdata;

public class Value {
public Short start = null;
public short start = -1;
public Short value_len = null;
public short value_len_bytes = 1;

Expand Down
48 changes: 37 additions & 11 deletions applet/src/test/java/tests/AppletControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,53 @@
import applet.*;
import cz.muni.fi.crocs.rcard.client.CardManager;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class AppletControl extends BaseTest{
public class AppletControl extends BaseTest {
CardManager manager = connect();
Upload mu = new Upload();
Download md = new Download();
byte[] psbt = MyTests.fromHex(TransactionsImported.validTransaction1);

public void DownloadTest() throws Exception {
Upload mu = new Upload();
Download du = new Download();
byte[] psbt = MyTests.fromHex(TransactionsImported.validTransaction1);
public void UploadData(CardManager manager, Upload mu) throws Exception {
mu.sendData(psbt, (byte) AppletInstructions.CLASS_PSBT_UPLOAD, manager);
System.out.print(Arrays.toString(du.downloadMap(manager, AppletInstructions.CLASSS_DOWNLOAD_GLOBAL_ALL, (byte) 0)) + System.lineSeparator());
System.out.print(Arrays.toString(du.downloadMap(manager, AppletInstructions.CLASSS_DOWNLOAD_OUTPUT_ALL, (byte) 0)) + System.lineSeparator());
System.out.print(Arrays.toString(du.downloadMap(manager, AppletInstructions.CLASSS_DOWNLOAD_INPUT_ALL, (byte) 0)) + System.lineSeparator());
System.out.print(Arrays.toString(du.downloadDebugArray(manager, (short) 120, (short) 3000)) + System.lineSeparator());
}

public void DownloadDebug(CardManager manager) throws Exception {
System.out.print("Parsing transaction of version: " + md.downloadVersion(manager) + (" (-1 means unspecified)") + System.lineSeparator());
System.out.print("And of size: " + md.downloadSize(manager) + " bytes" + System.lineSeparator());
short inps = md.downloadNumOfInp(manager);
short outs = md.downloadNumOfOut(manager);
System.out.print("Number of inputs: " + inps + System.lineSeparator());
System.out.print("Number of outputs: " + outs + System.lineSeparator());
System.out.print(Arrays.toString(md.downloadDebugArray(manager, (short) 0, (short) 100)) + System.lineSeparator());

System.out.print(Arrays.toString(md.downloadMap(manager, AppletInstructions.CLASS_DOWNLOAD_GLOBAL_ALL, (byte) 0)) + System.lineSeparator());
int i;
for (i = 0; i <= 0; i++){ // it is very possible to break if global keypair with index i is absent
System.out.print("Global map keypair with index: " + i + System.lineSeparator());
System.out.print(Arrays.toString(md.downloadGlobalKeypair(manager, i)) + System.lineSeparator());
}
for (i = 0; i <= 3; i++){
System.out.print("Output map array with index: " + i + System.lineSeparator());
System.out.print(Arrays.toString(md.downloadMap(manager, AppletInstructions.CLASS_DOWNLOAD_OUTPUT_ALL, (byte) 0)) + System.lineSeparator());
System.out.print("Input map array with index: " + i + System.lineSeparator());
System.out.print(Arrays.toString(md.downloadMap(manager, AppletInstructions.CLASS_DOWNLOAD_INPUT_ALL, (byte) 0)) + System.lineSeparator());
}
//System.out.print(Arrays.toString(du.downloadDebugArray(manager, (short) 0, (short) 555)) + System.lineSeparator());
}


public AppletControl() throws Exception {
public void AppletDebug() throws Exception {
UploadData(manager, mu);
DownloadDebug(manager);
}


public AppletControl() throws Exception {
}
}




17 changes: 9 additions & 8 deletions applet/src/test/java/tests/AppletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
* Example test class for the applet
Expand Down Expand Up @@ -41,9 +42,9 @@ public void tearDownMethod() throws Exception {
public void hello() throws Exception {
final CommandAPDU cmd = new CommandAPDU(0x00, 0x90, 0, 0);
final ResponseAPDU responseAPDU = connect().transmit(cmd);
Assert.assertNotNull(responseAPDU);
Assert.assertEquals(0x9000, responseAPDU.getSW());
Assert.assertNotNull(responseAPDU.getBytes());
Assertions.assertNotNull(responseAPDU);
Assertions.assertEquals(0x9000, responseAPDU.getSW());
Assertions.assertNotNull(responseAPDU.getBytes());
System.out.print("Test hello: passed" + System.lineSeparator());
}

Expand All @@ -54,10 +55,10 @@ public void hell() throws Exception {
final CommandAPDU cmd = new CommandAPDU(0x00, 0x80, 0, 0, welcomeB, 0, welcomeB.length, 32);
final ResponseAPDU responseAPDU = connect().transmit(cmd);

Assert.assertNotNull(responseAPDU);
Assert.assertEquals(0x9000, responseAPDU.getSW());
Assert.assertNotNull(responseAPDU.getBytes());
System.out.print(responseAPDU.getBytes() + System.lineSeparator());
Assertions.assertNotNull(responseAPDU);
Assertions.assertEquals(0x9000, responseAPDU.getSW());
Assertions.assertNotNull(responseAPDU.getBytes());
System.out.print(Arrays.toString(responseAPDU.getBytes()) + System.lineSeparator());
System.out.print("Test hell: passed" + System.lineSeparator());
}

Expand All @@ -68,6 +69,6 @@ public void callMyTest() throws Exception {
ImpTrxUnitTests IT = new ImpTrxUnitTests();
AppletControl AC = new AppletControl();
//IT.runAllTests();
AC.DownloadTest();
AC.AppletDebug();
}
}
45 changes: 42 additions & 3 deletions applet/src/test/java/tests/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,45 @@
import applet.AppletInstructions;
import cz.muni.fi.crocs.rcard.client.CardManager;

import javax.smartcardio.Card;
import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import java.util.Arrays;

public class Download {
public short downloadVersion(CardManager manager) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP, AppletInstructions.INS_DOWNLOAD_VERSION, 0, 0);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return (short) (ar[0] << 8 | ar[1]);
}

public short downloadSize(CardManager manager) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP, AppletInstructions.INS_DOWNLOAD_SIZE, 0, 0);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return (short) (ar[0] << 8 | ar[1]);
}
public short downloadNumOfInp(CardManager manager) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP, AppletInstructions.INS_DOWNLOAD_NUM_INPUT, 0, 0);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return (short) (ar[0] << 8 | ar[1]);
}

public short downloadNumOfOut(CardManager manager) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP, AppletInstructions.INS_DOWNLOAD_NUM_OUTPUT, 0, 0);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return (short) (ar[0] << 8 | ar[1]);
}

public byte[] downloadMap(CardManager manager, short map, byte position) throws CardException {
CommandAPDU cmd;
Expand All @@ -19,7 +50,6 @@ public byte[] downloadMap(CardManager manager, short map, byte position) throws
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return downloadDebugArray(manager,(short) (ar[0] << 8 | ar[1]), (short) (ar[2] << 8 | ar[3]));

}

public byte[] downloadDebugArray(CardManager manager, short from, short to) throws CardException {
Expand Down Expand Up @@ -56,4 +86,13 @@ public byte[] downloadDebugArray(CardManager manager, short from, short to) thro
}
return res;
}

public byte[] downloadGlobalKeypair(CardManager manager, int position) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP_KEYPAIR, 0,position, 0, null, 0, 0, 4);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return downloadDebugArray(manager,(short) (ar[0] << 8 | ar[1]), (short) (ar[2] << 8 | ar[3]));
}
}
4 changes: 1 addition & 3 deletions applet/src/test/java/tests/Upload.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tests;
import applet.*;
import cz.muni.fi.crocs.rcard.client.CardManager;
import javacard.framework.Util;

import javax.smartcardio.Card;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import java.nio.charset.StandardCharsets;
Expand All @@ -26,7 +24,7 @@ public Upload() {
public void handshake() throws Exception{
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.HAND_SHAKE, AppletInstructions.INS_REQUEST, 0, 0);
cmd = new CommandAPDU(AppletInstructions.CLASS_HAND_SHAKE, AppletInstructions.INS_REQUEST, 0, 0);

CardManager manager = connect();
rsp = manager.transmit(cmd);
Expand Down

0 comments on commit e2e1cad

Please sign in to comment.