Java Card Binary Parser
Introduction
Normally .java files are compiled using java compiler which generates binary in a file called .class files. Similarly, Java Card applications written in java are compiled to class files using Java compiler. Java Card convertor used then to convert class files from java card applets to CAP(converted applet) file. In this process along with the .class files .exp (export) files of the imported packages are also provided to the convertor.
Note - Please read Oracle Java Card 3.0.5 Specification for more details.Export File
The export file contains information about the public APIs of the entire package. It is used by java card convertor to convert a java package into a CAP file.
CAP File
CAP file contains information about all the items in the package like class, method, instance fields etc. Package items are represented by tokens. A CAP consists of a set of components each has significant information about the package items. Each component is a stream of bytes stored in big endian order.
Header
This component contains general information about the CAP file such as AID of the package which identifies the package along with the major and a minor version numbers.
Applet Component
An applet component contains an entry for each of the applets(AID) defined in this package. If no applets are defined in the current package, this component must not be present in this CAP file.
Directory Component
The directory component lists size of each of the components defined in this CAP file.
Constant Pool Component
This component contains an entry for each of the classes, methods, and fields referenced by elements in the Method Component of this CAP file. Each entry is four byte long. ByteCode accesses each entry by using index into the constant pool table. Entry in the table pointed by an index contains item tokens. Tokens are resolved by VM on the card to access item in the memory.
Class Component
The class component describes all classes and interfaces defined in this package. It contains enough information to create instances, to perform method or field access and to check cast of the instance. For invoking methods it references method component. If the superclasses are defined in outside of the package, then they are referenced through Import component.
Method Component
The component contains bytecode info of all the methods defined in the current package. Component also gives the information about exception handler which represents a catch or finally block defined in a method of this package.
Static Field Component
The static field Component contains all the information required to create and initialize an image of all static fields defined in this package.
Import and Export Components
The import component contains information of the packages which are imported in the current package. The imported packages are identified by their AID. The Export component describes all classes, static functions or fields which are offered to other packages.
Reference Location Component
The reference location Component represents lists of offsets into the info item of the method component. Info at the offsets are indices into the constant pool table.
Descriptor Component
The descriptor component provides sufficient information to parse and verify all elements of the CAP file.
Debug Component
This section specifies the format for the Debug Component. The Debug Component contains all the metadata necessary for debugging a package on a suitably instrumented Java Card virtual machine. It is not required for executing Java Card programs in a non-debug environment.
Parser
My parser script interprets the java card binaries with a fixed format according Java Card 3.0.5 spec, and converts the data into human readable data and prints on the console. Which can then be analyzed to know more inside into the Java Card Binary.
CapParser.py [option] .cap [option] 'component name'/'all'.
All the options are case sensitive.
--help--capPath
-p valid path to cap file.
--component
-c caomponent name e.g 'header' or 'all'to get all component info in one shot.