使用 C 函数搜索标签 (TLV)

searching Tags (TLV) with C function

我正在处理 Mastercard Paypass 交易,我发送了一个 READ RECORD 命令并得到了结果:

70 81 AB 57 11 54 13 33 00 89 60 10 83 D2 51 22 
20 01 23 40 91 72 5A 08 54 13 33 00 89 60 10 83 
5F 24 03 25 12 31 5F 25 03 04 01 01 5F 28 02 00 
56 5F 34 01 01 8C 21 9F 02 06 9F 03 06 9F 1A 02 
95 05 5F 2A 02 9A 03 9C 01 9F 37 04 9F 35 01 9F 
45 02 9F 4C 08 9F 34 03 8D 0C 91 0A 8A 02 95 05 
9F 37 04 9F 4C 08 8E 0E 00 00 00 00 00 00 00 00 
42 03 5E 03 1F 03 9F 07 02 FF 00 9F 08 02 00 02 
9F 0D 05 00 00 00 00 00 9F 0E 05 00 08 00 60 00 
9F 0F 05 00 00 00 00 00 9F 42 02 09 78 9F 4A 01 
82 9F 14 01 00 9F 23 01 00 9F 13 02 00 00 

此响应包含 TLV 数据对象(无空格)。我已按照以下所述转换响应:

// Read Record 1 with SFI2
//---------------------------------SEND READ RECORD-------------------
inCtlsSendVAPDU(0x2C,0x03,(unsigned char *)"\x00\xB2\x01\x14\x00",5);
clrscr();

inRet = inCTLSRecv2(Response, 269);
LOG_HEX_PRINTF("Essai EMV4 Read record 1 EMV Paypass:",Response,inRet);


if(Response[14]==0x70)
{
    sprintf(Response_PPSE,"%02X%02X",Response[12],Response[13]);//To retrieve  length of received data 
    t1=hexToInt(Response_PPSE);// Convert length to integer
    t11=t1-2;
    i=14; 
    k=0;

    //--------------------------- Converting data to be used later---------
    while(i<t11+14)// 14 to escape  the header+ command+ status+ length 
    {
        sprintf(READ1+(2*k),"%02X",Response[i]);
        i++;
        k++;
    }

现在我应该检查这个响应是否包含强制标签:

所以我尝试了以下方法来检查 5A 标签(应用程序主帐号 (PAN)):

i=0;
t11=2*t11;

while(i<=t11)
{
    strncpy(Response_PPSE,READ1+i,2);

    if(strncmp(Response_PPSE,"\x05\x0A")==0)
    {
        write_at("true",4,1,1);// Just to test on the terminal display 
        goto end;
    }
    else 
        i=i+2;
}  
goto end;

不知道为什么终端没有任何显示,if块没有执行!

我尝试通过以下方式手动打印 5A 标签:

strncpy(Response_PPSE,READ1+44,2);
write_at(Response_PPSE,strlen(Response_PPSE),1,1);

它显​​示正确的值!!

有人可以帮助解决这个问题吗?

您没有找到该标记,因为您不是在搜索字符串“5A”,而是在搜索字符串“\x05\x0A”(ENQ 字符 + 换行符)。此外,我想知道上面的代码是否真的可以编译,因为你没有为 strncmp() 指定强制长度参数。你可以试试

if(strncmp(Response_PPSE,"5A", 2)==0)

相反。

但是,您应该了解您正在扫描整个响应数据以查找值 5A。因此,找到这个值也可能意味着它是某些其他 TLV 标记的 data 字段、length 字段的一部分,甚至是多字节的一部分标签 字段。因此,为 BER(基本编码规则)格式实施(或使用现有的)TLV 解析器是有意义的。

首先使用字符串函数在原始字节流数据中搜索特定字节并不是一个好的方法。

通用 TLV 解析器是一种非常简单的算法,您将在 30 分钟左右完成。

一般来说,寻找特定标签的 TLV 解析器的伪代码如下所示:

index = 0
while (byte[i] != 0x5A or EOF)
{
     length = DecodeLength(byte[i+1])
     i += length + 2 // + 1 for L (length) byte itself, it might be encoded with
                     // 2 bytes so the function DecodeLength can return the number 
                     // of bytes lenght has been encoded
                     // +1 for T (tag) byte
}

if(EOF) return tag_not_found

return byte[i + 2], length // pointer to data for Tag '5A'and length of data