Switcloud L2

Switcloud L2 is the lowest layer of the Switcloud infrastructure. It wraps a native EMV L2 stack (Moka, or the terminal's own certified stack) into a Kotlin library exposing the GLA interfaces. Switcloud Client, or any payment application, can then drive every supported EMV L2 stack with the exact same API.

Warning

A payment application using Switcloud L2 directly doesn't benefit from the Switcloud infrastructure (configuration management, payment data upload, etc.). Most applications should use Switcloud Client, which drives Switcloud L2 for them.

Note

To add Switcloud L2 to your application, follow the instructions in deliverables.

1. Overview

Switcloud L2 follows the Moka data model, or rather the GLA data model: data goes in and out of the library as BER-TLV streams.

It is organized in services, which the ISwitcloudL2 instance gives access to:

Service Getter Role
GLA gla() EMV configuration and card processing (entry point and kernels)
Reader reader() Interaction with the card readers: supported interfaces, timeouts
Virtual card vcard() Virtual cards, for testing only (null if not available)
GSA gsa() Secure services: provisioning, secure PIN entry (null if not available)

Besides these services, Switcloud L2 raises user interface events (messages and LEDs) through the userInfo flow, and can delegate sensitive operations to the application.

2. Code samples conventions

To keep them readable, the samples on this page build TLV streams with the helpers below. Use your own TLV library if you have one.

/** Parses a hexadecimal string, ignoring spaces: hex("DF A0 06") */
fun hex(value: String): ByteArray =
    value.replace(" ", "").chunked(2).map { it.toInt(16).toByte() }.toByteArray()

/** Concatenates TLVs into a TLV stream. */
fun tlvs(vararg items: ByteArray): ByteArray =
    items.fold(ByteArray(0)) { stream, item -> stream + item }

/** Builds a TLV, computing its length: tlv("9C", hex("00")) */
fun tlv(tag: String, vararg values: ByteArray): ByteArray {
    val value = tlvs(*values)
    val length = when {
        value.size < 0x80 -> byteArrayOf(value.size.toByte())
        value.size <= 0xFF -> byteArrayOf(0x81.toByte(), value.size.toByte())
        else -> byteArrayOf(0x82.toByte(), (value.size shr 8).toByte(), value.size.toByte())
    }
    return hex(tag) + length + value
}

3. Getting started

3.1 Instance

SwitcloudL2 is a singleton: there is no constructor, get the instance with:

val switcloudL2 = SwitcloudL2.getInstance()

On Android, give it the activity, so that it can use the NFC adapter:

switcloudL2.setActivity(activity)

3.2 Reader

The Reader service drives the interaction between the readers and the card. Its only function, configure, sets the supported interfaces and the timeouts:

val readerParams = tlvs(
    tlv("DFA006", hex("04")), // supported interfaces (mask): 01 magstripe, 02 contact, 04 contactless
    tlv("DFA018", hex("01")), // traces: on
    tlv("DFA008", hex("19")), // interface detection timeout: 25 seconds
    tlv("DFA007", hex("1E")), // polling timeout: 30 seconds
)

switcloudL2.reader().configure(readerParams)

3.3 EMV configuration

The GLA service drives the EMV L2 processing, including its configuration, in 4 steps:

  1. entry point configuration;
  2. combinations;
  3. CAPKs;
  4. CRLs.

Note

CAPKs and CRLs can also be flushed. See the GLA service documentation.

3.3.1 Entry point

Configure the entry point with configureEntryPoint:

val gla = switcloudL2.gla()

gla.configureEntryPoint(
    tlv("DFA025", hex("00")) // reference transaction type: purchase
)

3.3.2 Combinations

A combination gives the kernel what it needs to process a type of contactless transaction. It is bound to a triplet AID × kernel ID × transaction type. Add combinations with addCombination.

The sample below adds two combinations:

  • Mastercard AID (A0000000041010) × Mastercard kernel (C-2) × purchase;
  • Visa AID (A0000000031010) × Visa kernel (C-3) × purchase.
// Mastercard
gla.addCombination(
    tlv("E1",
        tlv("9F06", hex("A0000000041010")),        // AID
        tlv("DF810C", hex("02")),                  // kernel ID
        tlv("DFA010", hex("01")),                  // partial selection: on
        tlv("9C", hex("00")),                      // transaction type: purchase
        tlv("E2",                                  // kernel configuration
            tlv("DF8123", hex("000000001600")),    // reader contactless floor limit
            tlv("DF8124", hex("000000005000")),    // reader contactless transaction limit (no on-device CVM)
            tlv("DF811F", hex("08")),              // security capability
        ),
    )
)

// Visa
gla.addCombination(
    tlv("E1",
        tlv("9F06", hex("A0000000031010")),        // AID
        tlv("DF810C", hex("03")),                  // kernel ID
        tlv("DFA010", hex("01")),                  // partial selection: on
        tlv("9C", hex("00")),                      // transaction type: purchase
        tlv("E2",                                  // kernel configuration
            tlv("9F66", hex("27004000")),          // terminal transaction qualifiers
            tlv("9F1B", hex("00002710")),          // terminal floor limit
            tlv("9F820D", hex("000000000200")),    // reader contactless floor limit
            tlv("9F820E", hex("000000010000")),    // reader CVM required limit
            tlv("DFA020", hex("000000000400")),    // reader contactless transaction limit
            tlv("DFA014", hex("01")),              // status check support: on
            tlv("DFA015", hex("01")),              // zero amount allowed: on
            tlv("DFA016", hex("01")),              // zero amount allowed for offline: on
            tlv("DFA017", hex("01")),              // extended selection support: on
            tlv("9F1A", hex("0840")),              // terminal country code
        ),
    )
)

3.3.3 CAPKs and CRLs

Add a certification authority public key with addCAKey. Its value has a fixed layout:

val modulus = hex(
    "9C6BE5ADB10B4BE3DCE2099B4B210672B89656EBA091204F613ECC623BEDC9C6" +
    "D77B660E8BAEEA7F7CE30F1B153879A4E36459343D1FE47ACDBD41FCD710030C" +
    "2BA1D9461597982C6E1BDD08554B726F5EFF7913CE59E79E357295C321E26D0B" +
    "8BE270A9442345C753E2AA2ACFC9D30850602FE6CAC00C6DDF6B8D9D9B48790B" +
    "826B042A07F0E5AE526A3D3C4D22C72B9EAA52EED8893866F866387AC05A1399"
)

gla.addCAKey(
    tlv("DFA023",
        hex("A000000004"),         // RID
        hex("00"),                 // index
        hex("00"),                 // hash indicator (RFU)
        hex("00"),                 // algorithm indicator (RFU)
        hex("00A0"),               // modulus length: 160 bytes
        modulus.copyOf(256),       // modulus, padded with zeros to 256 bytes
        hex("01"),                 // exponent length
        hex("03").copyOf(3),       // exponent, padded with zeros to 3 bytes
        ByteArray(20),             // hash (RFU)
    )
)

Revoked certificates work the same way, with addRevokedCertificate.

4. Processing a transaction

A typical Switcloud L2 life cycle is:

  • at application startup:
  • for each transaction:
    • initialize the services (transient data);
    • configure the EMV L2 stack;
    • process the card with the GLA service;
    • clean the services up.

A contactless transaction goes through the EMV entry point starts: pre-processing (Start A), protocol activation (Start B, waits for a card), combination selection (Start C) and kernel activation (Start D). Each step returns signals, among which the outcome parameter set tells how to proceed:

Outcome parameter set Tag
Kernel C-2, or C-2 signal mode DF8129
Other kernels 9F8210

Its first byte is the status (10 approved, 20 declined, 30 online request, 40 end application, 70 try again, etc.) and its second byte the start to proceed from (10 Start B, 30 Start D, F0 none). When an outcome asks to start again from Start B, call protocolActivation again: this is how a card is presented again, for example after the cardholder performed an on-device CVM on their phone.

val trd = tlvs(
    tlv("9F02", hex("000000001500")), // amount, authorized
    tlv("9A", hex("261126")),         // transaction date
    tlv("9C", hex("00")),             // transaction type: purchase
    tlv("5F2A", hex("0978")),         // transaction currency code
)

// Transaction
switcloudL2.initializeServices()
configure(gla) // entry point, combinations, CAPKs, CRLs (see above)

try {
    val (preProcessing, proceed) = gla.preProcessing(trd) // Start A
    var signals = preProcessing

    while (proceed) {
        gla.protocolActivation(null) // Start B: waits for a card

        val (selection, selected) = gla.combinationSelection() // Start C
        signals = if (selected) gla.kernelActivation(null) else selection // Start D

        // outcomeOf() reads the outcome parameter set with your TLV library
        val outcome = outcomeOf(signals)
        if (outcome.start != START_B) {
            break
        }
        // start again from Start B, e.g. try again, or present the card again
    }

    // signals now hold the final outcome: data record, discretionary data,
    // outcome parameter set and user interface request data
} finally {
    switcloudL2.cleanupServices()
}

Note

This sample leaves out online processing (outcomes with Start D), data exchange (DEK/DET) and online PIN. Switcloud Client handles all of them: see Switcloud Client.

5. User interface events

During a transaction, the EMV L2 stack asks the payment application to show messages to the cardholder (user interface request data, or UIRD) and to switch LEDs on. These requests reach the application in two ways:

  1. The data returned by the GLA service. preProcessing, combinationSelection and kernelActivation return the UIRD of the outcome they end with.
  2. The userInfo flow. It carries every request, as it happens: those of the outcomes above, but also those sent while a call is running, and those of the intermediate outcomes the entry point handles on its own.

The application must capture both. It then decides which messages to display, and for how long, according to its own user interface. The final message may be received both ways.

5.1 The userInfo flow

userInfo is a Kotlin SharedFlow<ByteArray>. Collect it once, at application startup:

scope.launch {
    switcloudL2.userInfo.collect { event ->
        // a single TLV, its length is always on 1 byte
        val tag = event.copyOfRange(0, 3).toHexString().uppercase()
        val value = event.copyOfRange(4, event.size)

        when (tag) {
            "DF8116", "9F8205" -> showMessage(value)                    // UIRD
            "DFA01B" -> if (value[0].toInt() and 0x80 != 0) {           // LED on
                switchLedsOn(upTo = value[0].toInt() and 0x0F)
            }
        }
    }
}

Warning

The flow replays its last 10 events to new collectors, including events of previous transactions. Collect it once, rather than once per transaction.

Each event is a single TLV:

Tag Length Content
DF8116 13 bytes UIRD, from kernel C-2, or in C-2 signal mode
9F8205 13 bytes UIRD, from other kernels
DFA01B 1 byte LED: bit 8 set when the LED is on, bits 1 to 4 for the LED number

The UIRD value is as follows (EMVCo Book A):

Offset Length Content
0 1 byte Message identifier: 15 present card, 17 card read OK, 1B authorizing, please wait, 20 see phone for instructions, 21 present card again, etc.
1 1 byte Status: 00 not ready, 01 idle, 02 ready to read, 03 processing, 04 card read successfully, 05 processing error
2 3 bytes Hold time: how long to display the message, in units of 100 ms (BCD: 000030 is 3 seconds)
5 8 bytes Language preference: ISO 639-1 codes, zero padded

Note

LEDs are only ever switched on: LED 1 at protocol activation (waiting for the card), LED 2 at combination selection (a card was detected), LED 3 at kernel activation (the card is being read). When LED n is switched on, LEDs 1 to n−1 should be on as well.

5.2 When events are raised

When Event
Protocol activation (Start B) LED 1; UIRD "present card", or "present card again" on later presentations. On a restart, the UIRD on restart of the previous outcome instead, if any.
Combination selection (Start C) LED 2
Kernel activation (Start D) LED 3; UIRDs sent by the kernel while it reads the card (e.g. "card read OK"); the UIRD of the outcome, if any

5.3 Example: on-device CVM

When a phone or a watch asks the cardholder to authenticate on the device (CDCVM), the kernel ends with an outcome asking to start again from Start B, with the UIRD "see phone for instructions". The application then calls protocolActivation again, and the entry point raises the UIRD of the next presentation, such as "present card again". The sequence on userInfo is:

  1. LED 1, "present card": waiting for the card;
  2. LED 2, LED 3: the kernel reads the phone;
  3. "see phone for instructions": the cardholder authenticates on the phone;
  4. LED 1, "present card again": waiting for the phone again;
  5. LED 2, LED 3, "card read OK", then the final outcome's message.

Only the final outcome is in the data returned at the end: the intermediate messages (3 and 4) are only available from userInfo, unless the application reads the outcome returned by each kernelActivation call.

6. Delegated security

Switcloud L2 can delegate operations on sensitive assets to more secure components: card readers (contact, contactless and magstripe), random numbers, offline data authentication and secure data (SRED), CAPK storage and virtual cards.

The application implements IXsecCallback, overriding the operations it delegates, and registers it:

switcloudL2.registerCallbacks(myXsecCallback)