1

I am working with some EMV tags which most of them have a length of 1 or 2 bytes for example 9F02, 81,.. I understand that there is a certain bit configuration to know how I can determine the tag length for determine if a tags is 2 byte length or larger, Im using:

unsigned char tags[]={0x9F,0x02};
if((tags[0]&0x1F)==0x1F){
  ...
}

but I dont know to do when I have some tag larger than this.

Im working with EMV Data, Im testing with certification card , I'm receiving these tags are: DF8111, DF8119, DF811E, DF812C they are relate to CVM.

Aneury Perez
  • 82
  • 2
  • 5
  • You have to know what the system defines. You've not provided enough information for us to be able to help you. There are many possible systems for TLV-encoding. Does 'EMV' mean "EuroPay, MasterCard, Visa"? BER is presumably the ASN.1 Basic Encoding Rules. But we shouldn't be having to guess. – Jonathan Leffler Mar 21 '20 at 19:09
  • sorry, Im working with EMV(Europay,mastercard, Visa) card data. – Alexandra Jimenez Mar 21 '20 at 19:13
  • I tried to find out in google but the practice of using tags that are 3 byte length seems no common in fact. – Alexandra Jimenez Mar 21 '20 at 19:19
  • That suggests that there probably aren't 3 byte tags, then. Why do you think that's what you're getting? – Jonathan Leffler Mar 21 '20 at 19:20
  • Searching on 'emv specification' comes up with versions 4.2 and 4.3 (the latter seems to be from November 2011, so version 4.2 shouldn't be very relevant), and a site https://www.emvco.com/ and links to PDF files. There's a Book 3 — I'm not sure what might be in Book 1 or Book 2. There's mention of BER-TLV in Book 3. The PDF URL includes a date in 2017. You still need to clarify what your problem is — where you think you're getting an undocumented encoding from. I can't help much more. – Jonathan Leffler Mar 21 '20 at 19:27
  • BER-TLV encoding is documented in EMV Book 3, Annex B "Rules for BER-TLV Data Objects". You might find [this answer](https://stackoverflow.com/a/36644826/5128464) interesting...Good luck! – vlp Mar 22 '20 at 08:56

2 Answers2

4

Go through EMV 4.3 Book 3, Annex B - Rules for BER-TLV Data Objects sections B1, B2, B3. If you follow this precisely, then you wouldn't need to store a static list of tags; it says clearly how to code and interpret tag length and values.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Adarsh Nanu
  • 2,133
  • 1
  • 13
  • 18
1

According to

EMV 4.3 Book 3

, Annex B - Rules for BER-TLV Data Objects sections B1, B2 that was linked above, you should check the bit 8 in the current byte in order to know if there are more byte in tag, assuming that you are using c/c++ (as you tag it in this post) here is a code that could be taken in order to check that condition, I commented the code bellow where the condition is and could used by you.

        int start = 0;
        int end = start + len;
        while (start < end) {

            int tag = src[start++] & 0xFF;

            if (tag == 0x00 || tag == 0xFF) {
                continue;
            }

            if ((tag & 0x1F) == 0x1F) {
                if (start >=  len ) {
                    break;
                }

                tag = (tag << 8) | src[start++] & 0xFF;
                // tag has 3 bytes (0xFFFFFF)
                /// check this line it could what you need.
                if ((tag & 0x80) != 0) {
                    if (start >=  len ) {
                        break;
                    }
                    //// Append third byte to the Tag.
                    tag = (tag << 8) | src[start++] & 0xFF;
                } 
            }
       /// ...
} ///end while

I hope this help you.

Aneury Perez
  • 82
  • 2
  • 5
  • I've found some EMV Tags that I think are excepted from 0x1F rule! Based on the EMV Book 4.3 Annex B, B1~B5 = 1 means there is an subsequent Byte Then why this rule is not imply to "628C Security attribute in compact format" or any other similar tags? – Mahmoud Hosseinipour Mar 09 '22 at 11:40
  • You seem to be taking all 8 bits of the extended tag bytes, but aren't only the lowest 7 bits part of the tag, according to the very spec you mentioned? – Fabio A. Jun 07 '23 at 16:17