Refactor format of the MyApplet.java file

This commit is contained in:
Manuel Thalmann 2023-11-03 01:33:27 +01:00
parent 6070b53cdf
commit 88dc8bb094

View file

@ -29,13 +29,13 @@ public class MyApplet extends Applet {
* @param bLength the length in bytes of the parameter data in bArray * @param bLength the length in bytes of the parameter data in bArray
*/ */
public static void install(byte[] buffer, short offset, byte length) { public static void install(byte[] buffer, short offset, byte length) {
byte aidLength = buffer[offset]; byte aidLength = buffer[offset];
short controlInfoOffset = (short)(offset + 1 + aidLength); short controlInfoOffset = (short)(offset + 1 + aidLength);
byte controlInfoLength = buffer[controlInfoOffset]; byte controlInfoLength = buffer[controlInfoOffset];
short dataOffset = (short)(controlInfoOffset + 1 + controlInfoLength); short dataOffset = (short)(controlInfoOffset + 1 + controlInfoLength);
byte dataLength = buffer[dataOffset]; byte dataLength = buffer[dataOffset];
new MyApplet(buffer, (short)(dataOffset + 1), dataLength); new MyApplet(buffer, (short)(dataOffset + 1), dataLength);
} }
@ -55,6 +55,7 @@ public class MyApplet extends Applet {
public boolean select() { public boolean select() {
if (pin.getTriesRemaining() == 0) { if (pin.getTriesRemaining() == 0) {
ISOException.throwIt(SW_VERIFICATION_FAILED);
return false; return false;
} else { } else {
return super.select(); return super.select();
@ -62,8 +63,8 @@ public class MyApplet extends Applet {
} }
public void deselect() { public void deselect() {
super.deselect(); super.deselect();
pin.reset(); pin.reset();
} }
/** /**
@ -82,61 +83,61 @@ public class MyApplet extends Applet {
if (buffer[ISO7816.OFFSET_CLA] != (byte)0x80) { if (buffer[ISO7816.OFFSET_CLA] != (byte)0x80) {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
} else { } else {
boolean authenticationRequired = false; boolean authenticationRequired = false;
byte instruction = buffer[ISO7816.OFFSET_INS]; byte instruction = buffer[ISO7816.OFFSET_INS];
switch (instruction) { switch (instruction) {
case 0x04: case 0x02:
case 0x02: case 0x04:
authenticationRequired = !pin.isValidated(); authenticationRequired = !pin.isValidated();
break; break;
} }
if (authenticationRequired) { if (authenticationRequired) {
ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED); ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
} }
else { else {
switch (instruction) { switch (instruction) {
case 0x00: case 0x00:
case 0x04: case 0x04:
byte[] response; byte[] response;
length = apdu.setOutgoing(); length = apdu.setOutgoing();
if (instruction == 0x00) { if (instruction == 0x00) {
response = authorName; response = authorName;
length = (short)response.length; length = (short)response.length;
} else { } else {
response = storage; response = storage;
if (length > response.length) { if (length > response.length) {
ISOException.throwIt((short)(ISO7816.SW_CORRECT_LENGTH_00 | response.length)); ISOException.throwIt((short)(ISO7816.SW_CORRECT_LENGTH_00 | response.length));
} }
} }
apdu.setOutgoingLength(length); apdu.setOutgoingLength(length);
apdu.sendBytesLong(response, (short) 0, 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;
case 0x20:
length = apdu.setIncomingAndReceive();
if (!pin.check(buffer, (short) ISO7816.OFFSET_CDATA, (byte)length)) {
ISOException.throwIt(SW_VERIFICATION_FAILED);
}
break; break;
default: case 0x02:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); length = apdu.setIncomingAndReceive();
break; 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;
case 0x20:
length = apdu.setIncomingAndReceive();
if (!pin.check(buffer, (short) ISO7816.OFFSET_CDATA, (byte)length)) {
ISOException.throwIt(SW_VERIFICATION_FAILED);
}
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
break;
}
} }
ISOException.throwIt(ISO7816.SW_NO_ERROR); ISOException.throwIt(ISO7816.SW_NO_ERROR);