I have written a program to communicate with a smart card (Gemalto Company MPCOS applet).
I could successfully connect to card and transmit commands and fetch data.
However I have a problem:
When I used 00 A4 01 00 02 02 00
command to select DF(Dedicated File),
It returned error 69 81
(file indicator is incorrect).
This is so weird because after this command I used another command to fetch sub-file of this DF and it returned success 61 12
.
command1(Select MPCOS Applet): 00 A4 04 00 10 A0 00 00 00 18 30 03 01 00 00 00 00 00 00 00 00
-> response: [97,18] (in decimal) or 6112 (in hex)
command2: 00 C0 00 00 12
-> response: [105,133,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] (in decimal) or
69 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (in hex)
command3(Select Root): 00 A4 00 00 02 3f 00
-> response: [97,18] (in decimal) or 6112 (in hex)
command4: 00 C0 00 00 12
-> response: [105,133,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] (in decimal) or
69 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (in hex)
command5(Select DF): 00 A4 01 00 02 02 00
-> response: [105,129] (in decimal) or 6981 (in hex)
command6(Select EF): 00 A4 02 00 02 02 01
-> response: [97,18] (in decimal) or 6112 (in hex)
command7: 00 C0 00 00 12
-> response: [105,133,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] (in decimal) or
69 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (in hex)
You can use the 00A4010002020000
command APDU (i.e. the same as before, but with a Le field) if you need to process the SELECT
response (you can get it using the GET RESPONSE
command as you probably already know).
Or you can use the 00A4010C020200
command APDU (i.e. the same as before, but with P2=0x0C
to indicate no response data wanted) if you do not need to process the SELECT
response.
Based on your comments this approach should work.
EDIT>
This is strange, I just reproduced your situation on a native MPCOS card (do not have access to the MPCOS applet right now):
00A40000023F00 -> 851080013F0038000000C100C100000000679000
00A40100020200 -> 6F15840E315041592E5359532E4444463031A5038801019000
00A40200020201 -> 85104302020105000040C000C0000000006B9000
Note: This trace does not show the GET RESPONSE
APDU exchanges.
So probably I can't help you :(
I have found the problem:
The problem was due to invoking the SCardTransmit function for two times. Indeed, one time to get response length and the second time for execute the command and getting response.
This dual invoke lead to error 6981:
function SCardTransmitFunc(aCallbackName, myCommand){
var _SCARD_IO_REQUEST = new CONST.SCARD_IO_REQUEST;
_SCARD_IO_REQUEST.dwProtocol = AProtocol;
_SCARD_IO_REQUEST.cbPciLength = CONST.SCARD_IO_REQUEST.size;
var myArrayCommand = hex2Dec(myCommand);
var command = TYPES.LPBYTE.targetType.array(myArrayCommand.length)(myArrayCommand);
var commandLength = command.length;
var responseLength = TYPES.DWORD();
var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, null, responseLength.address());
var response = TYPES.LPBYTE.targetType.array(parseInt(responseLength.value))();
var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, response, responseLength.address());
var myResponse = "";//new Array();
for(i = response.length - 2; i < response.length ; i++)
{
myResponse += dec2Hex(response[i]);
}
}
and the corrected code is this:
function SCardTransmitFunc(aCallbackName, myCommand){
var _SCARD_IO_REQUEST = new CONST.SCARD_IO_REQUEST;
_SCARD_IO_REQUEST.dwProtocol = AProtocol;
_SCARD_IO_REQUEST.cbPciLength = CONST.SCARD_IO_REQUEST.size;
var myArrayCommand = hex2Dec(myCommand);
var command = TYPES.LPBYTE.targetType.array(myArrayCommand.length)(myArrayCommand);
var commandLength = command.length;
var responseLength = TYPES.DWORD(1024);
var response = TYPES.BYTE.array(parseInt(1024))();
var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, response, responseLength.address());
var myResponse = "";//new Array();
var myLength = parseInt(responseLength.value);
for(i = myLength - 2; i < myLength ; i++)
{
myResponse += dec2Hex(response[i]);
}
}
I really thanks @guidot for his good hint and dear @vlp for his helps