SmartCard
is responsible for smartcard operations. To use Smartcard
, the brand of the must be specified since it operates by using the smartcard driver. On Java 6, the type of the smartcard can be detected via java libraries. As for Java 5 and .NET this information must be provided by the user.
On Java 6,the method findCardTypeAndSlot()
of SmartOp
finds the card type and slot number. If only single card is plugged in then this function immediately return the card information whereas it shows javax.swing.JOptionPane
to the user to select one of the cards when multiple smartcards are plugged in.
Pair<Long, CardType> slotAndCardType = SmartOp.findCardTypeAndSlot(); Long slot = slotAndCardType.getObject1(); SmartCard smartCard = new SmartCard(slotAndCardType.getObject2()); longsession = smartCard.openSession(slot);
Alternatively, the method getCardTerminals()
of SmartOp
returns the names of smartcard readers. By using these names the correct card is acquired from the user and then by calling getSlotAndCardType(String terminal)
the type and slot number of that card can be obtained.
If the selection is performed according to the card type, the method findCardTypesAndSlots()
of SmartOp
can be used to collect slot numbers and types of all cards currently plugged in.
As for Java 5 and .NET, the type of the smartcard must be known in advance. In the sample code below, without any selection, a session is opened in the first card. The method getSlotInfo(Long slot)
of SmartCard
returns information about the given slot. The user can be prompted to choose a card with the information returned by this function.
In order to start a smartcard operation, a session must be opened by openSession()
. Reading a certificate from a smartcard does not require login. Signing and decryption operations requires login as these operations need private key access.
SmartCard sc = new SmartCard(CardType.AKIS); long [] slots = sc.getSlotList(); //sc.getSlotInfo(slots[0]).slotDescription; long session = sc.openSession(slots[0]); sc.login(session, "12345")
On Java 6, it is possible with java library to work with AKIS cards by sending APDU commands. Even if AKIS driver does not exist in the system, the card can be accessed with AkisCIF.x.x.x.jar. Using AkisCIF makes card operations faster and shortens the signature creation time. A drawback o f using AkisCIF is that it locks the card such that no other program can use card during its operation. AkisCIF must be updated whenever the card version is changed.
APDUSmartCard
is responsible for accessing smartcard via APDU commands using AkisCIF.x.x.x.jar.
A sample usage is as follows:
APDUSmartCard sc = new APDUSmartCard(); long [] slots = sc.getSlotList(); sc.openSession(slots[0]); List<byte []> certs = sc.getSignatureCertificates(); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(certs.get(0))); BaseSignersigner = sc.getSigner(cert, Algorithms.SIGNATURE_RSA_SHA1);
As mentioned above, AkisCIF may not support all card types. To use AkisCIF if supported and use driver dll if not ,you can use the following code:
BaseSmartCard bsc = null; int index = 0; String [] terminals = SmartOp.getCardTerminals(); String selectedTerminal = terminals[index]; long slot; if(APDUSmartCard.isSupported(selectedTerminal)) { bsc = new APDUSmartCard(); slot = index + 1; } else { Pair<Long, CardType> slotAndCardType = SmartOp.getSlotAndCardType(selectedTerminal); slot = slotAndCardType.getObject1(); bsc = new P11SmartCard(slotAndCardType.getObject2()); } bsc.openSession(slot);