javax.smartcardio.CardChannel Java Examples

The following examples show how to use javax.smartcardio.CardChannel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CardImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public CardChannel openLogicalChannel() throws CardException {
   this.checkSecurity("openLogicalChannel");
   this.checkState();
   this.checkExclusive();

   try {
      byte[] response = PCSC.SCardTransmit(this.cardId, this.protocol, commandOpenChannel, 0, commandOpenChannel.length);
      if (response.length == 3 && getSW(response) == 36864) {
         return new ChannelImpl(this, response[0]);
      } else {
         throw new CardException("openLogicalChannel() failed, card response: " + PCSC.toString(response));
      }
   } catch (PCSCException var2) {
      this.handleError(var2);
      throw new CardException("openLogicalChannel() failed", var2);
   }
}
 
Example #2
Source File: CardImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public CardChannel openLogicalChannel() throws CardException {
   this.checkSecurity("openLogicalChannel");
   this.checkState();
   this.checkExclusive();

   try {
      byte[] response = PCSC.SCardTransmit(this.cardId, this.protocol, commandOpenChannel, 0, commandOpenChannel.length);
      if (response.length == 3 && getSW(response) == 36864) {
         return new ChannelImpl(this, response[0]);
      } else {
         throw new CardException("openLogicalChannel() failed, card response: " + PCSC.toString(response));
      }
   } catch (PCSCException var2) {
      this.handleError(var2);
      throw new CardException("openLogicalChannel() failed", var2);
   }
}
 
Example #3
Source File: CardImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public CardChannel openLogicalChannel() throws CardException {
   this.checkSecurity("openLogicalChannel");
   this.checkState();
   this.checkExclusive();

   try {
      byte[] response = PCSC.SCardTransmit(this.cardId, this.protocol, commandOpenChannel, 0, commandOpenChannel.length);
      if (response.length == 3 && getSW(response) == 36864) {
         return new ChannelImpl(this, response[0]);
      } else {
         throw new CardException("openLogicalChannel() failed, card response: " + PCSC.toString(response));
      }
   } catch (PCSCException var2) {
      this.handleError(var2);
      throw new CardException("openLogicalChannel() failed", var2);
   }
}
 
Example #4
Source File: CardImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public CardChannel openLogicalChannel() throws CardException {
   this.checkSecurity("openLogicalChannel");
   this.checkState();
   this.checkExclusive();

   try {
      byte[] response = PCSC.SCardTransmit(this.cardId, this.protocol, commandOpenChannel, 0, commandOpenChannel.length);
      if (response.length == 3 && getSW(response) == 36864) {
         return new ChannelImpl(this, response[0]);
      } else {
         throw new CardException("openLogicalChannel() failed, card response: " + PCSC.toString(response));
      }
   } catch (PCSCException var2) {
      this.handleError(var2);
      throw new CardException("openLogicalChannel() failed", var2);
   }
}
 
Example #5
Source File: Utils.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #6
Source File: GenericAPDU.java    From openjavacard-tools with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void sendRaw(PrintStream os, GenericCard card, CardChannel channel) throws CardException {
    for(byte[] apdu: raw) {
        CommandAPDU capdu = new CommandAPDU(apdu);
        os.println("APDU > " + APDUUtil.toString(capdu));
        ResponseAPDU rapdu = card.transmit(channel, capdu);
        os.println("APDU < " + APDUUtil.toString(rapdu));
        int sw = rapdu.getSW();
        if(sw != ISO7816.SW_NO_ERROR) {
            throw new SWException("Error executing command", sw);
        }
    }
}
 
Example #7
Source File: Utils.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #8
Source File: Utils.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #9
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #10
Source File: Utils.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #11
Source File: Utils.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #12
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #13
Source File: CardManager.java    From JCMathLib with MIT License 5 votes vote down vote up
private CardChannel ConnectJCardSimLocalSimulator(Class appletClass) throws Exception {
    System.setProperty("com.licel.jcardsim.terminal.type", "2");
    CAD cad = new CAD(System.getProperties());
    JavaxSmartCardInterface simulator = (JavaxSmartCardInterface) cad.getCardInterface();
    byte[] installData = new byte[0];
    AID appletAID = new AID(m_APPLET_AID, (short) 0, (byte) m_APPLET_AID.length);

    AID appletAIDRes = simulator.installApplet(appletAID, appletClass, installData, (short) 0, (byte) installData.length);
    simulator.selectApplet(appletAID);

    return new SimulatedCardChannelLocal(simulator);
}
 
Example #14
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #15
Source File: GenericAPDU.java    From openjavacard-tools with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void performOperation(GenericCard card) throws CardException {
    PrintStream os = System.out;
    CardChannel channel = card.getBasicChannel();

    byte[] data = buildData();

    if (raw == null || raw.isEmpty()) {
        sendArg(os, card, channel, data);
    } else {
        sendRaw(os, card, channel);
    }
}
 
Example #16
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #17
Source File: CardManager.java    From JCMathLib with MIT License 5 votes vote down vote up
private CardChannel ConnectJCardSimLocalSimulator(Class appletClass) throws Exception {
System.out.print("Setting up Javacard simulator...");
//CardSimulator simulator = new CardSimulator();
System.setProperty("com.licel.jcardsim.terminal.type", "2");
      CAD cad = new CAD(System.getProperties());
      JavaxSmartCardInterface simulator = (JavaxSmartCardInterface) cad.getCardInterface();	
byte[] installData = new byte[0];
AID appletAID = new AID(m_APPLET_AID, (short) 0, (byte) m_APPLET_AID.length);
AID appletAIDRes = simulator.installApplet(appletAID, appletClass, installData, (short) 0, (byte) installData.length);
simulator.selectApplet(appletAID);		
      return new SimulatedCardChannelLocal(simulator);
  }
 
Example #18
Source File: Utils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #19
Source File: Utils.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void transmitTestCommand(CardChannel channel) throws Exception {
    ResponseAPDU r = channel.transmit(new CommandAPDU(C1));
    byte[] rb = r.getBytes();
    if ((Arrays.equals(rb, R1a) == false) && (Arrays.equals(rb, R1b) == false)) {
        System.out.println("expected: " + toString(R1a));
        System.out.println("received: " + toString(rb));
        throw new Exception("Response does not match");
    }
}
 
Example #20
Source File: GenericAPDU.java    From openjavacard-tools with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void sendArg(PrintStream os, GenericCard card, CardChannel channel, byte[] data) throws CardException {
    CommandAPDU capdu = APDUUtil.buildCommand(
            apduCLA, apduINS, apduP12, data
    );
    os.println("APDU > " + APDUUtil.toString(capdu));
    ResponseAPDU rapdu = card.transmit(channel, capdu);
    os.println("APDU < " + APDUUtil.toString(rapdu));
    int sw = rapdu.getSW();
    if (sw != ISO7816.SW_NO_ERROR) {
        throw new SWException("Error executing command", sw);
    }
}
 
Example #21
Source File: TestTransmit.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }

    Card card = terminal.connect("T=0");
    CardChannel channel = card.getBasicChannel();

    BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));

    byte[] command = null;
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        if (line.startsWith(CMD_MARKER)) {
            System.out.println(line);
            line = line.substring(CMD_MARKER.length());
            command = parse(line);
        } else if (line.startsWith(RES_MARKER)) {
            System.out.println(line);
            line = line.substring(RES_MARKER.length());
            Bytes response = parseWildcard(line);
            CommandAPDU capdu = new CommandAPDU(command);
            ResponseAPDU rapdu = channel.transmit(capdu);
            byte[] received = rapdu.getBytes();
            if (received.length != response.bytes.length) {
                throw new Exception("Length mismatch: " + toString(received));
            }
            for (int i = 0; i < received.length; i++) {
                byte mask = response.mask[i];
                if ((received[i] & response.mask[i]) != response.bytes[i]) {
                    throw new Exception("Mismatch: " + toString(received));
                }
            }
        } // else ignore
    }

    // disconnect
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #22
Source File: TestTransmit.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }

    Card card = terminal.connect("T=0");
    CardChannel channel = card.getBasicChannel();

    BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));

    byte[] command = null;
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        if (line.startsWith(CMD_MARKER)) {
            System.out.println(line);
            line = line.substring(CMD_MARKER.length());
            command = parse(line);
        } else if (line.startsWith(RES_MARKER)) {
            System.out.println(line);
            line = line.substring(RES_MARKER.length());
            Bytes response = parseWildcard(line);
            CommandAPDU capdu = new CommandAPDU(command);
            ResponseAPDU rapdu = channel.transmit(capdu);
            byte[] received = rapdu.getBytes();
            if (received.length != response.bytes.length) {
                throw new Exception("Length mismatch: " + toString(received));
            }
            for (int i = 0; i < received.length; i++) {
                byte mask = response.mask[i];
                if ((received[i] & response.mask[i]) != response.bytes[i]) {
                    throw new Exception("Mismatch: " + toString(received));
                }
            }
        } // else ignore
    }

    // disconnect
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #23
Source File: TestTransmit.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);

    Card card = terminal.connect("T=0");
    CardChannel channel = card.getBasicChannel();

    BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));

    byte[] command = null;
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        if (line.startsWith(CMD_MARKER)) {
            System.out.println(line);
            line = line.substring(CMD_MARKER.length());
            command = parse(line);
        } else if (line.startsWith(RES_MARKER)) {
            System.out.println(line);
            line = line.substring(RES_MARKER.length());
            Bytes response = parseWildcard(line);
            CommandAPDU capdu = new CommandAPDU(command);
            ResponseAPDU rapdu = channel.transmit(capdu);
            byte[] received = rapdu.getBytes();
            if (received.length != response.bytes.length) {
                throw new Exception("Length mismatch: " + toString(received));
            }
            for (int i = 0; i < received.length; i++) {
                byte mask = response.mask[i];
                if ((received[i] & response.mask[i]) != response.bytes[i]) {
                    throw new Exception("Mismatch: " + toString(received));
                }
            }
        } // else ignore
    }

    // disconnect
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #24
Source File: TestConnect.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void transmit(Card card) throws Exception {
    CardChannel channel = card.getBasicChannel();
    System.out.println("Transmitting...");
    transmitTestCommand(channel);
}
 
Example #25
Source File: TestConnect.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void transmit(Card card) throws Exception {
    CardChannel channel = card.getBasicChannel();
    System.out.println("Transmitting...");
    transmitTestCommand(channel);
}
 
Example #26
Source File: CardManager.java    From JCMathLib with MIT License 4 votes vote down vote up
public CardChannel ConnectJCOPSimulator(int targetReaderIndex) throws Exception {
    // JCOP Simulators
    System.out.print("Looking for JCOP simulators...");
    int[] ports = new int[]{8050};
    return connectToCardByTerminalFactory(TerminalFactory.getInstance("JcopEmulator", ports), targetReaderIndex);
}
 
Example #27
Source File: TestConnect.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static void transmit(Card card) throws Exception {
    CardChannel channel = card.getBasicChannel();
    System.out.println("Transmitting...");
    transmitTestCommand(channel);
}
 
Example #28
Source File: TestTransmit.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }

    Card card = terminal.connect("T=0");
    CardChannel channel = card.getBasicChannel();

    BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));

    byte[] command = null;
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        if (line.startsWith(CMD_MARKER)) {
            System.out.println(line);
            line = line.substring(CMD_MARKER.length());
            command = parse(line);
        } else if (line.startsWith(RES_MARKER)) {
            System.out.println(line);
            line = line.substring(RES_MARKER.length());
            Bytes response = parseWildcard(line);
            CommandAPDU capdu = new CommandAPDU(command);
            ResponseAPDU rapdu = channel.transmit(capdu);
            byte[] received = rapdu.getBytes();
            if (received.length != response.bytes.length) {
                throw new Exception("Length mismatch: " + toString(received));
            }
            for (int i = 0; i < received.length; i++) {
                byte mask = response.mask[i];
                if ((received[i] & response.mask[i]) != response.bytes[i]) {
                    throw new Exception("Mismatch: " + toString(received));
                }
            }
        } // else ignore
    }

    // disconnect
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #29
Source File: CardManager.java    From JCMathLib with MIT License 4 votes vote down vote up
private CardChannel connectToCardByTerminalFactory(TerminalFactory factory, int targetReaderIndex) throws CardException {
    List<CardTerminal> terminals = new ArrayList<>();

    boolean card_found = false;
    CardTerminal terminal = null;
    Card card = null;
    try {
        for (CardTerminal t : factory.terminals().list()) {
            terminals.add(t);
            if (t.isCardPresent()) {
                card_found = true;
            }
        }
    } catch (Exception ignored) {
    }

    if (card_found) {
        System.out.println("Cards found: " + terminals);

        terminal = terminals.get(targetReaderIndex); 

        System.out.print("Connecting...");
        card = terminal.connect("*"); 

        System.out.println(" done.");

        System.out.print("Establishing channel...");
        m_channel = card.getBasicChannel();

        System.out.println(" done.");

        System.out.print("Selecting applet...");

        CommandAPDU cmd = new CommandAPDU(0x00, 0xa4, 0x04, 0x00, m_APPLET_AID);
        ResponseAPDU response = transmit(cmd);
        if (response.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
            System.out.print(" done");
        }
        else {
            System.out.print(" failed.");
        }
        
    } else {
        System.out.print("Failed to find required card.");
    }

    if (card != null) {
        return card.getBasicChannel();
    } else {
        return null;
    }
}
 
Example #30
Source File: CardManager.java    From JCMathLib with MIT License 4 votes vote down vote up
public CardChannel ConnectPhysicalCard(int targetReaderIndex) throws Exception {
    // JCOP Simulators
    System.out.print("Looking for physical cards... ");
    return connectToCardByTerminalFactory(TerminalFactory.getDefault(), targetReaderIndex);
}