CRM & Cloud

Issues I faced in CRM & Cloud solutions

ava(smart) Card is considered as future technology. They are getting fame and being used in many secure applications. In many countries like Estonia smart cards are mandatory to have for each citizen. They are being used as a secure transection medium.
Some of the formal definations of the java card are below:
Java Card refers to a technology that allows Java-dd applications (applets) to be run securely on smart cards and similar small memory footprint devices. Java Card is the tiniest of Java targeted for embedded devices. Java Card gives the user ability to program the device and make them application specific
Oracle say that:
Java Card technology provides a secure environment for applications that run on smart cards and other devices with very limited memory and processing capabilities. Multiple applications can be deployed on a single card, and new ones can be added to it even after it has been issued to the end-user. Applications written in the Java programming language can be executed securely on cards from different vendors
Also some site has the definition that:
Java Card is an open standard from Sun Microsystems for a smart card development platform. Smart cards created using the Java Card platform have Java applets stored on them. The applets can be added to or changed after the card is issued.
All above descriptions and definitions of java cards are correct and have meanings. But if we want to summarize them all then we can say that:
A Java Card is a smart card (having a chip) which can be programmed in java programming language using special application programming interface and is obeying set of rules and standards
.
To demonstrate how to write a Java Card basic applet and how to execute it, In the video blow I have wirtten a java card applet, just an example. In this example, Java Card Counter, Java card applet will initialize a variable with '0' and will start incrementing its value upto '4' on each APDU. It is a basic example, and I hope, it will help people who are just beginners in the Java Card field ...




Please write down comment and feel free to ask if you can't understand anything.
Before showing you and explaining how to write a Hello World applet for the java card, I would like to discuss how to set the development envirement for the java card applets.
Fortunately, Oracle has provided all the necessary tools, libraries and set of APIs for the development of the Java Card applets. Go to the Oracle offical website and download java card sdk version 2.2.1.
Note: Please don’t go for java card 3.0 as there is no real java card available for prgramming and there is not use to learn if you don’t have a real time envirement to deploy or test it. For further information click here
When you will download the development kit, you will will find that it also have complete java card specs and API documentation alongwith programemrs note. Mainly following tools are very helpful for development of java card apps:
  • Java Card Converter
  • scriptgen
  • Off-card Tool
  • exp2text tool
  • Off-card verifier
  • capdump
  • Installer
A brief description of all of the above mentioned tools are given below:
  • Java Card Converter
  • The converter tool is a very important tool. This tool tool’s duty is to convert your java source files thats .class files to .cap file. CAP file is a Converted Applet File, and this is the actual file which you load and install on the java card. You can’t directly upload your source files(.class files) onto the card.
    For more information click here
  • scriptgen
  • This tool simple converts your CAP file into a sequence of APDUs which can be send to java card for installation of that particular CAP file.
    For more information click here
  • Off-card Tool
  • The off-card verifier is a combination of three tools, verifycap, verifyexp, and verifyrev.
    For more information click here
  • exp2text tool
  • The exp2text tool is provided to allow you to view any export file in text format or human readable formate.
    For more information click here
  • capdump
  • This tool is similar as exp2text tool. For viewing cap files in the ascii format you will use this tool on the command prompt.
    For more information click here
  • Installer
    The development kit installer can be used to:
  • Dynamically download a Java Card technology package to a Java Card technology-compliant smart card. During development, the CAP file can be installed in the C-language Java Card RE rather than on a Java Card technology-compliant smart card. The installer is capable of downloading version 2.1, 2.2, 2.2.1, and 2.2.2 Java Card technology based CAP files (“Java Card CAP files”).
  • Perform necessary on-card linking.
  • Delete applets and packages from a Java Card technology-compliant smart card. Once the installer is selected, requests for deletion can be sent from the terminal to the Java Card technology-compliant smart card in the form of APDU commands. For more information, see Deleting Packages and Applets.
  • Setting default applets on different logical channels.
  • The installer is not a multiselectable application. On startup, the installer is the default applet on logical channel 0. The default applet on the other logical channels is set to No applet selected.
To set up java card development environment you will need all the tools which I have mentioned in my previous post. To set up all these tools there is a very fine link/book which explains each and everything very clearly. In this blog I will focus on the development/programming and will try to use already provided resources and documentation as there is no use to re-invent the wheel. Please Click here to learn how to set up the development environment for java card.
Another very informative link which will help you for setting your development environment. In this link everything is explain via images and screen shots.
Moreover, the above mentioned link will show you how to set up envirement with JCWD which is a tedious way for settting things up. As a beginner you will find a lot of troubles and problem during settings. To get rid of this mess you should go for JCOP Tools. JCOP is an eclipse plugin which is used for development of java card applets. In this blog my posts will using this tool.
There are many reason that I have recommend this tool, for of all it is very easy to integerate with eclipse. You can watch the video below to see how:
The other few advanges of using JCOP are as below:
  • Easy to use and write your code
  • Automatically converts your source code to cap file
  • Global Platform Compliant
  • Off card simulator
  • Debugging facility
  • Allow to install/delete cap files from card
A brief document of using JCOP is here
Before starting development and programming for java cards or any other smart card you must need to learn about the basics of them. For Java Card you need to learn and understand following documents:
  • ISO7816-3
  • ISO-7816-4
  • Global Platform 2.2 Or 2.1
  • JCVM 2.2.X
  • Java Card API 2.2.X/Java Card Programming Notes 2.2.X
A beirf description of the above documents are given below:
  • ISO7816-3
  • This document contains the information about the hardware of the (Java)smart card. By hardware I mean the chip size, its contacts, its voltage and other properties like that.
  • ISO-7816-4
  • This document provides all the information which is necessary for the communication between Java card and the CAD (Card Acceptance Device) or host application.
  • Global Platform 2.2 Or 2.1
  • This document is similar as ISO7816-4 but more and much comprehensive. It states all the commands and methods for the communication for the Java Card like installing and selecting an application within the java card
  • JCVM 2.2.X
  • This document is about the internal structure of the Java Card. It explains how java card interprets the APDUs and its instruction set.
  • Java Card API 2.2.X/Java Card Programming Notes 2.2.X
  • These documents are fore Java Card applet developer. It contains the explanations of the functions and programming interface which is available during the programming.
To download GP document you need to visit the GP Site and all other document you will get when you download the development kit from the oracle site. Lucky isn't :P


Writing Java Card applets is not a hard thing, but yes you need to be familiar with java card API and its standards to pass APDUs.
In this post I will try to explain the mandatory functions for the successful execution of the Java Card applet.
Following functions are necessary for an applet to execute and its installation etc.
public static void install(byte[] bArray, short bOffset, byte bLength){}
public void process(APDU apdu){}
protected final void register(){}
        //or
protected final void register(byte[] bArray, short bOffset, byte bLength){}
                                                           throws SystemException
public boolean selectingApplet(){}


  • install()

  • To create an instance of the Applet subclass, the Java Card runtime environment will call this static method first.
    All the initialization and memory allocations should be done in this method.
    All the memory allocations should be done in this method or in the constructor of the class. It will prevent you from memory overflow error later on.


  • process()

  • This method is like a main method of a normal java class. After the installation the execution of APDUs takes place from this method. Each and every APDU comes in this method and get processed as its name suggest ;)


  • register()

  • In order to register an instance of the applet with the Java Card Runtime enviroment this method must be called. This method should be called within install() method.


  • register(parameters)

  • This method works same as above register() plus it assigns the specified AID bytes to the instance AID bytes.


  • selectingApplet()

  • This method is used by the applet process() method to distinguish the SELECT APDU command which selected this applet, from all other SELECT APDU commands which may relate to file or internal applet state selection.
    It is a good programming practice that you should return 0x9000 when this function returns true
    Ok, I hope you are now familiar with the most important functions of the java card applet. Your class (your own applet) will inherit these functions from the Applet class of thejavacard.framework package by extending the javacard.framework.Applet class.
    Now if we sum-up all the above functions and write them in a way so that a complete applet can be written is as below:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import javacard.framework.APDU;
    import javacard.framework.Applet;
    import javacard.framework.ISO7816;
    import javacard.framework.ISOException;
    public class MyOwnApplet extends Applet {
        public static void install(byte[] bArray, short bOffset, byte bLength) {
            // GP-compliant JavaCard applet registration
            new MyOwnApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
        }
        public void process(APDU apdu) {
            // Good practice: Return 9000 on SELECT
            if (selectingApplet()) {
                return;
            }
            byte[] buf = apdu.getBuffer();
            switch (buf[ISO7816.OFFSET_INS]) {
            case (byte) 0x00:
                break;
            default:
                // good practice: If you don't know the INStruction, say so:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        }
    }