-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: uses manager instead of simulator now, still dunno why tests mu…
…st be ran twice to pass
- Loading branch information
1 parent
b214bff
commit 638aad8
Showing
10 changed files
with
357 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.