Reading desfire based virtual card using TapLinx

62 Views Asked by At

I'm trying to read a MIFARE 2GO DESfire based virtual card. For now my goal is to get the card's static UID. The first step in the card reading flow is to isoSelectApplicationByDFName. If success (SW = 0x9000), then go to the next step. I just learned about TapLinx library and I'd like to use it for this purpose. How would I perform isoSelectApplicationByDFName operation on MIFARE 2GO DESfire based virtual card using TapLinx? After this step I would use the library to perform other operations, e.g. authentication. Below you can see the code of how I do it without using the library.

import android.nfc.Tag
import android.nfc.tech.IsoDep
import java.io.IOException


class Mifare2GoReader {

    val SW_OK = byteArrayOf(0x90.toByte(), 0x00.toByte())

    fun read(tag: Tag) {
        var isoDep: IsoDep? = null
        try {
            isoDep = IsoDep.get(tag)
            isoDep.connect()

            val aid = listOf(0xF2, 0x13, 0xF0).toByteArray()
            val cmdIsoSelect = constructIsoSelectCmd(aid)
            val result = isoDep.transceive(cmdIsoSelect)

            if (result.contentEquals(SW_OK)) {
                // TODO perform next steps: Authenticate, GetCardUID
                // How to do the same but using the TapLinx library?
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                isoDep?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    /**
     * Construct ISO-7816-4 SELECT by DF Name command
     */
    private fun constructIsoSelectCmd(aid: ByteArray): ByteArray {
        val dfName = constructDFName(aid)

        return listOf(
            0x00, // CLA Class
            0xA4, // INS Instruction
            0x04, // P1  Parameter 1
            0x00, // P2  Parameter 2
            dfName.size, // Length
        ).toByteArray().plus(dfName)
    }

    /**
     * Mapping from DESFire application ID to an ISO/IEC 7816-4 Dedicated 230 Filename according to the IID Allocation Scheme defined in NXP AN11909 [2]
     */
    private fun constructDFName(aid: ByteArray): ByteArray {
        if (aid.size != 3) {
            throw IllegalAccessException("MIFARE DESFire AID must be 3 bytes long")
        }

        return listOf(0xA0, 0x00, 0x00, 0x03, 0x96, 0x56, 0x43, 0x41, 0x03).toByteArray()
            .plus(aid)
            .plus(listOf(0x00, 0x00, 0x00, 0x00).toByteArray())
    }

    private fun List<Int>.toByteArray(): ByteArray = map { it.toByte() }.toByteArray()

}
0

There are 0 best solutions below

Related Questions in VIRTUAL-CARD