imsd/tests/Aka/main.cpp

446 lines
29 KiB
C++
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Imsd:Aka unit tests — the AKAv1-MD5 digest against a vector cross-computed
// with the Python reference, identity assembly, the qmicli/mmcli text
// parsers (fed real device output), and the AUTHENTICATE APDU build/parse.
aka: the ISIM application of a card, its identity files and the identity they hold An ISIM holds the parameters a registration starts from (TS 24.229 5.1.1.1A): the private identity in EF_IMPI, the home domain in EF_DOMAIN and public identities in EF_IMPU (TS 31.103 4.2.2 to 4.2.4), of which the first record's is the one to register (GSMA IR.92 2.2.1). TS 23.003 has a UE derive its home domain and a temporary public identity from the IMSI "if there is no ISIM application" (13.2, 13.4B); those derived names are the only ones imsd has. This is the part of reading an ISIM that has no I/O; the daemon does not use it yet. ParseIsimApp finds the ISIM application of one slot in qmicli's card status, in whatever state the modem lists it. On the two cards with an ISIM that a read-only probe read (a US card, 2026-11-23, and a Norwegian one, 2026-11-21) the Fairphone 6's modem lists the ISIM `detected` beside the `ready` USIM, and on both a logical channel opened on it and its identity files were read. The commands are the ones that probe sent: SELECT by file identifier, READ BINARY, READ RECORD (TS 102 221 11.1.1, 11.1.3, 11.1.5). ApduAnswer is a card's answer, '90 00', '91 XX' and '92 XX' its normal endings (10.2.1.1), and ApduFollowUp the command it asks for next when the answer brought no data: GET RESPONSE for '61 XX', a command without data again with the length named for '6C XX' (7.3.1.1.5); an answer with data is taken for the command's last. HoldsRead takes a read's data under the warning '62 82' as well (10.2.1.3). ParseIsimValue takes the value of the data object with tag '80' that a file begins with, cuts 'FF' bytes from the end of its length as the probe does, and gives none for a value with a blank or a byte that is not printable ASCII. IsimIdentity checks the three values before they replace the derived names: they are written into a quoted string and between the angle brackets of SIP header fields. It refuses a value with a quote, a backslash, an angle bracket, a blank, a line break or another byte outside printable ASCII, a private or a public identity of more than 255 characters, a private identity without an @ between two parts, a domain that is not labels of letters, digits and inner hyphens, 63 characters a label and 253 in all at most, and a public identity that does not begin sip: or is that alone. The two readers of the card status share how they read a line; ParseCardStatus gives what it gave. Tested: tests/Aka (the application out of a card status with the US card's two applications, in either order, and out of a second slot with the Norwegian card's AID; the commands, the answers and their follow-ups, the values, the identity and what is refused) and tests/Messages (the two REGISTERs of an ISIM's identity against the pinned ones).
2026-11-23 19:32:58 +01:00
// Then the ISIM (since 1.2.3): the application of the slot in use out of a
// card status, the commands its files are read with, a card's answer and
// the command it asks for next, the value an identity file holds, and the
// check of the three values before they are an identity.
import std;
import Imsd;
using namespace imsd::aka;
namespace {
int Failures = 0;
void Check(bool cond, std::string_view msg) {
if (!cond) {
std::println(std::cerr, "FAIL: {}", msg);
++Failures;
}
}
// `qmicli --uim-get-card-status` in the shape a real FP6 eSIM (slot 2)
// produces.
constexpr std::string_view CardStatus =
"[qrtr://0] Successfully got card status\n"
"Provisioning applications:\n"
"\tPrimary GW: slot '2', application '1'\n"
"Slot [1]:\n"
"\tCard state: 'error: no-atr-received (3)'\n"
"\tUPIN state: 'not-initialized'\n"
"Slot [2]:\n"
"\tCard state: 'present'\n"
"\tApplication [1]:\n"
"\t\tApplication type: 'usim (2)'\n"
"\t\tApplication state: 'ready'\n"
"\t\tApplication ID:\n"
"\t\t\tA0:00:00:00:87:10:02:FF:31:F0:04:89:0E:00:00:01\n"
"\t\tPIN1 state: 'disabled'\n";
aka: the ISIM application of a card, its identity files and the identity they hold An ISIM holds the parameters a registration starts from (TS 24.229 5.1.1.1A): the private identity in EF_IMPI, the home domain in EF_DOMAIN and public identities in EF_IMPU (TS 31.103 4.2.2 to 4.2.4), of which the first record's is the one to register (GSMA IR.92 2.2.1). TS 23.003 has a UE derive its home domain and a temporary public identity from the IMSI "if there is no ISIM application" (13.2, 13.4B); those derived names are the only ones imsd has. This is the part of reading an ISIM that has no I/O; the daemon does not use it yet. ParseIsimApp finds the ISIM application of one slot in qmicli's card status, in whatever state the modem lists it. On the two cards with an ISIM that a read-only probe read (a US card, 2026-11-23, and a Norwegian one, 2026-11-21) the Fairphone 6's modem lists the ISIM `detected` beside the `ready` USIM, and on both a logical channel opened on it and its identity files were read. The commands are the ones that probe sent: SELECT by file identifier, READ BINARY, READ RECORD (TS 102 221 11.1.1, 11.1.3, 11.1.5). ApduAnswer is a card's answer, '90 00', '91 XX' and '92 XX' its normal endings (10.2.1.1), and ApduFollowUp the command it asks for next when the answer brought no data: GET RESPONSE for '61 XX', a command without data again with the length named for '6C XX' (7.3.1.1.5); an answer with data is taken for the command's last. HoldsRead takes a read's data under the warning '62 82' as well (10.2.1.3). ParseIsimValue takes the value of the data object with tag '80' that a file begins with, cuts 'FF' bytes from the end of its length as the probe does, and gives none for a value with a blank or a byte that is not printable ASCII. IsimIdentity checks the three values before they replace the derived names: they are written into a quoted string and between the angle brackets of SIP header fields. It refuses a value with a quote, a backslash, an angle bracket, a blank, a line break or another byte outside printable ASCII, a private or a public identity of more than 255 characters, a private identity without an @ between two parts, a domain that is not labels of letters, digits and inner hyphens, 63 characters a label and 253 in all at most, and a public identity that does not begin sip: or is that alone. The two readers of the card status share how they read a line; ParseCardStatus gives what it gave. Tested: tests/Aka (the application out of a card status with the US card's two applications, in either order, and out of a second slot with the Norwegian card's AID; the commands, the answers and their follow-ups, the values, the identity and what is refused) and tests/Messages (the two REGISTERs of an ISIM's identity against the pinned ones).
2026-11-23 19:32:58 +01:00
// A card with a USIM and an ISIM in one slot. The two applications,
// their states and their AIDs are those the fp6 journal's probe read off
// a reporter's card on 2026-11-23 (the modem lists the ISIM `detected`
// beside the `ready` USIM); the lines are the ones qmicli prints for an
// application, as in the fixture above: the probe prints its own listing
// of a card, not qmicli's.
constexpr std::string_view CardStatusIsim =
"[qrtr://0] Successfully got card status\n"
"Provisioning applications:\n"
"\tPrimary GW: slot '1', application '1'\n"
"Slot [1]:\n"
"\tCard state: 'present'\n"
"\tUPIN state: 'not-initialized'\n"
"\tApplication [1]:\n"
"\t\tApplication type: 'usim (2)'\n"
"\t\tApplication state: 'ready'\n"
"\t\tApplication ID:\n"
"\t\t\tA0:00:00:00:87:10:02:FF:FF:FF:FF:89:03:02:00:00\n"
"\t\tPIN1 state: 'disabled'\n"
"\tApplication [2]:\n"
"\t\tApplication type: 'isim (5)'\n"
"\t\tApplication state: 'detected'\n"
"\t\tApplication ID:\n"
"\t\t\tA0:00:00:00:87:10:04:FF:FF:FF:FF:89:03:02:00:00\n"
"\t\tPIN1 state: 'disabled'\n"
"Slot [2]:\n"
"\tCard state: 'absent'\n";
// The same two applications with the ISIM listed first, and a second
// card in slot 2 that has an ISIM of its own (its AID the twelve bytes
// of the other card with an ISIM the probe read, 2026-11-21) beside a USIM
// that is not ready.
constexpr std::string_view CardStatusIsimFirst =
"Slot [1]:\n"
"\tCard state: 'present'\n"
"\tApplication [1]:\n"
"\t\tApplication type: 'isim (5)'\n"
"\t\tApplication state: 'detected'\n"
"\t\tApplication ID:\n"
"\t\t\tA0:00:00:00:87:10:04:FF:FF:FF:FF:89:03:02:00:00\n"
"\tApplication [2]:\n"
"\t\tApplication type: 'usim (2)'\n"
"\t\tApplication state: 'ready'\n"
"\t\tApplication ID:\n"
"\t\t\tA0:00:00:00:87:10:02:FF:FF:FF:FF:89:03:02:00:00\n"
"Slot [2]:\n"
"\tCard state: 'present'\n"
"\tApplication [1]:\n"
"\t\tApplication type: 'usim (2)'\n"
"\t\tApplication state: 'detected'\n"
"\t\tApplication ID:\n"
"\t\t\tA0:00:00:00:87:10:02:FF:49:FF:05:89\n"
"\tApplication [2]:\n"
"\t\tApplication type: 'isim (5)'\n"
"\t\tApplication state: 'ready'\n"
"\t\tApplication ID:\n"
"\t\t\ta0:00:00:00:87:10:04:ff:49:ff:05:89\n"
"\tApplication [3]:\n"
"\t\tApplication type: 'isim (5)'\n"
"\t\tApplication state: 'detected'\n"
"\t\tApplication ID:\n"
"\t\t\tA0:00:00:00:87:10:04:FF:49:FF:05:99\n";
constexpr std::string_view MmcliSim =
" Properties | active: yes\n"
" | imsi: 001010123456789\n"
" | iccid: 8900101234567890123\n"
" | operator id: 00101\n"
" | operator name: Test PLMN 1-1\n";
}
int main() {
// ---- AKAv1-MD5 digest: pinned vector (the algorithm was cross-checked
// against the Python reference; this vector is recomputed for the
// synthetic test identity)
{
std::array<std::uint8_t, 8> res{0xa1,0xb2,0xc3,0xd4,0xe5,0xf6,0x07,0x18};
std::string d = DigestAkav1("abc123nonce==", res, "cnonce0123456789", "sip:ims.mnc001.mcc001.3gppnetwork.org", "001010123456789@ims.mnc001.mcc001.3gppnetwork.org", "ims.mnc001.mcc001.3gppnetwork.org");
Check(d == "09fd8f8f7ef716c029666a7c6df1db1e", "AKAv1-MD5 digest matches pinned vector");
}
// ---- identity assembly
{
Identity id = MakeIdentity("001010123456789", "001", "01");
Check(id.mnc == "001", "MNC zero-padded to 3");
Check(id.domain == "ims.mnc001.mcc001.3gppnetwork.org", "domain");
Check(id.impi == "001010123456789@ims.mnc001.mcc001.3gppnetwork.org", "IMPI");
Check(id.impu == "sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org", "IMPU");
Check(id.regUri == "sip:ims.mnc001.mcc001.3gppnetwork.org", "reg URI");
}
// ---- card-status parse (real device output)
{
auto sel = ParseCardStatus(CardStatus);
Check(sel.has_value(), "card status yields a ready USIM");
Check(sel && sel->slot == 2, "slot 2 selected (slot 1 is error/no-atr)");
Check(sel && sel->aid == "A0000000871002FF31F004890E000001", "AID uppercased, colons stripped");
}
Check(!ParseCardStatus("Slot [1]:\n\tCard state: 'absent'\n").has_value(), "no ready USIM -> nullopt");
aka: the ISIM application of a card, its identity files and the identity they hold An ISIM holds the parameters a registration starts from (TS 24.229 5.1.1.1A): the private identity in EF_IMPI, the home domain in EF_DOMAIN and public identities in EF_IMPU (TS 31.103 4.2.2 to 4.2.4), of which the first record's is the one to register (GSMA IR.92 2.2.1). TS 23.003 has a UE derive its home domain and a temporary public identity from the IMSI "if there is no ISIM application" (13.2, 13.4B); those derived names are the only ones imsd has. This is the part of reading an ISIM that has no I/O; the daemon does not use it yet. ParseIsimApp finds the ISIM application of one slot in qmicli's card status, in whatever state the modem lists it. On the two cards with an ISIM that a read-only probe read (a US card, 2026-11-23, and a Norwegian one, 2026-11-21) the Fairphone 6's modem lists the ISIM `detected` beside the `ready` USIM, and on both a logical channel opened on it and its identity files were read. The commands are the ones that probe sent: SELECT by file identifier, READ BINARY, READ RECORD (TS 102 221 11.1.1, 11.1.3, 11.1.5). ApduAnswer is a card's answer, '90 00', '91 XX' and '92 XX' its normal endings (10.2.1.1), and ApduFollowUp the command it asks for next when the answer brought no data: GET RESPONSE for '61 XX', a command without data again with the length named for '6C XX' (7.3.1.1.5); an answer with data is taken for the command's last. HoldsRead takes a read's data under the warning '62 82' as well (10.2.1.3). ParseIsimValue takes the value of the data object with tag '80' that a file begins with, cuts 'FF' bytes from the end of its length as the probe does, and gives none for a value with a blank or a byte that is not printable ASCII. IsimIdentity checks the three values before they replace the derived names: they are written into a quoted string and between the angle brackets of SIP header fields. It refuses a value with a quote, a backslash, an angle bracket, a blank, a line break or another byte outside printable ASCII, a private or a public identity of more than 255 characters, a private identity without an @ between two parts, a domain that is not labels of letters, digits and inner hyphens, 63 characters a label and 253 in all at most, and a public identity that does not begin sip: or is that alone. The two readers of the card status share how they read a line; ParseCardStatus gives what it gave. Tested: tests/Aka (the application out of a card status with the US card's two applications, in either order, and out of a second slot with the Norwegian card's AID; the commands, the answers and their follow-ups, the values, the identity and what is refused) and tests/Messages (the two REGISTERs of an ISIM's identity against the pinned ones).
2026-11-23 19:32:58 +01:00
{
auto odd = ParseCardStatus("Slot [2]:\n\tCard state: 'absent'\nSlot [x]:\n\tApplication [1]:\n\t\tApplication type: 'usim (2)'\n\t\tApplication state: 'ready'\n\t\tApplication ID:\n\t\t\tA0:00:00:00:87:10:02:FF\n");
Check(odd && odd->slot == 0, "a slot line whose number does not parse is slot 0, not the slot before it");
}
// ---- mmcli parsers
Check(ParsePrimarySimPath(" SIM | primary sim path: /org/freedesktop/ModemManager1/SIM/0\n") == "/org/freedesktop/ModemManager1/SIM/0", "primary sim path");
{
auto si = ParseSimInfo(MmcliSim);
Check(si.has_value(), "sim info parses");
Check(si && si->imsi == "001010123456789", "imsi");
Check(si && si->mcc == "001" && si->mnc == "01", "mcc/mnc split");
// full pipeline: parsed info -> identity
Identity id = MakeIdentity(si->imsi, si->mcc, si->mnc);
Check(id.domain == "ims.mnc001.mcc001.3gppnetwork.org", "info->identity domain");
}
Check(ParseEquipmentId(" Hardware | equipment id: 359999990000001\n") == "359999990000001", "equipment id");
Check(ImeiUrn("359999990000001") == "urn:gsma:imei:35999999-000000-1", "IMEI -> RFC 7254 URN");
Check(!ImeiUrn("35999999000000").has_value(), "14-digit IMEI rejected");
Check(!ImeiUrn("35999999000000x").has_value(), "non-digit IMEI rejected");
// ---- AUTHENTICATE APDU build + response parse
{
std::vector<std::uint8_t> rnd(16, 0x11), autn(16, 0x22);
std::string apdu = BuildAkaApdu(0x01, rnd, autn);
// CLA=01 INS=88 P1=00 P2=81 Lc=22(34) 10 <rand> 10 <autn>
Check(apdu.starts_with("0188008122"), "APDU header CLA/INS/P1/P2/Lc");
Check(apdu == "0188008122" "10" "11111111111111111111111111111111" "10" "22222222222222222222222222222222", "APDU byte-exact");
Check(BuildGetResponseApdu(0x01, 0x2E) == "01c000002e", "GET RESPONSE APDU");
}
{
// the class byte (TS 102 221 10.1.1, tables 10.3 and 10.4a): the
// channel itself for 0 to 3, '4X' with the channel less four for 4
// to 19
Check(ClassByte(0) == 0x00 && ClassByte(1) == 0x01 && ClassByte(3) == 0x03, "class byte of a standard channel is the channel");
Check(ClassByte(4) == 0x40, "class byte of channel 4 is 40");
Check(ClassByte(5) == 0x41, "class byte of channel 5 is 41");
Check(ClassByte(19) == 0x4F, "class byte of channel 19 is 4f");
std::vector<std::uint8_t> rnd(16, 0x11), autn(16, 0x22);
Check(BuildAkaApdu(3, rnd, autn).starts_with("0388008122"), "AUTHENTICATE on channel 3 starts 03");
Check(BuildAkaApdu(4, rnd, autn).starts_with("4088008122"), "AUTHENTICATE on channel 4 starts 40, not 04");
Check(BuildAkaApdu(4, rnd, autn).substr(2) == BuildAkaApdu(1, rnd, autn).substr(2), "the channel changes the class byte and nothing else");
Check(BuildGetResponseApdu(7, 0x35) == "43c0000035", "GET RESPONSE on channel 7 starts 43");
}
{
// DB | 08 RES | 10 CK | 10 IK | SW1 SW2(9000)
std::vector<std::uint8_t> body = {0xDB, 0x08};
for (int i = 0; i < 8; i++) body.push_back(0xA0 + i); // RES
body.push_back(0x10);
for (int i = 0; i < 16; i++) body.push_back(0xC0 + i); // CK
body.push_back(0x10);
for (int i = 0; i < 16; i++) body.push_back(0xD0 + i); // IK
body.push_back(0x90); body.push_back(0x00); // SW
auto r = ParseAkaResponse(body);
Check(r.has_value(), "AKA response parses");
Check(r && r->res.size() == 8 && r->res[0] == 0xA0, "RES extracted");
Check(r && r->ck.size() == 16 && r->ck[0] == 0xC0, "CK extracted");
Check(r && r->ik.size() == 16 && r->ik[0] == 0xD0, "IK extracted");
aka: the ISIM application of a card, its identity files and the identity they hold An ISIM holds the parameters a registration starts from (TS 24.229 5.1.1.1A): the private identity in EF_IMPI, the home domain in EF_DOMAIN and public identities in EF_IMPU (TS 31.103 4.2.2 to 4.2.4), of which the first record's is the one to register (GSMA IR.92 2.2.1). TS 23.003 has a UE derive its home domain and a temporary public identity from the IMSI "if there is no ISIM application" (13.2, 13.4B); those derived names are the only ones imsd has. This is the part of reading an ISIM that has no I/O; the daemon does not use it yet. ParseIsimApp finds the ISIM application of one slot in qmicli's card status, in whatever state the modem lists it. On the two cards with an ISIM that a read-only probe read (a US card, 2026-11-23, and a Norwegian one, 2026-11-21) the Fairphone 6's modem lists the ISIM `detected` beside the `ready` USIM, and on both a logical channel opened on it and its identity files were read. The commands are the ones that probe sent: SELECT by file identifier, READ BINARY, READ RECORD (TS 102 221 11.1.1, 11.1.3, 11.1.5). ApduAnswer is a card's answer, '90 00', '91 XX' and '92 XX' its normal endings (10.2.1.1), and ApduFollowUp the command it asks for next when the answer brought no data: GET RESPONSE for '61 XX', a command without data again with the length named for '6C XX' (7.3.1.1.5); an answer with data is taken for the command's last. HoldsRead takes a read's data under the warning '62 82' as well (10.2.1.3). ParseIsimValue takes the value of the data object with tag '80' that a file begins with, cuts 'FF' bytes from the end of its length as the probe does, and gives none for a value with a blank or a byte that is not printable ASCII. IsimIdentity checks the three values before they replace the derived names: they are written into a quoted string and between the angle brackets of SIP header fields. It refuses a value with a quote, a backslash, an angle bracket, a blank, a line break or another byte outside printable ASCII, a private or a public identity of more than 255 characters, a private identity without an @ between two parts, a domain that is not labels of letters, digits and inner hyphens, 63 characters a label and 253 in all at most, and a public identity that does not begin sip: or is that alone. The two readers of the card status share how they read a line; ParseCardStatus gives what it gave. Tested: tests/Aka (the application out of a card status with the US card's two applications, in either order, and out of a second slot with the Norwegian card's AID; the commands, the answers and their follow-ups, the values, the identity and what is refused) and tests/Messages (the two REGISTERs of an ISIM's identity against the pinned ones).
2026-11-23 19:32:58 +01:00
// the same answer parted into its data and its status word
auto parted = ParseApduAnswer(body);
auto r2 = parted ? ParseAkaResponse(*parted) : std::nullopt;
Check(r2 && r2->res == r->res && r2->ck == r->ck && r2->ik == r->ik, "an ApduAnswer's AKA result is the raw answer's");
std::vector<std::uint8_t> refused = {0x69, 0x82};
auto no = ParseApduAnswer(refused);
Check(no && !ParseAkaResponse(*no).has_value(), "a status word alone is no AKA result");
}
{
// sync-failure tag (0xDC) or truncated body -> nullopt
std::vector<std::uint8_t> dc = {0xDC, 0x0E, 0x90, 0x00};
Check(!ParseAkaResponse(dc).has_value(), "non-DB tag rejected");
std::vector<std::uint8_t> tr = {0xDB, 0x08, 0x00, 0x90, 0x00};
Check(!ParseAkaResponse(tr).has_value(), "length past end rejected");
}
{
// P-Access-Network-Info from `mmcli --location-get` (real-device
// output shape; TAC/CI zero-padded wider than the 4/7 hex digits
// PANI uses). Values here are synthetic.
constexpr std::string_view Loc =
" --------------------------\n"
" 3GPP | operator mcc: 001\n"
" | operator mnc: 01\n"
" | location area code: 0000\n"
" | tracking area code: 001234\n"
" | cell id: 00ABCDE0\n";
auto p = ParsePani(Loc);
Check(p.has_value(), "PANI parses");
Check(p == "3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0", "PANI concatenation mcc+mnc+TAC+ECI");
Check(!ParsePani("no location here").has_value(), "PANI absent fields rejected");
constexpr std::string_view Zeros =
" 3GPP | operator mcc: 001\n"
" | operator mnc: 01\n"
" | tracking area code: 000000\n"
" | cell id: 00000000\n";
Check(!ParsePani(Zeros).has_value(), "PANI all-zero cell rejected");
}
aka: the ISIM application of a card, its identity files and the identity they hold An ISIM holds the parameters a registration starts from (TS 24.229 5.1.1.1A): the private identity in EF_IMPI, the home domain in EF_DOMAIN and public identities in EF_IMPU (TS 31.103 4.2.2 to 4.2.4), of which the first record's is the one to register (GSMA IR.92 2.2.1). TS 23.003 has a UE derive its home domain and a temporary public identity from the IMSI "if there is no ISIM application" (13.2, 13.4B); those derived names are the only ones imsd has. This is the part of reading an ISIM that has no I/O; the daemon does not use it yet. ParseIsimApp finds the ISIM application of one slot in qmicli's card status, in whatever state the modem lists it. On the two cards with an ISIM that a read-only probe read (a US card, 2026-11-23, and a Norwegian one, 2026-11-21) the Fairphone 6's modem lists the ISIM `detected` beside the `ready` USIM, and on both a logical channel opened on it and its identity files were read. The commands are the ones that probe sent: SELECT by file identifier, READ BINARY, READ RECORD (TS 102 221 11.1.1, 11.1.3, 11.1.5). ApduAnswer is a card's answer, '90 00', '91 XX' and '92 XX' its normal endings (10.2.1.1), and ApduFollowUp the command it asks for next when the answer brought no data: GET RESPONSE for '61 XX', a command without data again with the length named for '6C XX' (7.3.1.1.5); an answer with data is taken for the command's last. HoldsRead takes a read's data under the warning '62 82' as well (10.2.1.3). ParseIsimValue takes the value of the data object with tag '80' that a file begins with, cuts 'FF' bytes from the end of its length as the probe does, and gives none for a value with a blank or a byte that is not printable ASCII. IsimIdentity checks the three values before they replace the derived names: they are written into a quoted string and between the angle brackets of SIP header fields. It refuses a value with a quote, a backslash, an angle bracket, a blank, a line break or another byte outside printable ASCII, a private or a public identity of more than 255 characters, a private identity without an @ between two parts, a domain that is not labels of letters, digits and inner hyphens, 63 characters a label and 253 in all at most, and a public identity that does not begin sip: or is that alone. The two readers of the card status share how they read a line; ParseCardStatus gives what it gave. Tested: tests/Aka (the application out of a card status with the US card's two applications, in either order, and out of a second slot with the Norwegian card's AID; the commands, the answers and their follow-ups, the values, the identity and what is refused) and tests/Messages (the two REGISTERs of an ISIM's identity against the pinned ones).
2026-11-23 19:32:58 +01:00
// ---- the ISIM application of the slot in use
{
auto sel = ParseCardStatus(CardStatusIsim);
Check(sel && sel->slot == 1 && sel->aid == "A0000000871002FFFFFFFF8903020000", "the ready USIM beside an ISIM is still the USIM read");
auto isim = ParseIsimApp(CardStatusIsim, 1);
Check(isim.has_value(), "the ISIM of slot 1 is found");
Check(isim && isim->aid == "A0000000871004FFFFFFFF8903020000", "its AID, uppercase, colons stripped");
Check(isim && isim->state == "detected", "its state is the modem's word, detected");
Check(!ParseIsimApp(CardStatusIsim, 2).has_value(), "an absent card in the slot asked for: no ISIM");
Check(!ParseIsimApp(CardStatus, 2).has_value(), "a card with a USIM only: no ISIM");
Check(!ParseIsimApp(CardStatus, 1).has_value(), "a slot with no card: no ISIM");
Check(!ParseIsimApp("", 1).has_value(), "no card status: no ISIM");
}
{
auto sel = ParseCardStatus(CardStatusIsimFirst);
Check(sel && sel->slot == 1 && sel->aid == "A0000000871002FFFFFFFF8903020000", "the USIM listed after an ISIM is read");
auto first = ParseIsimApp(CardStatusIsimFirst, 1);
Check(first && first->aid == "A0000000871004FFFFFFFF8903020000" && first->state == "detected", "the ISIM listed before the USIM is found");
// slot 2's ISIM is not slot 1's: each slot's reader gets its own card's
auto other = ParseIsimApp(CardStatusIsimFirst, 2);
Check(other && other->aid == "A0000000871004FF49FF0589", "the other slot's ISIM for the other slot: a twelve-byte AID, lower case made upper");
Check(other && other->state == "ready", "its state, ready");
Check(first && other && first->aid != other->aid, "slot 1 is not answered with slot 2's ISIM");
Check(!ParseIsimApp(CardStatusIsimFirst, 3).has_value(), "a slot the status does not list: no ISIM");
}
{
// an application of another type keeps its AID to itself, and an
// ISIM whose state line is missing is found with no state
auto none = ParseIsimApp("Slot [1]:\n\tApplication [1]:\n\t\tApplication type: 'csim (4)'\n\t\tApplication state: 'detected'\n\t\tApplication ID:\n\t\t\tA0:00:00:03:43:10:02:FF\n", 1);
Check(!none.has_value(), "a CSIM is no ISIM");
auto bare = ParseIsimApp("Slot [1]:\n\tApplication [1]:\n\t\tApplication type: 'isim (5)'\n\t\tApplication ID:\n\t\t\tA0:00:00:00:87:10:04:FF\n", 1);
Check(bare && bare->aid == "A0000000871004FF" && bare->state.empty(), "an ISIM without a state line: found, the state empty");
// the state of the application before it is not taken for the ISIM's
auto after = ParseIsimApp("Slot [1]:\n\tApplication [1]:\n\t\tApplication type: 'usim (2)'\n\t\tApplication state: 'ready'\n\t\tApplication ID:\n\t\t\tA0:00:00:00:87:10:02:FF\n\tApplication [2]:\n\t\tApplication type: 'isim (5)'\n\t\tApplication ID:\n\t\t\tA0:00:00:00:87:10:04:FF\n", 1);
Check(after && after->state.empty(), "the USIM's state is not the ISIM's");
}
// ---- the commands a file of the selected application is read with
Check(EfImpi == 0x6F02 && EfDomain == 0x6F03 && EfImpu == 0x6F04, "the three identity files' identifiers (TS 31.103 4.2.2 to 4.2.4)");
Check(BuildSelectFileApdu(1, EfImpi) == "01a40004026f02", "SELECT EF_IMPI on channel 1");
Check(BuildSelectFileApdu(5, EfDomain) == "41a40004026f03", "SELECT EF_DOMAIN on channel 5: the extended class byte");
Check(BuildSelectFileApdu(2, 0x00AD) == "02a400040200ad", "a file identifier below 1000 keeps its four digits");
Check(BuildReadBinaryApdu(1) == "01b0000000", "READ BINARY from offset 0, Le 00");
Check(BuildReadBinaryApdu(4) == "40b0000000", "READ BINARY on channel 4");
Check(BuildReadRecordApdu(1, 1) == "01b2010400", "READ RECORD 1, absolute, Le 00");
Check(BuildReadRecordApdu(6, 2) == "42b2020400", "READ RECORD 2 on channel 6");
// ---- a card's answer, and the command it asks for next
{
std::vector<std::uint8_t> offer = {0x61, 0x19};
auto a = ParseApduAnswer(offer);
Check(a && a->data.empty() && a->sw1 == 0x61 && a->sw2 == 0x19, "61 19: no data, the status word");
Check(a && !a->Ok() && a->Status() == "6119", "61 19 is not 90 00; written 6119");
Check(a && Selected(*a), "a SELECT answered 61 XX has selected the file");
Check(a && ApduFollowUp(1, "01a40004026f02", *a) == "01c0000019", "61 19 asks for GET RESPONSE with Le 19");
Check(a && ApduFollowUp(5, "41b0000000", *a) == "41c0000019", "GET RESPONSE on the command's channel");
std::vector<std::uint8_t> data = {0x80, 0x03, 'a', '.', 'b', 0x90, 0x00};
auto d = ParseApduAnswer(data);
Check(d && d->data.size() == 5 && d->data[0] == 0x80 && d->data[4] == 'b', "data before 90 00 is the answer's data");
Check(d && d->Ok() && d->Status() == "9000" && Selected(*d), "90 00 is Ok");
Check(d && !ApduFollowUp(1, "01b0000000", *d).has_value(), "90 00 with data asks for nothing");
std::vector<std::uint8_t> len = {0x6C, 0x23};
auto l = ParseApduAnswer(len);
Check(l && ApduFollowUp(1, "01b0000000", *l) == "01b0000023", "6C 23 to READ BINARY: the command again with Le 23");
Check(l && ApduFollowUp(4, "40b2010400", *l) == "40b2010423", "6C 23 to READ RECORD: the command again with Le 23");
Check(l && !ApduFollowUp(1, "0188008122" "10" "11111111111111111111111111111111" "10" "22222222222222222222222222222222", *l).has_value(), "6C XX to a command that carries data: nothing to send again");
Check(l && !Selected(*l), "6C XX is no selected file");
std::vector<std::uint8_t> missing = {0x6A, 0x82};
auto m = ParseApduAnswer(missing);
Check(m && m->Status() == "6a82" && !m->Ok() && !Selected(*m), "6A 82: file not found, nothing selected");
Check(m && !ApduFollowUp(1, "01a40004026f09", *m).has_value(), "an error asks for nothing");
std::vector<std::uint8_t> more = {0x80, 0x01, 'a', 0x61, 0x05};
auto w = ParseApduAnswer(more);
Check(w && !ApduFollowUp(1, "01b0000000", *w).has_value(), "data with 61 XX is not followed");
std::vector<std::uint8_t> one = {0x90};
Check(!ParseApduAnswer(one).has_value(), "one byte is no answer");
Check(!ParseApduAnswer({}).has_value(), "no bytes is no answer");
std::vector<std::uint8_t> sw = {0x90, 0x00};
auto e = ParseApduAnswer(sw);
Check(e && e->data.empty() && e->Ok(), "90 00 alone: Ok, no data");
// a card with a proactive command waiting ends a command 91 XX, one
// with a data transfer session going on 92 XX
std::vector<std::uint8_t> proactive = {0x80, 0x03, 'a', '.', 'b', 0x91, 0x0B};
auto p = ParseApduAnswer(proactive);
Check(p && p->Ok() && Selected(*p) && HoldsRead(*p) && p->data.size() == 5, "91 XX is a normal ending");
std::vector<std::uint8_t> transfer = {0x80, 0x03, 'a', '.', 'b', 0x92, 0x10};
auto x = ParseApduAnswer(transfer);
Check(x && x->Ok() && Selected(*x) && HoldsRead(*x), "92 XX is a normal ending");
std::vector<std::uint8_t> n9001 = {0x90, 0x01}, n9300 = {0x93, 0x00};
Check(!ParseApduAnswer(n9001)->Ok(), "90 01 is no normal ending");
Check(!ParseApduAnswer(n9300)->Ok(), "93 00 is no normal ending");
// a read may end 62 82 with all the file holds
std::vector<std::uint8_t> eof = {0x80, 0x03, 'a', '.', 'b', 0x62, 0x82};
auto f = ParseApduAnswer(eof);
Check(f && !f->Ok() && HoldsRead(*f) && !Selected(*f), "data under 62 82 is what a read holds, and no normal ending");
Check(d && HoldsRead(*d), "data under 90 00 is what a read holds");
std::vector<std::uint8_t> bare6282 = {0x62, 0x82}, d6281 = {0x80, 0x01, 'a', 0x62, 0x81}, d6382 = {0x80, 0x01, 'a', 0x63, 0x82};
Check(!HoldsRead(*ParseApduAnswer(bare6282)), "62 82 without data holds nothing");
Check(!HoldsRead(*ParseApduAnswer(d6281)), "data under 62 81 is not taken");
Check(!HoldsRead(*ParseApduAnswer(d6382)), "data under 63 82 is not taken");
Check(m && !HoldsRead(*m), "6A 82 holds nothing");
}
// ---- the value an ISIM identity file holds (the data object with tag 80)
{
auto bytes = [](std::initializer_list<std::uint8_t> head, std::string_view text, std::size_t pad) {
std::vector<std::uint8_t> v(head);
v.insert(v.end(), text.begin(), text.end());
v.insert(v.end(), pad, 0xFF);
return v;
};
Check(ParseIsimValue(bytes({0x80, 0x0F}, "ims.example.net", 17)) == "ims.example.net", "the value of the object, the file's FF padding after it not read");
Check(ParseIsimValue(bytes({0x80, 0x0F}, "ims.example.net", 0)) == "ims.example.net", "the value with nothing after it");
Check(ParseIsimValue(bytes({0x80, 0x81, 0x0F}, "ims.example.net", 3)) == "ims.example.net", "a length in the 81 form");
Check(ParseIsimValue(bytes({0x80, 0x82, 0x00, 0x0F}, "ims.example.net", 3)) == "ims.example.net", "a length in the 82 form");
Check(ParseIsimValue(bytes({0x80, 0x23}, "001010123456789@private.example.net", 29)) == "001010123456789@private.example.net", "a private identity");
Check(ParseIsimValue(bytes({0x80, 0x22}, "sip:001010123456789@ims.example.net", 30)) == "sip:001010123456789@ims.example.ne", "the length decides, not the bytes that follow");
std::vector<std::uint8_t> unused(64, 0xFF);
Check(!ParseIsimValue(unused).has_value(), "an unused record, all FF: no value");
Check(!ParseIsimValue(bytes({0x81, 0x0F}, "ims.example.net", 0)).has_value(), "another tag: no value");
Check(!ParseIsimValue(bytes({0x80, 0x10}, "ims.example.net", 0)).has_value(), "a length past the data: no value");
Check(!ParseIsimValue(bytes({0x80, 0x00}, "", 4)).has_value(), "a length of zero: no value");
Check(!ParseIsimValue(bytes({0x80, 0x83, 0x00, 0x00, 0x0F}, "ims.example.net", 0)).has_value(), "a length form of three bytes: no value");
Check(!ParseIsimValue(bytes({0x80, 0x81}, "", 0)).has_value(), "an 81 form with no length byte: no value");
Check(!ParseIsimValue(bytes({0x80, 0x82, 0x00}, "", 0)).has_value(), "an 82 form cut short: no value");
Check(!ParseIsimValue(bytes({0x80}, "", 0)).has_value(), "the tag alone: no value");
Check(!ParseIsimValue({}).has_value(), "no bytes: no value");
Check(ParseIsimValue(bytes({0x80, 0x12}, "ims.example.net", 3)) == "ims.example.net", "FF at the end of the length are padding counted into it");
std::vector<std::uint8_t> ffInside = {0x80, 0x05, 'a', '.', 0xFF, '.', 'b'};
Check(!ParseIsimValue(ffInside).has_value(), "an FF before the value's end: no value");
std::vector<std::uint8_t> ffOnly = {0x80, 0x03, 0xFF, 0xFF, 0xFF, 0xFF};
Check(!ParseIsimValue(ffOnly).has_value(), "a length of nothing but FF: no value");
Check(!ParseIsimValue(bytes({0x80, 0x0F}, "ims example.net", 0)).has_value(), "a blank in the value: no value");
Check(!ParseIsimValue(bytes({0x80, 0x11}, "ims.example.net\r\n", 0)).has_value(), "a line break in the value: no value");
std::vector<std::uint8_t> nul = {0x80, 0x03, 'a', 0x00, 'b'};
Check(!ParseIsimValue(nul).has_value(), "a NUL in the value: no value");
std::vector<std::uint8_t> utf8 = {0x80, 0x03, 'a', 0xC3, 0xA9};
Check(!ParseIsimValue(utf8).has_value(), "a byte above ASCII in the value: no value");
}
// ---- the identity an ISIM holds
{
const Identity derived = MakeIdentity("001010123456789", "001", "01");
// names unlike the derived ones: a domain of the operator's own
auto id = IsimIdentity(derived, "001010123456789@private.example.net", "one.example.net", "sip:001010123456789@one.example.net");
Check(id.has_value(), "an ISIM's three values make an identity");
Check(id && id->impi == "001010123456789@private.example.net", "the private identity is the card's");
Check(id && id->domain == "one.example.net", "the home domain is the card's");
Check(id && id->impu == "sip:001010123456789@one.example.net", "the public identity is the card's");
Check(id && id->regUri == "sip:one.example.net", "the registration URI is that domain's");
Check(id && id->imsi == derived.imsi && id->mcc == derived.mcc && id->mnc == derived.mnc, "the IMSI and the operator stay");
// an ISIM that holds the names the IMSI gives: the same identity
auto same = IsimIdentity(derived, derived.impi, derived.domain, derived.impu);
Check(same && same->impi == derived.impi && same->domain == derived.domain && same->impu == derived.impu && same->regUri == derived.regUri, "the derived names from an ISIM give the derived identity");
// a public identity that is a number, a domain with a hyphen and digits
Check(IsimIdentity(derived, "user@realm.example", "ims-1.example.net", "sip:+15555550100@ims-1.example.net").has_value(), "a number as the public identity, a hyphen in the domain");
auto no = [&](std::string_view impi, std::string_view domain, std::string_view impu) { return !IsimIdentity(derived, impi, domain, impu).has_value(); };
const std::string_view P = "001010123456789@private.example.net", D = "one.example.net", U = "sip:001010123456789@one.example.net";
Check(no("001010123456789", D, U), "a private identity without a realm is refused");
Check(no("@private.example.net", D, U), "a private identity without a user is refused");
Check(no("001010123456789@", D, U), "a private identity that ends at the @ is refused");
Check(no("", D, U), "an empty private identity is refused");
Check(no("0010101\"23456789@private.example.net", D, U), "a quote in the private identity is refused (it stands in a quoted string)");
Check(no("001010123456789\\@private.example.net", D, U), "a backslash in the private identity is refused");
Check(no("001010123456789@private.example.net\r\nVia: x", D, U), "a line break in the private identity is refused");
Check(no("0010101 23456789@private.example.net", D, U), "a blank in the private identity is refused");
Check(no(P, "", U), "an empty domain is refused");
Check(no(P, "one.example.net>", U), "an angle bracket in the domain is refused");
Check(no(P, "one_example.net", U), "an underscore in the domain is refused");
Check(no(P, "one..example.net", U), "an empty label in the domain is refused");
Check(no(P, ".one.example.net", U), "a domain that begins with a dot is refused");
Check(no(P, "one.example.net.", U), "a domain that ends with a dot is refused");
Check(no(P, "-one.example.net", U), "a label that begins with a hyphen is refused");
Check(no(P, "one-.example.net", U), "a label that ends with a hyphen is refused");
Check(no(P, "one.example.net-", U), "a domain that ends with a hyphen is refused");
Check(no(P, "one.example.net:5060", U), "a port on the domain is refused");
Check(no(P, "one.example.net;lr", U), "a parameter on the domain is refused");
Check(no(P, "one.example.net\r\nRoute: <sip:x>", U), "a line break in the domain is refused");
Check(no(P, D, "tel:+15555550100"), "a tel: public identity is refused");
Check(no(P, D, "sip:"), "a public identity that is the scheme alone is refused");
Check(no(P, D, "001010123456789@one.example.net"), "a public identity without a scheme is refused");
Check(no(P, D, "sip:001010123456789@one.example.net>;tag=x"), "an angle bracket in the public identity is refused (it stands between two)");
Check(no(P, D, "sip:001010123456789@one.example.net<sip:x@y"), "an opening angle bracket in the public identity is refused");
Check(no(P, std::string(63, 'a') + "." + std::string(63, 'b') + "." + std::string(63, 'c') + "." + std::string(63, 'd'), U), "a domain of 255 characters is refused");
Check(!no(P, std::string(63, 'a') + "." + std::string(63, 'b') + "." + std::string(63, 'c') + "." + std::string(61, 'd'), U), "a domain of 253 characters is taken");
Check(no(P, std::string(63, 'a') + "." + std::string(63, 'b') + "." + std::string(63, 'c') + "." + std::string(62, 'd'), U), "a domain of 254 characters is refused");
Check(no(P, D, "sip:001010123456789@one.example.net\r\nTo: <sip:x>"), "a line break in the public identity is refused");
Check(no(P, D, "sip:0010101 23456789@one.example.net"), "a blank in the public identity is refused");
Check(no(P, D, "sip:us\xC3\xA9r@one.example.net"), "a byte above ASCII in the public identity is refused");
Check(no(P, std::string(64, 'a') + ".example.net", U), "a label of 64 characters in the domain is refused");
Check(!no(P, std::string(63, 'a') + ".example.net", U), "a label of 63 characters is taken");
Check(no(std::string(300, 'a') + "@x.example", D, U), "a private identity of 300 characters is refused");
Check(no(std::string(246, 'a') + "@x.example", D, U), "a private identity of 256 characters is refused");
Check(!no(std::string(245, 'a') + "@x.example", D, U), "a private identity of 255 characters is taken");
Check(no(P, D, "sip:" + std::string(252, 'a')), "a public identity of 256 characters is refused");
Check(!no(P, D, "sip:" + std::string(251, 'a')), "a public identity of 255 characters is taken");
}
if (Failures == 0) std::println("Aka: all tests passed");
return Failures != 0; // not the count: `crafter-build test` takes an exit status of 77 for a skipped suite, and an exit status keeps 8 bits
}