Search code examples
c#smartcardemv

Read EMV data from Mastercard/VISA Debit/Credit Card


I am trying to build an application to read/encode data on Cards, information like PAN, expiry, customer name, PIN etc, So far I could figure out that I need to send APDU commands to read data from Card but there seems to be no clear documentation available as to what commands are used for what purpose and in what particular sequence, I couldn't find out specifications from Mastercard/VISA. Is there some documentation that can be referred to?

Thanks, Null


Solution

  • Extending the other answer:

    1. SELECT PSE:
    T-->C - 00A404000E315041592E5359532E444446303100   # select PSE
    T<--C - response with FCI
    T-->C - 00B2010C00
    T<--C - reponse with record from selected file, read records starting from 1 until receive 6A83 (optional step in your case)
    
    1. SELECT application DF with AID received in step 1):
    T-->C - 00A4040007A000000003101000   # as example, Visa AID
    T<--C - response with application DF FCI
    
    1. GET PROCESSING OPTIONS - initialize transaction:
    T-->C - 80A8000002830000    # check if PDOL presents on card, if not, only 8300 should be added to DATA filed of APDU
    T<--C - 771282023C00940C0802020010010300180102019000  # just example reswponse, it will differ on different cards
    

    The response on GET PROCESSING OPTIONS above is TLV encoded:

    77 12 - response templait, containing response data
        82 02 3C00 - AUC
        94 0C 080202001001030018010201 - AFL
        9000 - SW (Status Word), response ofapplication, telling you, that no errors occured
    

    Note, that response to GET PROCESSING OPTIONS may be returned as 80 template, in that case, you must parse it yourelf:

    80 0E - response templait, containing response data
        3C00 - AUC (always 2 bytes long)
        080202001001030018010201 - AFL
        9000 - SW (Status Word), response ofapplication, telling you, that no errors
    

    You are interesting in AFL, which points you, where to read data from (files and record numbers):

    94 0C 
        08020200
            08 - SFI (Short File Identifier)
            02 - first record in file
            02 - last record in file
            00 - no of records to be added to Static Data Authentication
        10010300
            10 - SFI
            01 - first record in file
            03 - last record in file (respectively, 3 records to be read - 01, 02, 03)
            00 - no of records to be added to Static Data Authentication
        18010201
            18 - SFI
            01 - first record in file
            03 - last record of file
            01 - count of records from first record to be used for Static Data Authentication (01 record must be used)
    

    SFI is encoded as follows:

    08 = 0000 1000 - first 5 bits are real SFI, it equals to 01, last 3 bits are always set to 0
    
    1. READ APPLICATION DATA - for precize READ APPLICATION DATA command coding check 3rd EMV Book:
    T-->C - 00B2020C00   # SFI = 01, record = 02
    T<--C - response with record
    T-->C - 00B2021400   # SFI = 02, record = 01
    T<--C - response with record
    T-->C - 00B2031400   # SFI = 02, record = 02
    T<--C - response with record
    etc until you process last AFL record...
    

    PAN, expiry, effective date, track 2 equivalent data, etc... usually is located in records which are set to be used in Sighed Data Authentication in AFL.

    The example above is for T=1 protocol. If card runs T=0 protocol, in response to each APDU which assumes R-APDU (Response APDU) to contain Data field, card will return byte count ready to be read and you should issue GET RESPONSE commands which is described in Book 1 of EMV specification.

    Hope it helps.