Tools · EMV Level 2 Bench

EMV Level 2 Bench Protocol

The EMV Level 2 Bench drives an EMV Level 2 stack over raw TCP. Any stack that implements the protocol below can be connected to the Bench: push its configuration and keys, run a contactless transaction with a test card or a card probe, and read the decoded outcome. This page is the reference for implementing it.

Transport

  • The stack listens on a TCP port. The Bench reaches it through a small relay on the visitor's machine, which turns the browser's WebSocket into a TCP connection and passes the bytes through untouched.
  • The Bench sends one command at a time and waits for its reply before sending the next one. Replies carry no command type, so they are matched to requests by order.
  • The Bench waits up to 90 seconds for the reply to 05, to leave time for the card tap, and 15 seconds for every other command.

Frame format

Request

FieldSizeContent
Length4 bytesBig-endian. Counts the type byte and the payload, not itself.
Type1 byteCommand type, 01 to 0E.
PayloadVariable: the Length value minus 1, for the type byteText (ASCII). May be empty.

A payload of 4 bytes gives a length of 5, coded 00000005. For example, selecting the configuration file mastercard:

0000000B 03 6D617374657263617264
length   type payload ("mastercard")

Reply

FieldSizeContent
Length4 bytesBig-endian. Counts the payload only.
PayloadVariable: the Length valueDepends on the command: a status, binary TLV or text.

There is no type byte in a reply. A store command that succeeds replies with two bytes 0000:

00000002 0000

Any other status is an error code defined by the stack. The Bench shows it as received.

Commands

TypeNameRequest payloadReply payload
01STORE_CAKEYSContent of the CA public keys file0000 if no error
02STORE_REVOKSContent of the revocation list file0000 if no error
03STORE_CONFIG_NAMEName of a configuration file already on the stack, for example mastercard0000 if no error
04STORE_CONFIGContent of a configuration file0000 if no error
05PERFORM_CL_TRANSACTIONTransaction Related Data: hex TLV text between square bracketsBinary TLV: the outcome
06CLEAN_FILESEmpty0000 if no error
07GET_STACKED_SIGNALSEmptyBinary TLV; empty if there is no stacked signal
08STORE_PATHFolder on the stack where the named files are read, for example opt/stack/files0000 if no error
09SHOW_OPTIONSEmptyText: the current options
0ASTORE_CAKEYS_NAMEName of a CA keys file already on the stack, for example capk.txt0000 if no error
0BSTORE_REVOKS_NAMEName of a revocation list file already on the stack0000 if no error
0CRESET_OPTIONSEmpty0000 if no error
0DSTORE_ISSUER_NAMEName of an issuer response file already on the stack0000 if no error
0ESTORE_ISSUERContent of an issuer response file0000 if no error

Each kind of file can be sent in two ways: by content (01, 02, 04, 0E) or by naming a file the stack already has (0A, 0B, 03, 0D). The format of the files themselves is defined by the stack; the Bench sends them as they are.

Transaction Related Data (05)

The payload of 05 is the transaction data as hex TLV text, between [ and ]. The Bench builds it from its form:

TagNameExample
9F02Purchase amount000000000010
9F03Amount, Other (cashback)000000000000
5F2ATransaction Currency Code0978
5F36Transaction Currency Exponent02
9ATransaction Date, YYMMDD261118
9F21Transaction Time, HHMMSS145000
9CTransaction Type00

Any other TLV entered in More TLV is appended as is, proprietary tags included. Tags may have an empty value.

[9F02060000000000109F03060000000000005F2A0209785F3601029A032611189F21031450009C0100]

In Transaction Related Data, 9F02 corresponds to a purchase amount, not the Amount, Authorized as per the EMVCo definition. The Bench behaves as a POS client not pertaining to the payment (EMV) domain.

Outcome (reply to 05)

The reply is binary TLV, sent as soon as the transaction ends. The Bench decodes the standard tags and lists the others without interpreting them. A Kernel 2 stack typically returns:

  • DF8129 Outcome Parameter Set
  • FF8105 Data Record, with the data for the authorization or clearing message
  • FF8106 Discretionary Data, with the Error Indication DF8115
  • DF8116 User Interface Request Data
  • Any proprietary template or tag the stack adds

Stack log port (optional)

A stack can also send its log as text on a second TCP port, dedicated to it. The Bench opens that port through the relay, separately from the protocol connection, and shows the text live in its Stack log panel. Nothing is sent to the log port, and its content has no framing: plain text, one or more lines at a time.

  • Keeping the log on its own port means the protocol frames and replies never mix with log text, and the stack does not have to buffer its log.
  • No code change is needed to expose the standard output of a stack on a port, for example:
stdbuf -oL ./stack | nc -lk 5001
  • stdbuf -oL makes the program write its standard output line by line instead of in 4 KB blocks.
  • Until a client reads the port, nc does not read the pipe: once the pipe is full (about 64 KB), the stack stops at its next write. Connect the Bench before running transactions.
  • To keep a copy on disk as well: stdbuf -oL ./stack | tee -p stack.log | nc -lk 5001.

Implementation checklist

  • Read the 4-byte length first, then exactly that many bytes. Do not rely on one TCP read per frame.
  • Reply to every request, once, in the order received.
  • Do not put a type byte in replies.
  • Reply 0000 to a store command that succeeds.
  • Strip the square brackets from the 05 payload before decoding the hex TLV.
  • Accept empty values in the Transaction Related Data.
  • Optional: write the log on its own port, never on the protocol connection.

← Back to the EMV Level 2 Bench