简体   繁体   English

Java Card客户端 - 服务器可共享接口返回6F00

[英]Java Card client-server Shareable interface returns 6F00

I've tried to create a simple Client and Server applets in java card 2.2.2 using Eclipse 3.7 SDK using the Shareable Interfaces. 我尝试使用可共享接口使用Eclipse 3.7 SDK在Java卡2.2.2中创建一个简单的客户端和服务器小程序。 When the method JCSystem.getAppletShareableInterfaceObject is called it throws an exception and so the return SW sets to 6F00. 调用方法JCSystem.getAppletShareableInterfaceObject时,它会抛出异常,因此返回SW设置为6F00。

This is the Client app code ( Test_Client.java ): 这是客户端应用程序代码( Test_Client.java ):

    package client;

import server.Test_ServerInf;
import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;

public class Test_Client extends Applet {

    protected static final byte INS1 = (byte)0xE2;
    protected static final byte INS2 = (byte)0x21;

    byte[] ServerAIDbyte={(byte)0x20,(byte)0x21,(byte)0x22,(byte)0x23,(byte)0x24,(byte)0x25,(byte)0x26,(byte)0x27,(byte)0x01};
    AID ServerAID;

    private Test_Client() {
    }

    public static void install(byte bArray[], short bOffset, byte bLength)
            throws ISOException {
        new Test_Client().register();
    }

    public void process(APDU apdu) throws ISOException {
        // TODO Auto-generated method stub
        byte[] apduBuffer = apdu.getBuffer();

        byte Ins=apduBuffer[ISO7816.OFFSET_INS];
        short byteread = apdu.setIncomingAndReceive();

        if (selectingApplet())
            return;

        switch (Ins){
        case INS1:
            Ins1_Handler(apdu);
            return;
        case INS2:
            Ins2_Handler(apdu,apduBuffer);
            return;
        default:
            ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
        }
    }
    private void Ins1_Handler(APDU apdu){
        Test_ServerInf sio = null;
        ServerAID=JCSystem.lookupAID(ServerAIDbyte,(short) 0,(byte) ServerAIDbyte.length);
        if(ServerAID==null)
            ISOException.throwIt( (short) 0x6A82);
        ////server request
        try{
        sio=(Test_ServerInf)(JCSystem.getAppletShareableInterfaceObject(ServerAID, (byte) 0));
        }
        catch(SecurityException e)
       {
           ISOException.throwIt((short)0x12);
       }
       catch(Exception e)
       {
           ISOException.throwIt((short)0x10);
       }
        if(sio==null)
            ISOException.throwIt((short)0x6A00);

    }

    private void Ins2_Handler(APDU apdu,byte[] apduBuffer){
            Test_ServerInf sio = null;
           ////connect to server  
          ServerAID=JCSystem.lookupAID(ServerAIDbyte,(short) 0,(byte) ServerAIDbyte.length);
           if(ServerAID==null)
                ISOException.throwIt( (short) 0x6A82);
           ////server request
           try{
               sio=(Test_ServerInf)(JCSystem.getAppletShareableInterfaceObject(ServerAID, (byte) 0));
           }
           catch(SecurityException e)
           {
               ISOException.throwIt((short)0x12);
           }
           catch(Exception e)
           {
               ISOException.throwIt((short)0x10);
           }
           if(sio==null)
                ISOException.throwIt((short)0x6A00); 
    }


}

And this is Server applet code ( Test_Server.java ): 这是服务器applet代码( Test_Server.java ):

  package server;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISOException;
import server.Test_ServerInf;
import javacard.framework.Shareable;
import javacard.framework.AID;

public class Test_Server extends Applet implements Test_ServerInf{


    private Test_Server() {
    }

    public static void install(byte bArray[], short bOffset, byte bLength)
            throws ISOException {
        new Test_Server().register();
    }

    public void process(APDU apdu) throws ISOException {
        // TODO Auto-generated method stub

    }

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

    public short method1(){
        return (short)0x01;
    }
    public short method2(){
        return (short)0x02;
    }

}

and the shareable interface file ( Test_ServerInf.java ): 和可共享的接口文件( Test_ServerInf.java ):

package server;

import javacard.framework.Shareable;

public interface Test_ServerInf extends Shareable {

    public short method1();
    public short method2();

}

You are trying to store a reference to the shareable interface object in a member field of your client applet class: 您正尝试在客户端applet类的成员字段中存储对可共享接口对象的引用:

sio = (Test_ServerInf)(JCSystem.getAppletShareableInterfaceObject(ServerAID, (byte) 0));

where sio is defined as private member of the applet class instance: 其中sio被定义为applet类实例的私有成员:

public class Test_Client extends Applet {
    private Test_ServerInf sio;

This will result in a SecurityException since the shareable interface object is owned by the server applet (ie by a different context). 这将导致SecurityException因为可共享的接口对象由服务器applet拥有(即通过不同的上下文)。 You are not allowed to store objects owned by other contexts in an instance field. 您不能在实例字段中存储其他上下文拥有的对象。

See Accessing Class Instance Object Fields (Section 6.2.8.3) , in Runtime Environment Specification, Java Card Platform, Version 2.2.2 : 请参阅运行时环境规范,Java Card Platform,版本2.2.2中的 访问类实例对象字段(第6.2.8.3节)

Bytecodes: getfield , putfield 字典: getfieldputfield

  • [...] if the object is owned by an applet in the currently active context, access is allowed. [...]如果对象由当前活动上下文中的applet拥有,则允许访问。
  • Otherwise, access is denied. 否则,访问被拒绝。

I found the source of this error. 我找到了这个错误的来源。 I used to load and install applets using an internally developed application instead of GPShell. 我曾经使用内部开发的应用程序而不是GPShell加载和安装applet。 When I tried to load and install applets using GPShell the problem solved and everything is ok. 当我尝试使用GPShell加载和安装applet时问题解决了,一切正常。 I don't know how that application compromises the loaded package but it works fine right know (After 2 weeks of debugging). 我不知道该应用程序如何破坏加载的软件包,但它工作得很好,知道(经过2周的调试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM