Java Card program compilation and running for new comers (newbies)
(FOR QUICKREF COMMANDS GO TO BOTTOM)
Now you may have not even heard of Java Card, but if you have it might be because you were thrown into the same situation that I was thrown into, an unfortunate one where it was tasked to you as part of a university project along with 8 other people.
If you haven’t, basically it’s a version of Java that is very, very small and is designed to work on smart cards, like a SIM card or a smart card chip in a chip and PIN card. (Although in my case it only had to work on an emulator)
I know a big group, though unfortunately it was just myself and 2 others doing the programming parts of it, and we really underestimated the difficulty of getting Java Card working.
Well I’ll give you a little background of the project that made it so difficult.
We weren’t just tasked with creating something in Java Card, that is easy, and we set up the environments for this quite easily, with using eclipse and a plugin called JCDE which can be found here and it actually has pretty good instructions with it to set up the environment and get a program running. Though we had to have a system where the Java Card interacted with a mobile phone application, programmed in J2ME (which we hadn’t used either – a sign of good things to come) using the Sun Java Wireless Toolkit. Now we thought this was fine, we already had an IDE set up that we presumed was compiling a Java Card program correctly. We had only heard bad things about trying to run J2ME programs in eclipse, so we went with the suggestion of a postgraduate and use netbeans for the J2ME code (I didn’t particularly want to use 2 different IDEs, but if need be).
This too, wasn’t too difficult, but then we ran into a problem. Actually running the 2 programs together, with netbeans running a phone emulator and Java Card running whatever it ran (we didn’t know exactly what we had to do at the time, I know now that the way it was running, eclipse stopped running the CREF – which I will come to later, and so it couldn’t be done easily, but that wasn’t the main problem).
Basically after a massive amount of time I spent with 2 postgraduates that know a lot about the 2 programming languages separately, we basically got something working. It turned out the problem was that we were using Java Card 2.2.2 in eclipse (as the JCDE plugin only accepts this version and after) and the wireless toolkit on netbeans, which only uses Java Card 2.2.1. And there are compatibility problems to do with the smart card emulator that means they cannot work together.
I said fine, and since I’m fairly competent on command line, I thought I would give it a try on there. This eventually worked, but as you will find out if you need to try and find out about Java Card, it isn’t too easy to simply find out these.
(NOTE: I’m giving you a MASSIVE tip here, if you have Java Card problem, don’t Google it like you normally would. Go to the Sun Java forums here. The help here is far beyond any help I ever got simply Googling my problems to do with Java Card)
Now I’m only going to tell you here how the Java Card compilation occurs of Java Card 2.2.1 (I think it’s the same for 2.2.2, but not sure about the newer versions of 3.*)
I’m going to give the commands for Windows, though on Linux (Java Card isn’t available on Mac) the commands are the same for the linux command constraints, i.e. \ becomes / and in like -classpath “.;%WHATEVER%;” would become -classpath “.:$WHATEVER:”. Though I warn you now – if you want to install Java Card on a Linux machine, it can be done, though it is a massive struggle to eventually get there. There’s lots of fiddling, and although I’ve been using *nix machines for a while, I still actually found it a great deal easier to install on Windows XP.
Ok so say you are in a directory (I’m using Windows XP equivalent here, just adapt to your system again) with a java card program Java_card_prog.java. So it’s directory would be this:
C:\Documents and Settings\User\Java_card_prog.java
Ok, so first you need to create a directory of the name of the programs package. So each java card program would belong to a package. So I’m going to call the package of this one test.
so do: mkdir test
Now you need to do the first command to actually. Don’t bother cd-ing into the package directory, there is no point.
javac -g -d test -classpath “.;%JC_HOME%\lib\api.jar” Java_card_prog.java
(you need to make sure all you’re environment variables are up to scratch – I’ll be making a video of the whole process of installing on Windows and running your first program to run working with a mobile phone application)
Now this last command basically compiles the java card program. I tried without the -d with the package directory, but couldn’t get commands after to work.
Now you will notice if you look this doesn’t actually make the class files in the directory test, but in test\test\ which is a little odd and I don’t know why, but it likes to do this, though it doesn’t change how it works overall, so it doesn’t matter in the end.
Next of all you need to convert the class files into a .CAP file. This is the file that can be used with scripts to load onto the smart card (emulator in this case). We do this with:
converter -applet 0×01:0×02:0×03:0×04:0×05:0×06:0×07:0×08:0×09:0×00:0×00 test.Java_card_prog -classdir test -exportpath %JC_HOME%\api_export_files test 0×01:0×02:0×03:0×04:0×05:0×06:0×07:0×08:0×09:0×00 1.0
Now I know this is a big command and that’s because it needs to be, basically. With -applet you define the applet ID (AID) and the package.Program, then you need to put in the directory where you put the class files (test) and you MUST put in -exportpath %JC_HOME%\api_export_files for this to work. Then you put the package name again, and the package ID (also AID) and then have minor version.major version. If you don’t understand this, just put 1.0 every time.
Now a .CAP and .EXP file are made (.EXP not needed but we didn’t state in the last command, so just done by default), and they will be in test\test\javacard\
Now you need to create the scripts with
scriptgen -o Java_card_prog.scr test\test\javacard\test.cap
this outputs the script into a file called Java_card_prog.scr, from here you need to change the script, so open it up in your favourite text editor, for instance notepad or notepad++ (personal preference).
You need to add the line at the start of the file powerup;
then it needs to have the line
This will possibly be in there already, but wasn’t in my cases. You don’t need the comments, but they helped me a lot to start off with.
Then it will load the .CAP file parts into it, starting 0×80 0xB0 0×00 0×00 0×00 0x7F;
and ending at the end 0×80 0xBA 0×00 0×00 0×00 0x7F;
Now you need to create the Applet and select it.
// create Applet
0×80 0xB8 0×00 0×00 0x0d 0x0b 0×01 0×02 0×03 0×04 0×05 0×06 0×07 0×08 0×09 0×00 0×00 0×00 0x7F;
// Select Applet
0×00 0xa4 0×04 0×00 0x0b 0×01 0×02 0×03 0×04 0×05 0×06 0×07 0×08 0×09 0×00 0×00 0x7F;
Now these will have to be changed to accord with yours. As you can see in ‘select applet’ command, it starts 0×00 0xa4 0×04 0×00 as it always will. Then it has 0x0b, the LC bit, which shows length of the CDATA being sent. In hexadecimal, 11 is B, so 0x0b or just 0xb would do. If you count the data, it is the next 11 bytes (every piece of hex before the end bit 0x7F;). The CDATA to select the applet is, of course the Applet ID. And there is a correlation between this and the create applet command you may see.
Basically the create applet has the LC of the AID and the AID as the CDATA for select applet with 0×00 added onto the end. And then the length of this is worked out (number of LC of select + another byte at end + actual LC byte = LC + 2) so oxod.
The final command to be added is simply powerdown;
If you do this then save the script. Go back to the command prompt (or terminal, or whatever), and put in
start cref -o Java_card_prog.eeprom
(in linux – you will have to open another terminal – start won’t work)
This command will open another command prompt which will be listening on a port to be fed in the script which it can then install to the card
Then in the first command prompt, type apdutool Java_card_prog.scr
You will see lots of lines of data come up, which will help you debug.
If you look at the last 2 bytes that are sent back, these are the only ones that count. The SW1: and SW2: or the second may be called something different, but it’s always the last 2 bytes of each line.
If SW1: 90 or 0×90 and SW2: 00 or 0×00 this means the command it OK. Which is good. Each line has this and you could go through every line. Basically as long as there haven’t been any errors so far, then it is fine and you just need to check the last 2 lines, which is the create and select applet commands, respectively.
If there is something wrong with either of these, first go back to the first line and make sure the whole applet installed properly, if it did look at this in the script – there will most probably be a problem here. If not then look in the script at the last 2 command to make sure they are completely correct (i.e. corresponding AID’s to what you compiled it with) – if they are fine then there is something with your code. Probably in the Select (for select) or a constructor or start method for creation. However you may have nothing in your select method but this is the only thing giving an error. Sometimes the select error is also for creating a constructor.
If you want to then run this when it’s fine with an emulator running in netbeans or just the wireless toolkit on it’s own, you simply run the command
cref -p 9025 -i Java_card_prog.eeprom
which opens the cref on port 9025 (standard cref port) and takes in the file Java_card_prog.eeprom (eeprom was initially used as it would be the actual EEPROM in a real smart card – if you don’t know you should look up smart card internal designs).
If anyone needs help or didn’t understand anything, I’m going to be putting up a post of how to install everything and run a simple program, there may be a video, which I’ve already done for installing on Windows XP. But haven’t uploaded yet to youtube or anywhere. This will explain everything, but if you have comments before then, feel free to ask. There is also a synopsis of all the commands and such at the bottom, for quick reference which you can save into a folder/make a batch script from etc)
making java card work:
javac -g -d prototype -classpath “.;%JC_HOME%\lib\api.jar” prog.java
converter -applet 0×01:0×02:0×03:0×04:0×05:0×06:0×07:0×08:0×09:0×00:0×00 prototype.Prog -classdir prototype -exportpath %JC_HOME%\api_export_files prototype 0×01:0×02:0×03:0×04:0×05:0×06:0×07:0×08:0×09:0×00 1.0
scriptgen -o Prog.scr prototype\prototype\javacard\protoytpe.cap
use eclipse to create script files
//Step 3: Edit the output of step 2 (the scr-file):
//This must be the first statement in the scr-file!
// Select the installer applet (I think this already exist in .scr-file — not in mine)
0×00 0xA4 0×04 0×00 0×09 0xa0 0×00 0×00 0×00 0×62 0×03 0×01 0×08 0×01 0x7F;
//CAP begin (this already exists in your cap-file)
0×80 0xB0 0×00 0×00 0×00 0x7F;
//Place the Generated CAP-stuff here
//CAP end (Also exists in your cap-file)
0×80 0xBA 0×00 0×00 0×00 0x7F;
// create Applet
0×80 0xB8 0×00 0×00 0x0d 0x0b 0×01 0×02 0×03 0×04 0×05 0×6 0×07 0×08 0×09 0×00 0×00 0×00 0x7F;
// Select Applet
0×00 0xa4 0×04 0×00 0x0b 0×01 0×02 0×03 0×04 0×05 0×6 0×07 0×08 0×09 0×00 0×00 0x7F;
start cref -o safetex.eeprom
//opens new window
//if SW1: 90 and SW2: 00 – last 2 bits of last few lines then is correct
start cref -e -p 9025 -i safetex.eeprom