diff --git a/AGENTS.md b/AGENTS.md index 6d3f42d..34816cd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -253,7 +253,10 @@ What the suites pin: form (the RFC's §4.1–4.2.3 examples, idempotence on `inet_ntop`-shaped text, what is refused), the address-list parser, hex/base64, MD5. - `Aka` — the AKAv1-MD5 digest, identity derivation, the `qmicli`/`mmcli` - text parsers, the AUTHENTICATE APDU and its response parser. + text parsers, the AUTHENTICATE APDU and its response parser, the class + byte of a command on a logical channel (the channel itself for 0 to 3, + `4X` for 4 to 19 — TS 102 221 10.1.1; AUTHENTICATE on channels 3 and + 4, GET RESPONSE on channel 7). - `Ipsec` — the exact `ip xfrm` argv lists for the SA pairs, the `EALG` setting, the Security-Server mechanism selection against one offered cipher and against two (KPN's entry and Odido's, O2's six with the SPIs diff --git a/interfaces/Imsd-Aka.cppm b/interfaces/Imsd-Aka.cppm index 4671984..32132db 100644 --- a/interfaces/Imsd-Aka.cppm +++ b/interfaces/Imsd-Aka.cppm @@ -248,20 +248,29 @@ export namespace imsd::aka { // ---- AUTHENTICATE(AKA) APDU (ISO 7816-4 over the qmicli UIM channel) ---- + // The class byte of a command on a logical channel (ETSI TS 102 221 + // 10.1.1): '0X' with the channel in its two low bits for the standard + // channels 0 to 3, '4X' with the channel less four in its low nibble for + // the extended ones, 4 to 19. Before 1.2.3 the channel number itself was + // the class byte, which is the first form only: a command for channel 4 + // went out as '04', channel 0 with a secure-messaging bit set. + inline int ClassByte(int channel) { + return channel < 4 ? channel & 0x03 : 0x40 | ((channel - 4) & 0x0F); + } + // The command APDU (hex, no separators) for a 3G AKA authentication: - // CLA= INS=88 P1=00 P2=81 Lc [10 RAND(16) 10 AUTN(16)] - // `channel` is the logical-channel number qmicli returned; the CLA low - // nibble carries it (Python: cla = "%02x" % ch). + // CLA= INS=88 P1=00 P2=81 Lc [10 RAND(16) 10 AUTN(16)] + // `channel` is the logical-channel number qmicli returned. std::string BuildAkaApdu(int channel, std::span rand16, std::span autn16) { std::string data = std::format("10{}10{}", imsd::util::ToHex(rand16), imsd::util::ToHex(autn16)); std::size_t lc = data.size() / 2; // CLA INS=88 P1=00 P2=81 Lc - return std::format("{:02x}880081{:02x}{}", channel, lc, data); + return std::format("{:02x}880081{:02x}{}", ClassByte(channel), lc, data); } // GET RESPONSE APDU for when the card answers 61xx (xx bytes available). std::string BuildGetResponseApdu(int channel, int le) { - return std::format("{:02x}c00000{:02x}", channel, le & 0xFF); + return std::format("{:02x}c00000{:02x}", ClassByte(channel), le & 0xFF); } struct AkaResult { diff --git a/tests/Aka/main.cpp b/tests/Aka/main.cpp index 49c8d1f..baaf359 100644 --- a/tests/Aka/main.cpp +++ b/tests/Aka/main.cpp @@ -98,6 +98,20 @@ int main() { 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 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 body = {0xDB, 0x08};