simplecard.js
Summary
A simple card simulation
load("filesystem.js");
load("commandinterpreter.js");
load("securechannel.js");
function SimpleCardSimulator() {
var aid = new ByteString("A0000000010101", HEX);
var efdir_example = new ASN1(0x61,
new ASN1(0x4F, aid),
new ASN1(0x50, new ByteString("Example Application", ASCII))
);
var efdir = [ efdir_example.getBytes() ];
this.mf = new DF(FCP.newDF("3F00", null),
new LinearEF(FCP.newLinearEF("2F00", 30, FCP.LINEARVARIABLE, 20, 10), efdir),
new TransparentEF(FCP.newTransparentEF("2F02", 1, 100), new ByteString("5A0A00010203040506070809", HEX)),
new TransparentEF(FCP.newTransparentEF("EF01", 2, 100), new ByteString("4041424344", HEX)),
new DF(FCP.newDF("DF01", this.aid),
new TransparentEF(FCP.newTransparentEF("EF01", 0, 100)),
new TransparentEF(FCP.newTransparentEF("EF02", 0, 100))
)
);
print(this.mf.dump(""));
this.initialize();
}
SimpleCardSimulator.prototype.initialize = function() {
this.fileSelector = new FileSelector(this.mf);
this.commandInterpreter = new CommandInterpreter(this.fileSelector);
var sm = new SecureChannel(new Crypto());
sm.setSendSequenceCounterPolicy(IsoSecureChannel.SSC_SYNC_ENC_POLICY);
var k = new Key();
k.setComponent(Key.AES, new ByteString("7CA110454A1A6E570131D9619DC1376E4A1A6E570131D961", HEX));
sm.setMacKey(k);
var k = new Key();
k.setComponent(Key.AES, new ByteString("0131D9619DC1376E7CA110454A1A6E579DC1376E7CA11045", HEX));
sm.setEncKey(k);
sm.setMACSendSequenceCounter(new ByteString("00000000000000000000000000000000", HEX));
this.commandInterpreter.setSecureChannel(sm);
}
SimpleCardSimulator.prototype.processAPDU = function(capdu) {
print("Command APDU : " + capdu);
var apdu;
try {
apdu = new APDU(capdu);
}
catch(e) {
GPSystem.trace(e);
var sw = APDU.SW_GENERALERROR;
if (e instanceof GPError) {
sw = e.reason;
}
var bb = new ByteBuffer();
bb.append(sw >> 8);
bb.append(sw & 0xFF);
return bb.toByteString();
}
this.commandInterpreter.processAPDU(apdu);
var rapdu = apdu.getResponseAPDU();
print("Response APDU: " + rapdu);
return rapdu;
}
SimpleCardSimulator.prototype.reset = function(type) {
print("Reset type: " + type);
this.initialize();
var atr = new ByteString("3B600000", HEX);
return atr;
}
SimpleCardSimulator.newInstance = function() {
var sim = new SimpleCardSimulator();
if (typeof(CARDSIM) == "undefined") {
var adapter = new CardSimulationAdapter("JCOPSimulation", "8050");
adapter.setSimulationObject(sim);
adapter.start();
CARDSIM = adapter;
print("Simulation running...");
} else {
CARDSIM.setSimulationObject(sim);
print("Simulation replaced...");
}
}
SimpleCardSimulator.newInstance();
Documentation generated by
JSDoc on Tue Sep 3 22:29:41 2013