Skip to content

Commit

Permalink
feat: uses manager instead of simulator now, still dunno why tests mu…
Browse files Browse the repository at this point in the history
…st be ran twice to pass
  • Loading branch information
jan-sekanina committed Feb 15, 2023
1 parent b214bff commit 638aad8
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 209 deletions.
8 changes: 7 additions & 1 deletion applet/src/main/java/main/AppletControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

public class AppletControl {
CardSimulator simulator = null;
CardManager manager = null;
CardManager manager = new CardManager();
Upload mu = new Upload();
Download md = new Download();
byte[] psbt = null;



public void UploadData(CardSimulator simulator, Upload mu) throws Exception {
mu.sendData(psbt, (byte) AppletInstructions.CLASS_PSBT_UPLOAD, simulator);
}
Expand Down Expand Up @@ -66,6 +68,10 @@ public AppletControl(CardSimulator sim, String psbt) throws Exception {
this.psbt = fromHex(psbt);
}

public AppletControl(CardManager man, byte[] psbt) throws Exception {
this.psbt = psbt;
}

public AppletControl(CardSimulator sim, byte[] psbt) throws Exception {
simulator = sim;
AID appletAID = AIDUtil.create("F000000001");
Expand Down
1 change: 0 additions & 1 deletion applet/src/main/java/main/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import applet.AppletInstructions;
import com.licel.jcardsim.smartcardio.CardSimulator;

import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
Expand Down
1 change: 0 additions & 1 deletion applet/src/main/java/main/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.licel.jcardsim.smartcardio.CardSimulator;


public class Run {
public static void main(String[] args) throws Exception {
// ./gradlew run --args="arg1 arg2 arg3"
Expand Down
143 changes: 143 additions & 0 deletions applet/src/test/java/communication/Download.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package communication;

import applet.AppletInstructions;
import com.licel.jcardsim.smartcardio.CardSimulator;
import cz.muni.fi.crocs.rcard.client.CardManager;

import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;

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] & 0xff) << 8 | (ar[1] & 0xff));
}
public short downloadNumOfInp(CardManager manager) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP, AppletInstructions.INS_DOWNLOAD_NUM_INPUT_V0, 0, 0);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return (short) ((ar[0] & 0xff) << 8 | (ar[1] & 0xff));
}

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

public byte[] downloadInput(CardManager manager, byte input_index) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_INPUT, 0, input_index, 0);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return download(manager ,(short) ((ar[0] & 0xff) << 8 | (ar[1] & 0xff)),
(short) ((ar[2] & 0xff) << 8 | (ar[3] & 0xff)));
}

public byte[] downloadOutput(CardManager manager, byte output_index) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(AppletInstructions.CLASS_DOWNLOAD_OUTPUT, 0, output_index, 0);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return download(manager,(short) ((ar[0] & 0xff) << 8 | (ar[1] & 0xff)),
(short) ((ar[2] & 0xff) << 8 | (ar[3] & 0xff)));
}

public byte[] downloadMap(CardManager manager, short map, byte position) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
cmd = new CommandAPDU(map, 0, position, 0, null, 0, 0, 4);
rsp = manager.transmit(cmd);
byte[] ar = rsp.getData();
return download(manager,(short) ((ar[0] & 0xff) << 8 | (ar[1] & 0xff)),
(short) ((ar[2] & 0xff) << 8 | (ar[3] & 0xff)));
}

public byte[] download(CardManager manager, short from, short to) throws CardException {
CommandAPDU cmd;
ResponseAPDU rsp;
short offset = 0;
byte[] communicationArray = new byte[4];

byte[] res = new byte[to - from];

while (offset + AppletInstructions.PACKET_BUFFER_SIZE < to) {
communicationArray[0] = (byte) ((from + offset) >> 8);
communicationArray[1] = (byte) (from + offset);
communicationArray[2] = (byte) ((from + offset + AppletInstructions.PACKET_BUFFER_SIZE) >> 8);
communicationArray[3] = (byte) (from + offset + AppletInstructions.PACKET_BUFFER_SIZE);
cmd = new CommandAPDU(AppletInstructions.CLASS_DEBUG_DOWNLOAD, AppletInstructions.INS_DOWNLOAD_ARRAY, 0, 0, communicationArray, communicationArray.length);
rsp = manager.transmit(cmd);
assert rsp.getSW() == 0x9000;
System.arraycopy(rsp.getData(), (short) 0, res, offset, AppletInstructions.PACKET_BUFFER_SIZE);
offset += AppletInstructions.PACKET_BUFFER_SIZE;
}

if (offset < to - from) {
communicationArray[0] = (byte) ((from + offset) >> 8);
communicationArray[1] = (byte) (from + offset);
communicationArray[2] = (byte) (to >> 8);
communicationArray[3] = (byte) to;
cmd = new CommandAPDU(AppletInstructions.CLASS_DEBUG_DOWNLOAD, AppletInstructions.INS_DOWNLOAD_ARRAY, 0, 0, communicationArray, communicationArray.length);
rsp = manager.transmit(cmd);
assert rsp.getSW() == 0x9000;
System.arraycopy(rsp.getData(), (short) 0, res, offset, to - (offset + from));
}
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 download(manager,(short) (ar[0] << 8 | ar[1]), (short) (ar[2] << 8 | ar[3]));
}

public static byte[] fromHex(String hex){
// ukradeno
byte[] res = new byte[hex.length() / 2];

for (int i = 0; i < res.length; i++) {
int index = i * 2;
int j = Integer.parseInt(hex.substring(index, index + 2), 16);
res[i] = (byte) j;
}

return res;
}

public String bytesToHex(byte[] bytes) {
StringBuilder res = new StringBuilder();
int cb;

for (byte aByte : bytes) {
cb = aByte & 0xFF;
res.append(Integer.toHexString(cb / 16));
res.append(Integer.toHexString(cb % 16));
}
return res.toString();
}
}
54 changes: 54 additions & 0 deletions applet/src/test/java/communication/Upload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package communication;

import applet.AppletInstructions;
import com.licel.jcardsim.smartcardio.CardSimulator;
import cz.muni.fi.crocs.rcard.client.CardManager;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;

/**
* class of my tests
*/

public class Upload {

public Upload() {
}

/**
*
* @param data data that are sent to the applet
* @param uploadClass information for applet what to do with the data
* @return array of byte of applet response, array size is the size of one packet
* @throws Exception
*/

public byte[] sendData(byte[] data, byte uploadClass, CardManager manager) throws Exception {
CommandAPDU cmd;
ResponseAPDU rsp;
short packetSize = AppletInstructions.PACKET_BUFFER_SIZE;
cmd = new CommandAPDU(uploadClass, AppletInstructions.INS_REQUEST, 0, 0);
rsp = manager.transmit(cmd);
assert rsp.getSW() == 0x9000;

int offset = 0;

while (offset + packetSize < data.length) {
cmd = new CommandAPDU(uploadClass, AppletInstructions.INS_UPLOAD, 0, 0, data, offset, packetSize, 0);
rsp = manager.transmit(cmd);
assert rsp.getSW() == 0x9000;
offset += packetSize;
}

cmd = new CommandAPDU(uploadClass, AppletInstructions.INS_UPLOAD, 0, 0, data, offset, data.length - offset);
rsp = manager.transmit(cmd);
assert rsp.getSW() == 0x9000;

cmd = new CommandAPDU(uploadClass, AppletInstructions.INS_FINISH, 0, 0);
rsp = manager.transmit(cmd);
assert rsp.getSW() == 0x9000;
// cmd = new CommandAPDU(1, 1, 1, 1, 1, 1, 1, 1);

return rsp.getBytes();
}
}
Loading

0 comments on commit 638aad8

Please sign in to comment.