Java Code Examples for javax.smartcardio.Card#getBasicChannel()

The following examples show how to use javax.smartcardio.Card#getBasicChannel() . 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: TestConnect.java    From jdk8u-dev-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 2
Source File: TestTransmit.java    From jdk8u-dev-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 3
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 4
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 5
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 6
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 7
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 8
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;
            }
        }
        System.out.println("Success.");
    } catch (Exception e) {
        System.out.println("Failed.");
    }

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

        terminal = terminals.get(targetReaderIndex); // Prioritize physical card over simulations

        System.out.print("Connecting...");
        card = terminal.connect("*"); // Connect with the card

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

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

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

        // Select applet (mpcapplet)
        System.out.println("Smartcard: Selecting applet...");

        CommandAPDU cmd = new CommandAPDU(0x00, 0xa4, 0x04, 0x00, m_APPLET_AID);
        ResponseAPDU response = transmit(cmd);
    } else {
        System.out.print("Failed to find physical card.");
    }

    if (card != null) {
        return card.getBasicChannel();
    } else {
        return null;
    }
}
 
Example 9
Source File: TestConnect.java    From openjdk-jdk8u 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 10
Source File: TestTransmit.java    From openjdk-jdk8u 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 11
Source File: TestTransmit.java    From dragonwell8_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 12
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 13
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new org.taktik.connector.technical.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 14
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 15
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 16
Source File: PCSCUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private static ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   LOG.debug("Terminals: " + terminals);
   Card card = null;
   Iterator i$ = terminals.iterator();

   while(i$.hasNext()) {
      CardTerminal terminal = (CardTerminal)i$.next();
      if (terminal.isCardPresent()) {
         card = terminal.connect("*");
         if (card != null && matchesEidAtr(card.getATR())) {
            break;
         }
      }
   }

   if (card == null) {
      throw new CardNotPresentException("EID is not present");
   } else {
      card.beginExclusive();
      LOG.debug("card: " + card);
      CardChannel cardChannel = card.getBasicChannel();
      ResponseAPDU responseApdu = cardChannel.transmit(commandApdu);
      if (108 == responseApdu.getSW1()) {
         LOG.debug("sleeping...");

         try {
            Thread.sleep(10L);
         } catch (InterruptedException var7) {
            throw new be.ehealth.technicalconnector.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 17
Source File: NdefClient.java    From openjavacard-ndef with GNU General Public License v3.0 4 votes vote down vote up
public NdefClient(Card card) {
    this(card.getBasicChannel(), NdefProtocol.AID_NDEF);
}
 
Example 18
Source File: NdefClient.java    From openjavacard-ndef with GNU General Public License v3.0 4 votes vote down vote up
public NdefClient(Card card, byte[] aid) {
    this(card.getBasicChannel(), aid);
}
 
Example 19
Source File: TestTransmit.java    From TencentKona-8 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 20
Source File: TestConnect.java    From dragonwell8_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);
}