【发布时间】:2019-01-06 14:04:54
【问题描述】:
我尝试使用可共享接口的 Eclipse 3.7 SDK 在 java card 2.2.2 中创建一个简单的客户端和服务器小程序。当方法JCSystem.getAppletShareableInterfaceObject 被调用时,它会抛出一个异常,因此返回SW 设置为6F00。
这是客户端应用代码 (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);
}
}
这是服务器小程序代码 (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;
}
}
以及可共享的接口文件(Test_ServerInf.java):
package server;
import javacard.framework.Shareable;
public interface Test_ServerInf extends Shareable {
public short method1();
public short method2();
}
【问题讨论】:
-
那么
catch捕获的异常是Ins2_Handler吗?如果是这样,您是否尝试打印出原因?请注意,我们在线“调试”特别困难,我们甚至不知道您如何以及是否已实例化服务器等。 -
@MaartenBodewes 是的,这是一个例外,但它是
RuntimeException而不是CardRuntimeException,因此它不包含除equals()之外的任何方法。我已经实例化了服务器和客户端。JCSystem.getAppletShareableInterfaceObject抛出的唯一异常是SecurityException。 -
您是否尝试移除演员表?鉴于该位置的官方 JC API,我只能看到另一个可能的异常。
-
@MaartenBodewes 是的,它再次抛出该异常,但是删除强制转换会导致无法使用接口的内部方法。
-
烦恼。可以在
Exception之前分别捕获SecurityException吗?它也没有原因码,所以我们至少知道抛出了哪个异常(是的,我知道,它是唯一应该被抛出的异常,但是......)。跨度>
标签: java applet smartcard javacard