imsd: with an identity read from the card's ISIM, a fresh registration is made under it and the ISIM is asked for the answer to the challenge
imsd registered the identity TS 23.003 derives from the IMSI on every card and authenticated in the USIM. TS 23.003 has a UE derive its home domain and a temporary public identity "if there is no ISIM application" (13.2, 13.4B); TS 24.229 5.1.1.1A names the ISIM as holding the parameters a registration starts from and says "The ISIM shall always be used for authentication to the IM CN subsystem, if it is present"; GSMA IR.92 2.2.1 has the UE register the public identity of EF_IMPU's first record. A US card on AT&T's network (operator name "Dark Star") holds names of the operator's own: <IMSI>@private.att.net, one.att.net and sip:<IMSI>@one.att.net (a read-only probe of the card, 2026-11-23). imsd 1.1.0's first REGISTER there, with the derived names, was answered 480 Temporarily Unavailable (2026-11-09); TS 24.229 5.3.1.3 has an I-CSCF answer 480 when the HSS lookup "cannot be completed". This daemon has not sent that network the card's own names. Unless ISIM=0 is set, a bring-up reads the ISIM application of the USIM's slot, in whatever state the modem lists it, over a logical channel: EF_IMPI, EF_DOMAIN and record 1 of EF_IMPU. When the three make an identity it is the card's, and the ISIM is asked for the answer to the challenge; an ISIM that gives no result is followed by the same challenge to the card's USIM, where 1.2.2 sent every challenge. Without an ISIM, with ISIM=0, or when the read or the check of the three fails, the derived names and the USIM stand as before. An `identity:` line in the journal says which and why. So with ISIM=0, with an ISIM that gives no identity and behind one that gives no result imsd asks the USIM where 5.1.1.1A has the ISIM used. A fresh registration is made under the card's identity. A resumed one keeps the identity it was made under until its next fresh registration, as it keeps its P-CSCF and its address: the state file records the three names, and a file without them is taken for one made under the derived ones. Otherwise a card whose ISIM holds other names, on a network that took the derived ones as well, would have its standing registration refreshed under new names at the first start after the upgrade. An ISIM that gave no identity at the bring-up is read again at each recovery by a fresh registration later in the same run, until it gives one: such a registration would else go out under the derived names. A state file whose three names are no identity imsd can register is no registration to resume, whatever transport the file names. Calls and texts follow the registration's identity. Its home domain is what RuriFor takes for the address of a dialled number, an outgoing call's Request-URI and To (the host, and the phone-context where it writes one), and what ScPsi takes for the service centre's address of an SMS submission; while no P-Associated-URI has named another, its public identity is the one the reg-event SUBSCRIBE names and CallerId gives. Under an identity read from an ISIM these are the ISIM's. The read and the AKA run share one channel helper (UimChannel), which closes its channel on every way out: 1.2.2 left the AKA's channel open when the card's answer was the single byte 61. Tested: the suites; the daemon of this commit on the workstation, its registration against a stand-in for qmicli and a stand-in network that answers a first REGISTER of any names but its subscriber's 480 and tells the two applications' answers to the challenge apart (the fp6 journal's 2026-11-23-isim-identity capture): 46 scenarios, among them an ISIM with names of the operator's own, one with the derived names, ISIM=0 on the first (480, as on 2026-11-09), an ISIM that cannot be read or does not answer the challenge, the resume by this daemon of a registration imsd 1.2.2 made under the derived names on a card whose ISIM holds others, and a fresh registration in a run that resumed. Not run on a phone yet, and on no network.
This commit is contained in:
parent
3754687b0d
commit
610d8e1300
4 changed files with 401 additions and 51 deletions
|
|
@ -5,8 +5,10 @@
|
|||
imsd — the userspace IMS/VoLTE daemon (control plane).
|
||||
|
||||
Owns net.catcrafts.IMS1 on the system bus with the project's frozen D-Bus ABI
|
||||
(AGENTS.md §8.1) and drives it with the engine core: register on the IMS PDN (USIM AKA
|
||||
+ IPsec sec-agree, fresh or warm-resume, with a keepalive re-REGISTER refresh),
|
||||
(AGENTS.md §8.1) and drives it with the engine core: register on the IMS PDN (by default the
|
||||
identity the card's ISIM holds, else the one derived from the IMSI; AKA in that
|
||||
ISIM, else in the USIM; IPsec sec-agree, fresh or warm-resume, with a keepalive
|
||||
re-REGISTER refresh),
|
||||
run outgoing and incoming calls through the Imsd:Engine call machine (inbound
|
||||
INVITEs on the protected-port listeners become ringing calls a dialer answers
|
||||
via Accept), spawn one imsd-media data-plane process per answered call, and
|
||||
|
|
@ -194,6 +196,90 @@ std::optional<std::string> CompletedHex(std::string_view out) {
|
|||
return h;
|
||||
}
|
||||
|
||||
// ---- the card, through qmicli's UIM logical channels ----------------------
|
||||
|
||||
// A logical channel to one application of the card: opened on the
|
||||
// application's AID, closed when the object goes. Exchange sends one command
|
||||
// and returns the card's answer to it; Transmit also sends what the card asks
|
||||
// for after it (imsd::aka::ApduFollowUp, for an answer without data: GET
|
||||
// RESPONSE for '61 XX', a command without data again with the length a
|
||||
// '6C XX' names — two follow-ups at most) and returns the last answer.
|
||||
// nullopt when one of the exchanges
|
||||
// brought no answer with a status word back: qmicli printed none, or fewer
|
||||
// than two bytes.
|
||||
class UimChannel {
|
||||
public:
|
||||
UimChannel(int slot, std::string_view aid) : slot_(slot) {
|
||||
auto open = RunCapture({"qmicli", "-d", "qrtr://0", std::format("--uim-open-logical-channel={},{}", slot, aid)});
|
||||
if (auto ch = CompletedInt(open.out)) channel_ = static_cast<int>(*ch);
|
||||
}
|
||||
UimChannel(const UimChannel&) = delete;
|
||||
UimChannel& operator=(const UimChannel&) = delete;
|
||||
~UimChannel() {
|
||||
if (channel_ >= 0) RunCapture({"qmicli", "-d", "qrtr://0", std::format("--uim-close-logical-channel={},{}", slot_, channel_)});
|
||||
}
|
||||
bool Open() const { return channel_ >= 0; }
|
||||
int Number() const { return channel_; }
|
||||
std::optional<imsd::aka::ApduAnswer> Exchange(std::string_view apdu) const {
|
||||
auto r = RunCapture({"qmicli", "-d", "qrtr://0", std::format("--uim-send-apdu={},{},{}", slot_, channel_, apdu)});
|
||||
auto hex = CompletedHex(r.out);
|
||||
if (!hex) return std::nullopt;
|
||||
auto bytes = imsd::util::FromHex(*hex);
|
||||
if (!bytes) return std::nullopt;
|
||||
return imsd::aka::ParseApduAnswer(*bytes);
|
||||
}
|
||||
std::optional<imsd::aka::ApduAnswer> Transmit(std::string apdu) const {
|
||||
auto answer = Exchange(apdu);
|
||||
for (int i = 0; answer && i < 2; i++) {
|
||||
auto next = imsd::aka::ApduFollowUp(channel_, apdu, *answer);
|
||||
if (!next) break;
|
||||
apdu = std::move(*next);
|
||||
answer = Exchange(apdu);
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
private:
|
||||
int slot_;
|
||||
int channel_ = -1;
|
||||
};
|
||||
|
||||
// Whether two identities are the same three names.
|
||||
bool SameNames(const imsd::aka::Identity& a, const imsd::aka::Identity& b) {
|
||||
return a.impi == b.impi && a.domain == b.domain && a.impu == b.impu;
|
||||
}
|
||||
|
||||
// The identity the card's ISIM application holds (TS 24.229 5.1.1.1A): the
|
||||
// private identity of EF_IMPI, the home domain of EF_DOMAIN and the public
|
||||
// identity of EF_IMPU's first record (GSMA IR.92 2.2.1), read over a logical
|
||||
// channel on the application with the commands that read two reporters'
|
||||
// cards (AGENTS.md §13); a SELECT's '61 XX' is taken for the file selected
|
||||
// and the control parameters it offers are not fetched, as there. nullopt,
|
||||
// with `why` saying what was missing, when it gives none.
|
||||
std::optional<imsd::aka::Identity> ReadIsimIdentity(int slot, const imsd::aka::IsimApp& app, const imsd::aka::Identity& derived, std::string& why) {
|
||||
UimChannel ch(slot, app.aid);
|
||||
if (!ch.Open()) { why = "no logical channel opened on it"; return std::nullopt; }
|
||||
auto read = [&](std::uint16_t file, std::string_view name, bool record) -> std::optional<std::string> {
|
||||
auto selected = ch.Exchange(imsd::aka::BuildSelectFileApdu(ch.Number(), file));
|
||||
if (!selected) { why = std::format("{}: no answer with a status word to the SELECT", name); return std::nullopt; }
|
||||
if (!imsd::aka::Selected(*selected)) { why = std::format("{}: the SELECT answered {}", name, selected->Status()); return std::nullopt; }
|
||||
auto got = ch.Transmit(record ? imsd::aka::BuildReadRecordApdu(ch.Number(), 1) : imsd::aka::BuildReadBinaryApdu(ch.Number()));
|
||||
if (!got) { why = std::format("{}: an exchange of the read brought no answer with a status word", name); return std::nullopt; }
|
||||
if (!imsd::aka::HoldsRead(*got)) { why = std::format("{}: the read answered {}", name, got->Status()); return std::nullopt; }
|
||||
auto value = imsd::aka::ParseIsimValue(got->data);
|
||||
if (!value) why = std::format("{}: no value in the {} bytes read", name, got->data.size());
|
||||
return value;
|
||||
};
|
||||
auto impi = read(imsd::aka::EfImpi, "EF_IMPI", false);
|
||||
if (!impi) return std::nullopt;
|
||||
auto domain = read(imsd::aka::EfDomain, "EF_DOMAIN", false);
|
||||
if (!domain) return std::nullopt;
|
||||
auto impu = read(imsd::aka::EfImpu, "EF_IMPU record 1", true);
|
||||
if (!impu) return std::nullopt;
|
||||
auto id = imsd::aka::IsimIdentity(derived, *impi, *domain, *impu);
|
||||
if (!id) why = std::format("EF_IMPI '{}', EF_DOMAIN '{}' and EF_IMPU record 1 '{}' are no identity imsd can register", *impi, *domain, *impu);
|
||||
return id;
|
||||
}
|
||||
|
||||
// The UE address on the ims PDN when LOCAL is not given: a global IPv6 on
|
||||
// <dev> that is neither temporary nor RA-derived (`dynamic`) — the one
|
||||
// ims-pdn-up.sh assigned from ModemManager — else the first non-temporary
|
||||
|
|
@ -760,8 +846,15 @@ struct RegState {
|
|||
std::uint32_t spiUc = 0;
|
||||
std::uint32_t spiUs = 0;
|
||||
long expiry = 0;
|
||||
// The card application the challenge goes to, by its slot and AID: the
|
||||
// ISIM when the card's identity was read from it, else the USIM.
|
||||
int slot = 0;
|
||||
std::string aid;
|
||||
// The identity this registration was made under. Set on the fresh 200
|
||||
// and when a resume takes its state file's (the file's since 1.2.3; the
|
||||
// derived one for an older file, as 1.2.2 wrote it for a registration
|
||||
// under it); empty before the first of either.
|
||||
imsd::aka::Identity id;
|
||||
// Contact user-part of the stored binding (UUID since the s53 oracle
|
||||
// diff; empty = legacy IMSI binding from an older state file).
|
||||
std::string contactUser;
|
||||
|
|
@ -815,6 +908,10 @@ void SaveTpMr(std::uint8_t mr) {
|
|||
// since 0.3.12. The file's values are quoted without escaping; a value
|
||||
// with a quote, a backslash or a newline in it (none RFC 3329's token
|
||||
// syntax produces) is written empty, and the resume falls back to the SA.
|
||||
// `impi`, `domain` and `impu` (since 1.2.3) are the identity the
|
||||
// registration was made under, which a resume keeps (TryResume); the three
|
||||
// have passed imsd::aka::IsimIdentity or are MakeIdentity's, so none holds
|
||||
// a character the quoting cannot carry.
|
||||
void PersistState(const RegState& r, const std::string& route, const std::string& ppi, const std::string& aor, const std::string& transport, const std::string& pcscf) {
|
||||
std::string ss = r.securityServer.find_first_of("\"\\\n") == std::string::npos ? r.securityServer : std::string();
|
||||
std::string j = std::format(
|
||||
|
|
@ -822,9 +919,11 @@ void PersistState(const RegState& r, const std::string& route, const std::string
|
|||
"\"spi_us\": {}, \"expiry\": {}, \"route\": \"{}\", \"ppi\": \"{}\", "
|
||||
"\"aor\": \"{}\", \"contact_user\": \"{}\", \"transport\": \"{}\", "
|
||||
"\"auth_nonce\": \"{}\", \"auth_cnonce\": \"{}\", \"auth_response\": \"{}\", "
|
||||
"\"security_server\": \"{}\", \"pcscf\": \"{}\"}}",
|
||||
"\"security_server\": \"{}\", \"pcscf\": \"{}\", "
|
||||
"\"impi\": \"{}\", \"domain\": \"{}\", \"impu\": \"{}\"}}",
|
||||
r.callid, r.ftag, r.cseq, r.spiUc, r.spiUs, r.expiry, route, ppi, aor,
|
||||
r.contactUser, transport, r.authNonce, r.authCnonce, r.authResponse, ss, pcscf);
|
||||
r.contactUser, transport, r.authNonce, r.authCnonce, r.authResponse, ss, pcscf,
|
||||
r.id.impi, r.id.domain, r.id.impu);
|
||||
std::string path = StatePath();
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(std::filesystem::path(path).parent_path(), ec);
|
||||
|
|
@ -850,7 +949,7 @@ void DumpRaw(std::string_view name, std::string_view msg) {
|
|||
}
|
||||
|
||||
struct PersistedState {
|
||||
std::optional<std::string> callid, ftag, route, ppi, aor, contactUser, transport, authNonce, authCnonce, authResponse, securityServer, pcscf;
|
||||
std::optional<std::string> callid, ftag, route, ppi, aor, contactUser, transport, authNonce, authCnonce, authResponse, securityServer, pcscf, impi, domain, impu;
|
||||
std::optional<long> cseq, spiUc, spiUs, expiry;
|
||||
};
|
||||
std::optional<PersistedState> LoadState() {
|
||||
|
|
@ -873,6 +972,9 @@ std::optional<PersistedState> LoadState() {
|
|||
ps.authResponse = QuotedAfter(s, "\"auth_response\": \"");
|
||||
ps.securityServer = QuotedAfter(s, "\"security_server\": \""); // absent in pre-0.3.12 files
|
||||
ps.pcscf = QuotedAfter(s, "\"pcscf\": \""); // absent in pre-1.0.0 files
|
||||
ps.impi = QuotedAfter(s, "\"impi\": \""); // the three absent in pre-1.2.3 files
|
||||
ps.domain = QuotedAfter(s, "\"domain\": \"");
|
||||
ps.impu = QuotedAfter(s, "\"impu\": \"");
|
||||
ps.cseq = IntAfter(s, "\"cseq\": ");
|
||||
ps.spiUc = IntAfter(s, "\"spi_uc\": ");
|
||||
ps.spiUs = IntAfter(s, "\"spi_us\": ");
|
||||
|
|
@ -1636,7 +1738,7 @@ private:
|
|||
// what follows a resume that failed: with a fresh target the fresh
|
||||
// registration, without one the restart's new resume attempt
|
||||
std::string ResumeFailedNext() const { return pcscfTarget_.empty() ? "the restart tries the resume again" : "the next registration is a fresh one"; }
|
||||
// a pair nothing can resume (no state file, the file another attempt's): with a fresh target the fresh registration replaces it; with none BringUp writes it off and takes it out of the kernel
|
||||
// a pair nothing can resume (no state file, the file another attempt's or its identity failing the check): with a fresh target the fresh registration replaces it; with none BringUp writes it off and takes it out of the kernel
|
||||
std::string ResumeDeadNext() const { return pcscfTarget_.empty() ? "nothing can resume it: the pair is removed" : "resume falls back to fresh"; }
|
||||
// the failed resume's line tail by the engine's verdict: a registration it
|
||||
// wrote off is removed (no fresh target) or replaced by the fresh one; with
|
||||
|
|
@ -1685,6 +1787,22 @@ private:
|
|||
// registration
|
||||
imsd::msg::Context ctx_;
|
||||
RegState reg_;
|
||||
// The identity the card gives — its ISIM's, else the one derived from
|
||||
// the IMSI — and the derived one. A fresh registration is made under the
|
||||
// first. A resumed one keeps the identity it was made under (reg_.id)
|
||||
// until the next fresh registration, as it keeps its P-CSCF and its
|
||||
// address: the binding to refresh is the one made under those names.
|
||||
imsd::aka::Identity cardId_;
|
||||
imsd::aka::Identity derivedId_;
|
||||
// The card's USIM: the challenge goes to it when the identity is not an
|
||||
// ISIM's, and after an ISIM that gave the identity and gives no result
|
||||
// for the challenge.
|
||||
std::string usimAid_;
|
||||
// The ISIM application that has given no identity since the bring-up:
|
||||
// asked again at each recovery by a fresh registration later in the
|
||||
// same run (FreshRecover), which else would go out under the derived
|
||||
// names.
|
||||
std::optional<imsd::aka::IsimApp> isimOwed_;
|
||||
std::string route_, ppi_, ss_;
|
||||
std::string simOperator_; // the SIM's operator id as read at bring-up: MCC then MNC
|
||||
bool registered_ = false;
|
||||
|
|
@ -1798,7 +1916,8 @@ private:
|
|||
auto siminfo = RunCapture({"mmcli", "-i", *simPath});
|
||||
auto info = imsd::aka::ParseSimInfo(siminfo.out);
|
||||
if (!info) throw std::runtime_error("SIM IMSI/operator not found");
|
||||
ctx_.id = imsd::aka::MakeIdentity(info->imsi, info->mcc, info->mnc);
|
||||
derivedId_ = imsd::aka::MakeIdentity(info->imsi, info->mcc, info->mnc);
|
||||
cardId_ = derivedId_;
|
||||
simOperator_ = info->mcc + info->mnc;
|
||||
ctx_.local = local_;
|
||||
ctx_.ealgOffer = ealgOffer_;
|
||||
|
|
@ -1816,7 +1935,26 @@ private:
|
|||
if (auto imei = imsd::aka::ParseEquipmentId(modem.out))
|
||||
if (auto urn = imsd::aka::ImeiUrn(*imei)) ctx_.instanceId = *urn;
|
||||
reg_.slot = sel->slot;
|
||||
reg_.aid = sel->aid;
|
||||
reg_.aid = usimAid_ = sel->aid;
|
||||
// The identity so far is the one TS 23.003 derives from the IMSI,
|
||||
// the names of a UE without an ISIM (TS 24.229 5.1.1.1A). When the
|
||||
// card's ISIM gives an identity, that one is the card's and the
|
||||
// challenge goes to the ISIM. The names can be the operator's own: a US
|
||||
// card on AT&T's network holds <IMSI>@private.att.net and
|
||||
// one.att.net, and the derived names drew a 480 to the first
|
||||
// REGISTER there (2026-11-09; the read of the card 2026-11-23,
|
||||
// AGENTS.md §13). An ISIM that gives none leaves the derived
|
||||
// identity and the USIM, what 1.2.2 registered with; ISIM=0 asks for
|
||||
// that.
|
||||
std::optional<imsd::aka::IsimApp> app;
|
||||
if (EnvOr("ISIM", "1") == "0") {
|
||||
Log("identity: derived from the IMSI (ISIM=0)");
|
||||
} else {
|
||||
app = imsd::aka::ParseIsimApp(card.out, sel->slot);
|
||||
if (!app) Log("identity: derived from the IMSI (the card has no ISIM application)");
|
||||
}
|
||||
isimOwed_ = app && !TakeIsimIdentity(*app) ? app : std::nullopt;
|
||||
ctx_.id = cardId_;
|
||||
Log(std::format("SIM slot={} IMSI={} domain={}", sel->slot, info->imsi, ctx_.id.domain));
|
||||
ctx_.smsOverIp = smsOverIp_;
|
||||
if (smsOverIp_) LoadSmsParameters();
|
||||
|
|
@ -1874,7 +2012,8 @@ private:
|
|||
// binding; a 401, a refusal with a Retry-After, the second refusal —
|
||||
// the count travels in the holds; Dead above); a pair nothing can
|
||||
// resume (its UE address left the link, no
|
||||
// state file, the file another attempt's, written off before this
|
||||
// state file, the file another attempt's or its identity failing
|
||||
// the check, written off before this
|
||||
// start) is written off and taken out of the kernel by exact key
|
||||
// — the fresh registration that would have replaced it does not
|
||||
// come — so the next bring-up finds nothing to resume, and the
|
||||
|
|
@ -1968,11 +2107,27 @@ private:
|
|||
sa->spiUc, sa->spiUs, pcscf_, ResumeDeadNext()));
|
||||
return ResumeOutcome::Dead;
|
||||
}
|
||||
// The registration's identity is the one it was made under, whatever
|
||||
// the card gives today: the file's since 1.2.3, and for an older file
|
||||
// the derived one, under which 1.2.2 registered. A file whose three
|
||||
// names are no identity imsd can write names a registration nothing
|
||||
// can refresh, whatever else the file holds: judged before the
|
||||
// transport, as the file of another attempt is.
|
||||
imsd::aka::Identity made = derivedId_;
|
||||
if (ps->impi || ps->domain || ps->impu) {
|
||||
auto filed = imsd::aka::IsimIdentity(derivedId_, ps->impi.value_or(""), ps->domain.value_or(""), ps->impu.value_or(""));
|
||||
if (!filed) { Log(std::format("the state file's identity is not one imsd can register; {}", ResumeDeadNext())); return ResumeOutcome::Dead; }
|
||||
made = *filed;
|
||||
}
|
||||
// The registration's transport is what the P-CSCF knows; a resume
|
||||
// must reuse it. A forced policy that disagrees means a fresh start
|
||||
// — decided before anything from the file is adopted.
|
||||
Transport t = ps->transport.value_or("TCP") == "UDP" ? Transport::Udp : Transport::Tcp;
|
||||
if (sipPolicy_ != "auto" && t != transport_) { Log(std::format("state file transport differs from SIP_TRANSPORT; {}", ResumeFailedNext())); return ResumeOutcome::Failed; }
|
||||
if (!SameNames(made, cardId_))
|
||||
Log(std::format("identity: the registration to resume was made under private identity {}, home domain {}, public identity {}; it keeps them until the next fresh registration, which takes the card's", made.impi, made.domain, made.impu));
|
||||
ctx_.id = made;
|
||||
reg_.id = made;
|
||||
|
||||
route_ = ps->route.value_or(std::format("<sip:{};lr>", imsd::util::HostPort(pcscf_, sa->portPs)));
|
||||
// No fallback identity: an empty ppi omits P-Preferred-Identity and
|
||||
|
|
@ -2004,7 +2159,8 @@ private:
|
|||
}
|
||||
// The public identity our requests are signed with (From) — restored
|
||||
// here so a call placed before the refresh 200 re-learns it does not
|
||||
// fall back to the barred temporary IMPU.
|
||||
// fall back to the identity's own (for a registration made under the
|
||||
// derived names, the temporary IMPU).
|
||||
ctx_.aor = ps->aor.value_or("");
|
||||
ctx_.route = route_; ctx_.ppi = ppi_; ctx_.securityServer = ss_;
|
||||
ctx_.portUc = PortUc; ctx_.portUs = PortUs;
|
||||
|
|
@ -2050,18 +2206,48 @@ private:
|
|||
Log(std::format("binding: {}", ct));
|
||||
}
|
||||
|
||||
// The ISIM application's identity for the card: when the three files
|
||||
// read and pass the check it is the card's, and the challenge goes to
|
||||
// the ISIM. The journal's `identity:` line says which way it went.
|
||||
bool TakeIsimIdentity(const imsd::aka::IsimApp& app) {
|
||||
std::string state = app.state.empty() ? "unknown" : app.state;
|
||||
std::string why;
|
||||
auto id = ReadIsimIdentity(reg_.slot, app, derivedId_, why);
|
||||
if (!id) {
|
||||
Log(std::format("identity: derived from the IMSI: the ISIM application (state {}) gave none — {}", state, why));
|
||||
return false;
|
||||
}
|
||||
cardId_ = *id;
|
||||
reg_.aid = app.aid;
|
||||
Log(std::format("identity: the ISIM's (application state {}): private identity {}, home domain {}, public identity {}; {}",
|
||||
state, id->impi, id->domain, id->impu,
|
||||
SameNames(cardId_, derivedId_) ? "the names the IMSI gives" : std::format("not the names the IMSI gives (home domain {})", derivedId_.domain)));
|
||||
return true;
|
||||
}
|
||||
|
||||
// A fresh registration. The attempt mints its own Contact user-part into
|
||||
// ctx_ for its REGISTERs; on any failure the stored binding's comes back,
|
||||
// so requests over the existing SA meanwhile (a reconnect, a refresh)
|
||||
// carry the binding the network knows. reg_ takes the new one on the 200.
|
||||
// The identity goes the same way: the attempt registers the card's
|
||||
// (cardId_), and on a failure the one ctx_ held before it comes back —
|
||||
// a resumed registration's can be another than the card's (TryResume).
|
||||
void FreshRegister() {
|
||||
saReplaced_ = false;
|
||||
imsd::aka::Identity before = ctx_.id;
|
||||
try { FreshRegisterAttempt(); }
|
||||
catch (...) { ctx_.contactUser = reg_.contactUser; throw; }
|
||||
catch (...) {
|
||||
ctx_.contactUser = reg_.contactUser;
|
||||
ctx_.id = before;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void FreshRegisterAttempt() {
|
||||
Log("FRESH registration");
|
||||
if (!SameNames(ctx_.id, cardId_))
|
||||
Log(std::format("identity: the fresh registration takes the card's: private identity {}, home domain {}, public identity {}", cardId_.impi, cardId_.domain, cardId_.impu));
|
||||
ctx_.id = cardId_;
|
||||
// Oracle diff (journal/ims.md s53): stock's Contact user-part is a
|
||||
// fresh UUID per registration, not the IMSI. Minted before reg1 so
|
||||
// both REGISTERs and the stored binding carry the same URI.
|
||||
|
|
@ -2204,6 +2390,7 @@ private:
|
|||
}
|
||||
reg_.callid = callidReg; reg_.ftag = ftag; reg_.cseq = 2;
|
||||
reg_.contactUser = contactUser;
|
||||
reg_.id = ctx_.id;
|
||||
reg_.spiUc = spiUc; reg_.spiUs = spiUs;
|
||||
reg_.authNonce = *nonceB64; reg_.authCnonce = cnonce; reg_.authResponse = response;
|
||||
reg_.securityServer = ss_;
|
||||
|
|
@ -2385,33 +2572,40 @@ private:
|
|||
Log(std::format("self AOR: {}", ctx_.aor.empty() ? ctx_.id.impu : ctx_.aor));
|
||||
}
|
||||
|
||||
// USIM AKA via qmicli UIM logical channel.
|
||||
// AKA in the card: the challenge to the application that goes with the
|
||||
// card's identity (reg_.aid: the ISIM when the identity was read from it,
|
||||
// else the USIM), over a UIM logical channel. The command is the same in
|
||||
// both: AUTHENTICATE with P2 '81' is the 3G context of a USIM (TS 31.102
|
||||
// 7.1.2) and the IMS AKA context of an ISIM (TS 31.103 7.1.2), and both
|
||||
// answer RES, CK and IK under tag DB.
|
||||
// An ISIM that gives no result is not the end of the attempt: the same
|
||||
// card's USIM is asked, where 1.2.2 sent every challenge —
|
||||
// no AKA in a real ISIM has run on this code (AGENTS.md §5).
|
||||
std::optional<imsd::aka::AkaResult> Authenticate(std::span<const std::uint8_t> rand16, std::span<const std::uint8_t> autn16) {
|
||||
auto open = RunCapture({"qmicli", "-d", "qrtr://0",
|
||||
std::format("--uim-open-logical-channel={},{}", reg_.slot, reg_.aid)});
|
||||
auto ch = CompletedInt(open.out);
|
||||
if (!ch) return std::nullopt;
|
||||
int channel = static_cast<int>(*ch);
|
||||
auto closeCh = [&] {
|
||||
RunCapture({"qmicli", "-d", "qrtr://0",
|
||||
std::format("--uim-close-logical-channel={},{}", reg_.slot, channel)});
|
||||
};
|
||||
std::string apdu = imsd::aka::BuildAkaApdu(channel, rand16, autn16);
|
||||
auto r1 = RunCapture({"qmicli", "-d", "qrtr://0",
|
||||
std::format("--uim-send-apdu={},{},{}", reg_.slot, channel, apdu)});
|
||||
auto b1 = CompletedHex(r1.out);
|
||||
if (!b1) { closeCh(); return std::nullopt; }
|
||||
if (b1->size() >= 2 && b1->substr(0, 2) == "61") {
|
||||
std::string gr = imsd::aka::BuildGetResponseApdu(channel, std::stoi(b1->substr(2, 2), nullptr, 16));
|
||||
auto r2 = RunCapture({"qmicli", "-d", "qrtr://0",
|
||||
std::format("--uim-send-apdu={},{},{}", reg_.slot, channel, gr)});
|
||||
b1 = CompletedHex(r2.out);
|
||||
if (!b1) { closeCh(); return std::nullopt; }
|
||||
std::string what;
|
||||
auto result = AkaIn(reg_.aid, rand16, autn16, what);
|
||||
if (!result && reg_.aid != usimAid_) {
|
||||
Log(std::format("AKA: the ISIM application gave no result ({}); asking the USIM", what));
|
||||
result = AkaIn(usimAid_, rand16, autn16, what);
|
||||
}
|
||||
closeCh();
|
||||
auto bytes = imsd::util::FromHex(*b1);
|
||||
if (!bytes) return std::nullopt;
|
||||
return imsd::aka::ParseAkaResponse(*bytes);
|
||||
if (!result) Log(std::format("AKA: no result ({})", what));
|
||||
return result;
|
||||
}
|
||||
|
||||
// One AUTHENTICATE in the application `aid` of the card in reg_.slot;
|
||||
// `what` says why there is no result.
|
||||
std::optional<imsd::aka::AkaResult> AkaIn(const std::string& aid, std::span<const std::uint8_t> rand16, std::span<const std::uint8_t> autn16, std::string& what) {
|
||||
UimChannel ch(reg_.slot, aid);
|
||||
if (!ch.Open()) { what = "no logical channel opened"; return std::nullopt; }
|
||||
auto answer = ch.Transmit(imsd::aka::BuildAkaApdu(ch.Number(), rand16, autn16));
|
||||
if (!answer) { what = "an exchange brought no answer with a status word"; return std::nullopt; }
|
||||
auto result = imsd::aka::ParseAkaResponse(*answer);
|
||||
if (!result) {
|
||||
what = answer->data.empty() ? std::format("status word {}", answer->Status())
|
||||
: answer->data[0] == 0xDC ? std::string("a synchronisation failure, tag DC")
|
||||
: std::format("{} bytes that are no AKA result, status word {}", answer->data.size(), answer->Status());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ---- keepalive refresh (only while idle) ------------------------------
|
||||
|
|
@ -2610,6 +2804,9 @@ private:
|
|||
bool ok = false;
|
||||
try {
|
||||
RefreshPani();
|
||||
// an ISIM that has given no identity since the bring-up is asked
|
||||
// again: its names can be the only ones the network takes
|
||||
if (isimOwed_ && TakeIsimIdentity(*isimOwed_)) isimOwed_.reset();
|
||||
FreshRegister();
|
||||
subRetryAt_ = 0; // a new registration: its subscription starts afresh
|
||||
OnRegistered();
|
||||
|
|
|
|||
Loading…
Reference in a new issue