SEChannel

Tags: 2.5 Privileged

Description

A SEChannel interface represents an [ISO7816-4] channel opened to a Secure Element. It can be either a supplementary logical channel or the basic logical channel. It can be used to send commands to and receive responses from an applet in secure element.

Properties

  • SEChannel.session 'Session' object this channel is bound to

  • SEChannel.openResponse Response to openBasicChannel / openLogicalChanneloperation

  • SEChannel.isClosed Status of channel

  • SEChannel.type Type of channel

Methods

  • SEChannel.transmit() Available since v2.5 Transmits the APDU command to the secure element.

  • SEChannel.close() Available since v2.5 Closes the active channel.

Example

window.AID = {
  CRS: "A00000015143525300",
  PPSE: "325041592E5359532E4444463031",
  ISD:"A000000151000000"
};

window.APDU = {
  ISD: {
    getCplc: { cla: 0x80, ins: 0xCA, p1: 0x9F, p2: 0x7F, le: 0x00},
  },
};

function hexString2byte(str) {
  var a = [];
  for(var i = 0, len = str.length; i < len; i+=2) {
    a.push(parseInt(str.substr(i, 2), 16));
  }
  return new Uint8Array(a);
}

function byte2hexString(uint8arr) {
  if (!uint8arr) {
    return '';
  }

  var hexStr = '';
  for (var i = 0; i < uint8arr.length; i++) {
    var hex = (uint8arr[i] & 0xff).toString(16);
    hex = (hex.length === 1) ? '0' + hex : hex;
    hexStr += hex;
  }
  return hexStr.toUpperCase();
}

function checkResponse(response, expectedSw1, expectedSw2, expectedData) {
  var sw1Check = response.sw1 === expectedSw1;
  var sw2Check = response.sw2 === expectedSw2;
  var dataCheck = (expectedData) ? byte2hexString(response.data) === expectedData : true;
  if (sw1Check && sw2Check && dataCheck) {
    return true;
  } else {
    return false;
  }
}

window.navigator.seManager.getSEReaders()
.then((readers) => {
  window.reader = readers[0];
  return readers[0].openSession();
})
.then((session) => {
  console.log("Open a session successfully");
  window.testSession = session;
  return session.getAtr();
})
.then((result) => {
  console.log("Get answer to reset successfully");
  return window.testSession.openBasicChannel(hexString2byte(window.AID.ISD));
})
.then((channel) => {
  console.log("Open a basic channel successfully");
  window.testChannel = channel;
  return channel.transmit(window.APDU.ISD.getCplc);
})
.then((response) => {
  if (checkResponse(response, 0x90, 0x00) == true) {
    console.log("Transmit get cplc command successfully");
  } else {
    console.log("Failed to get cplc response");
  }
  return window.reader.closeAll();
})
.then(() => {
  console.log("Close all channels successfully");
})
.catch((err) => {
  console.log("Failed");
  window.reader.closeAll();
});