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

后端 未结 2 952
余生分开走
余生分开走 2020-12-18 13:31

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.

相关标签:
2条回答
  • 2020-12-18 13:53

    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

    0 讨论(0)
  • 2020-12-18 13:54

    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 :(

    0 讨论(0)
提交回复
热议问题