Skip to content

Level 2 test agent

Before a terminal accepts contactless cards, an accredited lab tests its kernels against each scheme’s test plan: its Level 2 certification (see the glossary). The lab doesn’t test the kernels on a PC: it tests them on your device, with your card reader, through an application you install on it for the purpose. a7pay’s is the agent: the same code on every device (a countertop terminal, an embedded reader, an Android or iOS phone), only the user interface and the link to the lab’s tools differing.

The lab runs hundreds of test cases. Each one sets the reader’s configuration (which applications it accepts, with which kernel and limits, which CA keys), gives the transaction’s data (amount, currency, type), presents a test card, and checks what the reader did: the commands it sent the card, the Outcome, the messages it displayed.

There are two ways to drive the device:

  • Automated, with the scheme’s own tool: Visa’s ART, Mastercard’s TEI. The tool sends each test case to the device, the device runs the transaction and sends the results back, and the tool moves to the next case.
  • Manual: a person prepares each test in a tool on a PC (choose the configuration, enter the transaction data, choose the CA keys and revocation lists), starts the transaction, presents the card, and reads the results.

Either way, there is a controller on one side, the program telling the device what to do, and the agent on the device. They speak the agent’s protocol. The schemes’ tools speak their own (ART is XML over HTTP), so a translator sits between them and the agent: on the PC for an embedded device, inside the application on a phone. The translators are built from the schemes’ documentation, which isn’t public: they are in a7pay’s private repository.

 lab PC                                              device
 manual test tool ───────┐                           ┌───────────────────────────┐
                         ├── agent protocol ────────▶│ agent                     │
 ART / TEI ── translator ┘   TCP, UART, USB          │ Entry Point, the kernels  │
                                                     │ your Reader, your Crypto  │
                                                     └───────────────────────────┘

The agent (apps/emvco/agent) is a library without the standard library, like the kernels. Your device provides:

  • a card reader and cryptography: the Reader and Crypto interfaces of Platform, the same ones your payment application uses;
  • a link to the controller: any byte stream (TCP, a UART, USB), or, when the controller runs in the same application (a phone), direct calls;
  • a screen for the messages the kernels ask to display (“Present card”, “Approved”), through Reader::display_message.

The agent’s features choose what the device is certified with, like the library’s (see Building):

  • its kernels: default-features = false, features = ["c2", "c3"] for a device with Kernels 2 and 3;
  • the kernels’ optional features it implements (c6-data-storage, c2-de-ds, …, see Kernels);
  • the schemes’ certified behaviours, for the schemes it is certified with (brand-mastercard, brand-amex, brand-discover, see Kernels).

The agent tells the lab’s tools what it was built with.

On a byte stream, the device’s loop gives the agent what it receives and sends what it answers:

let mut agent = agent::Agent::new("Acme T1, a7pay agent 0.1");
let mut session = agent::stream::Session::new();
loop {
    let n = link.read(&mut buffer)?;
    session.receive(
        &mut agent,
        &buffer[..n],
        &mut reader,
        &mut crypto,
        &mut |frame| link.write_all(frame).unwrap(),
        // While waiting for the card: what arrived meanwhile (None: the link is lost).
        &mut |buffer| link.read_waiting_at_most(20, buffer),
    );
}

A transaction runs inside receive: the agent drives the card through your reader and sends each message as it happens, so the lab sees the transaction live. Until a card is presented, the agent polls your reader and, between polls, reads the link for the controller’s Cancel: waiting a little there (tens of milliseconds) paces the polling. A link lost cancels the transaction too.

agent-desktop is the agent on a PC, listening on TCP. Its card is an EMVCo L3 card image, played by the card simulator, or, with feature pcsc, a real card on a PC/SC contactless reader. agentctl is a controller on the command line. From apps/emvco/, in two windows (--features reference builds it with every optional feature and every scheme’s behaviour, as the reference devices are):

cargo run -p agent-desktop -- --card ../nexo/deploy/samples/cards/k8-offline.xml
cargo run -p agent-controller --bin agentctl -- run samples/k8.toml --amount 1000
agent: a7pay agent 0.1.0, desktop, card image ../nexo/deploy/samples/cards/k8-offline.xml (protocol 1, kernels [2, 3, 4, 6, 8])
EP MSG | PresentCard (ReadyToRead)
MSG | ClearDisplay (CardReadSuccessfully)
OUT Approved, start NA, CVM NoCvm, online response data NA | Approved (NotReady)

agent-desktop --no-card is a reader on which no card is ever presented, to try cancelling.

agentctl
info The agent’s description
run <config.toml> A transaction on that configuration
--keys keys.toml, --crl crl.toml Adds a file’s CA keys, or its revoked certificates, to the configuration
--agent host:port The agent’s address (before the command; 127.0.0.1:9100 by default, agent-desktop’s --listen default)
--amount n, --amount-other n In minor units (1000 is 10.00)
--currency 978, --exponent 2, --type 00, --date YYMMDD The transaction’s currency, type and date (today’s by default)
--data TAG=VALUE Other Transaction Related Data, hex (--data 9F21=120000)
--online-response TLV The host’s answer, BER-TLV hex (8A023030: approved)
--processing processing.toml What the Terminal answers the kernels for the test (below)
--trace Every command to the card and its answer

The configuration file is TOML, with EMV data in hex: samples/k8.toml is an example, and agent_controller::file describes every field, Kernel 8’s proprietary tags included. Some tests also say what the Terminal answers the kernels, which the Books leave to the terminal’s own logic: a processing file, TOML too (agent_controller::processing):

[operator]                     # Kernel 6: Data Storage, Extended Logging
data_storage_update_template = "DF3F0101"
commit_control = "40"

[data_exchange]                # Kernels 2 and 8: given when the kernel asks for them
DF8110 = "01"

[ids_operator]                 # Kernel 3: the IDS records to read, then the update
records = [[5, 1]]

The library behind agentctl (agent-controller: the connection, the file formats, the display) is the one other controllers build on. Its decode reads the agent’s messages field by field, for people: the Outcome Parameter Set, the User Interface Request Data and the Error Indication, BER-TLV as a tree of named data objects (the names from EMV Book 3 and Books C-2 and C-8), the commands to the card and their status words.

apps/emvco/agent-protocol, shared by the agent and every controller.

A session. The controller says Hello with its protocol version; the agent answers Info: its version, the device, the kernels, optional features and scheme behaviours it was built with. Then, test after test:

Controller Agent
StartTransaction: the configuration’s name, the transaction’s data, the host’s answer if the test goes online, what the Terminal answers the kernels if the test says, whether to report the card exchanges ConfigurationNeeded if it holds another configuration (or none)
Configure: the configuration and its name Configured
StartTransaction again As the transaction runs: Signal (each message and Outcome of the kernels and Entry Point), Apdu (each command to the card and its answer, if asked); then TransactionEnd
Cancel, while it waits for the card TransactionEnd, failed: cancelled

Anything the agent can’t do (a message it can’t read, a configuration it can’t use) is answered with an Error saying why.

The configuration travels with the tests. It is sent only when the device doesn’t hold it: the controller names each configuration by its SHA-256 and starts each transaction with that name, and the agent says when it holds another one. A series of tests on one configuration sends it once; a single test is never run on a stale configuration, whatever happened to the device in between (a reboot, another controller).

A configuration holds:

  • the Combinations: each application (AID) the reader accepts, with its kernel, the transaction type, Entry Point’s settings (limits, zero amount, TTQ) and the kernel’s configuration data (for Kernel 8, its proprietary tags too);
  • the CA public keys (RSA, and Kernel 8’s ECC keys), the revoked certificates, the exception file;
  • the reader’s behaviour where the schemes’ readers depart from Book B (see Entry Point): how Entry Point shows its errors, selection by a list of AIDs, …

EMV data stays as EMV encodes it (BER-TLV): kernel configuration, transaction data, the signals’ Outcome Parameter Set and Data Record.

The Terminal’s answers travel with each test. Some kernels ask the terminal for answers the Books leave to its own logic: Kernel 6 its operator’s Data Storage decisions, Kernels 2 and 8 data through Data Exchange, Kernel 3 its IDS operator’s records. A product implements that logic; for a test, the test plan says what to answer, and the controller sends it with the transaction.

Encoding. Messages are CBOR, a compact binary format readable from any language. Fields are numbered: a new version adds fields (optional ones, so that older peers still read the messages) and messages, and never renumbers them. On a byte stream, each message travels in a frame: COBS-encoded, with a CRC, ended by a zero byte. A receiver that lost bytes, or connected mid-stream, finds the next frame at the next zero; a damaged frame is answered with an error.

  • The ART and TEI translators. The manual test tool is in progress, as a product of its own.
  • The embedded reference device. The Android one is there.