For example I have R-APDU like this
uchar rApdu[] = {0x6F, 0x35 ,0x84 ,0x08 ,0x45 ,0x4F ,0x50 ,
0x43 ,0x43 ,0x41 ,0x52 ,0x44 ,0xA5 ,0x29 ,0x50 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0x5F,
0x2D, 0x06 ,0x75 ,0x7A ,0x72 ,0x75 ,0x65 ,0x6E ,0x87 ,0x01 ,0x01 ,0x9F ,0x11 ,0x01 ,0x01 ,0x9F,
0x12 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0xBF ,0x0C ,0x05 ,0x9F ,0x4D ,0x02 ,0x0B ,0x0A};
I want to parse this TLV in C, like on this page: parsed TLV
It should parse all tags and their value and take in structure. I tried to parse and that's what I got:
#include <stdio.h>
#include <stdlib.h>
#define uchar unsigned char
#define uint unsigned int
#define ushort unsigned short
#define k6f 0x6F
typedef enum{
false,
true
}bool;
struct TAG{
uchar *data;
uint len;
};
struct TLVdata{
struct TAG s6F;
struct TAG s84;
struct TAG sA5;
struct TAG s50;
struct TAG s5F2D;
struct TAG s87;
struct TAG s9F11;
struct TAG s9F12;
struct TAG sBF0C;
struct TAG s9F4D;
};
static int readTLV(uchar rApdu[]);
int main(void){
uchar rApdu[] = {0x6F, 0x35 ,0x84 ,0x08 ,0x45 ,0x4F ,0x50 ,
0x43 ,0x43 ,0x41 ,0x52 ,0x44 ,0xA5 ,0x29 ,0x50 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0x5F,
0x2D, 0x06 ,0x75 ,0x7A ,0x72 ,0x75 ,0x65 ,0x6E ,0x87 ,0x01 ,0x01 ,0x9F ,0x11 ,0x01 ,0x01 ,0x9F,
0x12 ,0x06 ,0x55 ,0x5A ,0x4B ,0x41 ,0x52 ,0x54 ,0xBF ,0x0C ,0x05 ,0x9F ,0x4D ,0x02 ,0x0B ,0x0A};
if (readTLV(rApdu) == 0)
printf("ok!\n");
system("PAUSE");
return false;
}
static int readTLV(uchar rApdu[]){
uint i, j, aLen, tLen, tlvIndex;
struct TLVdata newtlv;
aLen = sizeof(rApdu);
tlvIndex = 0;
switch(rApdu[tlvIndex]){
case k6f:
tLen = (int)rApdu[++tlvIndex];
for(i=0; i <= tLen; i++, ++tlvIndex){
if(rApdu[tlvIndex] == 0x84){
newtlv.s84.len = (int)rApdu[++tlvIndex];
++tlvIndex;
newtlv.s84.data = (char*)malloc(10*sizeof(char));
for(j=0; j < newtlv.s84.len; j++, ++tlvIndex){
newtlv.s84.data[j] = rApdu[tlvIndex];
}
newtlv.s84.data[j] = '\0';
printf("%s\n", newtlv.s84.data);
}
}
break;
}
return 0;
}
I found one parser. Only it is written in Java or do not know in what language. Can someone help? apduparser
I can provide the following code to parse BER-TLV:
To implement EMV TLV data parsing you need to follow ISO 8825-1 BER-TLV encoding rules, also known as ASN.1 Basic Encoding Rules (BER).
To be short, to extract correct BER-TLV Value you need analyse Tag and Length bits to get its correct size.
Then your sample buffer will be like this:
Or, in case you wish to apply EMV analysis: