欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 【Java Card】Applet 使用Shareable进行数据分享以及部分问题处理

【Java Card】Applet 使用Shareable进行数据分享以及部分问题处理

2025/2/24 12:12:57 来源:https://blog.csdn.net/TangYuanTuoHai/article/details/145712829  浏览:    关键词:【Java Card】Applet 使用Shareable进行数据分享以及部分问题处理

文章目录

  • 前言
  • 一、定义接口
  • 二、server端实现
  • 三、client端实现
  • 四、遇到的问题


前言

在进行开发时,可能会将业务放到不同的applet中,这时常常会需要进行数据的分享。
比如在一个applet中存储了密钥,而在另一个业务applet中需要进行签名时,需要将数据传给第一个applet进行签名后,再返回签名后的数据。
这里介绍Java Card中的Shareable的数据分享的方式。

一、定义接口

import javacard.framework.Shareable;public interface DataShareable extends Shareable {public byte getDataInfo(byte[] buffer, short offset, short length);
}

需要继承Java Card的Shareable 类,定义好我们需要的接口,这里定义了getDataInfo方法,用于将数据写到buffer中以提供给client。

二、server端实现

public class DataInfoApplet extends Applet implements DataShareable{byte[] DataInfo;protected DataInfoApplet () {DataInfo = new byte[16];register();}public static void install(byte[] bArray, short bOffset, byte bLength) {new DataInfoApplet ();}@Overridepublic Shareable getShareableInterfaceObject(AID clientAID, byte parameter) {if (!clientAID.equals(sAIDClientApplet, (short) 0, (byte) sAIDClientApplet.length)) {return null;}return this;}@Overridepublic void process(APDU apdu) {}@Overridepublic byte getDataInfo(byte[] out, short offset, short length) {try {Util.arrayCopy(DataInfo, (short) 0, out, offset, length);return OK;} catch (ArrayIndexOutOfBoundsException e) {return NOT_OK;}}
}
  • server端需要实现DataShareable接口。
  • 需要实现getShareableInterfaceObject,该方法会在client端调用JCSystem.getShareableInterfaceObject时执行。在这里面可以进行clientAID的判断,以使得仅有特定的applet才能够调用shareable接口。

三、client端实现

注意:client和server是在不同的package中的。

public class ClientApplet extends Applet {DataShareable sio = null;protected ClientApplet () {register();}public static void install(byte[] bArray, short bOffset, byte bLength) {new ClientApplet ();}private boolean getSIO() {if (this.sio == null) {this.sio = (DataShareable)JCSystem.getAppletShareableInterfaceObject(JCSystem.lookupAID(DATAINFO_APPLET_AID_BYTES,(short)0, (byte)(DATAINFO_APPLET_AID_BYTES.length)),(byte)0);}if(this.sio != null) {return true;} else {return false;}}@Overridepublic void process(APDU apdu) {byte[] buffer = apdu.getBuffer();sio.getDataInfo(buffer , (short) 0, (short) 16);apdu.setOutgoing();apdu.setOutgoingLength((short) 16);apdu.sendBytesLong(buffer , (short) 0, (short) 16);return;}@Overridepublic boolean select() {return getSIO();}@Overridepublic void deselect() {sio = null;}
}
  • getSIO中通过JCSystem.getAppletShareableInterfaceObject来获得接口对象sio。
  • 使用sio来调用接口方法,并传入buffer参数。

四、遇到的问题

1、接口中创建对象失败

@Overridepublic byte getDataInfo(byte[] out, short offset, short length) {try {byte[] temp = JCSystem.makeTransientByteArray((short) 4, JCSystem.CLEAR_ON_DESELECT);Util.arrayCopy(DataInfo, (short) 0, out, offset, length);return OK;} catch (ArrayIndexOutOfBoundsException e) {return NOT_OK;}}

在server实现的接口方法中,添加了byte[] temp = JCSystem.makeTransientByteArray((short) 4, JCSystem.CLEAR_ON_DESELECT);则调用该接口时会报错。

分析:

JCCRE中规定,对于CLEAR_ON_DESELECT类型的瞬态数据,只有当前活动的context是当前选择的applet的context时,才能够创建和使用。

这个案例中,对于两个不同package中的applet进行接口调用时,会进行context的切换,所以调用到getDataInfo接口时,当前活动的context已经切换为了server的context。但是接口是在client处理apdu请求时调用的,此时被选择的applet仍然是client。所以导致前面的条件无法满足,所以出现错误。
在这里插入图片描述
解决:
使用CLEAR_ON_RESET。

byte[] temp = JCSystem.makeTransientByteArray((short) 4, JCSystem.CLEAR_ON_RESET);

CLEAR_ON_RESET类型要求,当前活动的context是对象所有者的context,因此可以在这样的情况下满足条件

2、数据无法通过接口传输

 @Overridepublic void process(APDU apdu) {//byte[] buffer = apdu.getBuffer();byte[] data = JCSystem.makeTransientByteArray((short) 16, JCSystem.CLEAR_ON_RESET);sio.getDataInfo(data , (short) 0, (short) 16);apdu.setOutgoing();apdu.setOutgoingLength((short) 16);apdu.sendBytesLong(data , (short) 0, (short) 16);return;}

如果client中这样写,即使用makeTransientByteArray来创建瞬态数据data并作为参数传递,则调用时会出现数据的访问失败

分析:
在这里插入图片描述
从日志可以看出,是由于临时数据无法在不同的context之间进行访问。临时数据是收到防火墙保护的,这个data数据是属于client的context的,而调用接口后,活动的context切换为了server的context,那么在server中就无法访问这个data,进而无法向其中写入数据。

解决:
使用byte[] buffer = apdu.getBuffer();来进行数据的发送和接收。

 @Overridepublic void process(APDU apdu) {byte[] buffer = apdu.getBuffer();sio.getDataInfo(data , (short) 0, (short) 16);apdu.setOutgoing();apdu.setOutgoingLength((short) 16);apdu.sendBytesLong(data , (short) 0, (short) 16);return;}

apdu.getBuffer() 返回的是 APDU缓冲区,它是一个特殊的 全局缓冲区,在 Java Card 上被认为是跨防火墙允许的共享对象。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词