jCardsim and Raspberry Pi Hackathon

Tags: jcardsim, Java Card, raspberry pi, linux, hackathon

Hi all,

We had a few interesting happenings in the life of our project during November and we would like to tell you about them.

Duke’s Choice Award Statue is finally here in Moscow! Hurray! Thanks to Oracle and judges once again!

Licel's jCardSim Project is a winner of Oracle Duke's Choive Award 2013

Last week our team had the Raspberry Pi Hackathon. Our goal was to come up with a plan of how to bring together jCardSim and Raspberry Pi, and to implement it.

jCardSim and Raspberry Pi

Raspberry Pi is a very interesting, popular and (what is the most valuable) affordable for every developer platform. As a heart, Raspberry Pi has an ARM-chip. It is great because there are many cards on the Smart Card market which are based on ARM-chip. Also, there is the Java 7 Embedded from Oracle for Linux on ARM (ARMv6/7). “Great!” - We thought and decided to make a hackathon.

The architecture was developed immediately: to run jCardSim on Raspberry Pi; it emulates Java Card, and then a client application connects to jCardSim instance on Raspberry Pi and works with it.

jCardSim and Raspberry Pi

First of all we needed to choose a communication protocol. We had a Raspberry Pi B model, which unfortunately doesn’t support USB in a client mode. So we had only one approach left: to communicate with RaspBerry via a network. As it turned out later, this approach allowed us to add one more cool feature to jCardSim.

Further, Ivan Kinash (our CEO) took up the installation of Linux and Java 7 Embedded on to RPI. And I started to saw jCardSim to the server and the client.

Initially it was necessary to create a common interface which could be provided both by the regular in-memory and remote implementations of the Simulator.

As a result we got the following picture (first class with javadoc):
https://github.com/licel/jcardsim/blob/Raspberry_Pi_Hackathon/src/main/java/com/licel/jcardsim/io/CardInterface.java:

public interface CardInterface {
   public void reset();
   public byte[] getATR();
   public byte[] transmitCommand(byte[] data);
}

and https://github.com/licel/jcardsim/blob/Raspberry_Pi_Hackathon/src/main/java/com/licel/jcardsim/io/JavaCardInterface.java:

public interface JavaCardInterface extends CardInterface {
   public AID loadApplet(AID aid, String appletClassName, byte[] appletJarContents) throws SystemException;
   public AID loadApplet(AID aid, String appletClass);
   public AID createApplet(AID aid, byte bArray[], short bOffset, byte bLength) throws SystemException;
   public AID installApplet(AID aid, String appletClassName, bytebArray[], short bOffset, byte bLength) throws SystemException;
   public AID installApplet(AID aid, String appletClassName, byte[] appletJarContents, byte bArray[], short bOffset, byte bLength) throws SystemException;
   public boolean selectApplet(AID aid);
}

Note that we added two methods here:

   public AID loadApplet(AID aid, String appletClassName, byte[] appletJarContents) throws SystemException;
   public AID installApplet(AID aid, String appletClassName, byte[] appletJarContents, byte bArray[], short bOffset, byte bLength) throws SystemException;

The implementation of these methods allows loading a jar file with an applet and its depending classes into Raspberry Pi.

A bit of refactoring, launching unit-tests and in-memory implementation works.

By that time we had our Raspberry filled by Raspbian (Debian for Raspberry Pi) and Java 7 Embedded. We copied the in-memory version to our device, launched it - everything works. However, who would doubt :-)

Now we need to implement the server and the protocol of interaction with it. I am an old school java guy, that’s why I like to use Java RMI for such kind of things. Minimum of code, ease of work - what else can be needed for a hackathon. Well, maybe great atmosphere and sushi are not to be redundant :-)

So we are coding, launching … and it doesn’t work. As it turned out when Raspbian was installed, it added a strange line to /etc/hosts:
127.0.1.1 raspberry-pi
For some reasons Java and RMI Registry were mad about this. After deleting this line we got everything working. APDUScriptTool interacts with applets inside jCardSim thru javax.smartcardio on Raspberry Pi.

To share our results with all jCardSim users and not to affect the stability of the master, we created a branch which is called Raspberry_Pi_Hackathon:

git checkout -b Raspberry_Pi_Hackathon
git commit -m "Raspberry_Pi_Hackathon"
git pull

It is ready: https://github.com/licel/jcardsim/tree/Raspberry_Pi_Hackathon

To check our results out, it is not necessary to have Raspberry Pi. Although believe – it is a little bit more interesting and funny if it is installed, kind of a very big Java Card in your pocket … with external batteries :-)

You need to download jcardsim-2.2.1-all.jar, apdu.script, jcardsim.cfg from the Raspberry_Pi_Hackathon branch.

To start the server, use the command:
java -jar jcardsim-2.2.1-all.jar com.licel.jcarsim.remote.JavaCardRemoteServer jcardsim.cfg

To start the client, run as usual:
java -jar jcardsim-2.2.1-all.jar com.licel.jcarsim.utils.APDUScriptTool jcardsim.cfg apdu.script

The server and port where jCardSim works in the server mode can be set in jcardsim.cfg:

com.licel.jcardsim.terminal.host=localhost
com.licel.jcardsim.terminal.port=1234

Hurray! Our goal was reached. The jCardSim now has a new interesting functionality, which will be added to the master soon. Now it is necessary to write a lot of documentation and unit-tests. As for Raspberry Pi, it confirmed its title - one of the best platforms for developers.

P.S.
During our hackathon we found a very cool distro of Raspberry Pi - Raspnmc (http://www.raspbmc.com). If you are looking for a media center solution or just a network media player - it is a great solution.

Thanks to you guys! Good luck!