Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

SmartCard Java Serial Interface

5.00/5 (1 vote)
12 Nov 2010CPOL2 min read 22.2K   336  
Getting SmartCard lifecycle state from Java

Introduction

I'm writing this article as second fascicle of my last one titled "SmartCard Direct Serial Interface". This one explains how to interact with the same Phoenix interface, but now, to make it more educational, I'm going to explain how to know the Personalization phase of a SmartCard. SmartCards are usually assembled by different companies: one company manufactures the resine, other one manufactures the polysilicon chip, another one installs the firmware, and another one performs the personalization (creation of directory and file structures). Usually, manufacturers use one historical ATR byte to indicate the phase in what the card is. Concretely, I'm going to use in this laboratory a smartcard firmware that updates the H4 (fourth historical byte) each time the SmartCard changes phase.

Fases.jpg

First of all, a SmartCard is at Fabrication state, and doesn't transit to Personalization state until a file containing Pre-Personalization keys is made; this is done to protect the logistic between the company that assembles the firmware and the one that generates the filesystem.

Laboratory

The hardware necessary is a Phoenix Programmer described in my last article "SmartCard Direct Serial Interface". Now the implementation of this Lab is made in Java. So, it needs JDK (not less than 1.4) and Java Communications API; installing this API basically consists of copying a jar (comm.jar), a .properties (javax.comm.properties) and a DLL (win32com.dll). Here you have a manual on how to install and configure it (http://www.oracle.com/technetwork/java/index-jsp-141752.html).

Using the Code

The application uses swing to draw the Graphic part of the application. After start, it creates a Thread dedicated to communication with RS-232 serial port. The Class dedicated to serial comm. is named CommSerie, and SerialPort object is abstracted in puertoSerie property. To set RTS line to 5V and after to 0V resetting the SmartCard and getting the ATR after about 40000 clock cycles. Next, reading serial I/O buffer we get the ATR as byte[]. The ATR format is:

-----------------------------------------------
| TS | T0 | TB | H1 || H2 | H3 | H4 | H5 | H6 |
-----------------------------------------------

Historical bytes are H1..H6; in this lab, H4 byte (String 11-12th) contains the information about SmartCard lifecycle phase. This is the code to read it:

Java
public void enviaReset(){
    // En flanco de bajada inicia el ATR
    puertoSerie.setRTS(true);
    puertoSerie.setRTS(false);    
  }

Once read, comparing it to the corresponding state is mapped in 'Constantes' interface.

Java
if (estado.equals(Constantes.ESTADO_FABRICACION))
      sol = "Fase de Fabricacion. Imposible personalizar";
    else if (estado.equals(Constantes.ESTADO_PREPERSONAL))
      sol = "Fase de Pre-PersonalizaciĆ³n. Se puede Reutilizar";
    else if (estado.equals(Constantes.ESTADO_PERSONALZDA))  
      sol = "Tarjeta Personalizada. No se puede reutilizar";
    else if (estado.equals(Constantes.ESTADO_OFFLINE))
      sol = "Ponga en tensiĆ³n el lector e inserte la tarjeta";
    else
      sol = "Tarjeta en fase desconocida. No se puede reutilizar";

I hope you enjoy it!!!

History

  • First draft

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)