Forum Stats

  • 3,839,693 Users
  • 2,262,529 Discussions
  • 7,901,042 Comments

Discussions

Sharing Objects between Applets...

875315
875315 Member Posts: 4
edited Sep 24, 2013 9:33PM in Java Card
Hello!

i have information like that we can only share primitive data types by using shareable interface methods between two different applets which are under different context, different packages.
is it possible to share objects between applets??
i mean;
for example we have cardholder class which was written by us and in a java card applet we define a variable whose data type cardholder class.. can i have this variable and its reference from another applet which is under different package.

Thanks a lot in advance!
Tagged:

Answers

  • safarmer
    safarmer Member Posts: 2,829 Bronze Trophy
    edited Jul 13, 2011 4:39PM
    Hi,

    You can share objects as long as they are also shareable objects. For this they do not need to be an applet, just have an interface that extends Shareable.

    Here is an example of a User class that is shareable like this
    public interface SharedUser extends Shareable {
    
        /**
         * @return the id
         */
        public short getId();
    
        /**
         * @return the name
         */
        public short getName(byte[] buf, short off);
    
    }
    
    public class User implements SharedUser {
        private static short count = 1;
        private short id;
        private byte[] name;
    
        /**
         * 
         */
        public User() {
            id = count++;
            name = new byte[] { 'S', 'h', 'a', 'n', 'e' };
        }
    
        /* (non-Javadoc)
         * @see oracle.other.SharedUser#getId()
         */
        public short getId() {
            return id;
        }
    
        /* (non-Javadoc)
         * @see oracle.other.SharedUser#getName(byte[], short)
         */
        public short getName(byte[] buf, short off) {
            return Util.arrayCopy(name, (short) 0, buf, off, (short) name.length);
        }
    }
    
    public interface SharedAccess extends Shareable {
        public SharedUser getUser();
    }
    
    public class AccessApplet extends Applet implements SharedAccess {
    
        public static void install(byte[] bArray, short bOffset, byte bLength) {
            // GP-compliant JavaCard applet registration
            new AccessApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see javacard.framework.Applet#select()
         */
        public boolean select() {
            return false;
        }
    
        public void process(APDU apdu) {
            return;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see javacard.framework.Applet#getShareableInterfaceObject(javacard.framework.AID, byte)
         */
        public Shareable getShareableInterfaceObject(AID clientAID, byte parameter) {
            return this;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see oracle.other.SharedAccess#getUser()
         */
        public SharedUser getUser() {
            return new User();
        }
    
    }
    You can add a method to your main SOI to return the shareable interface (SharedUser in this case) and then use that in your client applet. Be aware that the only way to get a byte array across a SOI is using the APDU buffer to copy. You will notice the getName method of the shared user takes an array rather than returning the array. The copy happens in the server context and the server is allowed to access the APDU buffer as it is global. If you return the name buffer or pass a buffer from the server you will get a security exception.

    Cheers,
    Shane

    Edited by: safarmer on 13/07/2011 13:39
  • safarmer
    safarmer Member Posts: 2,829 Bronze Trophy
    Here is the client applet. To illustrate the security issue INS 0x00 works, 0x01 returns 0x6F00 as I have no error handling.
    public class Main extends Applet {
        private SharedAccess acc;
        private byte[] sharedAID = { 'a', 'c', 'c', 'e', 's', 's' };
        private byte[] buffer = new byte[256];
    
        /**
         * 
         */
        public Main(byte[] bArray, short bOffset, byte bLength) {
            register(bArray, (short) (bOffset + 1), bArray[bOffset]);
    
            AID aid = JCSystem.lookupAID(sharedAID, (short) 0, (byte) sharedAID.length);
            acc = (SharedAccess) JCSystem.getAppletShareableInterfaceObject(aid, (byte) 0);
        }
    
        public static void install(byte[] bArray, short bOffset, byte bLength) {
            // GP-compliant JavaCard applet registration
            new Main(bArray, bOffset, bLength);
        }
    
        public void process(APDU apdu) {
            // Good practice: Return 9000 on SELECT
            if (selectingApplet()) {
                return;
            }
    
            byte[] buf = apdu.getBuffer();
            apdu.setIncomingAndReceive();
    
            short rlen = 0;
    
            switch (buf[ISO7816.OFFSET_INS]) {
                case (byte) 0x00:
                    Util.setShort(buf, (short) 0, acc.getUser().getId());
                    rlen = acc.getUser().getName(buf, (short) 2);
                    apdu.setOutgoingAndSend((short) 0, rlen);
                    break;
    
                case (byte) 0x01:
                    Util.setShort(buffer, (short) 0, acc.getUser().getId());
                    rlen = acc.getUser().getName(buffer, (short) 2);
                    apdu.setOutgoing();
                    apdu.setOutgoingLength(rlen);
                    apdu.sendBytesLong(buffer, (short) 0, rlen);
                    break;
    
                default:
                    // good practice: If you don't know the INStruction, say so:
                    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        }
    }
    Cheers,
    Shane
  • safarmer
    safarmer Member Posts: 2,829 Bronze Trophy
    edited Jul 13, 2011 4:43PM
    Removed duplicate post as the forum wasn't posting my reply. I guess it did. Thanks Oracle : )

    Edited by: safarmer on 13/07/2011 13:43
  • 875315
    875315 Member Posts: 4
    It works..and also i am using global apdu buffer to share byte arrays between applets.
    Thanks a lot..
  • Dreameng
    Dreameng Member Posts: 3

    Dear,

    You are using process(APDU apdu) method to get a global array which can be passed between applets. But I am developing a Telecom application, it is triggered by event through processToolkit(byte event)  method, so the applet will not receive any apdu, then how can I get the APDU buffer?             Thank you very much!

  • Dreameng
    Dreameng Member Posts: 3

    Dear Safarmer,

    You are using process(APDU apdu) method to get a global array which can be passed between applets. But I am developing a Telecom application, it is triggered by event through processToolkit(byte event)  method, so the applet will not receive any apdu, then how can I get the APDU buffer?             Thank you very much!

This discussion has been closed.