阿里云网站备案多少天,wordpress页面相册,抖音推广方式有哪些,淘宝客推广一天80单文章目录 前言一、定义接口二、server端实现三、client端实现四、遇到的问题 前言
在进行开发时#xff0c;可能会将业务放到不同的applet中#xff0c;这时常常会需要进行数据的分享。 比如在一个applet中存储了密钥#xff0c;而在另一个业务applet中需要进行签名时… 文章目录 前言一、定义接口二、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 上被认为是跨防火墙允许的共享对象。