问题
I wrote two java card applet named MasterApp and SlaveApp that are in two different packages named masterPack and slavePack :
Master AIDs :
- Package :
01 02 03 04 01
- Applet :
01 02 03 04 01 01
Slave AIDs :
- Package :
01 02 03 04 02
- Applet :
01 02 03 04 02 02
As you see below I want to call SharedMethod()
of the MasterApp in the Process()
method of my SlaveApp :
Master Program :
package masterPack;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISOException;
import javacard.framework.Shareable;
import javacard.framework.Util;
public class MasterApp extends Applet implements Shareable {
// goodNews = "Shared Method Called Successful"
byte[] goodNews = { (byte) 'S', (byte) 'h', (byte) 'a', (byte) 'r',
(byte) 'e', (byte) 'd', (byte) ' ', (byte) 'M', (byte) 'e',
(byte) 't', (byte) 'h', (byte) 'o', (byte) 'd', (byte) ' ',
(byte) 'C', (byte) 'a', (byte) 'l', (byte) 'l', (byte) 'e',
(byte) 'd', (byte) ' ', (byte) 'S', (byte) 'u', (byte) 'c',
(byte) 'c', (byte) 'e', (byte) 's', (byte) 's', (byte) 'f',
(byte) 'u', (byte) 'l' };
private MasterApp() {
}
public static void install(byte bArray[], short bOffset, byte bLength)
throws ISOException {
new MasterApp().register();
}
public void process(APDU arg0) throws ISOException {
if (selectingApplet()) {
return;
}
}
public void SharedMethod(APDU apdu) {
byte[] buffer = apdu.getBuffer();
Util.arrayCopyNonAtomic(goodNews, (short) 0, buffer, (short) 0,
(short) goodNews.length);
apdu.setOutgoingAndSend((short) 0, (short) goodNews.length);
}
}
Slave Program :
package slavePack;
import masterPack.MasterApp;
import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
public class SlaveApp extends Applet {
byte[] masterAppletAID={(byte)0x01,(byte)0x02,(byte)0x03,(byte)0x04,(byte)0x01,(byte)0x01};
AID aid = JCSystem.lookupAID(masterAppletAID, (short)0, (byte)masterAppletAID.length);
MasterApp MasterAppInstance = (MasterApp)JCSystem.getAppletShareableInterfaceObject(aid, (byte)0);
private SlaveApp() {
}
public static void install(byte bArray[], short bOffset, byte bLength)
throws ISOException {
new SlaveApp().register();
}
public void process(APDU apdu) throws ISOException {
if(selectingApplet()){
return;
}
MasterAppInstance.SharedMethod(apdu);
}
}
In the first step I convert the MasterApp to .cap file successfully and now I have its .exp file in the same directory of its .cap file.
As the second step I wanted to convert SlaveApp to .cap file, but I failed. I guessed that I need to the previous .exp file in the directory of SlaveApp for conversion, so I move it to the directory of SlaveApp.class and tried again, but nothing changed and I received the same below errors again :
Questions :
1- What's the origin of the error?
2- How can I restrict this sharedMethod only for one applet? I mean, in this situation all the other applets can call it, is there any way to limit this accessibility?
3- In cases that we don't have the .exp file of MasterApp, what we must do?
Note that I appreciate any other comment to make my program more efficient (For example the type of variables and their declaration's points).
Update :
Based on @Paul-Bastin answer, I added an interface named MyShareableInterface
to my the MasterApp package and this interface implements the Shareable
interface. In this Interface I declared the same SharedMethod
method that I used in MasterApp as interface. After that I implement this Interface using MasterApp applet. All the other are the same, but nothing changed.
MyShareableInterface.java :
package masterPack;
import javacard.framework.APDU;
import javacard.framework.Shareable;
public interface MyShareableInterface extends Shareable {
public void SharedMethod(APDU apdu);
}
MasterApp.java :
public class MasterApp extends Applet implements MyShareableInterface {
//Same Body
}
SlaveApp.java :
It is exactly the same as before.
Problem : Nothing changed. I had the same error.
Update 2:
APDU
object removed from input arguments of SharedMethod
in all three files. It's body replaced with ISOException.throwIt((short)0x9100)
also (In the MasterApp.java
). I have the same errors againg.
After the above changes I recreate the project from the beginning and now I am dealing with three .java
file in two packages as follow :
MyShareableInterface.java :
package masterPack;
import javacard.framework.Shareable;
public interface MyShareableInterface extends Shareable{
public void SharedMthod();
}
MasterApp.java :
package masterPack;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISOException;
public class MasterApp extends Applet implements MyShareableInterface {
private MasterApp() {
}
public static void install(byte bArray[], short bOffset, byte bLength)
throws ISOException {
new MasterApp().register();
}
public void process(APDU arg0) throws ISOException {
// TODO Auto-generated method stub
}
public void SharedMthod() {
// TODO Auto-generated method stub
ISOException.throwIt((short)0x9100);
}
}
SlaveApp.java :
package slavePack;
import masterPack.MasterApp;
import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
public class SlaveApp extends Applet {
byte[] masterAppletAID={(byte)0x01,(byte)0x02,(byte)0x03,(byte)0x04,(byte)0x01,(byte)0x01};
AID aid = JCSystem.lookupAID(masterAppletAID, (short)0, (byte)masterAppletAID.length);
MasterApp MasterAppInstance = (MasterApp)JCSystem.getAppletShareableInterfaceObject(aid, (byte)0);
private SlaveApp() {
}
public static void install(byte bArray[], short bOffset, byte bLength)
throws ISOException {
new SlaveApp().register();
}
public void process(APDU apdu) throws ISOException {
if(selectingApplet()){
return;
}
MasterAppInstance.SharedMthod();
}
}
There is no error in the code. I can even convert the MasterApp.java
to .cap file successfully. There problem is failure in SlaveApp.java
conversion procedure. I failed in this step.
Note that : AID-s are the same as first version of question. Two first file are in masterPack
and the latter is in slavePack
.
回答1:
- You must create an interface that extends Shareable.
- Then your applet must implement that interface
- You get this interface in your slave applet yb calling
JCSystem.getAppletShareableInterfaceObject
- You call the public method of your interface over this object
- Do not share the apdu object!
I highly recommend Zhiqun Chen - Java Card Technology Book
来源:https://stackoverflow.com/questions/29339911/why-applet-conversion-failed-in-case-of-using-shareable-interface-how-to-use-e