I finally recieved the Smart-Cards I’m going to use for my implementation:

The JCOP21 18K and the Cosmo V7 128K.

Note that the JCOP21 requires the Java Card SDK 2.2.1.

This is a guide how to compile and install a JavaCard Applet for the JCOP21 JavaCard:

1. Install gpshell

You will need gpshell to install the applets to the JavaCard as apdutool pcsc support is optional (and didn’t work for me…).

Go to the  gpshell “sourceforge” website:

-> http://sourceforge.net/projects/globalplatform/files/

Download the GlobalPlatform Library 6.0.0

Unpack and install:

./configure
make
sudo make install

Now Download GPShell-1.4.4 and repeat the above steps for GPShell.

You will maybe need to install some dependencies.

NOTE: Note that in the unpacked GPShell folder, you will find useful gpshell scripts examples.

2. Compile and convert your applet

Let’s suppose we work on an applet called HelloWorld in the package me.test.hello.

The source-file HelloWorld.java (you can take some code from the SDK samples) goes to “HelloWorld/src/me/test/hello“. Make sure it fits to the package structure specified in HelloWorld.java.

Compile from the “HelloWorld” folder by using javac: The -source and -target options will make sure you don’t get the “class file format error”.

error: unsupported class file format of version 49:0

If you get the error, try modifying the source and target arguments.

javac -g -source 1.2 -target 1.2 -classpath $JC_HOME/lib/api.jar:$JC_HOME/lib/installer.jar src/me/test/hello/HelloWorld.java -d bin

Now we need to convert the class file to a cap file: For this we will write a little config file for the Java Card converter tool:

Lets call the file “HelloWorld.opt”:

-classdir bin
-out EXP JCA CAP
-exportpath $JC_HOME/api_export_files
-applet 0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08:0x09:0x00:0x00 me.test.hello.HelloWorld
me.test.hello 0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08:0x09:0x00 1.0

This file should be located in the “HelloWorld” folder. Note that you can of course choose your own AIDs.

You will now have the .cap .exp and .jca files in the “bin/me/test/hello/javacard” folder.

3. Download the Applet to the Java Card

If not already installed: install “pcscd” with your packet manager (apt-get)

Now first, check if you have a card reader and a card connected, by using “pcsc_scan”.

Go to the folder where you unpacked GPShell. And try if the communication with the card works by using one of the sample gpshell scripts provided. For example:

gpshell listGP211.txt

this should give you the names of some applets that are currently installed.

WARNING: if you have problem authenticating with your card (cryptogram errors), you’ll have to check what keys are used. The card will be locked after too many attempts! (JCOP, 10 attempts I think)

Next, you will have to copy the helloInstall<YOUR-CARD>.txt file to the “HelloWorld/bin/me/test/hello/javacard” and edit a few lines in order to fit your program. You will have to change the AID in the delete command and the cap file for the install command. For simplicity I call the file “install.gpshell.txt“:

mode_211
enable_trace
establish_context
card_connect
select -AID a000000003000000
open_sc -scp 2 -scpimpl 0x15 -security 1 -keyind 0 -keyver 0 -mac_key 404142434445464748494a4b4c4d4e4f -enc_key 404142434445464748494a4b4c4d4e4f // Open secure channel
delete -AID 01020304050607080900 //your AID
delete -AID 0102030405060708090 //package AID
install -file test.cap -instParam C90145 -priv 2 // test.cap gen. by converter
card_disconnect
release_context

Finally run:

gpshell install.gpshell.txt

If everything was OK, you should be able to see the application using the “listGP211.txt” file.

If you want to delete the applet, just check the sample script files in the GPShell folder and change the AID.

Tagged with →  
Share →

One Response to JavaCard – Compiling and Installing an Applet

  1. dan mbachu says:

    please i am a student. how can i determine the authentication key of a card. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *