/**
 * 
 */
package attack.firewall.clientfortypeconfusion;

import javacard.framework.Applet;
import javacard.framework.ISOException;
import javacard.framework.ISO7816;
import javacard.framework.APDU;
import javacard.framework.JCSystem;
import javacard.framework.AID;

import attack.firewall.fortypeconfusion.A;
import attack.firewall.fortypeconfusion.SIForTypeConfusion;


/**
 * @author sauveron
 *
 */
public class ClientForTypeConfusion extends Applet {
	
	private final static byte[] serverAID = {(byte)0x00,(byte)0x22,(byte)0x00,(byte)0x22,(byte)0x00,(byte)0x01,(byte)0x01};
	
	public static void install(byte[] bArray, short bOffset, byte bLength) {
		// GP-compliant JavaCard applet registration
		new ClientForTypeConfusion().register(bArray, (short) (bOffset + 1),
				bArray[bOffset]);
	}

	public void process(APDU apdu) {
		// Good practice: Return 9000 on SELECT
		if (selectingApplet()) {
			return;
		}
 
		byte[] buf = apdu.getBuffer();
		switch (buf[ISO7816.OFFSET_INS]) {
		case (byte) 0x00:
			AID sAID = JCSystem.lookupAID(serverAID, (short) 0, (byte)serverAID.length);
			SIForTypeConfusion sio = (SIForTypeConfusion) JCSystem.getAppletShareableInterfaceObject(sAID, (byte)0);
			if (sio == null)
				ISOException.throwIt((short) 0x9191);
//			byte[] ItsbArray = sio.giveArray();
//			sio.accessArray(ItsbArray);
			A a = sio.giveObj();
			sio.accessObj(a);
			break;
		default:
			// good practice: If you don't know the INStruction, say so:
			ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
		}
	}
}