|
| 1 | +package opencrypto.test; |
| 2 | + |
| 3 | +import com.licel.jcardsim.io.CAD; |
| 4 | +import com.licel.jcardsim.io.JavaxSmartCardInterface; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import javacard.framework.AID; |
| 8 | +import javax.smartcardio.Card; |
| 9 | +import javax.smartcardio.CardChannel; |
| 10 | +import javax.smartcardio.CardException; |
| 11 | +import javax.smartcardio.CardTerminal; |
| 12 | +import javax.smartcardio.CardTerminals; |
| 13 | +import javax.smartcardio.CommandAPDU; |
| 14 | +import javax.smartcardio.ResponseAPDU; |
| 15 | +import javax.smartcardio.TerminalFactory; |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | +* @author Petr Svenda |
| 20 | + */ |
| 21 | +public class CardManager { |
| 22 | + boolean m_bDebug = false; |
| 23 | + byte[] m_APPLET_AID = null; |
| 24 | + Long m_lastTransmitTime = (long) 0; |
| 25 | + CommandAPDU m_lastCommand = null; |
| 26 | + CardChannel m_channel = null; |
| 27 | + |
| 28 | + public CardManager(boolean bDebug, byte[] appletAID) { |
| 29 | + this.m_bDebug = bDebug; |
| 30 | + this.m_APPLET_AID = appletAID; |
| 31 | + } |
| 32 | + |
| 33 | + // Card Logistics |
| 34 | + public boolean Connect(RunConfig runCfg) throws Exception { |
| 35 | + boolean bConnected = false; |
| 36 | + switch (runCfg.testCardType) { |
| 37 | + case PHYSICAL: { |
| 38 | + m_channel = ConnectPhysicalCard(runCfg.targetReaderIndex); |
| 39 | + break; |
| 40 | + } |
| 41 | + case JCOPSIM: { |
| 42 | + m_channel = ConnectJCOPSimulator(runCfg.targetReaderIndex); |
| 43 | + break; |
| 44 | + } |
| 45 | + case JCARDSIMLOCAL: { |
| 46 | + m_channel = ConnectJCardSimLocalSimulator(runCfg.appletToSimulate); |
| 47 | + break; |
| 48 | + } |
| 49 | + case JCARDSIMREMOTE: { |
| 50 | + m_channel = null; // Not implemented yet |
| 51 | + break; |
| 52 | + } |
| 53 | + default: |
| 54 | + m_channel = null; |
| 55 | + bConnected = false; |
| 56 | + |
| 57 | + } |
| 58 | + if (m_channel != null) { |
| 59 | + bConnected = true; |
| 60 | + } |
| 61 | + return bConnected; |
| 62 | + } |
| 63 | + |
| 64 | + public void Disconnect(boolean bReset) throws CardException { |
| 65 | + m_channel.getCard().disconnect(bReset); // Disconnect from the card |
| 66 | + } |
| 67 | + |
| 68 | + public CardChannel ConnectPhysicalCard(int targetReaderIndex) throws Exception { |
| 69 | + // JCOP Simulators |
| 70 | + System.out.print("Looking for physical cards... "); |
| 71 | + return connectToCardByTerminalFactory(TerminalFactory.getDefault(), targetReaderIndex); |
| 72 | + } |
| 73 | + |
| 74 | + public CardChannel ConnectJCOPSimulator(int targetReaderIndex) throws Exception { |
| 75 | + // JCOP Simulators |
| 76 | + System.out.print("Looking for JCOP simulators..."); |
| 77 | + int[] ports = new int[]{8050}; |
| 78 | + return connectToCardByTerminalFactory(TerminalFactory.getInstance("JcopEmulator", ports), targetReaderIndex); |
| 79 | + } |
| 80 | + |
| 81 | + private CardChannel ConnectJCardSimLocalSimulator(Class appletClass) throws Exception { |
| 82 | + System.setProperty("com.licel.jcardsim.terminal.type", "2"); |
| 83 | + CAD cad = new CAD(System.getProperties()); |
| 84 | + JavaxSmartCardInterface simulator = (JavaxSmartCardInterface) cad.getCardInterface(); |
| 85 | + byte[] installData = new byte[0]; |
| 86 | + AID appletAID = new AID(m_APPLET_AID, (short) 0, (byte) m_APPLET_AID.length); |
| 87 | + |
| 88 | + AID appletAIDRes = simulator.installApplet(appletAID, appletClass, installData, (short) 0, (byte) installData.length); |
| 89 | + simulator.selectApplet(appletAID); |
| 90 | + |
| 91 | + return new SimulatedCardChannelLocal(simulator); |
| 92 | + } |
| 93 | + |
| 94 | + private CardChannel connectToCardByTerminalFactory(TerminalFactory factory, int targetReaderIndex) throws CardException { |
| 95 | + List<CardTerminal> terminals = new ArrayList<>(); |
| 96 | + |
| 97 | + boolean card_found = false; |
| 98 | + CardTerminal terminal = null; |
| 99 | + Card card = null; |
| 100 | + try { |
| 101 | + for (CardTerminal t : factory.terminals().list()) { |
| 102 | + terminals.add(t); |
| 103 | + if (t.isCardPresent()) { |
| 104 | + card_found = true; |
| 105 | + } |
| 106 | + } |
| 107 | + System.out.println("Success."); |
| 108 | + } catch (Exception e) { |
| 109 | + System.out.println("Failed."); |
| 110 | + } |
| 111 | + |
| 112 | + if (card_found) { |
| 113 | + System.out.println("Cards found: " + terminals); |
| 114 | + |
| 115 | + terminal = terminals.get(targetReaderIndex); // Prioritize physical card over simulations |
| 116 | + |
| 117 | + System.out.print("Connecting..."); |
| 118 | + card = terminal.connect("*"); // Connect with the card |
| 119 | + |
| 120 | + System.out.println(" Done."); |
| 121 | + |
| 122 | + System.out.print("Establishing channel..."); |
| 123 | + m_channel = card.getBasicChannel(); |
| 124 | + |
| 125 | + System.out.println(" Done."); |
| 126 | + |
| 127 | + // Select applet (mpcapplet) |
| 128 | + System.out.println("Smartcard: Selecting applet..."); |
| 129 | + |
| 130 | + CommandAPDU cmd = new CommandAPDU(0x00, 0xa4, 0x04, 0x00, m_APPLET_AID); |
| 131 | + ResponseAPDU response = transmit(cmd); |
| 132 | + } else { |
| 133 | + System.out.print("Failed to find physical card."); |
| 134 | + } |
| 135 | + |
| 136 | + if (card != null) { |
| 137 | + return card.getBasicChannel(); |
| 138 | + } else { |
| 139 | + return null; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public ResponseAPDU transmit(CommandAPDU cmd) |
| 144 | + throws CardException { |
| 145 | + |
| 146 | + m_lastCommand = cmd; |
| 147 | + if (m_bDebug == true) { |
| 148 | + log(cmd); |
| 149 | + } |
| 150 | + |
| 151 | + long elapsed = -System.currentTimeMillis(); |
| 152 | + ResponseAPDU response = m_channel.transmit(cmd); |
| 153 | + elapsed += System.currentTimeMillis(); |
| 154 | + m_lastTransmitTime = elapsed; |
| 155 | + |
| 156 | + if (m_bDebug == true) { |
| 157 | + log(response, m_lastTransmitTime); |
| 158 | + } |
| 159 | + |
| 160 | + return response; |
| 161 | + } |
| 162 | + |
| 163 | + private void log(CommandAPDU cmd) { |
| 164 | + System.out.printf("--> %s\n", Util.toHex(cmd.getBytes()), |
| 165 | + cmd.getBytes().length); |
| 166 | + } |
| 167 | + |
| 168 | + private void log(ResponseAPDU response, long time) { |
| 169 | + String swStr = String.format("%02X", response.getSW()); |
| 170 | + byte[] data = response.getData(); |
| 171 | + if (data.length > 0) { |
| 172 | + System.out.printf("<-- %s %s (%d) [%d ms]\n", Util.toHex(data), swStr, |
| 173 | + data.length, time); |
| 174 | + } else { |
| 175 | + System.out.printf("<-- %s [%d ms]\n", swStr, time); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void log(ResponseAPDU response) { |
| 180 | + log(response, 0); |
| 181 | + } |
| 182 | + |
| 183 | + private Card waitForCard(CardTerminals terminals) |
| 184 | + throws CardException { |
| 185 | + while (true) { |
| 186 | + for (CardTerminal ct : terminals |
| 187 | + .list(CardTerminals.State.CARD_INSERTION)) { |
| 188 | + |
| 189 | + return ct.connect("*"); |
| 190 | + } |
| 191 | + terminals.waitForChange(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments