smartcardapdusmartcard-readercardreader

Selecting DF (Dedicated File) in Smart Card, Return Error 6981


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)

Solution

  • 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