emoney: Add Thinca authentication card stuff

This commit is contained in:
kyoubate-haruka 2024-09-03 17:48:40 +02:00
parent ac0f9f0587
commit aeba46e9f1
11 changed files with 363 additions and 24 deletions

View File

@ -74,6 +74,15 @@ void aime_config_load(struct aime_config *cfg, const wchar_t *filename)
cfg->enable = GetPrivateProfileIntW(L"aime", L"enable", 1, filename);
cfg->high_baudrate = GetPrivateProfileIntW(L"aime", L"highBaud", 1, filename);
cfg->gen = GetPrivateProfileIntW(L"aime", L"gen", 0, filename);
cfg->proxy_flag = GetPrivateProfileIntW(L"aime", L"proxyFlag", 2, filename);
GetPrivateProfileStringW(
L"aime",
L"authdataPath",
L"DEVICE\\authdata.bin",
cfg->authdata_path,
_countof(cfg->authdata_path),
filename);
}
void io4_config_load(struct io4_config *cfg, const wchar_t *filename)

View File

@ -5,21 +5,21 @@
#pragma pack(push, 1)
enum {
SG_NFC_CMD_GET_FW_VERSION = 0x30,
SG_NFC_CMD_GET_HW_VERSION = 0x32,
SG_NFC_CMD_RADIO_ON = 0x40,
SG_NFC_CMD_RADIO_OFF = 0x41,
SG_NFC_CMD_POLL = 0x42,
SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43,
SG_NFC_CMD_MIFARE_SET_KEY_A = 0x50,
SG_NFC_CMD_MIFARE_AUTHENTICATE_A = 0x51,
SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52,
SG_NFC_CMD_MIFARE_SET_KEY_B = 0x54,
SG_NFC_CMD_MIFARE_AUTHENTICATE_B = 0x55,
SG_NFC_CMD_TO_UPDATE_MODE = 0x60,
SG_NFC_CMD_SEND_HEX_DATA = 0x61,
SG_NFC_CMD_RESET = 0x62,
SG_NFC_CMD_FELICA_ENCAP = 0x71,
SG_NFC_CMD_GET_FW_VERSION = 0x30,
SG_NFC_CMD_GET_HW_VERSION = 0x32,
SG_NFC_CMD_RADIO_ON = 0x40,
SG_NFC_CMD_RADIO_OFF = 0x41,
SG_NFC_CMD_POLL = 0x42,
SG_NFC_CMD_MIFARE_SELECT_TAG = 0x43,
SG_NFC_CMD_MIFARE_SET_KEY_AIME = 0x50,
SG_NFC_CMD_MIFARE_AUTHENTICATE_AIME = 0x51,
SG_NFC_CMD_MIFARE_READ_BLOCK = 0x52,
SG_NFC_CMD_MIFARE_SET_KEY_BANA = 0x54,
SG_NFC_CMD_MIFARE_AUTHENTICATE_BANA = 0x55,
SG_NFC_CMD_TO_UPDATE_MODE = 0x60,
SG_NFC_CMD_SEND_HEX_DATA = 0x61,
SG_NFC_CMD_RESET = 0x62,
SG_NFC_CMD_FELICA_ENCAP = 0x71,
};
struct sg_nfc_res_get_fw_version {

View File

@ -16,6 +16,7 @@
#include "util/dprintf.h"
#include "util/dump.h"
#include "util/slurp.h"
static HRESULT sg_nfc_dispatch(
void *ctx,
@ -87,6 +88,8 @@ void sg_nfc_init(
uint8_t addr,
const struct sg_nfc_ops *ops,
unsigned int gen,
unsigned int proxy_flag,
const wchar_t* authdata_path,
void *ops_ctx)
{
assert(nfc != NULL);
@ -96,6 +99,8 @@ void sg_nfc_init(
nfc->ops_ctx = ops_ctx;
nfc->addr = addr;
nfc->gen = gen;
nfc->proxy_flag = proxy_flag;
nfc->authdata_path = authdata_path;
}
#ifdef NDEBUG
@ -189,7 +194,8 @@ static HRESULT sg_nfc_dispatch(
&req->felica_encap,
&res->felica_encap);
case SG_NFC_CMD_MIFARE_AUTHENTICATE:
case SG_NFC_CMD_MIFARE_AUTHENTICATE_AIME:
case SG_NFC_CMD_MIFARE_AUTHENTICATE_BANA:
case SG_NFC_CMD_SEND_HEX_DATA:
return sg_nfc_cmd_send_hex_data(nfc, &req->simple, &res->simple);
@ -381,18 +387,62 @@ static HRESULT sg_nfc_cmd_mifare_read_block(
sg_nfc_dprintf(nfc, "Read uid %08x block %i\n", uid, req->payload.block_no);
if (req->payload.block_no > 3) {
if (req->payload.block_no > 14) {
sg_nfc_dprintf(nfc, "MIFARE block number out of range\n");
return E_FAIL;
} else if (req->payload.block_no >= 5){ // emoney auth encrypted
sg_res_init(&res->res, &req->req, sizeof(res->block));
char* auth;
long size = wslurp(nfc->authdata_path, &auth, false);
if (size < 0){
sg_nfc_dprintf(nfc, "Failed to read %ls: %lx!\n", nfc->authdata_path, GetLastError());
return E_FAIL;
}
int offset = 0;
if (req->payload.block_no == 6){
offset = 16;
} else if (req->payload.block_no == 8){
offset = 32;
} else if (req->payload.block_no == 9){
offset = 48;
} else if (req->payload.block_no == 10){
offset = 64;
} else if (req->payload.block_no == 12){
offset = 82;
} else if (req->payload.block_no == 13){
offset = 98;
} else if (req->payload.block_no == 14){
offset = 114;
}
for (int i = 0; i < 16 && offset + i < size; i++){
res->block[i] = auth[offset + i];
}
free(auth);
} else if (req->payload.block_no == 4){ // emoney auth plain
sg_res_init(&res->res, &req->req, sizeof(res->block));
res->block[0] = 0x54; // header
res->block[1] = 0x43;
res->block[2] = nfc->proxy_flag; // 2 or 3 depending on game (useProxy in env.json)
res->block[3] = 0x01; // unknown flag
} else { // read all other blocks normally
sg_res_init(&res->res, &req->req, sizeof(res->block));
memcpy( res->block,
nfc->mifare.sectors[0].blocks[req->payload.block_no].bytes,
sizeof(res->block));
}
sg_res_init(&res->res, &req->req, sizeof(res->block));
memcpy( res->block,
nfc->mifare.sectors[0].blocks[req->payload.block_no].bytes,
sizeof(res->block));
return S_OK;
}

View File

@ -23,8 +23,10 @@ struct sg_nfc {
void *ops_ctx;
uint8_t addr;
unsigned int gen;
unsigned int proxy_flag;
struct felica felica;
struct mifare mifare;
const wchar_t* authdata_path;
};
void sg_nfc_init(
@ -32,6 +34,8 @@ void sg_nfc_init(
uint8_t addr,
const struct sg_nfc_ops *ops,
unsigned int gen,
unsigned int proxy_flag,
const wchar_t* authdata_path,
void *ops_ctx);
void sg_nfc_transact(

View File

@ -76,7 +76,7 @@ HRESULT sg_reader_hook_init(
return E_INVALIDARG;
}
sg_nfc_init(&sg_reader_nfc, 0x00, &sg_reader_nfc_ops, gen, NULL);
sg_nfc_init(&sg_reader_nfc, 0x00, &sg_reader_nfc_ops, gen, cfg->proxy_flag, cfg->authdata_path, NULL);
sg_led_init(&sg_reader_led, 0x08, &sg_reader_led_ops, gen, NULL);
InitializeCriticalSection(&sg_reader_lock);

View File

@ -11,6 +11,8 @@ struct aime_config {
bool enable;
bool high_baudrate;
unsigned int gen;
unsigned int proxy_flag;
wchar_t authdata_path[MAX_PATH];
};
HRESULT sg_reader_hook_init(

View File

@ -84,6 +84,18 @@ emulates an IC card in its proximity. A variety of different IC cards can be
emulated; the exact choice of card that is emulated depends on the presence or
absence of the configured card ID files.
### `proxyFlag`
Default: `2`
The "proxy flag" of the emulated Thinca authentication card. This should be 2 if no proxy is used, and 3 if it is. Invalid values will break Thinca authentication card reading. This information can be obtained by checking for the presence of "use_proxy: true" `tfps-res-pro\env.json`.
### `authdataPath`
Default: `DEVICE\authdata.bin`
Path to the binary file containing data for a Thinca authentication card (see `emoney.txt`)
## `[vfd]`
Controls emulation of the VFD GP1232A02A FUTABA assembly.

185
doc/emoney.txt Normal file
View File

@ -0,0 +1,185 @@
E-Money Authentication Procedure
by Haruka Akechi
SETTING UP:
1) Open Windows Powershell and execute these commands:
New-Item 'HKLM:\Software\TFPaymentService' -Force | Out-Null
$acl = Get-Acl HKLM:\SOFTWARE\TFPaymentService
$rule = New-Object System.Security.AccessControl.RegistryAccessRule($env:username, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl.SetAccessRule($rule)
$acl | Set-Acl -Path HKLM:\SOFTWARE\TFPaymentService
2) Obtain the 64 byte long authentication card encryption key and the 32 byte long static authentication card ID. amdaemon.exe holds the secrets.
3) Get this java file, insert the ID and key, probably edit the passphrase and compile+run to generate authcard.bin: https://gist.github.com/akechi-haruka/a506184638e695a04eabe8cb53f62c36
4) Place authcard.bin in your DEVICE folder.
5) Check tfps-res-pro\env.json for your game. If it contains a "use_proxy: true" statement, add "proxy_flag=3" under [aime]
6) Replace the two URLs in tfps-res-pro\resource.xml to your servers'. This is to ensure the Host header will match the certificate's.
7) In segatools.ini add following
[epay]
enable=0
8) Where amdaemon.exe is located, there should be a "ca.pem". Replace this file with either this (https://curl.se/ca/cacert.pem) for the most common CA's (including Let's Encrypt), or whatever CA the server is using.
9) Run your game and enter the test menu, and navigate to E-Money Settings.
10) Select "Terminal Authentication"
11) Hold your key for scanning a card (default: RETURN)
12) If your shop name shows up, everything was done succesfully. Otherwise, check the VFD.
TECHNICAL INFO:
For debugging anything e-money related, I highly recommend setting "emoney.log.level" to 4 in your game's amdaemon config.json. This should create a <appdata>\<gameid>\emoney_log\thincapayment.log.
When terminal authentication is started from the test menu, the game will check for an Aime reader of at least generation 3. If that is not fulfilled, the VFD will display "unsupported card reader" and abort. If the card reader is good, the VFD will prompt for a card to be touched, and the reader will start scanning for a MIFARE card, which from this point we call "Thinca Authentication Card". This card contains one unencrypted block (3) which contains:
[0] = 0x54 // T
[1] = 0x43 // C
[2] = proxy_type
[3] = 0x01
Afterwards, a number of encrypted blocks are read, namely the blocks 5,6,8,9,10,12,13 and 14. These blocks together form a 130 byte long binary blob that contains the authentication data.
This data is encrypted as following:
Given a fixed 0x40 byte long encryption key and a fixed 0x20 byte long static "card ID", both of which can be found in amdaemon:
XOR every byte of the encryption key with 0x1C.
Calculate a 0x20 bytes long HMAC-SHA-256 of the card ID with the XOR'ed encryption key as the key.
Calculate the needed IV by XORing the lower 0x10 bytes of the HMAC with the upper 0x10 bytes of the HMAC:
byte[] iv = new byte[16];
for (int i = 0; i < 16; i++) {
iv[i] = (byte) (hmac[i + 16] ^ hmac[i]);
}
With this IV, and the HMAC as the key, finally encrypt the data with AES/CBC/PKCS5Padding.
Now what is actually stored on such a card? This:
+---------------+---------------+-----------------+------------+----------+
| Store Card ID | Merchant Code | Store Branch ID | Passphrase | NULL |
+---------------+---------------+-----------------+------------+----------+
| 0x10 bytes | 0x14 bytes | 0xC bytes | 0x10 bytes | 0x1 byte |
| char* | char* | uint128_t | char* | NULL |
+---------------+---------------+-----------------+------------+----------+
Only two things really matter here. The Store Branch ID must be non-zero, otherwise amdaemon will reject it, and the passphrase, which is the PFX key password for the certificate returned in the network response (see below).
Technically with the Store Card ID you could bind different auth cards to different users, but for home usage, it really doesn't matter.
That's the Thinca Authentication Card out of the way, so continue on to:
NETWORK:
First, a regular HTTP(S) connection will be made to the URL specified in tfps-res-pro\env.json, tasms.root_endpoint.
Request Data:
{"modelName":"ACA","serialNumber":"ACAE01A9999","merchantCode":"NOTSEGA","storeBranchNumber":11111,"storeCardId":"FAKESTORE"}
Note that the serialNumber here actually isn't the keychip ID, but rather the PCBID. The three other values are read from the Thinca Authentication Card.
Response Headers:
x-certificate-md5: <md5 of the certificate string>
Response Data:
{
"certificate": "<base 64 encoded pfx string, with the authentication card's passphrase being the pfx password>",
"initSettings": {
"endpoints": {
"terminals": {
"uri": "https://localhost/emoney/terminals"
},
"statuses": {
"uri": "https://localhost/emoney/statuses"
},
"sales": {
"uri": "https://localhost/emoney/sales"
},
"counters": {
"uri": "https://localhost/emoney/counters"
}
},
"intervals": {
"checkSetting": 60,
"sendStatus": 60
},
"settigsType": "AmusementTerminalSettings", // sic
"status": "1", // a string
"terminalId": "536453645364536453645364536453", // must be exactly 30 characters
"version": "2024-01-01T01:01:01", // a timestamp
"availableElectronicMoney": [
1,
2,
3,
5,
6,
8,
9,
91, // aimepay
101 // "cash" ???
],
"cashAvailability": true,
"productCode": 1337
}
}
Next up, we will connect to the TLAM service to get the URL for the TCAP service. Everything from this point on requires not only HTTPS, but also client certificate validation (which is the certificate returned from the previous request). Technically you don't need to validate it, but you must accept a client certificate or the client HTTPS library will not be happy.
The client certificate itself must be signed with the same key than the server's HTTPS certificate.
The client AND server certificate must have it's CA included in the "ca.pem" file in amdaemon's directory. You can freely replace this file with this https://curl.se/ca/cacert.pem to allow Let's Encrypt and whatever else.
At this point, "ThincaPayment::setClientCertificate(). ErrCode 203" means that the downloaded file couldn't be found.
[Warn ] TCAP communicate error 06514086 means the ca.pem has no entry for the given server CA.
TLAM:
The TLAM url comes from tftp-res-pro\resource.xml, commonPrimaryUri.
<commonPrimaryUri>/initauth.jsp
Request Data:
<none>
Response Headers:
Content-Type: application/x-tlam
Response Data:
SERV=https://localhost/emoney/tcap
TCAP:
Here be dragons.
Is it this?? https://en.wikipedia.org/wiki/Transaction_Capabilities_Application_Part
Maybe thincatcapclient.dll holds the secrets?
Request Data:
02 05 01 00 ba 00 00 00 21 00 00 00 00 00 25 00 ....º... !.....%.
9f 00 01 00 00 07 47 65 6e 65 72 69 63 06 43 4c ......Ge neric.CL
49 45 4e 54 00 02 00 00 07 47 65 6e 65 72 69 63 IENT.... .Generic
06 53 54 41 54 55 53 00 03 00 00 07 47 65 6e 65 .STATUS. ....Gene
72 69 63 06 4f 50 54 49 4f 4e 00 04 00 00 06 46 ric.OPTI ON.....F
65 6c 69 43 61 03 52 2f 57 00 05 00 00 07 47 65 eliCa.R/ W.....Ge
6e 65 72 69 63 09 52 2f 57 5f 45 56 45 4e 54 00 neric.R/ W_EVENT.
06 00 00 07 47 65 6e 65 72 69 63 0a 52 2f 57 5f ....Gene ric.R/W_
53 54 41 54 55 53 00 07 00 00 07 47 65 6e 65 72 STATUS.. ...Gener
69 63 0a 52 2f 57 5f 4f 50 54 49 4f 4e 00 08 00 ic.R/W_O PTION...
00 07 47 65 6e 65 72 69 63 06 4e 46 43 5f 52 57 ..Generi c.NFC_RW
00 00 00 26 00 03 02 05 00 00 00 00 22 00 00 ...&.... ...."..
and
02 05 03 00 17 00 00 00 21 00 11 34 34 20 30 30 ........ !..44 00
20 30 31 20 32 31 20 30 30 20 30 30 01 21 0 0 00
Response Headers:
Content-Type: application/x-tcap
Transfer-Encoding: chunked
Response Data:
????
To be continued ...

View File

@ -19,6 +19,8 @@ util_lib = static_library(
'dump.h',
'lib.c',
'lib.h',
'slurp.c',
'slurp.h',
'str.c',
'str.h',
],

64
util/slurp.c Normal file
View File

@ -0,0 +1,64 @@
/*
* 'slurp' reads the file identified by 'path' into a character buffer
* pointed at by 'buf', optionally adding a terminating NUL if
* 'add_nul' is true. On success, the size of the file is returned; on
* failure, -1 is returned and ERRNO is set by the underlying system
* or library call that failed.
*
* WARNING: 'slurp' malloc()s memory to '*buf' which must be freed by
* the caller.
*/
long wslurp(const wchar_t* path, char **buf, bool add_nul)
{
FILE *fp;
size_t fsz;
long off_end;
int rc;
/* Open the file */
fp = _wfopen(path, L"rb");
if( NULL == fp ) {
return -1L;
}
/* Seek to the end of the file */
rc = fseek(fp, 0L, SEEK_END);
if( 0 != rc ) {
return -1L;
}
/* Byte offset to the end of the file (size) */
if( 0 > (off_end = ftell(fp)) ) {
return -1L;
}
fsz = (size_t)off_end;
/* Allocate a buffer to hold the whole file */
*buf = malloc( fsz+(int)add_nul );
if( NULL == *buf ) {
return -1L;
}
/* Rewind file pointer to start of file */
rewind(fp);
/* Slurp file into buffer */
if( fsz != fread(*buf, 1, fsz, fp) ) {
free(*buf);
return -1L;
}
/* Close the file */
if( EOF == fclose(fp) ) {
free(*buf);
return -1L;
}
if( add_nul ) {
/* Make sure the buffer is NUL-terminated, just in case */
buf[fsz] = '\0';
}
/* Return the file size */
return (long)fsz;
}

11
util/slurp.h Normal file
View File

@ -0,0 +1,11 @@
/*
* 'slurp' reads the file identified by 'path' into a character buffer
* pointed at by 'buf', optionally adding a terminating NUL if
* 'add_nul' is true. On success, the size of the file is returned; on
* failure, -1 is returned and ERRNO is set by the underlying system
* or library call that failed.
*
* WARNING: 'slurp' malloc()s memory to '*buf' which must be freed by
* the caller.
*/
long wslurp(const wchar_t* path, char **buf, bool add_nul);