Java Code Examples for javax.smartcardio.CardTerminal#connect()

The following examples show how to use javax.smartcardio.CardTerminal#connect() . 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: TestDirect.java    From jdk8u60 with GNU General Public License v2.0 7 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    System.out.println("Terminals: " + cardTerminals);
    if (cardTerminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 2
Source File: SmartcardTestService.java    From statelearner with Apache License 2.0 6 votes vote down vote up
public SmartcardTestService() throws Exception {
	System.setProperty("sun.security.smartcardio.t0GetResponse", "false");
	System.setProperty("sun.security.smartcardio.t1GetResponse", "false");
	
	//System.out.println("JRE version: " + System.getProperty("java.version"));
	//System.out.println("JRE vendor: " + System.getProperty("java.vendor"));
	
	//System.out.println("JRE spec version: " + System.getProperty("java.specification.version"));
	//System.out.println("JRE spec vendor: " + System.getProperty("java.specification.vendor"));
	
	// Get list of card readers
	List<CardTerminal> terminals = TerminalFactory.getDefault().terminals().list();
	
	if(terminals.size() == 0) {
		throw new Exception("No readers found.");
	}
	
	// Ask user to select a card reader to connect to
	terminal = (CardTerminal)JOptionPane.showInputDialog(null, "Reader", "Select a reader", JOptionPane.QUESTION_MESSAGE, null, terminals.toArray(), terminals.get(0));
	
	if(terminal == null) {
		throw new Exception("No reader selected.");
	}

	System.err.println("Selected reader: " + terminal.toString());
	
	// Connect to card in selected reader
	card = terminal.connect("*");
	channel = card.getBasicChannel();
	
	System.err.println("Connected to card");
}
 
Example 3
Source File: TestDirect.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 4
Source File: TestDirect.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 5
Source File: TestDirect.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    System.out.println("Terminals: " + cardTerminals);
    if (cardTerminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 6
Source File: TestDirect.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 7
Source File: TestDirect.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 8
Source File: SignUtils.java    From Websocket-Smart-Card-Signer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static ArrayList<String> getConnectedCardATR(){
    ArrayList<String> ret = new ArrayList<String>();
    try{
        List<CardTerminal> terminalList = TerminalFactory.getDefault().terminals().list();
        
        for(CardTerminal terminal:terminalList)
            if(terminal.isCardPresent()){
                javax.smartcardio.Card card = terminal.connect("*");
                ret.add(StringUtils.toHexString(card.getATR().getBytes()));
                card.disconnect(false);
            }
    }catch(Exception ex){}
    return ret;
}
 
Example 9
Source File: CardManager.java    From JCMathLib with MIT License 5 votes vote down vote up
private Card waitForCard(CardTerminals terminals)
        throws CardException {
    while (true) {
        for (CardTerminal ct : terminals
                .list(CardTerminals.State.CARD_INSERTION)) {

            return ct.connect("*");
        }
        terminals.waitForChange();
    }
}
 
Example 10
Source File: TestDirect.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    System.out.println("Terminals: " + cardTerminals);
    if (cardTerminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 11
Source File: TestDirect.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example 12
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 13
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 14
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 15
Source File: TestTransmit.java    From jdk8u60 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 org.taktik.connector.technical.exception.InterruptedException("Cannot sleep", var7);
         }

         responseApdu = cardChannel.transmit(commandApdu);
      }

      card.endExclusive();
      card.disconnect(false);
      return responseApdu;
   }
}
 
Example 17
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 18
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 19
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 20
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.");
}