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
| Field | Size | Content |
|---|---|---|
| Length | 4 bytes | Big-endian. Counts the type byte and the payload, not itself. |
| Type | 1 byte | Command type, 01 to 0E. |
| Payload | Variable: the Length value minus 1, for the type byte | Text (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
| Field | Size | Content |
|---|---|---|
| Length | 4 bytes | Big-endian. Counts the payload only. |
| Payload | Variable: the Length value | Depends 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
| Type | Name | Request payload | Reply payload |
|---|---|---|---|
01 | STORE_CAKEYS | Content of the CA public keys file | 0000 if no error |
02 | STORE_REVOKS | Content of the revocation list file | 0000 if no error |
03 | STORE_CONFIG_NAME | Name of a configuration file already on the stack, for example mastercard | 0000 if no error |
04 | STORE_CONFIG | Content of a configuration file | 0000 if no error |
05 | PERFORM_CL_TRANSACTION | Transaction Related Data: hex TLV text between square brackets | Binary TLV: the outcome |
06 | CLEAN_FILES | Empty | 0000 if no error |
07 | GET_STACKED_SIGNALS | Empty | Binary TLV; empty if there is no stacked signal |
08 | STORE_PATH | Folder on the stack where the named files are read, for example opt/stack/files | 0000 if no error |
09 | SHOW_OPTIONS | Empty | Text: the current options |
0A | STORE_CAKEYS_NAME | Name of a CA keys file already on the stack, for example capk.txt | 0000 if no error |
0B | STORE_REVOKS_NAME | Name of a revocation list file already on the stack | 0000 if no error |
0C | RESET_OPTIONS | Empty | 0000 if no error |
0D | STORE_ISSUER_NAME | Name of an issuer response file already on the stack | 0000 if no error |
0E | STORE_ISSUER | Content of an issuer response file | 0000 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:
| Tag | Name | Example |
|---|---|---|
9F02 | Purchase amount | 000000000010 |
9F03 | Amount, Other (cashback) | 000000000000 |
5F2A | Transaction Currency Code | 0978 |
5F36 | Transaction Currency Exponent | 02 |
9A | Transaction Date, YYMMDD | 261118 |
9F21 | Transaction Time, HHMMSS | 145000 |
9C | Transaction Type | 00 |
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:
DF8129Outcome Parameter SetFF8105Data Record, with the data for the authorization or clearing messageFF8106Discretionary Data, with the Error IndicationDF8115DF8116User 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 -oLmakes the program write its standard output line by line instead of in 4 KB blocks.- Until a client reads the port,
ncdoes 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
0000to a store command that succeeds. - Strip the square brackets from the
05payload 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.