|
| 1 | +/* |
| 2 | + * Copyright 2018 Joyent, Inc |
| 3 | + * Copyright 2020 The University of Queensland |
| 4 | + * Copyright 2013 Licel LLC. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.licel.jcardsim.remote; |
| 19 | + |
| 20 | +import com.licel.jcardsim.base.CardManager; |
| 21 | +import com.licel.jcardsim.base.Simulator; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.security.InvalidParameterException; |
| 25 | +import java.util.Enumeration; |
| 26 | +import java.util.Properties; |
| 27 | + |
| 28 | +/** |
| 29 | + * VSmartCard Card Implementation. |
| 30 | + * |
| 31 | + |
| 32 | + */ |
| 33 | +public class VSmartCard { |
| 34 | + |
| 35 | + Simulator sim; |
| 36 | + |
| 37 | + public VSmartCard(String host, int port) throws IOException { |
| 38 | + VSmartCardTCPProtocol driverProtocol = new VSmartCardTCPProtocol(); |
| 39 | + driverProtocol.connect(host, port); |
| 40 | + startThread(driverProtocol); |
| 41 | + } |
| 42 | + |
| 43 | + static public void main(String args[]) throws Exception { |
| 44 | + if (args.length !=1) { |
| 45 | + System.out.println("Usage: java com.licel.jcardsim.remote.VSmartCard <jcardsim.cfg>"); |
| 46 | + System.exit(-1); |
| 47 | + } |
| 48 | + Properties cfg = new Properties(); |
| 49 | + // init Simulator |
| 50 | + FileInputStream fis = null; |
| 51 | + try { |
| 52 | + fis = new FileInputStream(args[0]); |
| 53 | + cfg.load(fis); |
| 54 | + } catch (Throwable t) { |
| 55 | + System.err.println("Unable to load configuration " + args[0] + " due to: " + t.getMessage()); |
| 56 | + System.exit(-1); |
| 57 | + } finally { |
| 58 | + if (fis != null) { |
| 59 | + fis.close(); |
| 60 | + } |
| 61 | + } |
| 62 | + final Enumeration<?> keys = cfg.propertyNames(); |
| 63 | + while (keys.hasMoreElements()) { |
| 64 | + String propertyName = (String) keys.nextElement(); |
| 65 | + System.setProperty(propertyName, cfg.getProperty(propertyName)); |
| 66 | + } |
| 67 | + |
| 68 | + String propKey = "com.licel.jcardsim.vsmartcard.host"; |
| 69 | + String host = System.getProperty(propKey); |
| 70 | + if (host == null) { |
| 71 | + throw new InvalidParameterException("Missing value for property: " + propKey); |
| 72 | + } |
| 73 | + |
| 74 | + propKey = "com.licel.jcardsim.vsmartcard.port"; |
| 75 | + String port = System.getProperty(propKey); |
| 76 | + if (port == null) { |
| 77 | + throw new InvalidParameterException("Missing value for property: " + propKey); |
| 78 | + } |
| 79 | + |
| 80 | + new VSmartCard(host, Integer.parseInt(port)); |
| 81 | + } |
| 82 | + |
| 83 | + private void startThread(VSmartCardTCPProtocol driverProtocol) throws IOException { |
| 84 | + sim = new Simulator(); |
| 85 | + final IOThread ioThread = new IOThread(sim, driverProtocol); |
| 86 | + ShutDownHook hook = new ShutDownHook(ioThread); |
| 87 | + Runtime.getRuntime().addShutdownHook(hook); |
| 88 | + ioThread.start(); |
| 89 | + } |
| 90 | + |
| 91 | + static class ShutDownHook extends Thread { |
| 92 | + IOThread ioThread; |
| 93 | + |
| 94 | + public ShutDownHook(IOThread ioThread) { |
| 95 | + this.ioThread = ioThread; |
| 96 | + } |
| 97 | + |
| 98 | + public void run() { |
| 99 | + ioThread.isRunning = false; |
| 100 | + System.out.println("Shutdown connections"); |
| 101 | + ioThread.driverProtocol.disconnect(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static class IOThread extends Thread { |
| 106 | + VSmartCardTCPProtocol driverProtocol; |
| 107 | + Simulator sim; |
| 108 | + boolean isRunning; |
| 109 | + |
| 110 | + public IOThread(Simulator sim, VSmartCardTCPProtocol driverProtocol) { |
| 111 | + this.sim = sim; |
| 112 | + this.driverProtocol = driverProtocol; |
| 113 | + isRunning = true; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void run() { |
| 118 | + while (isRunning) { |
| 119 | + try { |
| 120 | + int cmd = driverProtocol.readCommand(); |
| 121 | + switch (cmd) { |
| 122 | + case VSmartCardTCPProtocol.POWER_ON: |
| 123 | + case VSmartCardTCPProtocol.RESET: |
| 124 | + sim.reset(); |
| 125 | + break; |
| 126 | + case VSmartCardTCPProtocol.GET_ATR: |
| 127 | + driverProtocol.writeData(sim.getATR()); |
| 128 | + break; |
| 129 | + case VSmartCardTCPProtocol.APDU: |
| 130 | + final byte[] apdu = driverProtocol.readData(); |
| 131 | + final byte[] reply = CardManager.dispatchApdu(sim, apdu); |
| 132 | + driverProtocol.writeData(reply); |
| 133 | + break; |
| 134 | + } |
| 135 | + } catch (Exception e) {} |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments