Page 1 of 2

Sharing object between different applet...

Posted: Mon Nov 14, 2016 5:20 am
by mhsnmaghsoodloo
Hello every one.
I am working on a project that should two applet can communicate to each other. one of them is client and one of them is server. now i am using shareable interface but i cannot succeed.
I wrote a sample by shareable interface. i created a sample for client and server. but when i want to get the shared object from server in client applet and cast it to shareable interface i get the error 6f00. in some website said, because i had created two shareable interface one for client and one for server it take back to me 6f00. it said i should use the server shareable interface and i should not create shareable interface on client too? my question is here, how i use server shareable interface? should i import server cap file to my project? (I think it's not possible for me) or anything else. can anyone suggest me a solution?
my server code is like this:
package nl.owlstead.javacard.sharedarray;

import javacard.framework.*;

public class Server extends Applet implements ISharedArray{

private static final byte PARAM_SHARED_ARRAY = 0;

//private final ISharedArray array;
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 Server().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}

public Server() {
}

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) {
return this;
}

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

Sharable interface:
package nl.owlstead.javacard.sharedarray;

import javacard.framework.Shareable;

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

Client:
package nl.owlstead.javacard.sharedarray;

import javacard.framework.*;
public class Client extends Applet implements ISO7816 {

private static final byte SHARABLE_PARAM = 0;
byte[] serverAID = new byte[]{(byte)0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x01};
private AID masterAID = new AID(serverAID, (short) 0, (byte) serverAID.length);
Shareable sio;
byte[] shared;

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

private Client() {
shared = new byte[20];
}

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) 0x04:
sendOut(apdu);
break;
default:
ISOException.throwIt(SW_INS_NOT_SUPPORTED);
}
}

public void sendOut(APDU apdu) {
sio = JCSystem.getAppletShareableInterfaceObject(masterAID, SHARABLE_PARAM);

// this part returns 6f00
ISharedArray array = (ISharedArray) sio;

apdu.setOutgoing();
apdu.setOutgoingLength((short) 2);
apdu.sendBytesLong(shared, (short) 0, (short) 2);
}
}

I have to mention that, the package name are same but package aid and applet aid are completely different between two applet. some body suggest to me in web to make the package name same, maybe it works.

Re: Sharing object between different applet...

Posted: Mon Nov 14, 2016 6:31 am
by UNKNwYSHSA
serverAID is AID of server client applet instance?

Re: Sharing object between different applet...

Posted: Mon Nov 14, 2016 8:40 am
by mhsnmaghsoodloo
UNKNwYSHSA wrote:serverAID is AID of server client applet instance?

Thanks for your comment. Actually i didn't understand what you asked but

server package aid: 0x01:0x02:0x03:0x04:0x05:0x06
server applet aid: 0x01:0x02:0x03:0x04:0x05:0x06:0x01

client package aid: 0x01:0x02:0x03:0x04:0x05
client applet aid: 0x01:0x02:0x03:0x04:0x05:0x01

Re: Sharing object between different applet...

Posted: Mon Nov 14, 2016 10:33 pm
by UNKNwYSHSA
I had a test.
The client applet get the shared array from shareable server;
But the array can not be used directly.
I modify code like following:

Interface(IShareableInterface):

Code: Select all

short readArray(byte[] buf, short off, short len);


Server(implements IShareableInterface):

Code: Select all

public Shareable getShareableInterfaceObject(AID clientAID, byte parameter) {
    return this;
}

public short getArray(byte[] buf, short off, short len) {
    Util.arrayCopy(testArray, (short) 0, buf, off, len);
    return len;
}


Client:

Code: Select all

byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS])
{
case (byte)0x00:
    IShareableInterface sio = (IShareableInterface) JCSystem.getAppletShareableInterfaceObject(new AID(serverAID, (short) 0, (byte) (serverAID.length & 0xFF)), (byte) 0);
    short len = sio.readArray(buf, (short)0, (short)5);
    apdu.setOutgoingAndSend((short)0, len);
    break;
default:
    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}


Result:

Code: Select all

>> /select 112233445500  // Server applet;
>> 00 A4 04 00 06 11 22 33 44 55 00 00
<< 90 00

>> /send 00000000051122334455  // Write shared array data: 1122334455;
>> 00 00 00 00 05 11 22 33 44 55
<< 90 00

>> /select 010203040500  // Client applet;
>> 00 A4 04 00 06 01 02 03 04 05 00 00
<< 90 00

>> /send 0000000005  // ReadArray from server applet by client applet;
>> 00 00 00 00 05
<< 11 22 33 44 55 90 00


All OK.

Re: Sharing object between different applet...

Posted: Tue Nov 15, 2016 5:37 am
by mhsnmaghsoodloo
UNKNwYSHSA wrote:I had a test.
The client applet get the shared array from shareable server;
But the array can not be used directly.
I modify code like following:

Interface(IShareableInterface):

Code: Select all

short readArray(byte[] buf, short off, short len);


Server(implements IShareableInterface):

Code: Select all

public Shareable getShareableInterfaceObject(AID clientAID, byte parameter) {
    return this;
}

public short getArray(byte[] buf, short off, short len) {
    Util.arrayCopy(testArray, (short) 0, buf, off, len);
    return len;
}


Client:

Code: Select all

byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS])
{
case (byte)0x00:
    IShareableInterface sio = (IShareableInterface) JCSystem.getAppletShareableInterfaceObject(new AID(serverAID, (short) 0, (byte) (serverAID.length & 0xFF)), (byte) 0);
    short len = sio.readArray(buf, (short)0, (short)5);
    apdu.setOutgoingAndSend((short)0, len);
    break;
default:
    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}


Result:

Code: Select all

>> /select 112233445500  // Server applet;
>> 00 A4 04 00 06 11 22 33 44 55 00 00
<< 90 00

>> /send 00000000051122334455  // Write shared array data: 1122334455;
>> 00 00 00 00 05 11 22 33 44 55
<< 90 00

>> /select 010203040500  // Client applet;
>> 00 A4 04 00 06 01 02 03 04 05 00 00
<< 90 00

>> /send 0000000005  // ReadArray from server applet by client applet;
>> 00 00 00 00 05
<< 11 22 33 44 55 90 00


All OK.

Thanks for code,
The question is here, how you define the shareable interface in client side?
you defined shareable interface in client too? or you used it from server applet and how? did you use cap file in client applet?

Re: Sharing object between different applet...

Posted: Tue Nov 15, 2016 6:04 am
by UNKNwYSHSA
Here's my code:
Interface:

Code: Select all

package testShareableInterface;

import javacard.framework.*;

public interface IShareableInterface extends Shareable {
   public short getArray(byte[] array, short off, short len);
}


Server(Package AID 1122334455, Applet AID 112233445500):

Code: Select all

package testShareableInterface;

import javacard.framework.*;

public class testShareableInterface extends Applet implements IShareableInterface
{
   private byte[] testArray;
   
   testShareableInterface() {
      testArray = new byte[5];
   }
   
   public Shareable getShareableInterfaceObject(AID clientAID, byte parameter) {
      return this;
   }
   
   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      new testShareableInterface().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }
   
   public short getArray(byte[] buf, short off, short len) {
      Util.arrayCopy(testArray, (short) 0, buf, off, len);
      return len;
   }

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

      byte[] buf = apdu.getBuffer();
      switch (buf[ISO7816.OFFSET_INS])
      {
      case (byte)0x00:
         apdu.setIncomingAndReceive();
         Util.arrayCopy(buf, (short) ISO7816.OFFSET_CDATA, testArray, (short) 0, (short) 5);
         break;
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }

}


Client(Package AID 0102030405, Applet AID 010203040500):

Code: Select all

package testShareableInterfaceClient;

import javacard.framework.*;
import testShareableInterface.IShareableInterface;

public class testShareableInterfaceClient extends Applet
{
   final static byte[] serverAID = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x00 };
   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      new testShareableInterfaceClient().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }

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

      byte[] buf = apdu.getBuffer();
      switch (buf[ISO7816.OFFSET_INS])
      {
      case (byte)0x00:
         IShareableInterface sio = (IShareableInterface) JCSystem.getAppletShareableInterfaceObject(new AID(serverAID, (short) 0, (byte) (serverAID.length & 0xFF)), (byte) 0);
         short len = sio.getArray(buf, (short)0, (short)5);
         apdu.setOutgoingAndSend((short)0, len);
         break;
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }

}

Re: Sharing object between different applet...

Posted: Tue Nov 15, 2016 6:09 am
by UNKNwYSHSA
Here's is the project. And i test it on A22CR and J3D081 card.

Re: Sharing object between different applet...

Posted: Wed Nov 16, 2016 7:42 am
by mhsnmaghsoodloo
UNKNwYSHSA wrote:Here's is the project. And i test it on A22CR and J3D081 card.


I want to develop server applet and load on card and send the card to another company. they want to use our applet and call our shared functions. what should i give to other company to they use it to cast shareable object to it ?

Re: Sharing object between different applet...

Posted: Wed Nov 16, 2016 10:15 pm
by UNKNwYSHSA
Give them the Interface source file.
And docs to use the interface method(s).

Re: Sharing object between different applet...

Posted: Thu Nov 17, 2016 2:15 am
by mhsnmaghsoodloo
UNKNwYSHSA wrote:Give them the Interface source file.
And docs to use the interface method(s).

Yes you are right. But i couldn't create cap file file server.exp file. I use netbeans to compile and convert my project. but it says export file serverpackage.exp of package serverpackage not found. i create serverpackage folder in JC_home api_export_files folder and copy the exp file in it, but it doesn't find it. This is my ant script:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

<project name="ClientApplet" default="default" basedir=".">
    <description>Builds, tests, and runs the project pkiapplet1.</description>
    <import file="nbproject/build-impl.xml"/>
         <target name="-post-init">
       <!-- Java Card Ant Tasks definition -->
        <path id="jc-tasks" >
            <pathelement path="${jctasks.path}"/>
        </path>   
        <taskdef name="convert"
            classname="com.sun.javacard.ant.tasks.ConverterTask"
            classpathref="jc-tasks" />
        <taskdef name="verifycap"
            classname="com.sun.javacard.ant.tasks.VerifyCapTask"
            classpathref="jc-tasks" />
        <!-- Java Card classpath -->
        <path id="jc-classpath" >
            <pathelement path="${jc.home}/lib/converter.jar"/>
            <pathelement path="${jc.home}/lib/offcardverifier.jar"/>
            <pathelement path="${jc.home}/lib/scriptgen.jar"/>
            <pathelement path="${jc.home}/lib/apdutool.jar"/>
            <pathelement path="${jc.home}/lib/apduio.jar"/>           
            <pathelement path="."/>
        </path>
    </target>
 
    <!-- Target responsible for conversion and verification of the applet -->
    <target name="convert" depends="init">
      <copy todir="${build.classes.dir}">
         <fileset dir="${jc.export_files}">
            <filename name="**/*.exp"/>
         </fileset>
      </copy>
      
      <convert
         dir="${build.classes.dir}"
         Configfile="${src.dir}/clientpackage/config.opt"
         nobanner="true">
         <classpath refid="jc-classpath"/>   
                        <exportpath>
                            <pathelement path="E:/java_card_kit-2_2_1/api_export_files"/>
                        </exportpath>                   
                       
      </convert>
      <verifycap Verbose="true" CapFile="${build.classes.dir}/clientpackage/javacard/clientpackage.cap">
         <exportfiles file="${jc.export_files}/java/lang/javacard/lang.exp" />
         <exportfiles file="${jc.export_files}/javacard/framework/javacard/framework.exp" />
         <exportfiles file="${jc.export_files}/javacard/security/javacard/security.exp" />
         <exportfiles file="${jc.export_files}/javacardx/crypto/javacard/crypto.exp" />
         <exportfiles file="${build.classes.dir}/clientpackage/javacard/clientpackage.exp" />
         <classpath refid="jc-classpath"/>
      </verifycap>
      
   </target>
 
    <!-- Overriding target, which runs conversion and verification instead of creating JAR file -->
    <target name="jar" depends="init,compile,convert">
    </target>
 
    <!-- Target, which runs specific script via GPShell -->
    <target name="run-script" depends="init">
        <!-- Generate and execute build script -->
        <copy file="${gpshell.templatepath}\\${gpshell.script}" todir="build//scripts" overwrite="true">
            <filterchain>
                <expandproperties/>
            </filterchain>
        </copy>le
        <exec executable="${gpshell.cmd}">
            <arg value="build//scripts//${gpshell.script}"/>
        </exec>
    </target>
 
    <!-- Overriding target, which runs script during testing -->
    <target name="test" depends="jar,run-script">
    </target>
</project>


and my config.opt is

Code: Select all

-out EXP JCA CAP
-exportpath .
-applet  0x01:0x02:0x03:0x04:0x05:0x01 clientpackage.Client
clientpackage
0x01:0x02:0x03:0x04:0x05 2.3


would you help me to my problem?