From 88dc8bb094318f0bcd50776ad6241ab5cace17a2 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 3 Nov 2023 01:33:27 +0100 Subject: [PATCH] Refactor format of the `MyApplet.java` file --- hwb1/src/hwb1/MyApplet.java | 103 ++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/hwb1/src/hwb1/MyApplet.java b/hwb1/src/hwb1/MyApplet.java index 53d276e..30e1e08 100644 --- a/hwb1/src/hwb1/MyApplet.java +++ b/hwb1/src/hwb1/MyApplet.java @@ -29,13 +29,13 @@ public class MyApplet extends Applet { * @param bLength the length in bytes of the parameter data in bArray */ public static void install(byte[] buffer, short offset, byte length) { - byte aidLength = buffer[offset]; + byte aidLength = buffer[offset]; - short controlInfoOffset = (short)(offset + 1 + aidLength); - byte controlInfoLength = buffer[controlInfoOffset]; - - short dataOffset = (short)(controlInfoOffset + 1 + controlInfoLength); - byte dataLength = buffer[dataOffset]; + short controlInfoOffset = (short)(offset + 1 + aidLength); + byte controlInfoLength = buffer[controlInfoOffset]; + + short dataOffset = (short)(controlInfoOffset + 1 + controlInfoLength); + byte dataLength = buffer[dataOffset]; new MyApplet(buffer, (short)(dataOffset + 1), dataLength); } @@ -55,6 +55,7 @@ public class MyApplet extends Applet { public boolean select() { if (pin.getTriesRemaining() == 0) { + ISOException.throwIt(SW_VERIFICATION_FAILED); return false; } else { return super.select(); @@ -62,8 +63,8 @@ public class MyApplet extends Applet { } public void deselect() { - super.deselect(); - pin.reset(); + super.deselect(); + pin.reset(); } /** @@ -82,61 +83,61 @@ public class MyApplet extends Applet { if (buffer[ISO7816.OFFSET_CLA] != (byte)0x80) { ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); } else { - boolean authenticationRequired = false; + boolean authenticationRequired = false; byte instruction = buffer[ISO7816.OFFSET_INS]; switch (instruction) { - case 0x04: - case 0x02: - authenticationRequired = !pin.isValidated(); - break; + case 0x02: + case 0x04: + authenticationRequired = !pin.isValidated(); + break; } if (authenticationRequired) { - ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED); + ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED); } else { - switch (instruction) { - case 0x00: - case 0x04: - byte[] response; - length = apdu.setOutgoing(); + switch (instruction) { + case 0x00: + case 0x04: + byte[] response; + length = apdu.setOutgoing(); - if (instruction == 0x00) { - response = authorName; - length = (short)response.length; - } else { - response = storage; + 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)); - } - } + 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; - case 0x20: - length = apdu.setIncomingAndReceive(); - - if (!pin.check(buffer, (short) ISO7816.OFFSET_CDATA, (byte)length)) { - ISOException.throwIt(SW_VERIFICATION_FAILED); - } + apdu.setOutgoingLength(length); + apdu.sendBytesLong(response, (short) 0, length); break; - default: - ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); - 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; + default: + ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); + break; + } } ISOException.throwIt(ISO7816.SW_NO_ERROR);