This discussion is archived
6 Replies Latest reply: Apr 27, 2012 2:55 AM by Adriaan RSS

Accessing a Static Array in one CAP file from another CAP in same project

858973 Newbie
Currently Being Moderated
Hi all,
I have been developing a banking application on JavaCard. So the size of the applet has grown size able and have now split the file to multiple CAP files. I have been using NXP JCOP plugin in Eclipse for development. Below are the steps done

a.) I have created multiple packages under the same project where one project has all the variable which are public static final and don't reference any other class. Also this package does not extend Applet. So it finally generates to a CAP file.

b.) Another package which imports the above package and also extend Applet is the one which has all the UI and logic related to the project. The applet uses the static array defined in the another package to show the menu on phone.

Now the problem is that when the code come to point for fetching the menu data, i have some thing like menu.length . But when the code comes to this point in debugger i get a
+6F 00    Status: No precise diagnosis.+

To cross check i put a watch on the menu variable and found that it has all the data required for showing the menu (which is of 32 byes). So now i am perplexed on why this length is giving this problem. Am i doing some thing wrong. Below is the Snip of the larger code

This code is the package which has all the constants..
package constantsPack;

import javacard.framework.APDU;
import javacard.framework.ISO7816;
import javacard.framework.JCSystem;
import javacard.framework.Util;

public class ConstantsApplet {

//menu array decleration
public static final byte[] MAIN_SETUP_MENU = {
                   (byte) 0x81,  ConstantsApplet.CMD_DTL_LENGTH, ConstantsApplet.CMD_NUMBER, ConstantsApplet.MAIN_CMD_SET_UP_MENU, (byte) 0x00,
                   (byte) 0x82, (byte) 0x02, ConstantsApplet.DEVICEID_SIM, ConstantsApplet.DEVICEID_ME,
                   (byte) 0x85, (byte) 0x08 ,'A','B', 'C',' ','A','p','p','s',
                   (byte) 0x8F, (byte) 0x0D, ConstantsApplet.SB_MENU, 'A','b','c','d','e','f','g',' ','B','a','n','k',
         };
// other constants.
}
This is the package which has the applet and which uses the variables of the above package
package myPkgLib;

import constantsPack.*;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.OwnerPIN;
import javacard.framework.Util;
import javacard.framework.JCSystem;


public class myApplet extends Applet{
// other code

 fetch(ConstantsApplet.MAIN_SETUP_MENU.length);

.......
}
Any help regarding this would be very helpful...

Regards
Prakash

Edited by: prakash on Apr 18, 2012 11:57 PM
  • 1. Re: Accessing a Static Array in one CAP file from another CAP in same project
    801926 Explorer
    Currently Being Moderated
    Put a try/catch around the fetch method and see what Exception is thrown. If you have an applet in same package as the constants class, it will cause a security violation due to package firewall. Also static objects gets a special treatment in JC, see JCRE spec, so it might be better to have a method, e.g. getLength(byte[] x) to circumvent it.
  • 2. Re: Accessing a Static Array in one CAP file from another CAP in same project
    858973 Newbie
    Currently Being Moderated
    Hi lexdabear,
    I tried putting a function inside the library package where the static array is present, but the results are the same... Also by try catch . what exception you want to catch... i see Exception has just .equal function... Tried with ExternalException also but does not seem to enter it..

    Attaching the source code compilable in eclipse below ..
     package constantsPack;
    
    public class ConstantsApplet {
    
         public static final byte[] MAIN_SETUP_MENU = {
              (byte) 0x81, (byte) 0x03, (byte) 0x01, (byte) 0x25, (byte) 0x00,
              (byte) 0x82, (byte) 0x02, (byte) 0x81, (byte) 0x82,
              (byte) 0x85, (byte) 0x08 ,'A','B', 'C',' ','A','p','p','s',
              (byte) 0x8F, (byte) 0x0D,  0x0F, 't','e','s','t','a','p','p',' ','B','a','n','k',
    };
         
           
              public ConstantsApplet ()
              {
              
              }
    
    }
    The applet code where the above static array is used..
    package smilePkgLib;
    
    
    import constantsPack.*;
    import javacard.framework.APDU;
    import javacard.framework.Applet;
    import javacard.framework.ISOException;
    import javacard.framework.Util;
    
    public class BLApplet extends Applet{
         short fetchcode =0;
    
         public static void install(byte[] bArray, short bOffset, byte bLength) {
              // GP-compliant JavaCard applet registration
              new smilePkgLib.BLApplet().register(bArray,
                        (short) (bOffset + 1), bArray[bOffset]);
         }
    
    
         private BLApplet(){
    
              // put init code
         }
    
    
    
         /**
          * The main method.
          */
         public void process(APDU apdu) throws ISOException {
              if (selectingApplet()) {
                   return;
              }
    
              byte choosenMenuItem = 0;
    
              byte[] buf = apdu.getBuffer();
              byte bytesRead =(byte)(apdu.setIncomingAndReceive());
     
              fetchList(ConstantsApplet.MAIN_SETUP_MENU , (byte)0x00);
    
         }
    
         private void fetchList(byte[] list, byte command){
              shouldFetch(list); 
         }
    
         /**
          * Overloaded method to fetch the MENUs only.      
          * @param menu
          */
         private void shouldFetch(byte[] menu) {
              shouldFetch((byte)menu.length);
         }
    
    
         private void shouldFetch(byte len) {
              
                   fetchcode = Util.makeShort((byte) 0x91, (byte) (len));          
              
         }
    
    }
    The JCOP script to run the above project
    /select |DefaultApplet
    
    # Terminal Profile
    /send A0100000133B67FF7F1F020F0B4100000000869804000000 
    Have the eclipse loadable project but dont know how to put here...

    Regards
    prakash.
  • 3. Re: Accessing a Static Array in one CAP file from another CAP in same project
    858973 Newbie
    Currently Being Moderated
    Any thoughts or help from any one please....

    Regards
    Prakash
  • 4. Re: Accessing a Static Array in one CAP file from another CAP in same project
    Adriaan Explorer
    Currently Being Moderated
    Have you considered using the Shared Interface Objects (SIO) package in Java Card? There are several threads about it on this forum.

    Adriaan
  • 5. Re: Accessing a Static Array in one CAP file from another CAP in same project
    858973 Newbie
    Currently Being Moderated
    Yes Adriaan, for some of the other applets in the project i have used SIO as it fits the bill. But in this case i need it this way... any thoughts on this.. it is really a unsolved maze as in debugger i see the data structure filled with the data but when i do length it booms.... :-(

    Regards
    Prakash..
  • 6. Re: Accessing a Static Array in one CAP file from another CAP in same project
    Adriaan Explorer
    Currently Being Moderated
    I tested your code and found that it works fine if the classes are in the same package. I realise that doesn't answer your question but that should direct your research in the right direction...

    EDIT: have a look here:
    http://java.sun.com/developer/Books/consumerproducts/javacard/ch09.pdf

    From Section 9.1:
    "[The applet firewall] also provides protection against hacking. An applet might be able to obtain an object reference from a publicly accessible location, but if the object is owned by another applet in a different package, the firewall prevents access to the object."

    So that seems to be the issue: access to objects across packages is not (or should not be!) possible. However, I don't know why it's possible access the contents of a static byte[]; perhaps it's a special case.

    Section 9.1.3 and 9.1.3 give more info on object ownership and object access, but don't explain why reading a primitive static array is possible between different packages. According to my understanding of these sections, it should not be possible to access an array (static or not) across group contexts.

    More EDIT:
    If you make the array non-static, and instantiate the array's wrapper class in your applet, doing "menu.length" does work even if the wrapper class is in another package. This makes sense because the applet is the owner of the created wrapper class and thus also of the array.


    Adriaan

    Edited by: Adriaan on Apr 27, 2012 2:52 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points