0x6f00错误转换Javacard可共享接口

时间:2014-02-28 17:39:37

标签: smartcard javacard

我尝试将SIO(可共享接口对象)用于两个不同的包,以便将来更新我的applet的业务逻辑。 我正在使用eclipse,我启动了两个不同的JavaCard应用程序,ClientSIOApplet和ServerSIOApplet。 ClientSIOApplet中有一个名为appClient的包,ServerSIOApplet中有一个名为appServer的包。 此外,ClientApplet.java和ServerAppBankInterface.java是appClient和中的类 ServerAppBankInterface.java和ServerApplet.java位于appServer中。您可以在下面看到源代码:

appClient中的ClientApplet.java

package appClient;

import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Shareable;
import javacard.framework.Util;

public class ClientApplet extends Applet {

    Shareable  sio;

    byte[] serverAID = {(byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x01};

    public ClientApplet() {
        // TODO Auto-generated constructor stub

    }

    public static void install(byte[] bArray, short bOffset, byte bLength) {
        // GP-compliant JavaCard applet registration
        new ClientApplet().register(bArray, (short) (bOffset + 1),
                bArray[bOffset]);       
    }

    public void process(APDU apdu) {
        // Good practice: Return 9000 on SELECT
        if (selectingApplet()) {
            return;
        }
        byte[] buf = apdu.getBuffer();      
        byte cla = buf[ISO7816.OFFSET_CLA];

        if (( cla != ISO7816.CLA_ISO7816) && (cla != (byte) 0x10))
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

        switch (buf[ISO7816.OFFSET_INS]) {
        case (byte) 0x00:


             AID svrAid = JCSystem.lookupAID(serverAID, 
                                     (short)0, 
                                     (byte)serverAID.length);

            if(svrAid == null) {
                // Cannot find the serverAID AID
                ISOException.throwIt((short)0x0010);
            }

            /*sio = JCSystem.getAppletShareableInterfaceObject(svrAid, (byte)0);
            if (sio == null) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
            if (! (sio instanceof SharedArray))
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            SharedArray theSharedArray = (SharedArray) sio;
            final byte[] sa = theSharedArray.getSharedArray();*/

            //ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);

            sio = JCSystem.getAppletShareableInterfaceObject(svrAid, (byte)0);


            if(sio == null){
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
            }


            /*if (! (sio instanceof ServerAppBankInterface))
                ISOException.throwIt(ISO7816.SW_FILE_INVALID);*/

            try{
                ServerAppBankInterface bankInterface = (ServerAppBankInterface) sio;
            }catch(Exception ex){
                ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
            }


                        break;
        //default:
            // good practice: If you don't know the INStruction, say so:
            //ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

}

appClient中的ServerAppBankInterface.java

package appClient;

import javacard.framework.Shareable;

public interface ServerAppBankInterface extends Shareable{
    //public void saveMoneyInBank(short amount);
    public short getSavedMoneyInBank();
}
appServer中的ServerApplet.java

package appServer;

import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Shareable;


public class ServerApplet extends Applet implements ServerAppBankInterface{



    public ServerApplet(byte[] bArray, short bOffset, byte bLength){

        register(bArray, (short) (bOffset + 1), bArray[bOffset]);

        /*final byte[] sa = new byte[] { 'm' };
        sharedArray = new SharedArrayImpl(sa);*/
    }

    public Shareable getShareableInterfaceObject(AID clientID, byte parameter){

        byte[] tempAID = {(byte)0x05, (byte)0x04, (byte)0x03, (byte)0x02, (byte)0x01, (byte)0x01};

        if((clientID.equals(tempAID,
                (short)0,
                (byte)tempAID.length)) == false)
            return  null;
        else
            return this;
            //return sharedArray;
            //return serverAppBankObject;
            //return (ServerAppBankInterface) this;
            //return (Shareable) this;

    }

    public boolean select()
    {
         return true;
    }

    public static void install(byte[] bArray, short bOffset, byte bLength) {
        // GP-compliant JavaCard applet registration
        new ServerApplet(bArray, bOffset, bLength);
    }

    public void process(APDU apdu) {
        // Good practice: Return 9000 on SELECT

        byte[] buf = apdu.getBuffer();
        switch (buf[ISO7816.OFFSET_INS]) {
        case (byte) 0x00:
            break;
        default:
            // good practice: If you don't know the INStruction, say so:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

    public short getSavedMoneyInBank() {
        // TODO Auto-generated method stub
        return 0;
    }



}

appServer中的ServerAppBankInterface.java

package appServer;

import javacard.framework.Shareable;

public interface ServerAppBankInterface extends Shareable{
    //public void saveMoneyInBank(short amount);
    public short getSavedMoneyInBank();
}

问题是:

我有问题在线投射界面:

ServerAppBankInterface bankInterface = (ServerAppBankInterface) sio;
ClientApplet.java中的

如果我在该行中删除了Try-Catch,则会收到0x6F00错误,

2 个答案:

答案 0 :(得分:3)

服务器小程序提供类型

的可共享界面
appServer.ServerApplet <- appServer.ServerAppBankInterface <- javacard.framework.Shareable

但是当您在客户端小程序中收到此可共享的接口对象时,您正试图将其转换为

appClient.ServerAppBankInterface <- javacard.framework.Shareable

虽然接口appServer.ServerAppBankInterfaceappClient.ServerAppBankInterface具有相似的名称并且公开具有相同名称的方法,但这两个接口既不相同也不相互继承。因此,你不能在他们之间施放。

因此,您尝试将收到的可共享对象实例强制转换为不相关的类型。因此,演员表失败并且(未处理的)例外被提出。

要解决此问题,您需要将客户端小程序中收到的可共享对象强制转换为appServer.ServerAppBankInterface

答案 1 :(得分:0)

这是更新的代码 我在JCIDE上运行正常

打包appServer

  1. ServerAppel.java

    package appServer; 导入javacard.framework.AID;

    导入javacard.framework.APDU;

    导入javacard.framework.Applet;

    导入javacard.framework.ISO7816;

    import javacard.framework.ISOException;

    导入javacard.framework.Shareable;

    公共类ServerApplet扩展Applet实现ServerAppBankInterface {

    public ServerApplet(byte[] bArray, short bOffset, byte bLength){
    
        register(bArray, (short) (bOffset + 1), bArray[bOffset]);
    
    }
    
    public Shareable getShareableInterfaceObject(AID clientID, byte parameter){
    
        byte[] tempAID = {(byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01,(byte)0xAA};
    
        if((clientID.equals(tempAID,
                (short)0,
                (byte)tempAID.length)) == false)
            return  null;
        else
            return this;
    }
    
    public boolean select()
    {
         return true;
    }
    
    public static void install(byte[] bArray, short bOffset, byte bLength) {
        // GP-compliant JavaCard applet registration
        new ServerApplet(bArray, bOffset, bLength);
    }
    
    public void process(APDU apdu) {
        // Good practice: Return 9000 on SELECT
    
        byte[] buf = apdu.getBuffer();
        switch (buf[ISO7816.OFFSET_INS]) {
        case (byte) 0x00:
            break;
        default:
            // good practice: If you don't know the INStruction, say so:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }
    
    public short getSavedMoneyInBank() {
        // TODO Auto-generated method stub
        return 0;
    }
    

    }

  2. ServerAppBankInterface.java

    打包appServer;

    导入javacard.framework.Shareable;

    公共接口ServerAppBankInterface扩展了Shareable {     // public void saveMoneyInBank(short amount);     public short getSavedMoneyInBank(); }

打包appClient

1。客户端小程序

package appClient ;

import appServer.*;
import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Shareable;
import javacard.framework.Util;

public class ClientApplet extends Applet 
{

    ServerAppBankInterface  sio;

    byte[] serverAID = {(byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02,(byte)0xBB};

    public ClientApplet() {
        // TODO Auto-generated constructor stub

    }

    public static void install(byte[] bArray, short bOffset, byte bLength) {
        // GP-compliant JavaCard applet registration
        new ClientApplet().register(bArray, (short) (bOffset + 1),
                bArray[bOffset]);       
    }

    public void process(APDU apdu) {
        // Good practice: Return 9000 on SELECT
        if (selectingApplet()) {
            return;
        }
        byte[] buf = apdu.getBuffer();      
        byte cla = buf[ISO7816.OFFSET_CLA];

        if (( cla != ISO7816.CLA_ISO7816) && (cla != (byte) 0x10))
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

        switch (buf[ISO7816.OFFSET_INS]) {
        case (byte) 0x00:


             AID svrAid = JCSystem.lookupAID(serverAID, 
                                     (short)0, 
                                     (byte)serverAID.length);

            if(svrAid == null) {
                // Cannot find the serverAID AID
                ISOException.throwIt((short)0x0010);
            }


            sio = (ServerAppBankInterface)JCSystem.getAppletShareableInterfaceObject(svrAid, (byte)0);


            if(sio == null){
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
            }


            ServerAppBankInterface bankInterface = (ServerAppBankInterface) sio;
            bankInterface.getSavedMoneyInBank();

            break;
        default:
            // good practice: If you don't know the INStruction, say so:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }


}