javax.smartcardio.Card Java Examples

The following examples show how to use javax.smartcardio.Card. 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: TerminalImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public synchronized Card connect(String protocol) throws CardException {
   if (this.card != null) {
      if (this.card.isValid()) {
         String cardProto = this.card.getProtocol();
         if (!protocol.equals("*") && !protocol.equalsIgnoreCase(cardProto)) {
            throw new CardException("Cannot connect using " + protocol + ", connection already established using " + cardProto);
         }

         return this.card;
      }

      this.card = null;
   }

   try {
      this.card = new CardImpl(this, protocol);
      return this.card;
   } catch (PCSCException var3) {
      if (var3.code == -2146434967) {
         throw new CardNotPresentException("No card present", var3);
      } else {
         throw new CardException("connect() failed", var3);
      }
   }
}
 
Example #3
Source File: TerminalImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public synchronized Card connect(String protocol) throws CardException {
   if (this.card != null) {
      if (this.card.isValid()) {
         String cardProto = this.card.getProtocol();
         if (!protocol.equals("*") && !protocol.equalsIgnoreCase(cardProto)) {
            throw new CardException("Cannot connect using " + protocol + ", connection already established using " + cardProto);
         }

         return this.card;
      }

      this.card = null;
   }

   try {
      this.card = new CardImpl(this, protocol);
      return this.card;
   } catch (PCSCException var3) {
      if (var3.code == -2146434967) {
         throw new CardNotPresentException("No card present", var3);
      } else {
         throw new CardException("connect() failed", var3);
      }
   }
}
 
Example #4
Source File: GenericCommand.java    From openjavacard-tools with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void run() {
    PrintStream os = System.out;

    mCard = mContext.findSingleCard(reader);
    try {
        mCard.connect();
        Card card = mCard.getCard();
        os.println("CONNECTED " + card.getProtocol() + " ATR=" + ATRUtil.toString(card.getATR()));
        if (select != null) {
            os.println("SELECT " + select);
            mCard.performSelectByName(select.getBytes(), true);
        }
        performOperation(mCard);

        mCard.disconnect();
    } catch (Exception e) {
        throw new Error("Error performing operation", e);
    }
}
 
Example #5
Source File: TerminalImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public synchronized Card connect(String protocol) throws CardException {
   if (this.card != null) {
      if (this.card.isValid()) {
         String cardProto = this.card.getProtocol();
         if (!protocol.equals("*") && !protocol.equalsIgnoreCase(cardProto)) {
            throw new CardException("Cannot connect using " + protocol + ", connection already established using " + cardProto);
         }

         return this.card;
      }

      this.card = null;
   }

   try {
      this.card = new CardImpl(this, protocol);
      return this.card;
   } catch (PCSCException var3) {
      if (var3.code == -2146434967) {
         throw new CardNotPresentException("No card present", var3);
      } else {
         throw new CardException("connect() failed", var3);
      }
   }
}
 
Example #6
Source File: TerminalImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public synchronized Card connect(String protocol) throws CardException {
   if (this.card != null) {
      if (this.card.isValid()) {
         String cardProto = this.card.getProtocol();
         if (!protocol.equals("*") && !protocol.equalsIgnoreCase(cardProto)) {
            throw new CardException("Cannot connect using " + protocol + ", connection already established using " + cardProto);
         }

         return this.card;
      }

      this.card = null;
   }

   try {
      this.card = new CardImpl(this, protocol);
      return this.card;
   } catch (PCSCException var3) {
      if (var3.code == -2146434967) {
         throw new CardNotPresentException("No card present", var3);
      } else {
         throw new CardException("connect() failed", var3);
      }
   }
}
 
Example #7
Source File: PCSCProvider.java    From apdu4j with MIT License 5 votes vote down vote up
@Override
public Optional<BIBO> getBIBO(String spec) {
    try {
        String protocol = System.getProperty(CardTerminalApp.PROTOCOL_PROPERTY, "*");
        Optional<CardTerminal> terminal = TerminalManager.getInstance(TerminalManager.getTerminalFactory().terminals()).dwim(spec, null, null);
        if (terminal.isPresent()) {
            Card card = terminal.get().connect(protocol);
            return Optional.ofNullable(CardBIBO.wrap(card));
        }
    } catch (CardException e) {
        logger.error("Could not get a CardBIBO for {}: {}", spec, e.getMessage(), e);
    }
    return Optional.empty();
}
 
Example #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: TestDirect.java    From hottub 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 #14
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 #15
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 #16
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 #17
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 #18
Source File: TestDirect.java    From dragonwell8_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 #19
Source File: TestConnect.java    From hottub 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 #20
Source File: TestConnect.java    From TencentKona-8 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 #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: TestExclusive.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
OtherThread(Card card) {
    this.card = card;
}
 
Example #23
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 #24
Source File: TestExclusive.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
OtherThread(Card card) {
    this.card = card;
}
 
Example #25
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 #26
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 #27
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 #28
Source File: TestExclusive.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
OtherThread(Card card) {
    this.card = card;
}
 
Example #29
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 #30
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);
}