diff --git a/java/net/ss3t/javacard/gpg/Gpg.java b/java/net/ss3t/javacard/gpg/Gpg.java index b5e1bea..8f2cd7f 100644 --- a/java/net/ss3t/javacard/gpg/Gpg.java +++ b/java/net/ss3t/javacard/gpg/Gpg.java @@ -395,6 +395,20 @@ private void verify(APDU apdu) { ISOException.throwIt((short) (SW_PIN_FAILED_00 + pins[pinOffset].getTriesRemaining())); } + /** + * Udpate the PIN and its length in a transaction. + * @param pinId which PIN will be updated. + * @param data contains the new PIN. + * @param dataOffset first byte of the new PIN in the data array. + * @param newLength the new PIN length. + */ + private void updatePIN(short pinId, byte[] data, short dataOffset, byte newLength) { + JCSystem.beginTransaction(); + pins[pinId].update(data, dataOffset, newLength); + pinLength[pinId] = newLength; + JCSystem.commitTransaction(); + } + /** * CHANGE REFERENCE DATA APDU implementation. */ @@ -426,9 +440,8 @@ private void changeReferenceData(APDU apdu) { pinSubmitted[0] = false; ISOException.throwIt((short) (SW_PIN_FAILED_00 + pins[pinOffset].getTriesRemaining())); } - pins[pinOffset].update(buffer, (short) (ISO7816.OFFSET_CDATA + currentLength), - (byte) (length - currentLength)); - pinLength[pinOffset] = (byte) (length - currentLength); + updatePIN(pinOffset, buffer, (short) (ISO7816.OFFSET_CDATA + currentLength), + (byte) (length - currentLength)); } /** @@ -455,9 +468,8 @@ private void resetRetryCounter(APDU apdu) { ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); } if (pins[PIN_INDEX_RC].check(buffer, ISO7816.OFFSET_CDATA, rcLength)) { - pins[PIN_INDEX_PW1].update(buffer, (short) (ISO7816.OFFSET_CDATA + rcLength), - (byte) (length - rcLength)); - pinLength[PIN_INDEX_PW1] = (byte) (length - rcLength); + updatePIN(PIN_INDEX_PW1, buffer, (short) (ISO7816.OFFSET_CDATA + rcLength), + (byte) (length - rcLength)); } else { ISOException.throwIt((short) (SW_PIN_FAILED_00 + pins[PIN_INDEX_RC].getTriesRemaining())); } @@ -469,8 +481,7 @@ private void resetRetryCounter(APDU apdu) { if (length < MIN_PIN1_LENGTH || length > MAX_PIN_LENGTH) { ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); } - pins[PIN_INDEX_PW1].update(buffer, ISO7816.OFFSET_CDATA, (byte) length); - pinLength[PIN_INDEX_PW1] = (byte) length; + updatePIN(PIN_INDEX_PW1, buffer, ISO7816.OFFSET_CDATA, (byte) length); } else { ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2); } @@ -779,8 +790,7 @@ private void putData(APDU apdu) { if (length > MAX_PIN_LENGTH || (length != (byte) 0 && length < (byte) 8)) { ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); } - pinLength[PIN_INDEX_RC] = buffer[0]; - pins[PIN_INDEX_RC].update(buffer, (short) 1, buffer[0]); + updatePIN(PIN_INDEX_RC, buffer, (short) 1, buffer[0]); break; default: diff --git a/javatest/net/ss3t/javacard/SimulatedCard/SimulatedCard.java b/javatest/net/ss3t/javacard/SimulatedCard/SimulatedCard.java deleted file mode 100644 index 36ba7cf..0000000 --- a/javatest/net/ss3t/javacard/SimulatedCard/SimulatedCard.java +++ /dev/null @@ -1,60 +0,0 @@ -/* -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -package net.ss3t.javacard.SimulatedCard; - -import com.licel.jcardsim.base.Simulator; - -import net.ss3t.javacard.CardInterface; - -import java.util.logging.Logger; - -import javax.smartcardio.CardException; -import javax.smartcardio.CommandAPDU; -import javax.smartcardio.ResponseAPDU; - -import javacard.framework.AID; - -/** - * Uses the jcardSim classes - */ -public class SimulatedCard implements CardInterface { - - private Simulator simulator = null; - protected static final Logger logger = Logger.getLogger("Simulated Card"); - - public boolean initialize(byte[] aid, Class applet) { - simulator = new Simulator(); - simulator.resetRuntime(); - AID appletAID = new AID(aid, (short) 0, (byte) aid.length); - simulator.installApplet(appletAID, applet); - return true; - } - - @Override - public void reset() throws CardException { - simulator.reset(); - } - - @Override - public boolean selectApplet(byte[] aid) throws CardException { - return simulator.selectApplet(new AID(aid, (short) 0, (byte) aid.length)); - } - - @Override - public ResponseAPDU sendAPDU(CommandAPDU command) throws CardException { - return new ResponseAPDU(simulator.transmitCommand(command.getBytes())); - } -} diff --git a/javatest/net/ss3t/javacard/gpg/simulated/GpgCryptoTestSimulated.java b/javatest/net/ss3t/javacard/gpg/simulated/GpgCryptoTestSimulated.java deleted file mode 100644 index aebb9e3..0000000 --- a/javatest/net/ss3t/javacard/gpg/simulated/GpgCryptoTestSimulated.java +++ /dev/null @@ -1,30 +0,0 @@ -/* -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ -package net.ss3t.javacard.gpg.simulated; - -import net.ss3t.javacard.gpg.GpgCryptoTest; - -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - */ -@RunWith(JUnit4.class) -public class GpgCryptoTestSimulated extends GpgCryptoTest { - - public GpgCryptoTestSimulated() { - super(new GpgTestSimulated()); - } -} diff --git a/javatest/net/ss3t/javacard/gpg/simulated/GpgDataTestSimulated.java b/javatest/net/ss3t/javacard/gpg/simulated/GpgDataTestSimulated.java deleted file mode 100644 index 746848c..0000000 --- a/javatest/net/ss3t/javacard/gpg/simulated/GpgDataTestSimulated.java +++ /dev/null @@ -1,28 +0,0 @@ -/* -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ -package net.ss3t.javacard.gpg.simulated; - -import net.ss3t.javacard.gpg.GpgDataTest; - -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GpgDataTestSimulated extends GpgDataTest { - - public GpgDataTestSimulated() { - super(new GpgTestSimulated()); - } -} diff --git a/javatest/net/ss3t/javacard/gpg/simulated/GpgPINTestSimulated.java b/javatest/net/ss3t/javacard/gpg/simulated/GpgPINTestSimulated.java deleted file mode 100644 index 3936137..0000000 --- a/javatest/net/ss3t/javacard/gpg/simulated/GpgPINTestSimulated.java +++ /dev/null @@ -1,29 +0,0 @@ -/* -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -package net.ss3t.javacard.gpg.simulated; - -import net.ss3t.javacard.gpg.GpgPINTest; - -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GpgPINTestSimulated extends GpgPINTest { - - public GpgPINTestSimulated() { - super(new GpgTestSimulated()); - } -} diff --git a/javatest/net/ss3t/javacard/gpg/simulated/GpgTestSimulated.java b/javatest/net/ss3t/javacard/gpg/simulated/GpgTestSimulated.java deleted file mode 100644 index 75b43b5..0000000 --- a/javatest/net/ss3t/javacard/gpg/simulated/GpgTestSimulated.java +++ /dev/null @@ -1,38 +0,0 @@ -/* -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -package net.ss3t.javacard.gpg.simulated; - -import net.ss3t.javacard.CardInterface; -import net.ss3t.javacard.CardInterfaceBuilder; -import net.ss3t.javacard.SimulatedCard.SimulatedCard; -import net.ss3t.javacard.gpg.Gpg; -import net.ss3t.javacard.gpg.GpgPINTest; - -import javax.smartcardio.CardException; - -import static org.junit.Assert.assertTrue; - -/** - */ -public class GpgTestSimulated implements CardInterfaceBuilder { - - @Override - public CardInterface getCardInterface() throws CardException { - SimulatedCard card = new SimulatedCard(); - assertTrue(card.initialize(GpgPINTest.appletAID, Gpg.class)); - return card; - } -}