Ivan has posted 22 posts at DZone. View Full User Profile

jCardSim Project News and Raspberry Pi Hackathon Report

12.03.2013
| 555 views |
  • submit to reddit

What is jCardSim?

The jCardSim is an open source simulator implements Java Card, v.2.2.1:

  • javacard.framework.*
  • javacard.framework.security.*
  • javacardx.crypto.*
The jCardSim is a winner of Duke's Choice Award 2013.

With jCardSim you can rapidly prototype your Java Card applications, easily write Unit-tests. The jCardSim emulates Java Card Terminal so you are able to use javax.smartcardio. It is a cross-platform solution written in pure Java. To learn all project information see official page - http://jcardsim.org and our GitHub Repo - https://github.com/licel/jcardsim.

News

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

We are planning to make a poll about features to be included in jCardSim in 2014. Stay tuned to jCardSim.org, a poll will be available in early December 2013.

Raspberry Pi Hackathon

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, we 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): com/licel/jcardsim/io/CardInterface.java:

public interface CardInterface {
   public void reset();
   public byte[] getATR();
   public byte[] transmitCommand(byte[] data);
}
and 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. Our CTO Mikhail Dudarev is an old school java guy, that’s why he likes 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 community 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 you have it, 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!
0
Published at DZone with permission of its author, Ivan Kinash.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)