aka: a command on a logical channel from 4 to 19 carries the extended class byte

The AUTHENTICATE and the GET RESPONSE went out with the channel number
as their class byte. That is the coding of the standard logical
channels, 0 to 3 (ETSI TS 102 221 10.1.1, table 10.3: the channel in
bits b2 and b1). For the extended ones, 4 to 19, table 10.4a has
'01x0 xxxx' with the channel less four in the low four bits: a command
for channel 4 starts with 40. As 04 it is a command for channel 0 with
a secure-messaging bit set.

The channel is the one the modem returns when imsd opens a logical
channel for the AKA run; imsd does not log it, so whether a
registration ever got one above 3 is not on record.

tests/Aka pins the class byte of channels 0, 1, 3, 4, 5 and 19, the
AUTHENTICATE on channels 3 and 4 and the GET RESPONSE on channel 7.
This commit is contained in:
Jorijn van der Graaf 2026-11-23 14:47:32 +01:00
commit 460343e578
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 32 additions and 6 deletions

View file

@ -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=<channel byte> 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=<class byte> 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<const std::uint8_t> rand16, std::span<const std::uint8_t> 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 <data>
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 {