2

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)
4

2 回答 2

2

如果您需要处理响应,您可以使用00A4010002020000命令 APDU(即与以前相同,但带有L​​e字段)(您可以使用您可能已经知道的命令获取它)。SELECTGET RESPONSE

或者,如果您不需要处理响应,则可以使用00A4010C020200命令 APDU(即与之前相同,但用 with表示不需要响应数据) 。P2=0x0CSELECT

根据您的评论,这种方法应该有效。

编辑>

这很奇怪,我只是在原生 MPCOS 卡上重现了您的情况(目前无法访问 MPCOS 小程序):

00A40000023F00 -> 851080013F0038000000C100C100000000679000
00A40100020200 -> 6F15840E315041592E5359532E4444463031A5038801019000
00A40200020201 -> 85104302020105000040C000C0000000006B9000

注意:此跟踪不显示GET RESPONSEAPDU 交换。

所以可能我帮不了你:(

于 2015-09-30T18:54:09.800 回答
1

我发现了问题:

问题是由于两次调用 SCardTransmit 函数造成的。实际上,第一次获取响应长度,第二次执行命令并获取响应。

此双重调用导致错误 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]);
    }
}

更正后的代码是这样的:

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]);
    }
}

我真的很感谢@guidot 的好提示和亲爱的@vlp 的帮助

于 2016-01-04T12:12:15.627 回答