【问题标题】:searching Tags (TLV) with C function使用 C 函数搜索标签 (TLV)
【发布时间】:2015-04-16 16:17:24
【问题描述】:

我正在处理 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)
  • 5F24 - 应用程序到期日期
  • 8C - 卡风险管理数据对象列表 1 (CDOL1)
  • 8D - 卡风险管理数据对象列表 2 (CDOL2)

所以我尝试了以下方法来检查 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);

它显示正确的值!

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

【问题讨论】:

    标签: c hex apdu contactless-smartcard tlv


    【解决方案1】:

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

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

    改为。

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

    【讨论】:

    • 你能给我一些关于使用 C 语言的现有 TLV 解析器的想法吗? @迈克尔·罗兰
    • @SaraSara 不,我不能。
    • 好的,谢谢@Michael Roland
    • 我是新来的,所以我不知道所有的论坛规则,谢谢你提到它@Michael Roland
    • 顺便说一句;感谢您的帮助@Michael
    【解决方案2】:

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

    通用 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
    

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 2018-04-02
      • 1970-01-01
      相关资源
      最近更新 更多