0

I have a question about javacard sharable object. I want to read a shared array from server in client base on following link: [JavaCard: How can an applet's instance call a method in another instance's context?

I have a client and a server applet, when i call the function

theSharedArray = (SharedArray) JCSystem.getAppletShareableInterfaceObject(masterAID, SHARABLE_PARAM);

in client it returns 6f00. client applet is:

public class Client extends Applet implements ISO7816 {

    private static final byte SHARABLE_PARAM = 0;
    public interface SharedArray extends Shareable {
        public byte[] getSharedArray();
    }

    public static class SharedArrayImpl implements SharedArray {
        private byte[] sharedArray;

        public SharedArrayImpl(final byte[] arrayToShare) {
            this.sharedArray = arrayToShare;
        }

        public byte[] getSharedArray() {
            return sharedArray;
        }
    }


    byte[] idinAID = new byte[]{(byte)0xA0, 0x00, 0x00, 0x03, 0x08, 0x00, 0x00, 0x10, 0x00};
    private AID masterAID = new AID(idinAID, (short) 0, (byte) idinAID.length);

    public static void install(byte[] bArray, short bOffset, byte bLength) throws SystemException {
        new Client().register(bArray, (short) (bOffset + 1),
                bArray[bOffset]);
    }

    private Client() {        

    }

    public void process(APDU apdu) {

        if (selectingApplet()) {
            return;
        }

        byte[] buf = apdu.getBuffer();
        byte cla = buf[OFFSET_CLA];
        byte ins = buf[OFFSET_INS];

        switch (ins) {
            case (byte) 0x03: 
                sendOut(apdu);
                break;            
            default:
                ISOException.throwIt(SW_INS_NOT_SUPPORTED);
        }
    }

    public void sendOut(APDU apdu)
    {
        final SharedArray theSharedArray;

        theSharedArray = (SharedArray) JCSystem.getAppletShareableInterfaceObject(masterAID, SHARABLE_PARAM);
        byte[] sa = theSharedArray.getSharedArray();
        apdu.setOutgoing();
        apdu.setOutgoingLength((short) 10);
        apdu.sendBytesLong(sa, (short) 0, (short) 10);
    }


}

and the server applet is:

public class SharingApplet extends Applet {

    public interface SharedArray extends Shareable {
        public byte[] getSharedArray();
    }

    public static class SharedArrayImpl implements SharedArray {
        private byte[] sharedArray;

        public SharedArrayImpl(final byte[] arrayToShare) {
            this.sharedArray = arrayToShare;
        }

        public byte[] getSharedArray() {
            return sharedArray;
        }
    }


    private static final byte PARAM_SHARED_ARRAY = 0;

    private final SharedArray sharedArray;
    byte[] idinAID = new byte[]{(byte)0xA0, 0x00, 0x00, 0x03, 0x08, 0x00, 0x00, 0x10, 0x00};
    private AID masterAID = new AID(idinAID, (short) 0, (byte) idinAID.length);

    public static void install(byte[] bArray, short bOffset, byte bLength) {
        new SharingApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
    }

    public SharingApplet() {
        sharedArray = new SharedArrayImpl(idinAID);       
    }

    public void process(APDU apdu) {
        if (selectingApplet()) {
            return;
        }

        byte[] buf = apdu.getBuffer();
        switch (buf[ISO7816.OFFSET_INS]) {

        case (byte) 0x03:
        {
            apdu.setOutgoing();
            apdu.setOutgoingLength((short) 9);
            apdu.sendBytesLong(idinAID, (short) 0, (short) 9);
            break;
        }               
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

    public Shareable getShareableInterfaceObject(AID clientAID, byte parameter) {
        if (sharedArray == null || parameter != PARAM_SHARED_ARRAY) {
            return null;
        }
        return sharedArray;
    }
}

I will be gratefull if anyone can help me. Thanks

Community
  • 1
  • 1
Mohsen Gorgani
  • 420
  • 4
  • 18
  • Possible duplicate of [0x6f00 error casting Javacard Shareable Interface](http://stackoverflow.com/questions/22102106/0x6f00-error-casting-javacard-shareable-interface) – Michael Roland Nov 12 '16 at 15:29
  • 2
    You are using two different sharable interfaces on the client and server side. Consequently casting between the two fails. – Michael Roland Nov 12 '16 at 15:31
  • Thank you Michael for your comment. In your link is said client and server should be in a same package. but i don't want they be in a same package. is it possible that client and server be in different package? – Mohsen Gorgani Nov 13 '16 at 07:48
  • No, that's not what that answer says. Only the sharable interface needs to be in the same Java package. – Michael Roland Nov 13 '16 at 09:06
  • sharable interface needs to be in the same Java package with what?Clent or server? – Mohsen Gorgani Nov 13 '16 at 09:15
  • I'm not sure I follow your question. The shareable interface needs bot be in the same Java package namespace on the client and the server side. So if you have the interface in package x.y.z (leading to the shareable interface name x.y.z.MySharableInterface) in your server application, then you need to have that same package namespace for the sharable interface in your client application (i.e. the sharable interface of the client needs to be in x.y.z.MySharableInterface). – Michael Roland Nov 13 '16 at 09:18
  • this is my way, client package is : x.y.z and server package is: a.b.c. The sharable interface is created in server applet by package a.b.c and sharable interface is created in client applet by package x.y.z. you said i have to create another package in client just like server package mean a.b.c? and create sharable interface in it? – Mohsen Gorgani Nov 13 '16 at 09:33
  • Yes, you would need to have a.b.c.MySharableInterface in both applications. – Michael Roland Nov 13 '16 at 09:40
  • i test it and will tell you the result. – Mohsen Gorgani Nov 13 '16 at 10:02
  • it didn't work. In mentioned link, he said he created two applet in same application and same package, but i have two application with same package name. are you sure it can work on two application with same package name? What about package AIDs and appletAID?Does is matter what values they have? – Mohsen Gorgani Nov 13 '16 at 12:49

0 Answers0