This project contains a PC/SC wrapper and smartcard abstraction layer.
In addition an alternative javax.smartcardio provider is provided.
The project is committed as a self contained Eclipse project and should compile without problems there.
Anyone in Ant is kindly asked to provide a script… (as for maven)
First of all, we need some code from the intarsys runtime. This is included in the “lib”
dependencies. The code itself is BSD licensed and again hosted at Github at “intarsys/runtime”.
Second, the native code is accessed via JNA in conjunction with the intarsys native platform
model (as we (1) didn’t like the JNA provided one (2) have introduced a very small provider interface
for decoupling from JNA or any other implementation). Both libraries are included in the “lib” folder.
The code for the intarsys native abstraction is BSD, available at Github, “intarsys/native-c”.
A pre-built jar file is available in the “deploy” folder. Together with the dependencies in “lib”, add
to the classpath for your project.
On the most basic layer, we simply use a declarative approach to wrap PC/SC API functions. To work on this “abstraction” you can simply use de.intarsys.security.smartcard.pcsc.nativec._PCSC.
If you create this without parameter, the default library for the platform will be used.
There’s a small abstract layer around de.intarsys.security.smartcard.pcsc.nativec._PCSC that allows for a little bit more readable code and also includes some indirections to ease life with 3rd part libraries and workarounds for them.
In the “examples” directory you will find some demo code.
The most simple use case is
IPCSCContext context = PCSCContextFactory.get().establishContext();
List<IPCSCCardReader> readers = context.listReaders();
if (readers.isEmpty()) {
System.out.println("no reader found");
return;
}
for (IPCSCCardReader reader : readers) {
PCSCStatusMonitor monitor = new PCSCStatusMonitor(reader);
monitor.addStatusListener(new IStatusListener() {
@Override
public void onException(PCSCException e) {
e.printStackTrace();
}
@Override
public void onStatusChange(PCSCCardReaderState cardReaderState) {
System.out.println("reader " + cardReaderState.getReader()
+ " state " + cardReaderState);
}
});
}
To connect to a reader
// recommended use: create a new context for the connection
System.out.println("" + reader + " establish context");
IPCSCContext connectionContext = reader.getContext().establishContext();
System.out.println("" + reader + " connect");
IPCSCConnection connection = connectionContext.connect(
reader.getName(), _IPCSC.SCARD_SHARE_SHARED,
_IPCSC.SCARD_PROTOCOL_Tx);
System.out.println("" + reader + " begin transaction");
connection.beginTransaction();
System.out.println("" + reader + " end transaction");
connection.endTransaction(_IPCSC.SCARD_LEAVE_CARD);
System.out.println("" + reader + " disconnect");
connection.disconnect(_IPCSC.SCARD_LEAVE_CARD);
System.out.println("" + reader + " dispose context");
connectionContext.dispose();
The library comes with an alternative javax.smartcardio provider. There are a couple of things to consider:
- Intended differences
- dedicated PCSC context for terminals, terminal and card
- waitForChange(timeout) semantics improved(?), state change is reset even in case of timeout
- reader insertion is handled, too
- no finalizer for card!
- Open design questions
- “exclusive” card access is NOT restricted to a Java thread. I found no technical reason for this constraint. In imho this makes handling of shared and pooled resources ugly. Next to “1 PCSC context” this would be my top level reason not to use smartcardio. Any opinions?
- “connect” always returns the identical {@link Card} object. Why is this? This implementation currently opens a new shared connection. If we revert to the spec, I’d at least expect some kind of reference counting. Opinions?
- Todos
- no channel support yet
- implement “GET RESPONSE”
- no permission checks so far
- review exceptions to be standard compliant (IllegalStateException)
The provider is implemented in de.intarsys.security.smartcard.smartcardio.SmartcardioProvider, so programmatically you can active it with this code
// add provider and ensure that it will be used first...
Security.insertProviderAt(new SmartcardioProvider(), 1);
There are a couple of simple examples for use with smartcardio included.
/*
* Copyright (c) 2013, intarsys consulting GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of intarsys nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
Service & support should be funneled through the tools available with Github.
If you need further assistance, contact us.
intarsys consulting GmbH
Kriegstrasse 100
76135 Karlsruhe
Fon +49 721 38479-0
Fax +49 721 38479-60
[email protected]
www.intarsys.de